understanding commited on
Commit
4a35888
·
verified ·
1 Parent(s): bc116f9

Create app/progress.py

Browse files
Files changed (1) hide show
  1. app/progress.py +58 -0
app/progress.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+ import time
3
+
4
+ def human_bytes(n: float) -> str:
5
+ for unit in ["B", "KB", "MB", "GB", "TB"]:
6
+ if n < 1024:
7
+ return f"{n:.1f}{unit}"
8
+ n /= 1024
9
+ return f"{n:.1f}PB"
10
+
11
+ def fmt_eta(sec: float) -> str:
12
+ sec = max(0, int(sec))
13
+ if sec < 60:
14
+ return f"{sec}s"
15
+ m = sec // 60
16
+ s = sec % 60
17
+ if m < 60:
18
+ return f"{m}m {s}s"
19
+ h = m // 60
20
+ m = m % 60
21
+ return f"{h}h {m}m"
22
+
23
+ class RateProgress:
24
+ def __init__(self, total: int, edit_every: float = 2.5):
25
+ self.total = max(1, total)
26
+ self.edit_every = edit_every
27
+ self.t0 = time.time()
28
+ self.last_edit = 0.0
29
+ self.last_bytes = 0
30
+ self.last_t = self.t0
31
+
32
+ def snapshot(self, done: int) -> dict:
33
+ now = time.time()
34
+ done = max(0, min(done, self.total))
35
+ dt = max(1e-6, now - self.last_t)
36
+ dbytes = done - self.last_bytes
37
+ speed = dbytes / dt # B/s
38
+ elapsed = now - self.t0
39
+ pct = (done / self.total) * 100.0
40
+ remaining = self.total - done
41
+ eta = remaining / speed if speed > 1 else float("inf")
42
+ return {
43
+ "pct": pct,
44
+ "done": done,
45
+ "total": self.total,
46
+ "speed": speed,
47
+ "eta": eta,
48
+ "elapsed": elapsed,
49
+ "now": now
50
+ }
51
+
52
+ def should_edit(self, now: float) -> bool:
53
+ return (now - self.last_edit) >= self.edit_every
54
+
55
+ def mark_edit(self, done: int, now: float):
56
+ self.last_edit = now
57
+ self.last_bytes = done
58
+ self.last_t = now