#!/usr/bin/env python3 """In trạng thái ngắn của job core upgrade để dùng cùng `watch`.""" from __future__ import annotations import datetime as dt import os import re import subprocess from pathlib import Path ROOT = Path(__file__).resolve().parents[1] LOG = ROOT / "reports/logs/core_upgrade.log" LABELS = ROOT / "data/splits/nlu_labels_planner.jsonl" PID_FILE = ROOT / "artifacts/core_upgrade.pid" def duration(seconds: float) -> str: if seconds < 0 or seconds == float("inf"): return "--" seconds = int(seconds) hours, rem = divmod(seconds, 3600) minutes, secs = divmod(rem, 60) return f"{hours:02d}:{minutes:02d}:{secs:02d}" def main() -> None: text = LOG.read_text(encoding="utf-8", errors="replace") if LOG.exists() else "" pid = int(PID_FILE.read_text().strip()) if PID_FILE.exists() else -1 alive = pid > 0 and Path(f"/proc/{pid}").exists() child = subprocess.run( ["pgrep", "-P", str(pid), "-a"], capture_output=True, text=True ).stdout.strip() if alive else "" if "DONE core upgrade" in text: print("ĐÃ HOÀN THÀNH core upgrade") eval_lines = [line for line in text.splitlines() if "eval_civic_nlu" in line and ("dev:" in line or "test:" in line)] for line in eval_lines[-2:]: print(line) print("Toàn bộ pytest: PASS") return if not alive: print("JOB ĐÃ DỪNG - xem cuối file log để biết lỗi") for line in text.splitlines()[-5:]: print(line) return match = re.search(r"Planner labels: chọn (\d+) mẫu train, đã có (\d+), còn (\d+)", text) planner_running = "planner_label" in child if planner_running and match: initial = int(match.group(2)) # Bộ lấy mẫu có thể chứa vài doc_id trùng do dữ liệu nguồn; vòng chạy vẫn # xử lý đúng `chọn N` dòng, còn bước build dataset sẽ index theo doc_id. target = int(match.group(1)) with LABELS.open("rb") as handle: current = sum(1 for _ in handle) generated = max(0, current - initial) stamps = re.findall(r"(\d{2}:\d{2}:\d{2}).*Prompt: system", text) now = dt.datetime.now(dt.timezone.utc) if stamps: clock = dt.time.fromisoformat(stamps[-1]).replace(tzinfo=dt.timezone.utc) started = dt.datetime.combine(now.date(), clock) if started > now: started -= dt.timedelta(days=1) elapsed = max(1.0, (now - started).total_seconds()) else: elapsed = 1.0 rate = generated * 3600 / elapsed eta = (target - current) * 3600 / rate if rate > 0 else float("inf") pct = 100 * current / max(1, target) width = 30 filled = min(width, round(width * current / max(1, target))) bar = "#" * filled + "-" * (width - filled) print(f"Planner [{bar}] {current:,}/{target:,} ({pct:5.2f}%)") print(f"Tốc độ: {rate:,.0f} mẫu/giờ | ETA planner: {duration(eta)} | GPU PID: {child.split()[0]}") return train_lines = [line for line in text.splitlines() if " step " in line and "ETA" in line] if train_lines: print("Giai đoạn: train Model A v2") print(train_lines[-1]) else: stage = child.split(" ", 1)[1] if " " in child else "chuyển giai đoạn" print(f"Job đang chạy | giai đoạn: {stage}") print("Dùng `tail -f reports/logs/core_upgrade.log` để xem chi tiết.") if __name__ == "__main__": main()