Files
Kaname/src/worker/speed.ts
T
Pascal LinxweilerandClaude Opus 5.5 49440154a2 Milestone (c): worker and downloads with live progress
- Worker queue with per-model exclusivity and MAX_CONCURRENT_DOWNLOADS,
  spawns `hf download --revision <sha> --local-dir ... --include <exact paths>`
  with HF_TOKEN via env only
- Progress from bytes on disk (final + .incomplete files matched by etag),
  per job and per file, with speed and ETA; post-download size/etag check
- Job logs (sanitized), cancel, graceful interrupt on shutdown,
  partial-file cleanup (hf 2.1.1 does not resume partials)
- Single-worker DB lease, free-space check
- SSE live updates for library, model and job pages
- Dockerfile + compose.yaml (web, worker, data/models volumes)
- HF_HUB_DISABLE_XET default on for measurable progress

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 16:57:12 +02:00

33 lines
1.1 KiB
TypeScript

/**
* Download speed over a sliding time window, from periodic (time, totalBytes) samples.
* Using deltas means bytes that were already on disk before the job started never count as speed.
*/
export class SpeedMeter {
private samples: { t: number; bytes: number }[] = [];
constructor(private windowMs = 10_000) {}
add(t: number, bytes: number) {
this.samples.push({ t, bytes });
while (this.samples.length > 2 && t - this.samples[1].t >= this.windowMs) this.samples.shift();
}
/** Bytes per second, or null until there are two samples at least 1s apart. */
bytesPerSecond(): number | null {
if (this.samples.length < 2) return null;
const a = this.samples[0];
const b = this.samples[this.samples.length - 1];
const dt = (b.t - a.t) / 1000;
if (dt < 1) return null;
return Math.max(0, (b.bytes - a.bytes) / dt);
}
/** Seconds remaining, or null if unknown or stalled. */
eta(remainingBytes: number): number | null {
const bps = this.bytesPerSecond();
if (remainingBytes <= 0) return 0;
if (!bps || bps < 1) return null;
return Math.round(remainingBytes / bps);
}
}