From ed3aa7f5160a6cb510b4be3969fa66f80170d3b0 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Wed, 6 Jul 2022 15:15:08 +0200 Subject: [PATCH] Add Lightning charts in /graph --- backend/src/api/explorer/general.routes.ts | 9 ++++- backend/src/api/explorer/statistics.api.ts | 24 +++++++++++- .../components/graphs/graphs.component.html | 32 +++++++++++----- .../src/app/graphs/graphs.routing.module.ts | 10 +++++ .../app/lightning/lightning-api.service.ts | 7 +++- .../lightning-dashboard.component.html | 6 ++- .../nodes-networks-chart.component.html | 37 +++++++------------ .../nodes-networks-chart.component.ts | 21 +++++++---- .../lightning-statistics-chart.component.html | 36 +++++++++++++++++- .../lightning-statistics-chart.component.ts | 30 ++++++++++----- 10 files changed, 154 insertions(+), 58 deletions(-) diff --git a/backend/src/api/explorer/general.routes.ts b/backend/src/api/explorer/general.routes.ts index c7af43b42..07620e84a 100644 --- a/backend/src/api/explorer/general.routes.ts +++ b/backend/src/api/explorer/general.routes.ts @@ -10,7 +10,7 @@ class GeneralLightningRoutes { app .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/search', this.$searchNodesAndChannels) .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/statistics/latest', this.$getGeneralStats) - .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/statistics', this.$getStatistics) + .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/statistics/:interval', this.$getStatistics) ; } @@ -33,7 +33,12 @@ class GeneralLightningRoutes { private async $getStatistics(req: Request, res: Response) { try { - const statistics = await statisticsApi.$getStatistics(); + const statistics = await statisticsApi.$getStatistics(req.params.interval); + const statisticsCount = await statisticsApi.$getStatisticsCount(); + res.header('Pragma', 'public'); + res.header('Cache-control', 'public'); + res.header('X-total-count', statisticsCount.toString()); + res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString()); res.json(statistics); } catch (e) { res.status(500).send(e instanceof Error ? e.message : e); diff --git a/backend/src/api/explorer/statistics.api.ts b/backend/src/api/explorer/statistics.api.ts index 71d71276b..d29bf1ed4 100644 --- a/backend/src/api/explorer/statistics.api.ts +++ b/backend/src/api/explorer/statistics.api.ts @@ -1,10 +1,21 @@ import logger from '../../logger'; import DB from '../../database'; +import { Common } from '../common'; class StatisticsApi { - public async $getStatistics(): Promise { + public async $getStatistics(interval: string | null = null): Promise { + interval = Common.getSqlInterval(interval); + + let query = `SELECT UNIX_TIMESTAMP(added) AS added, channel_count, node_count, total_capacity, tor_nodes, clearnet_nodes, unannounced_nodes + FROM lightning_stats`; + + if (interval) { + query += ` WHERE added BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()`; + } + + query += ` ORDER BY id DESC`; + try { - const query = `SELECT UNIX_TIMESTAMP(added) AS added, channel_count, node_count, total_capacity, tor_nodes, clearnet_nodes, unannounced_nodes FROM lightning_stats ORDER BY id DESC`; const [rows]: any = await DB.query(query); return rows; } catch (e) { @@ -27,6 +38,15 @@ class StatisticsApi { } } + public async $getStatisticsCount(): Promise { + try { + const [rows]: any = await DB.query(`SELECT count(*) as count FROM lightning_stats`); + return rows[0].count; + } catch (e) { + logger.err('$getLatestStatistics error: ' + (e instanceof Error ? e.message : e)); + throw e; + } + } } export default new StatisticsApi(); diff --git a/frontend/src/app/components/graphs/graphs.component.html b/frontend/src/app/components/graphs/graphs.component.html index 59aa61aa1..8914adf0a 100644 --- a/frontend/src/app/components/graphs/graphs.component.html +++ b/frontend/src/app/components/graphs/graphs.component.html @@ -1,7 +1,10 @@ -