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 May 4, 2023
1 parent 925a10d commit bee7c6b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 34 deletions.
89 changes: 69 additions & 20 deletions app/persistence/fabric/CRUDService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,40 +135,89 @@ 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;
if(orgs == null || orgs.length == 0 ) {
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)`;
} else {
sqlBlockTxList =`SELECT c.name AS channelname,
b.blocknum, b.txcount, b.datahash, b.blockhash, b.prehash,b.createdt, b.blksize,
array_agg(t.txhash) AS txhash
FROM channel c
INNER JOIN blocks b ON b.channel_genesis_hash = c.channel_genesis_hash AND
b.network_name = c.network_name
INNER JOIN transactions t ON t.blockid = b.blocknum AND t.channel_genesis_hash = c.channel_genesis_hash
AND t.network_name = c.network_name AND t.createdt between $3 and $4 = c.createdt between $3 and $4
AND t.creator_msp_id IS NOT NULL AND t.creator_msp_id != ' ' AND length(t.creator_msp_id) > 0
WHERE c.channel_genesis_hash =$1 AND c.network_name = $2 AND b.blocknum >= 0 ${byOrgs} AND b.createdt between $3 and $4
GROUP BY c.name, b.blocknum, b.txcount, b.datahash, b.blockhash, b.prehash,b.createdt, b.blksize
ORDER BY b.blocknum DESC
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(orgs == null || orgs.length == 0 ) {
sqlBlockTxCount = `SELECT COUNT(DISTINCT blocks.blocknum) FROM blocks
JOIN transactions ON blocks.blocknum = transactions.blockid
WHERE blockid = blocks.blocknum ${byOrgs} AND
blocknum >= 0 AND blocks.channel_genesis_hash = $1 AND blocks.network_name = $2 AND
blocks.createdt between $3 AND $4`
} else {
sqlBlockTxCount = `SELECT COUNT(DISTINCT blocks.blocknum) FROM blocks
JOIN transactions ON blocks.blocknum = transactions.blockid
WHERE blockid = blocks.blocknum ${byOrgs}
AND blocks.channel_genesis_hash = $1 and blocks.network_name = $2 AND blocks.createdt between $3 AND $4
AND transactions.creator_msp_id IS NOT NULL AND transactions.creator_msp_id != ' ' AND length(creator_msp_id) > 0`
}
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 +554,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 bee7c6b

Please sign in to comment.