s-ttp commited on
Commit
29bb590
·
verified ·
1 Parent(s): 0889614

Upload analyst.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. analyst.py +19 -1
analyst.py CHANGED
@@ -12,6 +12,7 @@ import queue
12
  import re
13
  import subprocess
14
  import threading
 
15
  import uuid
16
 
17
  import config
@@ -65,6 +66,7 @@ MILESTONES = [
65
  ]
66
 
67
  _jobs: dict[str, dict] = {}
 
68
 
69
 
70
  ROLE_SENTINEL = "@@CRUCIBLE_ROLE@@"
@@ -154,10 +156,11 @@ def _rating(expected_return_pct: float) -> str:
154
  def _run(job: dict) -> None:
155
  if not _run_lock.acquire(blocking=False): # another run in flight → queue behind it
156
  job["status"] = "queued"
157
- job["queue"].put({"t": "log", "line": "Another analysis is running — queued…"})
158
  _run_lock.acquire()
159
  try:
160
  job["status"] = "running"
 
161
  report_path = config.RECOMMENDER_DIR / f"{job['ticker']}_report.json"
162
  report_path.unlink(missing_ok=True) # never read a stale report from a prior run
163
  proc = subprocess.Popen(
@@ -188,6 +191,7 @@ def _run(job: dict) -> None:
188
  watchdog.cancel()
189
  except Exception:
190
  pass
 
191
  _run_lock.release()
192
 
193
  if report_path.exists():
@@ -227,11 +231,25 @@ def start(ticker: str) -> str:
227
  "debate": None,
228
  "damodaran": None,
229
  }
 
230
  _jobs[job_id] = job
231
  threading.Thread(target=_run, args=(job,), daemon=True).start()
232
  return job_id
233
 
234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
  def exists(job_id: str) -> bool:
236
  return job_id in _jobs
237
 
 
12
  import re
13
  import subprocess
14
  import threading
15
+ import time
16
  import uuid
17
 
18
  import config
 
66
  ]
67
 
68
  _jobs: dict[str, dict] = {}
69
+ _current: dict = {"job_id": None, "ticker": None, "started": None} # who holds the run lock
70
 
71
 
72
  ROLE_SENTINEL = "@@CRUCIBLE_ROLE@@"
 
156
  def _run(job: dict) -> None:
157
  if not _run_lock.acquire(blocking=False): # another run in flight → queue behind it
158
  job["status"] = "queued"
159
+ job["queue"].put({"t": "log", "line": f"Another analysis ({_current.get('ticker') or '?'}) is running — queued…"})
160
  _run_lock.acquire()
161
  try:
162
  job["status"] = "running"
163
+ _current.update(job_id=job.get("id"), ticker=job["ticker"], started=time.time())
164
  report_path = config.RECOMMENDER_DIR / f"{job['ticker']}_report.json"
165
  report_path.unlink(missing_ok=True) # never read a stale report from a prior run
166
  proc = subprocess.Popen(
 
191
  watchdog.cancel()
192
  except Exception:
193
  pass
194
+ _current.update(job_id=None, ticker=None, started=None)
195
  _run_lock.release()
196
 
197
  if report_path.exists():
 
231
  "debate": None,
232
  "damodaran": None,
233
  }
234
+ job["id"] = job_id; job["created"] = time.time()
235
  _jobs[job_id] = job
236
  threading.Thread(target=_run, args=(job,), daemon=True).start()
237
  return job_id
238
 
239
 
240
+ def jobs_view() -> dict:
241
+ """Observability: who holds the engine, who is waiting, how long."""
242
+ now = time.time()
243
+ cur = dict(_current)
244
+ if cur.get("started"):
245
+ cur["age_s"] = int(now - cur["started"])
246
+ queued = [{"job_id": j, "ticker": v["ticker"], "waiting_s": int(now - v.get("created", now))}
247
+ for j, v in _jobs.items() if v["status"] == "queued"]
248
+ recent = sorted(((v.get("created", 0), j, v["ticker"], v["status"]) for j, v in _jobs.items()), reverse=True)[:10]
249
+ return {"running": cur, "queued": queued,
250
+ "recent": [{"job_id": j, "ticker": t, "status": st} for _, j, t, st in recent]}
251
+
252
+
253
  def exists(job_id: str) -> bool:
254
  return job_id in _jobs
255