diff --git a/src/models/StratumV1ClientStatistics.ts b/src/models/StratumV1ClientStatistics.ts index 7f3eb2e..c5c5fc9 100644 --- a/src/models/StratumV1ClientStatistics.ts +++ b/src/models/StratumV1ClientStatistics.ts @@ -22,7 +22,7 @@ export class StratumV1ClientStatistics { } - // We don't want to save them here because it can be DB intensive, stead do it every once in + // We don't want to save them here because it can be DB intensive, instead do it every once in // awhile with saveShares() public async addShares(client: ClientEntity, targetDifficulty: number) { @@ -41,7 +41,10 @@ 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, shares: this.shares, @@ -52,6 +55,20 @@ 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, + 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, shares: this.shares, @@ -60,11 +77,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, shares: this.shares, @@ -74,12 +91,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) {