31 lines
908 B
Python
31 lines
908 B
Python
import json, hashlib
|
|
from pathlib import Path
|
|
|
|
|
|
class DiskCache:
|
|
def __init__(self, d=".cache"):
|
|
self.d = Path(d)
|
|
self.d.mkdir(parents=True, exist_ok=True)
|
|
|
|
def _p(self, k):
|
|
return self.d / f"{hashlib.sha256(k.encode()).hexdigest()}.json"
|
|
|
|
def get(self, k):
|
|
p = self._p(k)
|
|
return json.loads(p.read_text()) if p.exists() else None
|
|
|
|
def set(self, k, v):
|
|
self._p(k).write_text(json.dumps(v))
|
|
|
|
|
|
class ReportStore:
|
|
def __init__(self, base="./reports"):
|
|
self.b = Path(base)
|
|
|
|
def save_history(self, prog, status, matched, dur):
|
|
t = self.b / "trends" / f"{prog}.jsonl"
|
|
t.parent.mkdir(parents=True, exist_ok=True)
|
|
import datetime
|
|
t.write_text(json.dumps({"ts": datetime.datetime.now().isoformat(), "status": status,
|
|
"fields_matched": matched, "duration_s": dur}) + "\n")
|