feat(scheduler): per-source cadence + source-health log with failure diagnosis + UI copy

This commit is contained in:
Kunthawat Greethong committed 2026-08-28 11:41:48 +07:00
1 parent 105ab86bf6
commit 14b2aeff7c
7 files changed
+363 -32

No files matched your search

+62
View File
@@ -128,6 +128,68 @@ class VintageCollectionTest(unittest.TestCase):
len(__import__("json").loads(manifest.read_text()).get("snapshots", {})), 1)
class CadenceAndHealthTest(unittest.TestCase):
"""Per-source cadence + source-health log (user requirements)."""
def test_job_due_respects_frequency_marker(self):
import json
snap_dir = Path(tempfile.mkdtemp())
sched = AppDataScheduler(_FakeCache(), snap_dir, interval_seconds=99999)
job = {"key": "macro_thai", "frequency": "monthly"}
# never run -> due
self.assertTrue(sched._job_due(job))
sched._mark_job_run(job)
# just marked -> not due again for ~30 days
self.assertFalse(sched._job_due(job))
# age the marker back 40 days -> due again
import datetime as _dt
old = (_dt.datetime.now().astimezone() - _dt.timedelta(days=40)).isoformat(timespec="seconds")
(snap_dir / "scheduler" / "job_macro_thai.json").write_text(
json.dumps({"at": old}), encoding="utf-8")
self.assertTrue(sched._job_due(job))
def test_analyze_error_classifies(self):
from app.scheduler import AppDataScheduler
self.assertEqual(AppDataScheduler._analyze_error("timed out connecting"), "timeout")
self.assertEqual(AppDataScheduler._analyze_error("Connection refused to host"), "network")
self.assertEqual(AppDataScheduler._analyze_error("HTTP 404 Not Found"), "http")
self.assertEqual(AppDataScheduler._analyze_error("JSONDecodeError: expecting value"), "parse")
self.assertEqual(AppDataScheduler._analyze_error("page structure changed - KeyError 'field'"), "structure")
self.assertEqual(AppDataScheduler._analyze_error("unauthorized token expired"), "auth")
self.assertEqual(AppDataScheduler._analyze_error(""), "other")
def test_source_health_log_written_and_readable(self):
import json
snap_dir = Path(tempfile.mkdtemp())
sched = AppDataScheduler(_FakeCache(), snap_dir, interval_seconds=99999)
results = [
{"key": "macro_thai", "label": "ภาพรวม (BOT)", "ok": True, "at": "2026-08-28T00:00:00+07:00"},
{"key": "auto_npl", "label": "NPL รถ", "ok": False,
"error": "JSONDecodeError: expecting value at line 1 (structure change?)",
"at": "2026-08-28T00:01:00+07:00"},
]
sched._append_source_log(results)
entries = sched._load_source_health()
self.assertEqual(len(entries), 2)
by_key = {e["key"]: e for e in entries}
self.assertTrue(by_key["macro_thai"]["ok"])
self.assertFalse(by_key["auto_npl"]["ok"])
self.assertEqual(by_key["auto_npl"]["category"], "parse")
def test_refresh_only_runs_due_jobs(self):
# with frequency markers set to "now", a second refresh_all in the same
# tick should skip all factor jobs but still try daily jobs.
snap_dir = Path(tempfile.mkdtemp())
sched = AppDataScheduler(_FakeCache(), snap_dir, interval_seconds=99999)
from app.scheduler import _REFRESH_JOBS, _DAILY_JOBS
for job in _REFRESH_JOBS + _DAILY_JOBS:
sched._mark_job_run(job)
# all marked -> a refresh tick runs nothing successfully (no network)
results = sched.refresh_all()
# daily jobs that ARE collections we stubbed: none should hard-crash
self.assertIsInstance(results, list)
class DividendCooldownTest(unittest.TestCase):
def _sched(self, snap_dir, cooldown):
return AppDataScheduler(_FakeCache(), snap_dir, interval_seconds=99999,