data persistance init

This commit is contained in:
Ben Wilson
2023-06-23 22:30:54 -04:00
parent b963eaa514
commit 080471e52d
16 changed files with 2760 additions and 188 deletions
+8 -31
View File
@@ -1,45 +1,22 @@
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
import { ClientEntity } from '../ORM/client/client.entity';
export class StratumV1ClientStatistics {
public bestDifficulty: number = 0;
public historicSubmissions = [];
constructor() {
constructor(private readonly clientStatisticsService: ClientStatisticsService) {
}
public addSubmission(targetDifficulty: number, submissionDifficulty: number, networkDifficulty: number) {
public async addSubmission(client: ClientEntity, targetDifficulty: number) {
if (this.historicSubmissions.length >= 1000) {
this.historicSubmissions.shift();
}
this.historicSubmissions.push({
await this.clientStatisticsService.save({
time: new Date(),
difficulty: targetDifficulty,
time: new Date()
client
});
if (submissionDifficulty > this.bestDifficulty) {
this.bestDifficulty = submissionDifficulty;
}
}
public getHashRate() {
return this.historicSubmissions.reduce((pre, cur, idx, arr) => {
if (idx === 0) {
pre.time = cur.time;
}
pre.difficulty += cur.difficulty;
if (arr.length - 1 === idx) {
const duration = cur.time.getTime() - pre.time.getTime();
const sumDifficulty = pre.difficulty;
return (sumDifficulty * 4294967296) / (duration * 1000000)
}
return pre;
}, {
difficulty: 0,
time: undefined
})
}
}