Filter stale address workers

This commit is contained in:
Ben
2026-06-21 17:30:06 -04:00
parent 9998c93eae
commit 12f3983c8f
3 changed files with 74 additions and 11 deletions
@@ -7,9 +7,11 @@ import { PayoutSnapshotService } from '../../ORM/payout-snapshot/payout-snapshot
import { ShareAccountingService } from '../../ORM/share-accounting/share-accounting.service';
import { normalizePayoutMode, PayoutMode } from '../../types/payout-mode';
const DEFAULT_CLIENT_ACTIVE_WINDOW_MS = 30 * 60 * 1000;
@Controller('client')
export class ClientController {
private readonly activeWindowMs = this.readPositiveInt('CLIENT_REPORT_ACTIVE_WINDOW_MS', DEFAULT_CLIENT_ACTIVE_WINDOW_MS);
constructor(
private readonly clientService: ClientService,
@@ -178,8 +180,15 @@ export class ClientController {
private async getActiveAddressWorkers(address: string, payoutMode?: PayoutMode) {
const workers = await this.clientService.getByAddress(address);
const activeSince = Date.now() - this.activeWindowMs;
return workers
.filter(worker => payoutMode == null || worker.payoutMode === payoutMode)
.filter(worker => worker.deletedAt == null)
.filter(worker => Number(worker.hashRate ?? 0) > 0)
.filter(worker => {
const updatedAt = worker.updatedAt == null ? 0 : new Date(worker.updatedAt).getTime();
return Number.isFinite(updatedAt) && updatedAt > activeSince;
})
.map(worker => ({
clientId: worker.id,
address: worker.address,
@@ -193,4 +202,12 @@ export class ClientController {
bestDifficulty: Number(worker.bestDifficulty ?? 0),
}));
}
private readPositiveInt(name: string, defaultValue: number): number {
const value = Number(process.env[name]);
if (Number.isInteger(value) && value > 0) {
return value;
}
return defaultValue;
}
}