implemented watcher and rerun logic
This commit is contained in:
+58
-7
@@ -1,12 +1,17 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
# Immutable-in-practice graph state for the loaded DUT test set.
|
||||
_TESTS_BY_ID: dict[str, Any] = {}
|
||||
_GRAPH: dict[str, set[str]] = {}
|
||||
DUT = os.getenv("DUT", "CGW453").strip()
|
||||
APP_ROOT = Path(__file__).resolve().parent
|
||||
DB_PATH = APP_ROOT / "scheduler.db"
|
||||
GRAPH_CACHE_NAME = "dut_compatibility"
|
||||
|
||||
|
||||
def reset_graph_state() -> None:
|
||||
@@ -44,7 +49,58 @@ def build_graph_once(tests: list[Any]) -> dict[str, set[str]]:
|
||||
return _GRAPH
|
||||
|
||||
|
||||
def get_graph() -> dict[str, set[str]]:
|
||||
def serialize_graph(graph: dict[str, set[str]]) -> dict[str, list[str]]:
|
||||
return {str(test_id): sorted(str(neighbor) for neighbor in neighbors) for test_id, neighbors in graph.items()}
|
||||
|
||||
|
||||
def deserialize_graph(data: dict[str, list[str]]) -> dict[str, set[str]]:
|
||||
deserialized: dict[str, set[str]] = {}
|
||||
for test_id, neighbors in (data or {}).items():
|
||||
deserialized[str(test_id)] = {str(neighbor) for neighbor in (neighbors or [])}
|
||||
return deserialized
|
||||
|
||||
|
||||
def _persist_graph(db_path: str | Path = DB_PATH) -> None:
|
||||
import db as db_module
|
||||
db_module.save_graph_cache(
|
||||
name=GRAPH_CACHE_NAME,
|
||||
payload=serialize_graph(_GRAPH),
|
||||
test_count=len(_GRAPH),
|
||||
db_path=db_path,
|
||||
)
|
||||
|
||||
|
||||
def _load_graph_from_db(db_path: str | Path = DB_PATH) -> bool:
|
||||
global _GRAPH
|
||||
import db as db_module
|
||||
cached = db_module.load_graph_cache(GRAPH_CACHE_NAME, db_path)
|
||||
if cached is None:
|
||||
return False
|
||||
_GRAPH = deserialize_graph(cached.get("payload") or {})
|
||||
return True
|
||||
|
||||
|
||||
def build_and_persist_graph(tests: list[Any], db_path: str | Path = DB_PATH) -> dict[str, set[str]]:
|
||||
graph = build_graph_once(tests)
|
||||
_persist_graph(db_path)
|
||||
return graph
|
||||
|
||||
|
||||
def get_graph(
|
||||
db_path: str | Path = DB_PATH,
|
||||
dut_tests: list[Any] | None = None,
|
||||
) -> dict[str, set[str]]:
|
||||
if _GRAPH:
|
||||
return _GRAPH
|
||||
|
||||
if _load_graph_from_db(db_path):
|
||||
return _GRAPH
|
||||
|
||||
if dut_tests is None:
|
||||
import db as db_module
|
||||
dut_tests = db_module.list_tests_for_device(DUT, db_path)
|
||||
|
||||
build_and_persist_graph(dut_tests or [], db_path)
|
||||
return _GRAPH
|
||||
|
||||
|
||||
@@ -60,10 +116,6 @@ def _build_graph(tests: dict[str, Any]) -> dict[str, set[str]]:
|
||||
if _compatible(a, b):
|
||||
graph[a_id].add(b_id)
|
||||
graph[b_id].add(a_id)
|
||||
# Print out graph for debugging
|
||||
out = Path('output1.txt')
|
||||
with out.open('w', encoding='utf-8') as f:
|
||||
f.write(json.dumps({k: list(v) for k, v in graph.items()}, indent=2))
|
||||
return graph
|
||||
|
||||
|
||||
@@ -93,7 +145,6 @@ def _same_testpoint_to_station(
|
||||
stations_a = {s for s, t in station_map_a.items() if t == testpoint}
|
||||
stations_b = {s for s, t in station_map_b.items() if t == testpoint}
|
||||
if stations_a != stations_b:
|
||||
print(f"Tests {test_a.test_id} and {test_b.test_id} have conflicting stations for testpoint. {station_map_a} vs {station_map_b}")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user