diff --git a/src/ORM/address-settings/address-settings.service.ts b/src/ORM/address-settings/address-settings.service.ts index da55722..ec3baab 100644 --- a/src/ORM/address-settings/address-settings.service.ts +++ b/src/ORM/address-settings/address-settings.service.ts @@ -14,9 +14,9 @@ export class AddressSettingsService { } - public async getSettings(address: string) { + public async getSettings(address: string, createIfNotFound: boolean) { const settings = await this.addressSettingsRepository.findOne({ where: { address } }); - if (settings == null) { + if (createIfNotFound == true && settings == null) { return await this.createNew(address); } return settings; diff --git a/src/ORM/client-statistics/client-statistics.entity.ts b/src/ORM/client-statistics/client-statistics.entity.ts index 111a60e..115cc26 100644 --- a/src/ORM/client-statistics/client-statistics.entity.ts +++ b/src/ORM/client-statistics/client-statistics.entity.ts @@ -1,15 +1,16 @@ -import { Column, Entity, Index, PrimaryColumn } from 'typeorm'; +import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm'; import { TrackedEntity } from '../utils/TrackedEntity.entity'; @Entity({ withoutRowid: true }) -//Index for the heartbeat update +//Index for getHashRateForSession +@Index(["address", "clientName", "sessionId"]) +//Index for statistics save @Index(["address", "clientName", "sessionId", "time"]) export class ClientStatisticsEntity extends TrackedEntity { - @PrimaryColumn({ length: 64, type: 'varchar' }) - submissionHash: string; - + @PrimaryGeneratedColumn() + id: number; @Column({ length: 62, type: 'varchar' }) address: string; @@ -31,6 +32,4 @@ export class ClientStatisticsEntity extends TrackedEntity { acceptedCount: number; - - } \ No newline at end of file diff --git a/src/ORM/client-statistics/client-statistics.service.ts b/src/ORM/client-statistics/client-statistics.service.ts index 028ce48..2dfdfb4 100644 --- a/src/ORM/client-statistics/client-statistics.service.ts +++ b/src/ORM/client-statistics/client-statistics.service.ts @@ -18,28 +18,38 @@ export class ClientStatisticsService { } public async save(clientStatistic: Partial) { - const res1 = await this.clientStatisticsRepository.createQueryBuilder() + // Attempt to update the existing record + const updateResult = await this.clientStatisticsRepository + .createQueryBuilder() .update(ClientStatisticsEntity) .set({ - shares: () => `"shares" + ${clientStatistic.shares}`, // Use the actual value of shares here + shares: () => `"shares" + :sharesIncrement`, acceptedCount: () => `"acceptedCount" + 1` }) - .where('address = :address AND clientName = :clientName AND sessionId = :sessionId AND time = :time', { address: clientStatistic.address, clientName: clientStatistic.clientName, sessionId: clientStatistic.sessionId, time: clientStatistic.time }) + .where('address = :address AND clientName = :clientName AND sessionId = :sessionId AND time = :time', { + address: clientStatistic.address, + clientName: clientStatistic.clientName, + sessionId: clientStatistic.sessionId, + time: clientStatistic.time, + sharesIncrement: clientStatistic.shares + }) .execute(); - if (res1.affected == 0) { + // Check if the update affected any rows + if (updateResult.affected === 0) { + // If no rows were updated, insert a new record await this.clientStatisticsRepository.insert(clientStatistic); } } public async deleteOldStatistics() { - const eightDays = new Date(Date.now() - 8 * 24 * 60 * 60 * 1000); + const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000); return await this.clientStatisticsRepository .createQueryBuilder() .delete() .from(ClientStatisticsEntity) - .where('time < :time', { time: eightDays.getTime() }) + .where('time < :time', { time: oneDayAgo.getTime() }) .execute(); } @@ -60,7 +70,7 @@ export class ClientStatisticsService { ORDER BY time LIMIT 144; - + `; const result: any[] = await this.clientStatisticsRepository.query(query); diff --git a/src/app.controller.ts b/src/app.controller.ts index ff3eb5f..a46ffc4 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -21,17 +21,31 @@ export class AppController { private readonly bitcoinRpcService: BitcoinRpcService ) { } - @Get('info') public async info() { + + const CACHE_KEY = 'SITE_INFO'; + const cachedResult = await this.cacheManager.get(CACHE_KEY); + + if (cachedResult != null) { + return cachedResult; + } + + const blockData = await this.blocksService.getFoundBlocks(); const userAgents = await this.clientService.getUserAgents(); - return { + const data = { blockData, userAgents, uptime: this.uptime }; + + //1 min + await this.cacheManager.set(CACHE_KEY, data, 1 * 60 * 1000); + + return data; + } @Get('network') @@ -44,17 +58,17 @@ export class AppController { public async infoChart() { - // const CACHE_KEY = 'SITE_HASHRATE_GRAPH'; - // const cachedResult = await this.cacheManager.get(CACHE_KEY); + const CACHE_KEY = 'SITE_HASHRATE_GRAPH'; + const cachedResult = await this.cacheManager.get(CACHE_KEY); - // if (cachedResult != null) { - // return cachedResult; - // } + if (cachedResult != null) { + return cachedResult; + } const chartData = await this.clientStatisticsService.getChartDataForSite(); - //5 min - //await this.cacheManager.set(CACHE_KEY, chartData, 5 * 60 * 1000); + //10 min + await this.cacheManager.set(CACHE_KEY, chartData, 10 * 60 * 1000); return chartData; diff --git a/src/controllers/client/client.controller.ts b/src/controllers/client/client.controller.ts index 7a3d2c2..4fe77ac 100644 --- a/src/controllers/client/client.controller.ts +++ b/src/controllers/client/client.controller.ts @@ -20,7 +20,7 @@ export class ClientController { const workers = await this.clientService.getByAddress(address); - const addressSettings = await this.addressSettingsService.getSettings(address); + const addressSettings = await this.addressSettingsService.getSettings(address, false); return { bestDifficulty: addressSettings?.bestDifficulty, diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index 0d1edde..4cd9ded 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -479,7 +479,7 @@ export class StratumV1Client { parseInt(submission.ntime, 16) ); const header = updatedJobBlock.toBuffer(true); - const { submissionDifficulty, submissionHash } = this.calculateDifficulty(header); + const { submissionDifficulty } = this.calculateDifficulty(header); //console.log(`DIFF: ${submissionDifficulty} of ${this.sessionDifficulty} from ${this.clientAuthorization.worker + '.' + this.extraNonceAndSessionId}`); @@ -505,7 +505,7 @@ export class StratumV1Client { } } try { - await this.statistics.addSubmission(this.entity, submissionHash, this.sessionDifficulty); + await this.statistics.addSubmission(this.entity, this.sessionDifficulty); await this.clientService.heartbeat(this.entity.address, this.entity.clientName, this.entity.sessionId, this.hashRate); } catch (e) { console.log(e); @@ -524,7 +524,7 @@ export class StratumV1Client { if (submissionDifficulty > this.entity.bestDifficulty) { await this.clientService.updateBestDifficulty(this.extraNonceAndSessionId, submissionDifficulty); this.entity.bestDifficulty = submissionDifficulty; - if (submissionDifficulty > (await this.addressSettingsService.getSettings(this.clientAuthorization.address)).bestDifficulty) { + if (submissionDifficulty > (await this.addressSettingsService.getSettings(this.clientAuthorization.address, true)).bestDifficulty) { await this.addressSettingsService.updateBestDifficulty(this.clientAuthorization.address, submissionDifficulty); } } diff --git a/src/models/StratumV1ClientStatistics.ts b/src/models/StratumV1ClientStatistics.ts index 8aae0bf..9f72c4f 100644 --- a/src/models/StratumV1ClientStatistics.ts +++ b/src/models/StratumV1ClientStatistics.ts @@ -17,7 +17,7 @@ export class StratumV1ClientStatistics { this.submissionCacheStart = new Date(); } - public async addSubmission(client: ClientEntity, submissionHash: string, targetDifficulty: number) { + public async addSubmission(client: ClientEntity, targetDifficulty: number) { if (this.submissionCache.length > CACHE_SIZE) { this.submissionCache.shift(); @@ -31,15 +31,14 @@ export class StratumV1ClientStatistics { // 10 min var coeff = 1000 * 60 * 10; var date = new Date(); - var rounded = new Date(Math.round(date.getTime() / coeff) * coeff); + var rounded = new Date(Math.floor(date.getTime() / coeff) * coeff); await this.clientStatisticsService.save({ time: rounded.getTime(), shares: targetDifficulty, address: client.address, clientName: client.clientName, - sessionId: client.sessionId, - submissionHash + sessionId: client.sessionId }); }