Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latest Block Time Update #457

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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