Files
html/api/reclassify-health.py
2026-04-16 14:55:04 +02:00

46 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
import json, time, os, shutil
H = "/var/www/html/api/screens-health.json"
S = "/var/www/html/api/screens-health-smart.json"
if not os.path.exists(S):
print(json.dumps({"error":"no smart classification"}))
raise SystemExit(0)
h = json.load(open(H))
sm = json.load(open(S))
# Build URL → smart_status map from details_by_status OR false_positives
url_to_smart = {}
if "false_positives" in sm:
for fp in sm["false_positives"]:
url_to_smart[fp["url"]] = fp.get("status") or fp.get("smart_status")
if "details_by_status" in sm:
for status, urls in sm["details_by_status"].items():
for u in urls:
if u != "...":
url_to_smart[u] = status
remap = {"POST_OK":"UP","POST_BAD_REQUEST":"API_POST","POST_302":"UP",
"POST_NOT_ALLOWED":"API_POST","AUTH_REQUIRED":"PROTECTED"}
reclassified = 0
for u, smart in url_to_smart.items():
if smart not in remap: continue
if u in h.get("by_url",{}):
h["by_url"][u]["status"] = remap[smart]
h["by_url"][u]["smart_classified"] = smart
reclassified += 1
counts = {}
for u,d in h.get("by_url",{}).items():
s = d.get("status","UNKNOWN")
counts[s] = counts.get(s,0) + 1
h["counts"] = counts
h["reclassified_at"] = time.strftime("%Y-%m-%dT%H:%M:%S+0200")
h["reclassified_count"] = reclassified
bk = H + ".pre-reclass-" + time.strftime("%Y%m%d_%H%M%S")
shutil.copy(H, bk)
json.dump(h, open(H,"w"), indent=2)
print(json.dumps({"reclassified": reclassified, "new_counts": counts, "backup": bk, "smart_urls_mapped": len(url_to_smart)}))