mempool/lightning-backend/src/tasks/stats-updater.service.ts

87 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-04-19 17:37:06 +04:00
import DB from '../database';
import logger from '../logger';
import lightningApi from '../api/lightning/lightning-api-factory';
2022-04-19 17:37:06 +04:00
class LightningStatsUpdater {
constructor() {}
public async startService() {
2022-04-24 01:33:38 +04:00
logger.info('Starting Stats service');
2022-04-19 17:37:06 +04:00
const now = new Date();
const nextHourInterval = new Date(now.getFullYear(), now.getMonth(), now.getDate(), Math.floor(now.getHours() / 1) + 1, 0, 0, 0);
const difference = nextHourInterval.getTime() - now.getTime();
setTimeout(() => {
this.$logLightningStats();
setInterval(() => {
this.$logLightningStats();
2022-04-29 03:57:27 +04:00
this.$logNodeStatsDaily();
2022-04-19 17:37:06 +04:00
}, 1000 * 60 * 60);
}, difference);
2022-05-01 03:01:27 +04:00
this.$logNodeStatsDaily();
2022-04-19 17:37:06 +04:00
}
private async $logNodeStatsDaily() {
2022-04-29 03:57:27 +04:00
const currentDate = new Date().toISOString().split('T')[0];
try {
const [state]: any = await DB.query(`SELECT string FROM state WHERE name = 'last_node_stats'`);
// Only store once per day
2022-05-01 03:01:27 +04:00
if (state[0].string === currentDate) {
2022-04-29 03:57:27 +04:00
return;
}
2022-04-19 17:37:06 +04:00
2022-04-29 03:57:27 +04:00
const query = `SELECT nodes.public_key, c1.channels_count_left, c2.channels_count_right, c1.channels_capacity_left, c2.channels_capacity_right FROM nodes LEFT JOIN (SELECT node1_public_key, COUNT(id) AS channels_count_left, SUM(capacity) AS channels_capacity_left FROM channels GROUP BY node1_public_key) c1 ON c1.node1_public_key = nodes.public_key LEFT JOIN (SELECT node2_public_key, COUNT(id) AS channels_count_right, SUM(capacity) AS channels_capacity_right FROM channels GROUP BY node2_public_key) c2 ON c2.node2_public_key = nodes.public_key`;
const [nodes]: any = await DB.query(query);
// First run we won't have any nodes yet
if (nodes.length < 10) {
return;
}
2022-04-29 03:57:27 +04:00
for (const node of nodes) {
await DB.query(
`INSERT INTO node_stats(public_key, added, capacity, channels) VALUES (?, NOW(), ?, ?)`,
2022-05-05 23:19:24 +04:00
[node.public_key, (parseInt(node.channels_capacity_left || 0, 10)) + (parseInt(node.channels_capacity_right || 0, 10)),
node.channels_count_left + node.channels_count_right]);
2022-04-29 03:57:27 +04:00
}
await DB.query(`UPDATE state SET string = ? WHERE name = 'last_node_stats'`, [currentDate]);
2022-05-01 03:01:27 +04:00
logger.debug('Daily node stats has updated.');
2022-04-29 03:57:27 +04:00
} catch (e) {
logger.err('$logNodeStatsDaily() error: ' + (e instanceof Error ? e.message : e));
}
}
private async $logLightningStats() {
2022-04-19 17:37:06 +04:00
try {
2022-05-01 03:01:27 +04:00
const networkGraph = await lightningApi.$getNetworkGraph();
let total_capacity = 0;
for (const channel of networkGraph.channels) {
if (channel.capacity) {
total_capacity += channel.capacity;
}
}
2022-04-24 01:33:38 +04:00
const query = `INSERT INTO statistics(
2022-04-19 17:37:06 +04:00
added,
channel_count,
node_count,
2022-05-01 03:01:27 +04:00
total_capacity
2022-04-19 17:37:06 +04:00
)
2022-05-01 03:01:27 +04:00
VALUES (NOW(), ?, ?, ?)`;
2022-04-19 17:37:06 +04:00
await DB.query(query, [
2022-05-01 03:01:27 +04:00
networkGraph.channels.length,
networkGraph.nodes.length,
total_capacity,
2022-04-19 17:37:06 +04:00
]);
} catch (e) {
logger.err('$logLightningStats() error: ' + (e instanceof Error ? e.message : e));
}
}
}
export default new LightningStatsUpdater();