understanding commited on
Commit
28c0b4f
·
verified ·
1 Parent(s): f78e6de

Create progress.py

Browse files
Files changed (1) hide show
  1. bot/core/progress.py +38 -0
bot/core/progress.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PATH: bot/core/progress.py
2
+ import time
3
+
4
+ class SpeedETA:
5
+ def __init__(self):
6
+ self.t0 = time.time()
7
+ self.last_t = self.t0
8
+ self.last_bytes = 0
9
+ self.total = 0
10
+
11
+ def update(self, done_bytes: int, total_bytes: int) -> dict:
12
+ now = time.time()
13
+ self.total = total_bytes
14
+ dt = max(1e-6, now - self.last_t)
15
+ db = done_bytes - self.last_bytes
16
+ speed = db / dt # bytes/sec
17
+ remaining = max(0, total_bytes - done_bytes)
18
+ eta = remaining / max(1e-6, speed)
19
+ self.last_t = now
20
+ self.last_bytes = done_bytes
21
+ return {"done": done_bytes, "total": total_bytes, "speed_bps": speed, "eta_sec": eta}
22
+
23
+ def human_bytes(n: float) -> str:
24
+ units = ["B", "KB", "MB", "GB"]
25
+ x = float(n)
26
+ for u in units:
27
+ if x < 1024 or u == units[-1]:
28
+ return f"{x:.2f}{u}"
29
+ x /= 1024
30
+ return f"{x:.2f}GB"
31
+
32
+ def human_eta(sec: float) -> str:
33
+ s = int(max(0, sec))
34
+ m, s = divmod(s, 60)
35
+ h, m = divmod(m, 60)
36
+ if h: return f"{h}h {m}m"
37
+ if m: return f"{m}m {s}s"
38
+ return f"{s}s"