Reduce dashboard Timescale query load

This commit is contained in:
Ben
2026-06-07 21:26:22 -04:00
parent c0604aaf2a
commit 4a5d4bbb71
3 changed files with 2 additions and 11 deletions
@@ -3,7 +3,6 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { ClientEntity } from '../../client/client.entity'; import { ClientEntity } from '../../client/client.entity';
import { ShareAccountingService } from '../../share-accounting/share-accounting.service';
import { UserAgentReportView } from './user-agent-report.view'; import { UserAgentReportView } from './user-agent-report.view';
import { RedisMessagingService } from '../../../services/redis-messaging.service'; import { RedisMessagingService } from '../../../services/redis-messaging.service';
@@ -16,16 +15,12 @@ export class UserAgentReportService {
@InjectRepository(ClientEntity) @InjectRepository(ClientEntity)
private clientRepository: Repository<ClientEntity>, private clientRepository: Repository<ClientEntity>,
private redisMessagingService: RedisMessagingService, private redisMessagingService: RedisMessagingService,
private shareAccountingService: ShareAccountingService,
) { ) {
} }
public async getReport() { public async getReport() {
const presences = await this.redisMessagingService.getAllClientPresence(); const presences = await this.redisMessagingService.getAllClientPresence();
const shareSummaries = await this.shareAccountingService.getSessionSummaries(
presences.map(presence => presence.clientId),
);
const rows = new Map<string, { const rows = new Map<string, {
userAgent: string; userAgent: string;
count: number; count: number;
@@ -43,14 +38,12 @@ export class UserAgentReportService {
bestDifficulty: 0, bestDifficulty: 0,
totalHashRate: 0, totalHashRate: 0,
}; };
const shareSummary = shareSummaries.get(presence.clientId);
row.count++; row.count++;
row.bestDifficulty = Math.max( row.bestDifficulty = Math.max(
row.bestDifficulty, row.bestDifficulty,
Number(presence.bestDifficulty ?? 0), Number(presence.bestDifficulty ?? 0),
Number(shareSummary?.bestSubmissionDifficulty ?? 0),
); );
row.totalHashRate += Number(shareSummary?.hashRateLast10Minutes ?? presence.hashRate ?? 0); row.totalHashRate += Number(presence.hashRate ?? 0);
rows.set(userAgent, row); rows.set(userAgent, row);
}); });
@@ -6,7 +6,6 @@ const HASHES_PER_DIFFICULTY = 4294967296;
const CHART_BUCKET_SECONDS = 600; const CHART_BUCKET_SECONDS = 600;
const CHART_WINDOW = '24 hours'; const CHART_WINDOW = '24 hours';
const SITE_CHART_WINDOW = '7 days'; const SITE_CHART_WINDOW = '7 days';
const REALTIME_WINDOW = '20 minutes';
@Injectable() @Injectable()
export class ClientStatisticsService { export class ClientStatisticsService {
@@ -67,7 +66,7 @@ export class ClientStatisticsService {
WITH bounds AS ( WITH bounds AS (
SELECT SELECT
NOW() - INTERVAL '${windowSql}' AS since, NOW() - INTERVAL '${windowSql}' AS since,
time_bucket(INTERVAL '10 minutes', NOW() - INTERVAL '${REALTIME_WINDOW}') AS realtime_start time_bucket(INTERVAL '10 minutes', NOW()) AS realtime_start
), ),
aggregate_rows AS ( aggregate_rows AS (
SELECT SELECT
-1
View File
@@ -187,7 +187,6 @@ describe('TimescaleDB and Redis integration', () => {
dataSource.getRepository(UserAgentReportView), dataSource.getRepository(UserAgentReportView),
repository, repository,
redisMessagingService, redisMessagingService,
new ShareAccountingService(dataSource.getRepository(AcceptedShareEntity)),
); );
const activeClient = await repository.save({ const activeClient = await repository.save({