feat(store): recordTrain with pruning and getSummary
This commit is contained in:
+16
-1
@@ -1,4 +1,4 @@
|
||||
import { parseTarget } from './pure.js';
|
||||
import { parseTarget, pruneHistory, summary as computeSummary } from './pure.js';
|
||||
|
||||
const KEY_TARGETS = 'tat.targets';
|
||||
const KEY_HISTORY = 'tat.history';
|
||||
@@ -78,4 +78,19 @@ export class Store {
|
||||
this.prefs.pos = { x: pos.x, y: pos.y };
|
||||
return this._saveJson(KEY_PREFS, this.prefs);
|
||||
}
|
||||
|
||||
recordTrain(attr, delta, ts = Date.now()) {
|
||||
if (typeof delta !== 'number' || !Number.isFinite(delta) || delta <= 0) {
|
||||
return false;
|
||||
}
|
||||
const list = this.history[attr] || [];
|
||||
list.push({ ts, delta });
|
||||
this.history[attr] = pruneHistory(list, ts);
|
||||
return this._saveJson(KEY_HISTORY, this.history);
|
||||
}
|
||||
|
||||
getSummary(attr, now = Date.now()) {
|
||||
const list = this.history[attr] || [];
|
||||
return computeSummary(list, now);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user