Skip to content

Commit

Permalink
Latest Block Time Update
Browse files Browse the repository at this point in the history
Signed-off-by: Udhayakumari <[email protected]>
  • Loading branch information
Udhayakumari authored and ArchanaArige committed Oct 31, 2023
1 parent 7c506ba commit 96ecf13
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
15 changes: 10 additions & 5 deletions app/persistence/fabric/CRUDService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,19 +505,24 @@ export class CRUDService {
}

/**
* Returns the channels data
*
*
* @param {*} peerid
* @param {*} network_name
* @returns
* @memberof CRUDService
*/
async getChannelsInfo(network_name) {
const channels = await this.sql.getRowsBySQlNoCondition(
` select c.id as id,c.name as channelName,c.blocks as blocks ,c.channel_genesis_hash as channel_genesis_hash,c.trans as transactions,c.createdt as createdat,c.channel_hash as channel_hash from channel c,
peer_ref_channel pc where c.channel_genesis_hash = pc.channelid and c.network_name = $1 group by c.id ,c.name ,c.blocks ,c.trans ,c.createdt ,c.channel_hash,c.channel_genesis_hash order by c.name `,
` SELECT c.id as id, c.name as channelName, c.blocks as blocks, c.channel_genesis_hash as channel_genesis_hash,
c.trans as transactions, c.createdt as createdat, c.channel_hash as channel_hash, MAX(blocks.createdt)
as latestDate FROM channel c
INNER JOIN blocks ON c.channel_genesis_hash = blocks.channel_genesis_hash AND c.network_name = blocks.network_name
INNER JOIN peer_ref_channel pc ON c.channel_genesis_hash = blocks.channel_genesis_hash AND c.network_name = pc.network_name
WHERE c.network_name = $1
GROUP BY c.id, c.name, c.blocks, c.trans, c.createdt, c.channel_hash, c.channel_genesis_hash
ORDER BY c.name `,
[network_name]
);

return channels;
}

Expand Down
36 changes: 34 additions & 2 deletions app/platform/fabric/Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,39 @@ export class Proxy {
}

/**
* Returns the latest block time of the channel
*
*
* @param {*} channel
* @returns
* @memberof Proxy
*/
getLatestBlockTime(channel) {
let latestBlockEntry: Date;
let agoBlockTime: string;
latestBlockEntry = channel.latestdate;
const latestBlockEntryTime = latestBlockEntry.getTime();
const currentBlockDate = Date.now();
const agoBlockTimeDiff = currentBlockDate - latestBlockEntryTime;
const seconds = Math.floor(agoBlockTimeDiff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (days > 0) {
agoBlockTime = days + 'day(s)';
} else if (hours > 0) {
agoBlockTime = hours + 'hour(s)';
} else if (minutes > 0) {
agoBlockTime = minutes + 'minute(s)';
} else if (seconds > 0) {
agoBlockTime = seconds + 'second(s)';
}
return agoBlockTime;
}

/**
* Returns the channel data with latest block time
*
* @param {*} network_id
* @returns
* @memberof Proxy
*/
Expand All @@ -202,11 +233,12 @@ export class Proxy {
const currentchannels = [];
for (const channel of channels) {
const channel_genesis_hash = client.getChannelGenHash(channel.channelname);
let agoBlockTimes = this.getLatestBlockTime(channel);
if (
channel_genesis_hash &&
channel_genesis_hash === channel.channel_genesis_hash
) {
currentchannels.push(channel);
currentchannels.push({ ...channel, agoBlockTimes });
}
}
logger.debug('getChannelsInfo >> %j', currentchannels);
Expand Down

0 comments on commit 96ecf13

Please sign in to comment.