<commit_before>"""Copyright 2012 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License."""

import os
import datetime
import time

from carbon import state
from carbon.cache import MetricCache
from carbon.storage import getFilesystemPath, loadStorageSchemas, loadAggregationSchemas
from carbon.conf import settings
from carbon import log, events, instrumentation
from carbon.persister import BasePersister

import psycopg2

PG_BACKEND_SETTINGS = {
    "dbname" : "senzari_stats",
    "user" : "senzari_dev",
    "password" : "<PASSWORD>",
    "host" : "localhost"
}

class PostgresqlPersister(BasePersister):
    def __init__ (self):
        self._connection_alive = False

    def check_alive(self):
        if self._connection_alive:
            return

        if hasattr(self, '_connection'): #Try to close any previous connection
            self._connection.close()

        connection = psycopg2.connect(database=PG_BACKEND_SETTINGS["dbname"],
                        host=PG_BACKEND_SETTINGS["host"],
                        user=PG_BACKEND_SETTINGS["user"],
                        password=PG_BACKEND_SETTINGS["password"])
        self._connection = connection
        self._connection_alive = True

    #Our persister takes for granted the databases are
    #created already (which is different to Whisper, which creates
    #one per Metric, on demand)
    def get_dbinfo(self, metric):
        '''
        Gets the database info related to a metric, as well as whether it
        exists already or not (as a tuple)
        '''
        return ('latest_stats', True)

    def update_one(self, metric, datapoint):
        value = datapoint[1]
        timestamp = datetime.datetime.fromtimestamp(int(datapoint[0]))
        log.msg("INSERTING %s" % metric)
        sql_stmt =  """
            INSERT into latest_stats(name, tstamp, value)
            VALUES ('%s', '%s', %f);
        """
        cursor = self._connection.cursor()
        cursor.execute(sql_stmt % (metric, timestamp, value))
        log.msg("successfully inserted value to metric %s" % (metric,))

    def update_many(self, metric, datapoints, dbIdentifier):
        '''
        Updates the datapoints for a metric.
        'metric' is the name of the param ('my.value')
        'datapoints' is a list of tuples, containing the timestamp and value
        '''

        '''
        Ignore the metrics with _90 for now (not sure what they do,
        besides repeating the info under some scenarios
        '''
        if metric.endswith("_90"):
            return

        log.msg("updating metric %s using the postgresql persister" % (metric,))
        try:
            self.check_alive()

            for datapoint in datapoints:
                self.update_one(metric, datapoint)

            self._connection.commit()
        except psycopg2.Warning, e:<commit_msg>Add some metric cases to be ignored, statsd/carbon wise.<commit_after>"""Copyright 2012 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License."""

import os
import datetime
import time

from carbon import state
from carbon.cache import MetricCache
from carbon.storage import getFilesystemPath, loadStorageSchemas, loadAggregationSchemas
from carbon.conf import settings
from carbon import log, events, instrumentation
from carbon.persister import BasePersister

import psycopg2

PG_BACKEND_SETTINGS = {
    "dbname" : "senzari_stats",
    "user" : "senzari_dev",
    "password" : "<PASSWORD>",
    "host" : "localhost"
}

class PostgresqlPersister(BasePersister):
    def __init__ (self):
        self._connection_alive = False

    def check_alive(self):
        if self._connection_alive:
            return

        if hasattr(self, '_connection'): #Try to close any previous connection
            self._connection.close()

        connection = psycopg2.connect(database=PG_BACKEND_SETTINGS["dbname"],
                        host=PG_BACKEND_SETTINGS["host"],
                        user=PG_BACKEND_SETTINGS["user"],
                        password=PG_BACKEND_SETTINGS["password"])
        self._connection = connection
        self._connection_alive = True

    #Our persister takes for granted the databases are
    #created already (which is different to Whisper, which creates
    #one per Metric, on demand)
    def get_dbinfo(self, metric):
        '''
        Gets the database info related to a metric, as well as whether it
        exists already or not (as a tuple)
        '''
        return ('latest_stats', True)

    def update_one(self, metric, datapoint):
        value = datapoint[1]
        timestamp = datetime.datetime.fromtimestamp(int(datapoint[0]))
        log.msg("INSERTING %s" % metric)
        sql_stmt =  """
            INSERT into latest_stats(name, tstamp, value)
            VALUES ('%s', '%s', %f);
        """
        cursor = self._connection.cursor()
        cursor.execute(sql_stmt % (metric, timestamp, value))
        log.msg("successfully inserted value to metric %s" % (metric,))

    def update_many(self, metric, datapoints, dbIdentifier):
        '''
        Updates the datapoints for a metric.
        'metric' is the name of the param ('my.value')
        'datapoints' is a list of tuples, containing the timestamp and value
        '''

        '''
        Ignore the metrics with _90 for now (not sure what they do,
        besides repeating the info under some scenarios). Do the same
        with *.[upper|lower|mean], as we do not use them for now.
        '''
        if metric.endswith("_90") or metric.endswith(".upper") or
            metric.endswith(".lower") or metric.endswith("mean"):
            return

        log.msg("updating metric %s using the postgresql persister" % (metric,))
        try:
            self.check_alive()

            for datapoint in datapoints:
                self.update_one(metric, datapoint)

            self._connection.commit()
        except psycopg2.Warning, e:
{'fraction_non_alphanumeric': 0.08719743365412656, 'fraction_numerical': 0.004957713619130942, 'mean_word_length': 2.748087431693989, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '87468', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1838, 'n_words': 648}