mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
data persistance init
This commit is contained in:
@@ -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
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user