def rebuild_account_info_snapshot(self): harvester = PolkascanHarvesterService( db_session=self.session, type_registry=TYPE_REGISTRY, type_registry_file=TYPE_REGISTRY_FILE) last_full_snapshot_block_nr = 0 self.session.execute('truncate table {}'.format( AccountInfoSnapshot.__tablename__)) for account_id, block_id in self.session.query( SearchIndex.account_id, SearchIndex.block_id ).filter(SearchIndex.block_id >= settings.BALANCE_SYSTEM_ACCOUNT_MIN_BLOCK ).order_by('block_id').group_by( SearchIndex.account_id, SearchIndex.block_id).yield_per(1000): if block_id > last_full_snapshot_block_nr + settings.BALANCE_FULL_SNAPSHOT_INTERVAL: last_full_snapshot_block_nr = block_id - block_id % settings.BALANCE_FULL_SNAPSHOT_INTERVAL harvester.create_full_balance_snaphot(last_full_snapshot_block_nr) self.session.commit() else: harvester.create_balance_snapshot(block_id, account_id) self.session.commit() # set balances according to most recent snapshot account_info = self.session.execute(""" select a.account_id, a.balance_total, a.balance_free, a.balance_reserved, a.nonce from data_account_info_snapshot as a inner join ( select account_id, max(block_id) as max_block_id from data_account_info_snapshot group by account_id ) as b on a.account_id = b.account_id and a.block_id = b.max_block_id """) for account_id, balance_total, balance_free, balance_reserved, nonce in account_info: Account.query(self.session).filter_by(id=account_id).update( { Account.balance_total: balance_total, Account.balance_free: balance_free, Account.balance_reserved: balance_reserved, Account.nonce: nonce, }, synchronize_session='fetch') self.session.commit() return {'result': 'account info snapshots rebuilt'}
def update_balances_in_block(self, block_id): harvester = PolkascanHarvesterService(self.session, type_registry=TYPE_REGISTRY) harvester.create_full_balance_snaphot(block_id) self.session.commit() harvester.update_account_balances() self.session.commit() return 'Snapshot created for block {}'.format(block_id)