Preserve statistics on reset failures and reject invalid accounting data
This commit is contained in:
1 parent
a4ac70a276
commit
a749ef5c15
2 files changed
+77
-31
No files matched your search
@@ -135,8 +135,8 @@ class UploadManager extends EventEmitter {
|
||||
}
|
||||
|
||||
_recordTransferred(entry, bytes, total) {
|
||||
if (!Number.isFinite(bytes) || bytes < 0) return;
|
||||
const next = Math.min(bytes, total);
|
||||
if (!Number.isFinite(bytes) || bytes < 0 || !Number.isFinite(total) || total < 0) return;
|
||||
const next = Math.max(entry.bytesUploaded || 0, Math.min(bytes, total));
|
||||
const delta = Math.max(0, next - entry.bytesUploaded);
|
||||
this.transferredBytes += delta;
|
||||
if (delta > 0) this.emit('statistics-transfer', { hoster: entry.hoster, bytes: delta });
|
||||
|
||||
+75
-29
@@ -26,8 +26,37 @@ function merge(target, source) {
|
||||
for (const key of ['bytes', 'completed', 'failed']) provider[key] += count(bucket[key]);
|
||||
}
|
||||
}
|
||||
function isRecord(value) {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
||||
}
|
||||
function validNumber(value) {
|
||||
return Number.isFinite(value) && value >= 0;
|
||||
}
|
||||
function validBucket(value) {
|
||||
if (!isRecord(value) || !isRecord(value.providers)) return false;
|
||||
if (!['bytes', 'completed', 'failed', 'activeMs'].every(key => validNumber(value[key]))) return false;
|
||||
if (value.trackedBytes !== undefined && !validNumber(value.trackedBytes)) return false;
|
||||
const totals = { bytes: 0, completed: 0, failed: 0 };
|
||||
for (const [hoster, bucket] of Object.entries(value.providers)) {
|
||||
if (!Object.hasOwn(HOSTERS, hoster) || !isRecord(bucket)) return false;
|
||||
for (const key of Object.keys(totals)) {
|
||||
if (!validNumber(bucket[key])) return false;
|
||||
totals[key] += bucket[key];
|
||||
}
|
||||
}
|
||||
return Object.keys(totals).every(key => totals[key] === value[key]);
|
||||
}
|
||||
function validState(value) {
|
||||
return value?.version === 1 && value.days && value.minutes && value.total?.providers && Number.isFinite(value.startedAt);
|
||||
return value?.version === 1 && isRecord(value.days) && isRecord(value.minutes)
|
||||
&& validNumber(value.startedAt) && validBucket(value.total)
|
||||
&& Object.entries(value.days).every(([key, bucket]) => /^\d{4}-\d{2}-\d{2}$/.test(key) && validBucket(bucket))
|
||||
&& Object.entries(value.minutes).every(([key, bucket]) => /^\d+$/.test(key) && validBucket(bucket));
|
||||
}
|
||||
function applyRecord(state, sessionBucket, hoster, delta, time, session, now) {
|
||||
if (session) add(sessionBucket, delta, hoster);
|
||||
add(state.total, delta, hoster);
|
||||
add(state.days[dayKey(time)] ||= empty(), delta, hoster);
|
||||
if (time >= now - 2 * DAY) add(state.minutes[Math.floor(time / MINUTE)] ||= empty(), delta, hoster);
|
||||
}
|
||||
|
||||
class UploadStatistics {
|
||||
@@ -79,13 +108,9 @@ class UploadStatistics {
|
||||
}
|
||||
|
||||
record(hoster, delta, time = this.now(), session = true) {
|
||||
if (session) {
|
||||
delta = { ...delta, trackedBytes: count(delta.bytes) };
|
||||
add(this.session, delta, hoster);
|
||||
}
|
||||
add(this.state.total, delta, hoster);
|
||||
add(this.state.days[dayKey(time)] ||= empty(), delta, hoster);
|
||||
if (time >= this.now() - 2 * DAY) add(this.state.minutes[Math.floor(time / MINUTE)] ||= empty(), delta, hoster);
|
||||
if (session) delta = { ...delta, trackedBytes: count(delta.bytes) };
|
||||
applyRecord(this.state, this.session, hoster, delta, time, session, this.now());
|
||||
this.resetRecords?.push({ hoster, delta, time, session });
|
||||
this.revision++;
|
||||
}
|
||||
|
||||
@@ -145,20 +170,48 @@ class UploadStatistics {
|
||||
providers: Object.entries(HOSTERS).map(([id, label]) => ({ id, label, ...(total.providers[id] || { bytes: 0, completed: 0, failed: 0 }) })) };
|
||||
}
|
||||
|
||||
async reset(scope) {
|
||||
if (!['session', 'all', 'errors'].includes(scope)) throw new Error('Ungültiger Statistikreset');
|
||||
if (!this.ready) throw new Error('Statistik noch nicht verfügbar');
|
||||
this.activity(this.activeSince !== null);
|
||||
if (scope === 'session' || scope === 'all') { this.session = empty(); this.sessionStartedAt = this.now(); }
|
||||
if (scope === 'all') this.state = { version: 1, startedAt: this.now(), historyImported: false, total: empty(), days: {}, minutes: {} };
|
||||
if (scope === 'errors') {
|
||||
for (const bucket of [this.session, this.state.total, ...Object.values(this.state.days), ...Object.values(this.state.minutes)]) {
|
||||
bucket.failed = 0;
|
||||
for (const provider of Object.values(bucket.providers)) provider.failed = 0;
|
||||
reset(scope) {
|
||||
if (!['session', 'all', 'errors'].includes(scope)) return Promise.reject(new Error('Ungültiger Statistikreset'));
|
||||
if (!this.ready) return Promise.reject(new Error('Statistik noch nicht verfügbar'));
|
||||
const task = this.writeChain.catch(() => {}).then(async () => {
|
||||
this.activity(this.activeSince !== null);
|
||||
const resetAt = this.now();
|
||||
const nextState = scope === 'all'
|
||||
? { version: 1, startedAt: resetAt, historyImported: false, total: empty(), days: {}, minutes: {} }
|
||||
: structuredClone(this.state);
|
||||
const nextSession = scope === 'session' || scope === 'all' ? empty() : structuredClone(this.session);
|
||||
if (scope === 'errors') {
|
||||
for (const bucket of [nextSession, nextState.total, ...Object.values(nextState.days), ...Object.values(nextState.minutes)]) {
|
||||
bucket.failed = 0;
|
||||
for (const provider of Object.values(bucket.providers)) provider.failed = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.revision++;
|
||||
await this.flush();
|
||||
const revision = this.revision;
|
||||
this.resetRecords = [];
|
||||
try {
|
||||
await this.writePayload(JSON.stringify(nextState));
|
||||
for (const record of this.resetRecords) applyRecord(nextState, nextSession, record.hoster, record.delta, record.time, record.session, this.now());
|
||||
this.state = nextState;
|
||||
this.session = nextSession;
|
||||
if (scope === 'session' || scope === 'all') this.sessionStartedAt = resetAt;
|
||||
this.revision++;
|
||||
this.savedRevision = this.resetRecords.length ? revision : this.revision;
|
||||
this.error = '';
|
||||
} finally { this.resetRecords = null; }
|
||||
});
|
||||
this.writeChain = task;
|
||||
return task.catch(error => { this.error = 'Statistik konnte nicht gespeichert werden'; throw error; });
|
||||
}
|
||||
|
||||
async writePayload(data) {
|
||||
await fs.mkdir(path.dirname(this.filePath), { recursive: true });
|
||||
const handle = await fs.open(this.filePath + '.tmp', 'w');
|
||||
try { await handle.writeFile(data, 'utf8'); await handle.sync(); } finally { await handle.close(); }
|
||||
try {
|
||||
const previous = await fs.readFile(this.filePath, 'utf8');
|
||||
if (validState(JSON.parse(previous))) await fs.writeFile(this.filePath + '.bak', previous, 'utf8');
|
||||
} catch (error) { if (error.code !== 'ENOENT' && !(error instanceof SyntaxError)) throw error; }
|
||||
await fs.rename(this.filePath + '.tmp', this.filePath);
|
||||
}
|
||||
|
||||
flush() {
|
||||
@@ -168,14 +221,7 @@ class UploadStatistics {
|
||||
for (const minute of Object.keys(this.state.minutes)) if (Number(minute) < cutoff) delete this.state.minutes[minute];
|
||||
const revision = this.revision;
|
||||
const data = JSON.stringify(this.state);
|
||||
await fs.mkdir(path.dirname(this.filePath), { recursive: true });
|
||||
const handle = await fs.open(this.filePath + '.tmp', 'w');
|
||||
try { await handle.writeFile(data, 'utf8'); await handle.sync(); } finally { await handle.close(); }
|
||||
try {
|
||||
const previous = await fs.readFile(this.filePath, 'utf8');
|
||||
if (validState(JSON.parse(previous))) await fs.writeFile(this.filePath + '.bak', previous, 'utf8');
|
||||
} catch (error) { if (error.code !== 'ENOENT' && !(error instanceof SyntaxError)) throw error; }
|
||||
await fs.rename(this.filePath + '.tmp', this.filePath);
|
||||
await this.writePayload(data);
|
||||
this.savedRevision = revision;
|
||||
this.error = '';
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user