Skip to content

Commit

Permalink
Add blocks pagination in the backend
Browse files Browse the repository at this point in the history
Signed-off-by: Udhayakumari <pudhayakumari95@gmail.com>
  • Loading branch information
Udhayakumari committed Apr 4, 2023
1 parent 925a10d commit 9ecc41d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 34 deletions.
78 changes: 58 additions & 20 deletions app/persistence/fabric/CRUDService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,40 +135,78 @@ export class CRUDService {
*
* @param {*} channel_genesis_hash
* @param {*} blockNum
* @param {*} txid
* @param {*} from
* @param {*} to
* @param {*} page
* @param {*} size
* @param {*} orgs
* @returns
* @memberof CRUDService
*/
getBlockAndTxList(

async getBlockAndTxList(
network_name: any,
channel_genesis_hash: any,
blockNum: any,
from: any,
to: any,
orgs: string[]
orgs: string[],
page: number,
size: number
) {
const values = [channel_genesis_hash, network_name, from, to];
let byOrgs = '';
var countOfBlocks: number;
let byOrgs = ' ';
const values = [channel_genesis_hash, network_name, from, to, page, size];
if (orgs && orgs.length > 0) {
values.push(orgs);
byOrgs = ' and creator_msp_id = ANY($5)';
byOrgs = ' and creator_msp_id = ANY($7)';
}

logger.debug('getBlockAndTxList.byOrgs ', byOrgs);

const sqlBlockTxList = `select a.* from (
select (select c.name from channel c where c.channel_genesis_hash =$1 and c.network_name = $2)
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
SELECT array_agg(txhash) as txhash FROM transactions where blockid = blocks.blocknum ${byOrgs} and
channel_genesis_hash = $1 and network_name = $2 and createdt between $3 and $4) from blocks where
blocks.channel_genesis_hash =$1 and blocks.network_name = $2 and blocknum >= 0 and blocks.createdt between $3 and $4
order by blocks.blocknum desc) a where a.txhash IS NOT NULL`;

logger.debug('sqlBlockTxList ', sqlBlockTxList);

return this.sql.getRowsBySQlQuery(sqlBlockTxList, values);
let sqlBlockTxList = `select a.* from (
select (select c.name from channel c where c.channel_genesis_hash =$1 and c.network_name = $2)
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
SELECT array_agg(txhash) as txhash FROM transactions where blockid = blocks.blocknum ${byOrgs} and
channel_genesis_hash = $1 and network_name = $2 and createdt between $3 and $4) from blocks where
blocks.channel_genesis_hash =$1 and blocks.network_name = $2 and blocknum >= 0 and blocks.createdt between $3 and $4
order by blocks.blocknum desc) a where a.txhash IS NOT NULL LIMIT $6 OFFSET (($5 - 1) * $6)`;
if (page == 1) {
let sqlBlockTxCount: string;
let byOrgs = ' ';
const filterValues = [channel_genesis_hash, network_name, from, to];
if (orgs && orgs.length > 0) {
filterValues.push(orgs);
byOrgs = ' and creator_msp_id = ANY($5)';
}
if (byOrgs == null || '') {
sqlBlockTxCount = `select count(*) from (
select (select c.name from channel c where c.channel_genesis_hash =$1 and c.network_name = $2)
as channelname, blocks.blocknum, blocks.txcount, blocks.datahash, blocks.blockhash, blocks.prehash, blocks.createdt, blocks.blksize, (
SELECT creator_msp_id FROM transactions where blockid = blocks.blocknum ${byOrgs} and
channel_genesis_hash = $1 and network_name = $2 and createdt between $3 and $4
AND creator_msp_id IS NOT NULL AND creator_msp_id != ' ') from blocks where
blocks.channel_genesis_hash =$1 and blocks.network_name = $2 and blocknum >= 0 and blocks.createdt between $3 and $4
order by blocks.blocknum desc) a where creator_msp_id IS NOT NULL AND length(creator_msp_id) > 0 `;
} else {
sqlBlockTxCount = `select count(*) from (
select (select c.name from channel c where c.channel_genesis_hash =$1 and c.network_name = $2)
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
SELECT creator_msp_id FROM transactions where blockid = blocks.blocknum ${byOrgs} and
channel_genesis_hash = $1 and network_name = $2 and createdt between $3 and $4) from blocks where
blocks.channel_genesis_hash =$1 and blocks.network_name = $2 and blocknum >= 0 and blocks.createdt between $3 and $4
order by blocks.blocknum desc) a where creator_msp_id IS NOT NULL`;
}
countOfBlocks = await this.sql.getRowsCountBySQlQuery(
sqlBlockTxCount,
filterValues
);
}
let blocksData = await this.sql.getRowsBySQlQuery(sqlBlockTxList, values);
let noOfpages = Math.ceil(countOfBlocks / size);
let response = {
blocksData: blocksData,
noOfpages: noOfpages
};
return response;
}

/**
Expand Down Expand Up @@ -505,7 +543,7 @@ export class CRUDService {
* @returns
* @memberof CRUDService
*/
async getBlockByBlocknum(network_name:any, channel_genesis_hash:any, blockNo:any) {
async getBlockByBlocknum(network_name: any, channel_genesis_hash: any, blockNo: any) {
const sqlBlockTxList = `select a.* from (
select (select c.name from channel c where c.channel_genesis_hash =$1 and c.network_name = $2)
as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, blocks.blksize, (
Expand Down
37 changes: 23 additions & 14 deletions app/rest/dbroutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ export function dbroutes(router: Router, platform: Platform) {
* GET /blockAndTxList
* curl -i 'http://<host>:<port>/blockAndTxList/channel_genesis_hash/<blockNum>/<limitrows>/<offset>'
* Response:
* {'rows':[{'id':51,'blocknum':50,'datahash':'374cceda1c795e95fc31af8f137feec8ab6527b5d6c85017dd8088a456a68dee',
* {'rows': { "blocksData":[{'id':51,'blocknum':50,'datahash':'374cceda1c795e95fc31af8f137feec8ab6527b5d6c85017dd8088a456a68dee',
* 'prehash':'16e76ca38975df7a44d2668091e0d3f05758d6fbd0aab76af39f45ad48a9c295','channelname':'mychannel','txcount':1,
* 'createdt':'2018-03-13T15:58:45.000Z','txhash':['6740fb70ed58d5f9c851550e092d08b5e7319b526b5980a984b16bd4934b87ac']}]}
* 'createdt':'2018-03-13T15:58:45.000Z','txhash':['6740fb70ed58d5f9c851550e092d08b5e7319b526b5980a984b16bd4934b87ac']}], "noOfpages": 1}
*/
router.get(
'/blockAndTxList/:channel_genesis_hash/:blocknum',
Expand All @@ -252,20 +252,29 @@ export function dbroutes(router: Router, platform: Platform) {
req.query.from as string,
req.query.to as string
);
if (channel_genesis_hash && !isNaN(blockNum)) {
const { page, size } = req.query;
if (channel_genesis_hash ) {
const extReq = (req as unknown) as ExtRequest;
return dbCrudService
.getBlockAndTxList(
extReq.network,
channel_genesis_hash,
blockNum,
from,
to,
orgs
)
.then(handleResult(req, res));
let data = await dbCrudService.getBlockAndTxList(
extReq.network,
channel_genesis_hash,
blockNum,
from,
to,
orgs,
page,
size
);
if (data) {
return res.send({
status: 200,
rows: data
});
}
return requtil.notFound(req, res);
} else {
return requtil.invalidRequest(req, res);
}
return requtil.invalidRequest(req, res);
}
);

Expand Down

0 comments on commit 9ecc41d

Please sign in to comment.