async def save(self, buf_len=0): if not self.__track: return # code converts dictionaries of lists to list of dictionaries # https://stackoverflow.com/questions/5558418/list-of-dicts-to-from-dict-of-lists # v = [dict(zip(DL, t)) for t in zip(*DL.values())] metrics_len = len(self.__metrics['timestamp']) if metrics_len > buf_len: # a table will be created the first time metrics are being saved # this increases the likelihood of complete columnss if 'table' not in self.__db or self.__db['table'] is None: table_name = self.__db.pop('table_name') await db_utils.create_table( db_string=self.__db['path'], table_type='custom', custom_table=self.__create_metrics_table(table_name)) self.__db['table'] = db_utils.get_table( self.__db['path'], table_name) if self.__db['table'] is None: return metrics = { key: value for key, value in self.__metrics.items() if value } metrics = [dict(zip(metrics, t)) for t in zip(*metrics.values())] self.__metrics = { key: value[metrics_len:] for key, value in self.__metrics.items() } asyncio.create_task( db_utils.dump_data(metrics, self.__db['path'], self.__db['table']))
async def open_db(self, db_string, table_name): if not self.save_transactions: return self.__db['path'] = db_string self.__db['table_name'] = table_name if 'table' not in self.__db or self.__db['table'] is None: table_name = self.__db.pop('table_name') + '_market' await db_utils.create_table(db_string=db_string, table_type='market', table_name=table_name) self.__db['table'] = db_utils.get_table(db_string, table_name)
async def open_db(self, db_path): """Opens connection to the database where load and generation profiles are stored. Also stores references to the database object and the table object Args: db_path ([type]): [description] """ self.__profile_db['path'] = db_path self.__profile_db['db'] = databases.Database(db_path) self.__profile_db['table'] = db_utils.get_table( db_path, self.participant_id) if 'table' in self.__profile_db or self.__profile_db[ 'table'] is not None: await self.__profile_db['db'].connect()
async def fetch_one(self, timestamp): if 'db' not in self.__db: self.__db['db'] = databases.Database(self.__db['path']) if 'table' not in self.__db or self.__db['table'] is None: table_name = self.__db.pop('table_name') self.__db['table'] = db_utils.get_table(self.__db['path'], table_name) await self.__db['db'].connect() table = self.__db['table'] query = table.select().where(table.c.timestamp == timestamp) async with self.__db['db'].transaction(): row = await self.__db['db'].fetch_one(query) return row['actions_dict']