From fd24807c55ce54c69be5b732e07e010c5612498f Mon Sep 17 00:00:00 2001 From: Benjamin Wilson Date: Wed, 26 Feb 2025 09:50:42 -0500 Subject: [PATCH] cleanup --- .../client-statistics.service.ts | 27 +++++++++---------- src/models/StratumV1ClientStatistics.ts | 14 +++------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/ORM/client-statistics/client-statistics.service.ts b/src/ORM/client-statistics/client-statistics.service.ts index fe1afc4..1d70650 100644 --- a/src/ORM/client-statistics/client-statistics.service.ts +++ b/src/ORM/client-statistics/client-statistics.service.ts @@ -8,7 +8,9 @@ import { ClientStatisticsEntity } from './client-statistics.entity'; @Injectable() export class ClientStatisticsService { - private bulkAsyncUpdates: Partial[] = []; + private bulkAsyncUpdates: { + [key: string]: Partial + } = {}; constructor( @@ -30,26 +32,21 @@ export class ClientStatisticsService { // }); // } - public updateBulkAsync(clientStatistic: Partial, lastSeenIndex: number) { - - if(this.bulkAsyncUpdates.length > lastSeenIndex && this.bulkAsyncUpdates[lastSeenIndex].clientId == clientStatistic.clientId && this.bulkAsyncUpdates[lastSeenIndex].time == clientStatistic.time){ - this.bulkAsyncUpdates[lastSeenIndex].shares = clientStatistic.shares; - this.bulkAsyncUpdates[lastSeenIndex].acceptedCount = clientStatistic.acceptedCount; - return lastSeenIndex; - } - - return this.bulkAsyncUpdates.push(clientStatistic) - 1; + public updateBulkAsync(clientStatistic: Partial) { + this.bulkAsyncUpdates[clientStatistic.client + clientStatistic.time.toString()] = clientStatistic; } public async doBulkAsyncUpdate(){ - if(this.bulkAsyncUpdates.length < 1){ + if(Object.keys(this.bulkAsyncUpdates).length < 1){ console.log('No client stats to update.') return; } + // Step 1: Prepare data for bulk update - const values = this.bulkAsyncUpdates.map(stat => - `('${stat.clientId}', ${stat.time}, ${stat.shares ?? 0}, ${stat.acceptedCount ?? 0}, NOW())` - ).join(','); + const values = Object.entries(this.bulkAsyncUpdates).map(([key, value]) => { + return `('${value.clientId}', ${value.time}, ${value.shares}, ${value.acceptedCount}, NOW())` + }).join(','); + const query = ` DO $$ @@ -83,7 +80,7 @@ export class ClientStatisticsService { throw error; } - this.bulkAsyncUpdates = []; + this.bulkAsyncUpdates = {}; } public async insert(clientStatistic: Partial) { diff --git a/src/models/StratumV1ClientStatistics.ts b/src/models/StratumV1ClientStatistics.ts index 6e4692d..8b464b6 100644 --- a/src/models/StratumV1ClientStatistics.ts +++ b/src/models/StratumV1ClientStatistics.ts @@ -14,9 +14,6 @@ export class StratumV1ClientStatistics { private submissionCache: { time: Date, difficulty: number }[] = []; private currentTimeSlot: number = null; - private lastSave: number = null; - - private bulkUpdateIndex = 0; constructor( private readonly clientStatisticsService: ClientStatisticsService, @@ -43,7 +40,6 @@ export class StratumV1ClientStatistics { difficulty: targetDifficulty, }); - if (this.currentTimeSlot == null) { // First record, insert it this.currentTimeSlot = timeSlot; @@ -58,16 +54,15 @@ export class StratumV1ClientStatistics { clientName: client.clientName, sessionId: client.sessionId }); - 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 - this.bulkUpdateIndex = this.clientStatisticsService.updateBulkAsync({ + this.clientStatisticsService.updateBulkAsync({ time: this.currentTimeSlot, clientId: client.id, shares: this.shares, acceptedCount: this.acceptedCount, - }, this.bulkUpdateIndex); + }); // Set the new time slot and add incoming shares then insert it this.currentTimeSlot = timeSlot; this.shares = targetDifficulty; @@ -81,18 +76,17 @@ export class StratumV1ClientStatistics { clientName: client.clientName, 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.bulkUpdateIndex = this.clientStatisticsService.updateBulkAsync({ + this.clientStatisticsService.updateBulkAsync({ time: this.currentTimeSlot, clientId: client.id, shares: this.shares, acceptedCount: this.acceptedCount, - }, this.bulkUpdateIndex); + }); } }