From 3d3d1a55a8828725bc601c9aaaedab647f1dc989 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Sun, 18 Feb 2024 14:38:45 -0500 Subject: [PATCH] share stats --- src/models/StratumV1ClientStatistics.ts | 34 +++++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/models/StratumV1ClientStatistics.ts b/src/models/StratumV1ClientStatistics.ts index 5623865..47b7c1e 100644 --- a/src/models/StratumV1ClientStatistics.ts +++ b/src/models/StratumV1ClientStatistics.ts @@ -41,9 +41,11 @@ export class StratumV1ClientStatistics { }); - if (this.currentTimeSlot == null) { + // First record, insert it this.currentTimeSlot = timeSlot; + this.shares += targetDifficulty; + this.acceptedCount++; await this.clientStatisticsService.insert({ time: this.currentTimeSlot, clientId: client.id, @@ -55,6 +57,21 @@ export class StratumV1ClientStatistics { }); this.lastSave = new Date().getTime(); } else if (this.currentTimeSlot != timeSlot) { + // Transitioning to a new time slot, + // First update the old time slot with the latest data + await this.clientStatisticsService.update({ + time: this.currentTimeSlot, + clientId: client.id, + shares: this.shares, + acceptedCount: this.acceptedCount, + address: client.address, + clientName: client.clientName, + sessionId: client.sessionId + }); + // Set the new time slot and add incoming shares then insert it + this.currentTimeSlot = timeSlot; + this.shares = targetDifficulty; + this.acceptedCount = 1 await this.clientStatisticsService.insert({ time: this.currentTimeSlot, clientId: client.id, @@ -64,11 +81,11 @@ export class StratumV1ClientStatistics { clientName: client.clientName, sessionId: client.sessionId }); - this.shares = 0; - this.acceptedCount = 0; - this.currentTimeSlot = timeSlot; this.lastSave = new Date().getTime(); } else if ((date.getTime() - 60 * 1000) > this.lastSave) { + // If we haven't saved for a minute, update the table + this.shares += targetDifficulty; + this.acceptedCount++; await this.clientStatisticsService.update({ time: this.currentTimeSlot, clientId: client.id, @@ -79,12 +96,13 @@ export class StratumV1ClientStatistics { sessionId: client.sessionId }); this.lastSave = new Date().getTime(); + } else { + // Accept the shares if none of the prior conditions are met, + // saving to memory for storing later + this.shares += targetDifficulty; + this.acceptedCount++; } - - this.shares += targetDifficulty; - this.acceptedCount++; - } public getSuggestedDifficulty(clientDifficulty: number) {