def store(**kwargs): params = kwargs.copy() params['pid'] = os.getpid() params['created_at'] = tz.localize(datetime.now()) params['terminated'] = False with get_reql_connection(db=True) as conn: res = r.table('tasks').insert(params).run(conn) return res['generated_keys'][0]
def alert(self, anomaly): with db.get_reql_connection(db=True) as conn: r.table('alerts').insert([{ 'time': anomaly["time"], 'trade_pk': anomaly["id"], 'description': anomaly["description"], 'reviewed': False, 'error_code': anomaly["error_code"], 'severity': anomaly["severity"], 'symbol': anomaly["symbol"] }]).run(conn, durability='soft')
def __init__(self, args): ''' First, parse command line arguments and decide what to do: --init-db -> initialise db (tables etc) --reset-db -> delete tables and data -f trades.csv -> import trades from file -s cs261.dcs.warwick.ac.uk -p 80 -> import trades from live stream ''' global TASK_ENDED global TASK_PK # Drop or initialise the PostgreSQL db as necessary if args.reset_db: db.drop_tables() if args.init_db: db.create_tables() # Check whether there is no analysis happening currently if args.file or args.stream_url: with db.get_reql_connection(db=True) as conn: task_count = r.table('tasks').filter( r.row['terminated'] == False).count().run(conn) if task_count: notification_manager.add( level='warning', title='Cannot launch task', message= 'End current task before you can start new analysis', datetime=tz.localize(datetime.now())) return # Analyse a file if args.file: TASK_PK = task_manager.store(task='analysis', type='file') self.from_file(args.file) # Analyse a stream if args.stream_url: port = args.port or 80 TASK_PK = task_manager.store(task='analysis', type='stream') self.from_stream(url=args.stream_url, port=port)
def end(pk): with get_reql_connection(db=True) as conn: r.table('tasks').get(pk).update({ 'ended_at': tz.localize(datetime.now()), 'terminated': True }).run(conn)
def add(self): with get_reql_connection(db=True) as conn: pass
def add(self, **kwargs): with get_reql_connection(db=True) as conn: r.table('notifications').insert([ kwargs ]).run(conn, durability='soft')
def test_reql_connection(): # assume rethinkdb is running, otherwise connection will fail with db.get_reql_connection() as conn: assert conn.is_open() == True