From 6c704a67f938a0867589b7e6bce6db4af608f466 Mon Sep 17 00:00:00 2001 From: Phan Anh Tuan Date: Fri, 1 Dec 2023 11:08:25 +0700 Subject: [PATCH] fix: split query event block (not in block query) --- .../horoscope-handler/handler.service.ts | 52 +++++++++++-------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/services/horoscope-handler/handler.service.ts b/src/services/horoscope-handler/handler.service.ts index 871ac06ad..0033db107 100644 --- a/src/services/horoscope-handler/handler.service.ts +++ b/src/services/horoscope-handler/handler.service.ts @@ -6,7 +6,7 @@ import { Context, ServiceBroker } from 'moleculer'; import _ from 'lodash'; import BaseService from '../../base/base.service'; import { SERVICE } from '../../common'; -import { Block, Transaction } from '../../models'; +import { Block, Transaction, Event } from '../../models'; import networks from '../../../network.json' assert { type: 'json' }; @Service({ @@ -42,23 +42,29 @@ export default class HoroscopeHandlerService extends BaseService { Record > ) { - // TODO: handler filter from request - // query to get data const { startBlock, endBlock } = ctx.params; const queryBlock = Block.query() .select('height', 'hash', 'time') - .withGraphFetched('events.[attributes]') - .modifyGraph('events', (builder) => { - builder.select('type', 'source'); - }) - .modifyGraph('events.[attributes]', (builder) => { - builder.select('key', 'value'); - }) .where('height', '>=', startBlock) .andWhere('height', '<', endBlock) .orderBy('height', 'asc'); + const queryEventBlock = Event.query() + .select('type', 'source', 'block_height as height') + .withGraphFetched('attributes') + .modifyGraph('attributes', (builder) => { + builder.select('key', 'value'); + }) + .whereIn('source', [ + Event.SOURCE.BEGIN_BLOCK_EVENT, + Event.SOURCE.END_BLOCK_EVENT, + ]) + .andWhere('block_height', '>=', startBlock) + .andWhere('block_height', '<', endBlock) + .orderBy('block_height') + .orderBy('id'); + const queryTransaction = Transaction.query() .select('height', 'hash', 'code', 'codespace', 'memo', 'index') .withGraphFetched('messages') @@ -78,28 +84,32 @@ export default class HoroscopeHandlerService extends BaseService { .andWhere('height', '<', endBlock) .orderBy('height', 'asc') .orderBy('index', 'asc'); - const [listBlock, listTransaction] = await Promise.all([ + const [listBlock, listTransaction, listEventBlock] = await Promise.all([ queryBlock, queryTransaction, + queryEventBlock, ]); const resultGroupBy = _.groupBy( - [...listBlock, ...listTransaction], + [...listBlock, ...listTransaction, ...listEventBlock], 'height' ); const listData: any[] = []; Object.keys(resultGroupBy).forEach((height) => { if (resultGroupBy[height].length > 0) { if (resultGroupBy[height][0] instanceof Block) { - const data: any = resultGroupBy[height][0]; - - // resultGroupBy[height].length === 1 means there is block only, not has any txs - if (resultGroupBy[height].length === 1) { - data.txs = []; - } else { - data.txs = resultGroupBy[height].slice(1); - } - listData.push(data); + const block: any = resultGroupBy[height].filter( + (e) => e instanceof Block + ); + const eventBlocks = resultGroupBy[height].filter( + (e) => e instanceof Event + ); + const txs = resultGroupBy[height].filter( + (e) => e instanceof Transaction + ); + block.events = eventBlocks.length === 0 ? [] : eventBlocks; + block.txs = txs.length === 0 ? [] : txs; + listData.push(block); } else { throw Error('Block not found'); }