class CredSummary {
constructor({ accounts, intervals } ) {
this.accounts = accounts;
this.intervals = intervals;
this.nameToInfo = new Map();
this.totalCred = 0;
this.topCredByInterval = new Array(intervals.length).fill(0);
this.activeByInterval = new Array(intervals.length)
.fill(0)
.map(x => new Set());
this.totalCredByInterval = new Array(intervals.length).fill(0);
for (const { totalCred, cred } of accounts) {
this.totalCred += totalCred;
for (let i = 0; i < intervals.length; i++) {
this.totalCredByInterval[i] += cred[i];
}
}
for (const credAccount of accounts) {
const info = new AccountInfo(credAccount, this.totalCred);
this.nameToInfo.set(info.name, info);
for (let i = 0; i < intervals.length; i++) {
if (info.relCred[i] > CRED_ACTIVE_CUTOFF) {
this.activeByInterval[i].add(info.name);
}
this.topCredByInterval[i] = Math.max(
this.topCredByInterval[i],
info.cred[i]
);
}
}
this.credPerActiveByInterval = this.totalCredByInterval.map(
(x, i) => x / this.activeByInterval[i]
);
let cumCred = 0;
this.relCumCredByInterval = new Array(intervals.length).fill(0);
for (let i = 0; i < intervals.length; i++) {
cumCred += (this.totalCredByInterval[i] / this.totalCred) * 100;
this.relCumCredByInterval[i] = cumCred;
}
}
credByInterval() {
const result = [];
for (let i = 0; i < this.intervals.length; i++) {
const cred = this.totalCredByInterval[i];
const interval = this.intervals[i];
const active = this.activeByInterval[i];
const nActive = active.size;
const relCred = (cred / this.totalCred) * 10 ** 6;
const topCred = this.topCredByInterval[i];
const topRelCred = (topCred / this.totalCred) * 10 ** 6;
result.push({
cred,
relCred,
startTimeMs: interval.startTimeMs,
endTimeMs: interval.endTimeMs,
active,
nActive,
credPerActive: nActive > 0 ? cred / nActive : 0,
relCredPerActive: nActive > 0 ? relCred / nActive : 0,
topCred,
topRelCred,
relCumCred: this.relCumCredByInterval[i]
});
}
return result;
}
names() {
return Array.from(this.nameToInfo.keys());
}
infos() {
return Array.from(this.nameToInfo.values());
}
}