Muttered3 commited on
Commit
d4685a4
Β·
verified Β·
1 Parent(s): cc5bd79

Update job_state.py

Browse files
Files changed (1) hide show
  1. job_state.py +24 -29
job_state.py CHANGED
@@ -1,23 +1,24 @@
 
 
 
 
1
  import asyncio
2
  import time
3
 
4
  class JobState:
5
- def __init__(self):
6
- self.current_job_id: str | None = None
7
- self.task: asyncio.Task | None = None
8
- self.stop_event: asyncio.Event = asyncio.Event()
9
- self.total_words: int = 0
10
- self.completed_words: int = 0
11
- self.start_time: float = 0.0
12
- self.paused: bool = False
13
 
14
  def start(self, task: asyncio.Task, job_id: str, total: int):
15
- self.task = task
16
  self.current_job_id = job_id
 
17
  self.total_words = total
18
  self.completed_words = 0
19
  self.start_time = time.time()
20
- self.paused = False
21
  self.stop_event.clear()
22
 
23
  def stop(self):
@@ -28,38 +29,32 @@ class JobState:
28
  def reset(self):
29
  self.current_job_id = None
30
  self.task = None
 
31
  self.total_words = 0
32
  self.completed_words = 0
33
  self.start_time = 0.0
34
- self.paused = False
35
- self.stop_event.clear()
36
 
37
  def is_running(self) -> bool:
38
- return (
39
- self.task is not None
40
- and not self.task.done()
41
- and not self.stop_event.is_set()
42
- )
43
-
44
- def get_progress_text(self) -> str:
45
- if self.total_words == 0:
46
- return "0 / 0 (0.0%)"
47
- pct = (self.completed_words / self.total_words) * 100
48
- return f"{self.completed_words:,} / {self.total_words:,} ({pct:.1f}%)"
49
 
50
  def get_bar(self, width: int = 15) -> str:
51
  if self.total_words == 0:
52
  return "β–‘" * width
53
- filled = int((self.completed_words / self.total_words) * width)
54
  return "β–“" * filled + "β–‘" * (width - filled)
55
 
56
  def get_elapsed(self) -> str:
57
- if self.start_time == 0:
58
  return "00:00:00"
59
  elapsed = int(time.time() - self.start_time)
60
- h, r = divmod(elapsed, 3600)
61
- m, s = divmod(r, 60)
 
62
  return f"{h:02d}:{m:02d}:{s:02d}"
63
 
64
- # Module-level singleton
65
- job = JobState()
 
 
 
 
 
1
+ # ════════════════════════════════════════
2
+ # FILE: job_state.py
3
+ # ════════════════════════════════════════
4
+
5
  import asyncio
6
  import time
7
 
8
  class JobState:
9
+ current_job_id: str | None = None
10
+ task: asyncio.Task | None = None
11
+ stop_event: asyncio.Event = asyncio.Event()
12
+ total_words: int = 0
13
+ completed_words: int = 0
14
+ start_time: float = 0.0
 
 
15
 
16
  def start(self, task: asyncio.Task, job_id: str, total: int):
 
17
  self.current_job_id = job_id
18
+ self.task = task
19
  self.total_words = total
20
  self.completed_words = 0
21
  self.start_time = time.time()
 
22
  self.stop_event.clear()
23
 
24
  def stop(self):
 
29
  def reset(self):
30
  self.current_job_id = None
31
  self.task = None
32
+ self.stop_event.clear()
33
  self.total_words = 0
34
  self.completed_words = 0
35
  self.start_time = 0.0
 
 
36
 
37
  def is_running(self) -> bool:
38
+ return self.task is not None and not self.task.done() and not self.stop_event.is_set()
 
 
 
 
 
 
 
 
 
 
39
 
40
  def get_bar(self, width: int = 15) -> str:
41
  if self.total_words == 0:
42
  return "β–‘" * width
43
+ filled = int(self.completed_words / self.total_words * width)
44
  return "β–“" * filled + "β–‘" * (width - filled)
45
 
46
  def get_elapsed(self) -> str:
47
+ if self.start_time == 0.0:
48
  return "00:00:00"
49
  elapsed = int(time.time() - self.start_time)
50
+ h = elapsed // 3600
51
+ m = (elapsed % 3600) // 60
52
+ s = elapsed % 60
53
  return f"{h:02d}:{m:02d}:{s:02d}"
54
 
55
+ def pct(self) -> float:
56
+ if self.total_words == 0:
57
+ return 0.0
58
+ return (self.completed_words / self.total_words) * 100.0
59
+
60
+ job = JobState()