From 2c222008201dcb071dfbb6903975e77a1aae005c Mon Sep 17 00:00:00 2001 From: softsimon Date: Sat, 10 Feb 2024 20:45:58 +0800 Subject: [PATCH] Log mempool stats on new blocks --- backend/src/api/statistics/statistics.ts | 11 +++++++++-- backend/src/api/websocket-handler.ts | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/backend/src/api/statistics/statistics.ts b/backend/src/api/statistics/statistics.ts index 494777aad..2926a4b17 100644 --- a/backend/src/api/statistics/statistics.ts +++ b/backend/src/api/statistics/statistics.ts @@ -6,6 +6,7 @@ import statisticsApi from './statistics-api'; class Statistics { protected intervalTimer: NodeJS.Timer | undefined; + protected lastRun: number = 0; protected newStatisticsEntryCallback: ((stats: OptimizedStatistic) => void) | undefined; public setNewStatisticsEntryCallback(fn: (stats: OptimizedStatistic) => void) { @@ -23,15 +24,21 @@ class Statistics { setTimeout(() => { this.runStatistics(); this.intervalTimer = setInterval(() => { - this.runStatistics(); + this.runStatistics(true); }, 1 * 60 * 1000); }, difference); } - private async runStatistics(): Promise { + public async runStatistics(skipIfRecent = false): Promise { if (!memPool.isInSync()) { return; } + + if (skipIfRecent && new Date().getTime() / 1000 - this.lastRun < 30) { + return; + } + + this.lastRun = new Date().getTime() / 1000; const currentMempool = memPool.getMempool(); const txPerSecond = memPool.getTxPerSecond(); const vBytesPerSecond = memPool.getVBytesPerSecond(); diff --git a/backend/src/api/websocket-handler.ts b/backend/src/api/websocket-handler.ts index 3091a09cf..b78389b64 100644 --- a/backend/src/api/websocket-handler.ts +++ b/backend/src/api/websocket-handler.ts @@ -23,6 +23,7 @@ import priceUpdater from '../tasks/price-updater'; import { ApiPrice } from '../repositories/PricesRepository'; import accelerationApi from './services/acceleration'; import mempool from './mempool'; +import statistics from './statistics/statistics'; interface AddressTransactions { mempool: MempoolTransactionExtended[], @@ -723,6 +724,7 @@ class WebsocketHandler { } this.printLogs(); + await statistics.runStatistics(); const _memPool = memPool.getMempool(); @@ -1014,6 +1016,8 @@ class WebsocketHandler { client.send(this.serializeResponse(response)); } }); + + await statistics.runStatistics(); } // takes a dictionary of JSON serialized values