Beispiel #1
0
 def cxn(self):
     if self._cxn is None:
         self._cxn = LoggingConnection(self.dsn)
         self._cxn.initialize(self._log)
         register_uuid()
         register_inet()
     return self._cxn
Beispiel #2
0
class PG(object):
    def __init__(self, dsn, logger=None, cxn_init=None):
        self.dsn = dsn
        self._log = logger or log
        self._cxn = None
        self._mode = None

    @property
    def cxn(self):
        if self._cxn is None:
            self._cxn = LoggingConnection(self.dsn)
            self._cxn.initialize(self._log)
            register_uuid()
            register_inet()
        return self._cxn

    @contextmanager
    def txn(self, readonly=False):
        mode = serializable_ro if readonly else serializable_rw
        with self.cxn:
            if self._mode != mode:
                self.cxn.set_session(**mode)
                self._mode = mode
            with self.cxn.cursor() as cursor:
                yield cursor

    def listen(self, *channels):
        with self.cxn:
            with self.cxn.cursor() as cursor:
                for channel in channels:
                    cursor.execute('LISTEN %s' % channel)

    def unlisten(self, *channels):
        with self.cxn:
            with self.cxn.cursor() as cursor:
                for channel in channels:
                    cursor.execute('UNLISTEN %s' % channel)

    def notify(self, channel, message=None):
        with self.cxn:
            with self.cxn.cursor() as cursor:
                cursor.execute('NOTIFY %s' % channel if message is None else
                               'NOTIFY %s, %s' % (channel, message))

    def await(self, upto=0.1):
Beispiel #3
0
def create_call_trace_table(connection: LoggingConnection):
    with connection.cursor() as cursor:
        query = """
CREATE TABLE IF NOT EXISTS {func_table} (
    id          SERIAL PRIMARY KEY ,
    module      TEXT,
    qualname    TEXT,
    arg_types   JSONB,
    return_type JSONB,
    yield_type  JSONB
);

CREATE TABLE IF NOT EXISTS {class_table} (
    id SERIAL PRIMARY KEY ,
    module TEXT,
    qualname TEXT,
    props JSONB
)
""".format(func_table=FUNCTION_CALLS_TABLE, class_table=CLASS_PROPERTIES_TABLE)

        cursor.execute(query)
    connection.commit()
Beispiel #4
0
 def cursor(self, *args, **kwargs):
     kwargs.setdefault('cursor_factory', MyLoggingCursor)
     return LoggingConnection.cursor(self, *args, **kwargs)
Beispiel #5
0
 public | user_password      | table | users
 public | users              | table | users


"""
from psycopg2.extras import LoggingConnection

if __name__ == '__main__':
    import sys

    dsn = "host='{}' user='******' password='******' dbname={}".format(
        os.environ['OLD_USERS_DB_HOST'], os.environ['OLD_USERS_DB_USER'],
        os.environ['OLD_USERS_DB_PASSWORD'], os.environ['OLD_USERS_DB_NAME'])
    conn = psycopg2.connect(dsn)
    logfile = open('/tmp/db.log', 'a')
    conn = LoggingConnection(dsn)
    conn.initialize(logfile)

    cur = conn.cursor()
    try:
        dsn2 = "host='{}' user='******' password='******' dbname={}".format(
            os.environ['USERS_DB_HOST'], os.environ['USERS_DB_USER'],
            os.environ['USERS_DB_PASSWORD'], os.environ['USERS_DB_NAME'])
        conn2 = psycopg2.connect(dsn2)
        logfile2 = open('/tmp/db_new.log', 'a')
        conn2 = LoggingConnection(dsn2)
        conn2.initialize(logfile2)
        cur2 = conn2.cursor(cursor_factory=DictCursor)
        try:
            fecha = None
            ''' sinc usuarios '''
"""
Analytics consumer - pull events from analytics events queue and fan out to
worker lambdas.
"""
import logging
import json
import os

from psycopg2.extras import execute_values, LoggingConnection

DB_URL = os.environ['DB_URL']
TABLE_NAME = os.environ['TABLE_NAME']

logger = logging.getLogger()
logger.setLevel(logging.INFO)  # set to DEBUG to log SQL queries
conn = LoggingConnection(DB_URL)
conn.initialize(logger)

EVENT_KEYS = [
    'event_id', 'event_timestamp', 'event_type', 'event_version', 'app_title',
    'app_version', 'user_id', 'user_name', 'meta', 'token_payload'
]
JSON_FIELDS = ['meta', 'token_payload']
INSERT_QUERY = f"""
    INSERT INTO {TABLE_NAME} ({', '.join(EVENT_KEYS)}) VALUES %s
    ON CONFLICT (event_id) DO NOTHING
"""


def main(event, context):
    logger.info('Received %d event items', len(event))
Beispiel #7
0
import urlparse
import logging
import pandas as pd
import os
import urllib
import shutil

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
hdlr = logging.FileHandler('import_errors.log')
logger.addHandler(hdlr)
logger.info("getting started")

# Set up DB connection
conn_string = os.environ.get('DATABASE_URL') or "dbname=bactdbtest user=matthew"
conn = LoggingConnection(conn_string)
LoggingConnection.initialize(conn, logger)
c = conn.cursor()

dt = datetime.datetime.now()
logger.info(dt)
text_measurements = [
    ("+", dt, dt),
    ("-", dt, dt),
    ("(+)", dt, dt),
    ("(-)", dt, dt),
    ("+/-", dt, dt),
    ("-/+", dt, dt),
    ("TD", dt, dt),
    ("NR", dt, dt),
    ("S", dt, dt),
Beispiel #8
0
 def initialize(self, logobj):
     LoggingConnection.initialize(self, logobj)
Beispiel #9
0
 def cursor(self, *args, **kwargs):
     kwargs.setdefault('cursor_factory', PerfLoggingCursor)
     return LoggingConnection.cursor(self, *args, **kwargs)
Beispiel #10
0
 def initialize(self, logobj, mintime=0):
     LoggingConnection.initialize(self, logobj)