mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 17:15:03 -07:00
db refactor
This commit is contained in:
@@ -6,10 +6,14 @@ const TARGET_SUBMISSION_PER_SECOND = 10;
|
||||
const MIN_DIFF = 0.00001;
|
||||
export class StratumV1ClientStatistics {
|
||||
|
||||
private shareBacklog: number = 0;
|
||||
private shares: number = 0;
|
||||
private acceptedCount: number = 0;
|
||||
|
||||
private submissionCacheStart: Date;
|
||||
private submissionCache = [];
|
||||
private submissionCache: { time: Date, difficulty: number }[] = [];
|
||||
|
||||
private currentTimeSlot: number = null;
|
||||
private lastSave: number = null;
|
||||
|
||||
constructor(
|
||||
private readonly clientStatisticsService: ClientStatisticsService
|
||||
@@ -20,40 +24,60 @@ export class StratumV1ClientStatistics {
|
||||
|
||||
public async saveShares(client: ClientEntity) {
|
||||
|
||||
if (client == null || client.address == null || client.clientName == null || client.sessionId == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 10 min
|
||||
var coeff = 1000 * 60 * 10;
|
||||
var date = new Date();
|
||||
var rounded = new Date(Math.floor(date.getTime() / coeff) * coeff);
|
||||
// if (client == null || client.address == null || client.clientName == null || client.sessionId == null) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
await this.clientStatisticsService.save({
|
||||
time: rounded.getTime(),
|
||||
shares: this.shareBacklog,
|
||||
time: this.currentTimeSlot,
|
||||
clientId: client.id,
|
||||
shares: this.shares,
|
||||
acceptedCount: this.acceptedCount,
|
||||
address: client.address,
|
||||
clientName: client.clientName,
|
||||
sessionId: client.sessionId
|
||||
});
|
||||
|
||||
this.shareBacklog = 0;
|
||||
|
||||
|
||||
}
|
||||
|
||||
// We don't want to save them here because it can be DB intensive, stead do it every once in
|
||||
// We don't want to save them here because it can be DB intensive, instead do it every once in
|
||||
// awhile with saveShares()
|
||||
public async addShares(targetDifficulty: number) {
|
||||
public async addShares(client: ClientEntity, targetDifficulty: number) {
|
||||
|
||||
// 10 min
|
||||
var coeff = 1000 * 60 * 10;
|
||||
var date = new Date();
|
||||
var timeSlot = new Date(Math.floor(date.getTime() / coeff) * coeff).getTime();
|
||||
|
||||
if (this.submissionCache.length > CACHE_SIZE) {
|
||||
this.submissionCache.shift();
|
||||
}
|
||||
|
||||
this.submissionCache.push({
|
||||
time: new Date(),
|
||||
time: date,
|
||||
difficulty: targetDifficulty,
|
||||
});
|
||||
|
||||
this.shareBacklog += targetDifficulty;
|
||||
|
||||
|
||||
if (this.currentTimeSlot == null) {
|
||||
this.currentTimeSlot = timeSlot;
|
||||
}
|
||||
|
||||
if (this.currentTimeSlot != timeSlot) {
|
||||
await this.saveShares(client);
|
||||
this.shares = 0;
|
||||
this.acceptedCount = 0;
|
||||
this.currentTimeSlot = timeSlot;
|
||||
} else if ((date.getTime() - 60 * 1000) > this.lastSave) {
|
||||
await this.saveShares(client);
|
||||
this.lastSave = new Date().getTime();
|
||||
}
|
||||
|
||||
|
||||
this.shares += targetDifficulty;
|
||||
this.acceptedCount++;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user