back port some changes from postgres

This commit is contained in:
Ben
2023-12-23 19:30:05 -05:00
parent 8f6eeaba73
commit 38fda85f63
7 changed files with 55 additions and 33 deletions
@@ -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 } }); const settings = await this.addressSettingsRepository.findOne({ where: { address } });
if (settings == null) { if (createIfNotFound == true && settings == null) {
return await this.createNew(address); return await this.createNew(address);
} }
return settings; return settings;
@@ -1,15 +1,16 @@
import { Column, Entity, Index, PrimaryColumn } from 'typeorm'; import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
import { TrackedEntity } from '../utils/TrackedEntity.entity'; import { TrackedEntity } from '../utils/TrackedEntity.entity';
@Entity({ withoutRowid: true }) @Entity({ withoutRowid: true })
//Index for the heartbeat update //Index for getHashRateForSession
@Index(["address", "clientName", "sessionId"])
//Index for statistics save
@Index(["address", "clientName", "sessionId", "time"]) @Index(["address", "clientName", "sessionId", "time"])
export class ClientStatisticsEntity extends TrackedEntity { export class ClientStatisticsEntity extends TrackedEntity {
@PrimaryColumn({ length: 64, type: 'varchar' }) @PrimaryGeneratedColumn()
submissionHash: string; id: number;
@Column({ length: 62, type: 'varchar' }) @Column({ length: 62, type: 'varchar' })
address: string; address: string;
@@ -31,6 +32,4 @@ export class ClientStatisticsEntity extends TrackedEntity {
acceptedCount: number; acceptedCount: number;
} }
@@ -18,28 +18,38 @@ export class ClientStatisticsService {
} }
public async save(clientStatistic: Partial<ClientStatisticsEntity>) { public async save(clientStatistic: Partial<ClientStatisticsEntity>) {
const res1 = await this.clientStatisticsRepository.createQueryBuilder() // Attempt to update the existing record
const updateResult = await this.clientStatisticsRepository
.createQueryBuilder()
.update(ClientStatisticsEntity) .update(ClientStatisticsEntity)
.set({ .set({
shares: () => `"shares" + ${clientStatistic.shares}`, // Use the actual value of shares here shares: () => `"shares" + :sharesIncrement`,
acceptedCount: () => `"acceptedCount" + 1` 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(); .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); await this.clientStatisticsRepository.insert(clientStatistic);
} }
} }
public async deleteOldStatistics() { 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 return await this.clientStatisticsRepository
.createQueryBuilder() .createQueryBuilder()
.delete() .delete()
.from(ClientStatisticsEntity) .from(ClientStatisticsEntity)
.where('time < :time', { time: eightDays.getTime() }) .where('time < :time', { time: oneDayAgo.getTime() })
.execute(); .execute();
} }
+23 -9
View File
@@ -21,17 +21,31 @@ export class AppController {
private readonly bitcoinRpcService: BitcoinRpcService private readonly bitcoinRpcService: BitcoinRpcService
) { } ) { }
@Get('info')
public async 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 blockData = await this.blocksService.getFoundBlocks();
const userAgents = await this.clientService.getUserAgents(); const userAgents = await this.clientService.getUserAgents();
return { const data = {
blockData, blockData,
userAgents, userAgents,
uptime: this.uptime uptime: this.uptime
}; };
//1 min
await this.cacheManager.set(CACHE_KEY, data, 1 * 60 * 1000);
return data;
} }
@Get('network') @Get('network')
@@ -44,17 +58,17 @@ export class AppController {
public async infoChart() { public async infoChart() {
// const CACHE_KEY = 'SITE_HASHRATE_GRAPH'; const CACHE_KEY = 'SITE_HASHRATE_GRAPH';
// const cachedResult = await this.cacheManager.get(CACHE_KEY); const cachedResult = await this.cacheManager.get(CACHE_KEY);
// if (cachedResult != null) { if (cachedResult != null) {
// return cachedResult; return cachedResult;
// } }
const chartData = await this.clientStatisticsService.getChartDataForSite(); const chartData = await this.clientStatisticsService.getChartDataForSite();
//5 min //10 min
//await this.cacheManager.set(CACHE_KEY, chartData, 5 * 60 * 1000); await this.cacheManager.set(CACHE_KEY, chartData, 10 * 60 * 1000);
return chartData; return chartData;
+1 -1
View File
@@ -20,7 +20,7 @@ export class ClientController {
const workers = await this.clientService.getByAddress(address); const workers = await this.clientService.getByAddress(address);
const addressSettings = await this.addressSettingsService.getSettings(address); const addressSettings = await this.addressSettingsService.getSettings(address, false);
return { return {
bestDifficulty: addressSettings?.bestDifficulty, bestDifficulty: addressSettings?.bestDifficulty,
+3 -3
View File
@@ -479,7 +479,7 @@ export class StratumV1Client {
parseInt(submission.ntime, 16) parseInt(submission.ntime, 16)
); );
const header = updatedJobBlock.toBuffer(true); 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}`); //console.log(`DIFF: ${submissionDifficulty} of ${this.sessionDifficulty} from ${this.clientAuthorization.worker + '.' + this.extraNonceAndSessionId}`);
@@ -505,7 +505,7 @@ export class StratumV1Client {
} }
} }
try { 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); await this.clientService.heartbeat(this.entity.address, this.entity.clientName, this.entity.sessionId, this.hashRate);
} catch (e) { } catch (e) {
console.log(e); console.log(e);
@@ -524,7 +524,7 @@ export class StratumV1Client {
if (submissionDifficulty > this.entity.bestDifficulty) { if (submissionDifficulty > this.entity.bestDifficulty) {
await this.clientService.updateBestDifficulty(this.extraNonceAndSessionId, submissionDifficulty); await this.clientService.updateBestDifficulty(this.extraNonceAndSessionId, submissionDifficulty);
this.entity.bestDifficulty = 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); await this.addressSettingsService.updateBestDifficulty(this.clientAuthorization.address, submissionDifficulty);
} }
} }
+3 -4
View File
@@ -17,7 +17,7 @@ export class StratumV1ClientStatistics {
this.submissionCacheStart = new Date(); 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) { if (this.submissionCache.length > CACHE_SIZE) {
this.submissionCache.shift(); this.submissionCache.shift();
@@ -31,15 +31,14 @@ export class StratumV1ClientStatistics {
// 10 min // 10 min
var coeff = 1000 * 60 * 10; var coeff = 1000 * 60 * 10;
var date = new Date(); 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({ await this.clientStatisticsService.save({
time: rounded.getTime(), time: rounded.getTime(),
shares: targetDifficulty, shares: targetDifficulty,
address: client.address, address: client.address,
clientName: client.clientName, clientName: client.clientName,
sessionId: client.sessionId, sessionId: client.sessionId
submissionHash
}); });
} }