Add per-source status tracking, /health sources block and /sources

Collectors record the result of every source (schema v3, table
source_status); /health reports failing sources (failures in a row >=
source_failure_threshold) and turns degraded; GET /sources shows the full
state; runs end with a summary log line instead of "data saved".

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5 committed 2026-09-21 10:46:38 +03:00
1 parent ab89c87321
commit cb935fef0d
8 files changed
+351 -13

No files matched your search

+44
View File
@@ -145,3 +145,47 @@ def test_health_hides_internal_paths(env):
def json_dump_has_no_paths(text):
return "/data" not in text and "corrupt" not in text and "not a database" not in text
def test_health_reports_failing_sources(env):
cc.add_to_config_list("asns", 62041)
cc.add_to_config_list("fqdns", "example.com")
save_json_atomic(cc.STATUS_FILE, {"updated_at": datetime.datetime.now().isoformat(), "jobs": {}}) # демон "жив"
now = datetime.datetime.now()
def fail(times):
for _ in range(times):
with db.session() as conn, db.transaction(conn):
db.record_source_result(conn, "asn", "62041", now, "http_5xx")
# До порога (3 сбоя подряд) источник не считается неисправным
fail(2)
health = env.get("/health").json()
assert health["status"] == "ok" and health["sources"] == {"asn": {"total": 1, "failing": []},
"fqdn": {"total": 1, "failing": []}}
# На пороге: degraded, источник в списке, наружу - только категория ошибки
fail(1)
health = env.get("/health").json()
assert health["status"] == "degraded"
assert health["sources"]["asn"]["failing"] == [{"source": "62041", "failures": 3, "last_success": None,
"error_kind": "http_5xx"}]
# Полная картина: адреса источника и состояние; неопрошенный источник - пустое состояние
with db.session() as conn, db.transaction(conn):
db.merge_source(conn, "asn", "62041", {"1.0.0.0/24", "2.0.0.0/24"}, now, 90)
report = env.get("/sources").json()
assert report["failure_threshold"] == 3
asn, fqdn = report["sources"]
assert (asn["kind"], asn["source"], asn["addresses"], asn["failures"]) == ("asn", "62041", 2, 3)
assert (fqdn["source"], fqdn["addresses"], fqdn["last_attempt"], fqdn["failures"]) == ("example.com", 0, None, 0)
# Успех сбрасывает счётчик, /health снова ok
with db.session() as conn, db.transaction(conn):
db.record_source_result(conn, "asn", "62041", now)
assert env.get("/health").json()["status"] == "ok"
# Порог настраивается ключом source_failure_threshold
cc.update_config(lambda config: config.update(source_failure_threshold=1))
fail(1)
assert env.get("/health").json()["status"] == "degraded"