diff --git a/app/persistence/fabric/CRUDService.ts b/app/persistence/fabric/CRUDService.ts index 283058177..26ba30ee4 100644 --- a/app/persistence/fabric/CRUDService.ts +++ b/app/persistence/fabric/CRUDService.ts @@ -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; } diff --git a/app/platform/fabric/Proxy.ts b/app/platform/fabric/Proxy.ts index dd19657a0..b02caa869 100644 --- a/app/platform/fabric/Proxy.ts +++ b/app/platform/fabric/Proxy.ts @@ -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 */ @@ -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);