예제 #1
0
파일: realtime.py 프로젝트: ajmal017/cs261
    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]
예제 #2
0
파일: analysis.py 프로젝트: ajmal017/cs261
 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')
예제 #3
0
    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)
예제 #4
0
파일: realtime.py 프로젝트: ajmal017/cs261
 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)
예제 #5
0
파일: realtime.py 프로젝트: ajmal017/cs261
 def add(self):
     with get_reql_connection(db=True) as conn:
         pass
예제 #6
0
파일: realtime.py 프로젝트: ajmal017/cs261
 def add(self, **kwargs):
     with get_reql_connection(db=True) as conn:
         r.table('notifications').insert([
             kwargs
         ]).run(conn, durability='soft')
예제 #7
0
파일: test_db.py 프로젝트: ajmal017/cs261
def test_reql_connection():
    # assume rethinkdb is running, otherwise connection will fail
    with db.get_reql_connection() as conn:
        assert conn.is_open() == True