Skip to content

Commit

Permalink
BE-697 Add a column for block size on BLOCK view
Browse files Browse the repository at this point in the history
The size is approximate size calculated from block data(json) given via fabric node sdk.

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>
Change-Id: Id06b431aa01e3cee1eb0a5f598a415ad1fb2135b
  • Loading branch information
nekia committed Nov 1, 2019
1 parent 010f25c commit d99e177
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 251 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
"auth",
"authroutes",
"axios",
"blksize",
"blockactivity",
"blocksbyhour",
"blockandtx",
Expand Down Expand Up @@ -358,6 +359,7 @@
"txhash",
"robertfeng",
"vchinoy",
"Uint8",
"unhandled",
"utils",
"util",
Expand Down Expand Up @@ -421,7 +423,9 @@
"pltfrm",
"SAMEORIGIN",
"csurf",
"_csrf-hl-expl"
"_csrf-hl-expl",
"Uint8",
"Uint8Array"
],
"skipIfMatch": ["http://[^s]*"],
"skipWordIfMatch": ["^foobar.*$"],
Expand Down
4 changes: 2 additions & 2 deletions app/persistence/fabric/CRUDService.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CRUDService {
*/

getBlockActivityList(channel_genesis_hash) {
const sqlBlockActivityList = `select blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt,(
const sqlBlockActivityList = `select blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt, (
SELECT array_agg(txhash) as txhash FROM transactions where blockid = blocks.blocknum and
channel_genesis_hash = '${channel_genesis_hash}' group by transactions.blockid ),
channel.name as channelname from blocks inner join channel on blocks.channel_genesis_hash = channel.channel_genesis_hash where
Expand Down Expand Up @@ -105,7 +105,7 @@ class CRUDService {
}
const sqlBlockTxList = `select a.* from (
select (select c.name from channel c where c.channel_genesis_hash =
'${channel_genesis_hash}' ) as channelname, blocks.blocknum,blocks.txcount ,blocks.datahash ,blocks.blockhash ,blocks.prehash,blocks.createdt,(
'${channel_genesis_hash}' ) 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 ${blockTxListSql} and
channel_genesis_hash = '${channel_genesis_hash}' and createdt between '${from}' and '${to}') from blocks where
blocks.channel_genesis_hash ='${channel_genesis_hash}' and blocknum >= 0 and blocks.createdt between '${from}' and '${to}'
Expand Down
3 changes: 2 additions & 1 deletion app/persistence/fabric/postgreSQL/db/explorerpg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ CREATE TABLE blocks
createdt Timestamp DEFAULT NULL,
prev_blockhash character varying(256) DEFAULT NULL,
blockhash character varying(256) DEFAULT NULL,
channel_genesis_hash character varying(256) DEFAULT NULL
channel_genesis_hash character varying(256) DEFAULT NULL,
blksize integer DEFAULT NULL
);

ALTER table blocks owner to :user;
Expand Down
52 changes: 50 additions & 2 deletions app/platform/fabric/sync/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@ class SyncServices {
createdt,
prev_blockhash: '',
blockhash,
channel_genesis_hash
channel_genesis_hash,
blksize: jsonObjSize(block)
};
const txLen = block.data.data.length;
for (let i = 0; i < txLen; i++) {
Expand Down Expand Up @@ -651,7 +652,8 @@ class SyncServices {
} tx`,
time: createdt,
txcount: block.data.data.length,
datahash: block.header.data_hash
datahash: block.header.data_hash,
blksize: block_row.blksize
};

_self.platform.send(notify);
Expand Down Expand Up @@ -701,3 +703,49 @@ function convertValidationCode(code) {
}
return _validation_codes[code];
}

// Calculate data size of json object
function jsonObjSize(json) {
let bytes = 0;

function sizeOf(obj) {
if (obj !== null && obj !== undefined) {
switch (typeof obj) {
case 'number': {
bytes += 8;
break;
}
case 'string': {
bytes += obj.length;
break;
}
case 'boolean': {
bytes += 4;
break;
}
case 'object': {
const objClass = Object.prototype.toString.call(obj).slice(8, -1);
if (objClass === 'Object' || objClass === 'Array') {
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
sizeOf(obj[key]);
}
} else {
bytes += obj.length;
}
break;
}
default:
console.log(typeof obj);
break;
}
}
return bytes;
}

function formatByteSize(rawByte) {
return (rawByte / 1024).toFixed(0);
}

return formatByteSize(sizeOf(json));
}
3 changes: 2 additions & 1 deletion app/test/blockandtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ describe('GET /api/blockAndTxList/:channel/:blocknum', () => {
'createdt',
'blockhash',
'prev_blockhash',
'txhash'
'txhash',
'blksize'
);
}
done();
Expand Down
Loading

0 comments on commit d99e177

Please sign in to comment.