Exemplo n.º 1
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):
Exemplo n.º 2
0
 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 '''
            cur2.execute('select max(fecha) from scripts')
Exemplo n.º 3
0
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))
    values = []
Exemplo n.º 4
0
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),
    ("I", dt, dt),
Exemplo n.º 5
0
 def initialize(self, logobj):
     LoggingConnection.initialize(self, logobj)
Exemplo n.º 6
0
 def initialize(self, logobj, mintime=0):
     LoggingConnection.initialize(self, logobj)