Example #1
0
    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']))
Example #2
0
    async def record_transactions(self,
                                  buf_len=0,
                                  delay=True,
                                  check_table_len=False):
        """This function records the transaction records into the ledger

        """

        if check_table_len:
            table_len = db_utils.get_table_len(self.__db['path'],
                                               self.__db['table'])
            if table_len < self.transactions_count:
                return False

        if delay and buf_len:
            delay = buf_len / 100
            ts = datetime.datetime.now().timestamp()
            if ts - self.__transaction_last_record_time < delay:
                return False

        transactions_len = len(self.__transactions)
        if transactions_len < buf_len:
            return False

        transactions = self.__transactions[:transactions_len]
        asyncio.create_task(
            db_utils.dump_data(transactions, self.__db['path'],
                               self.__db['table']))

        self.__transaction_last_record_time = datetime.datetime.now(
        ).timestamp()
        del self.__transactions[:transactions_len]
        self.transactions_count += transactions_len
        return True
Example #3
0
    async def save_weights(self, **kwargs):
        '''
        Save the price tables at the end of the episode into database
        '''

        if 'validation' in kwargs['market_id']:
            return True

        self.status['weights_saving'] = True
        self.status['weights_saved'] = False

        weights = [{
            'generation': kwargs['generation'],
            'bid_prices': str(self.bid_prices),
            'ask_prices': str(self.ask_prices)
        }]

        asyncio.create_task(
            db_utils.dump_data(weights, kwargs['output_db'],
                               self.__db['table']))
        self.status['weights_saving'] = False
        self.status['weights_saved'] = True
        return True