#!/usr/bin/env python3
"""Data-flow freshness watchdog. Run hourly from cron.

Checks: (a) new files keep landing in DATA_DIR, (b) a nightly job keeps
writing to JOB_DIR, (c) a health endpoint returns {"ok": true}.
On anomaly: writes STATUS_FILE, prints to stderr, exits 1 so cron mails it.
Success is silent.
"""
import json, sys, time, urllib.request
from pathlib import Path

# --- config ---
DATA_DIR = Path("/path/to/incoming/data")       # files should appear here regularly
JOB_DIR = Path("/path/to/nightly/job/output")   # nightly job writes here
STATUS_FILE = Path("/path/to/watchdog-status.json")
HEALTH_URL = "http://your-server:8080/health"   # must return JSON with "ok": true
DATA_STALE_SECS = 36 * 3600
JOB_STALE_SECS = 30 * 3600   # ~daily job; 30h absorbs cron scheduling jitter
# --------------

def newest_mtime(paths):
    return max((p.stat().st_mtime for p in paths), default=0)

def check():
    anomalies = []
    now = time.time()
    data = newest_mtime(list(DATA_DIR.glob("*"))) if DATA_DIR.is_dir() else 0
    if not data:
        anomalies.append(f"no files under {DATA_DIR}")
    elif now - data > DATA_STALE_SECS:
        anomalies.append(f"data stale: newest {(now-data)/3600:.0f}h old (>{DATA_STALE_SECS//3600}h)")
    job = newest_mtime(list(JOB_DIR.glob("*"))) if JOB_DIR.is_dir() else 0
    if not job:
        anomalies.append(f"no files under {JOB_DIR}")
    elif now - job > JOB_STALE_SECS:
        anomalies.append(f"job output stale: newest {(now-job)/3600:.0f}h old (>{JOB_STALE_SECS//3600}h)")
    try:
        with urllib.request.urlopen(HEALTH_URL, timeout=5) as r:
            h = json.load(r)
        if not h.get("ok"):
            anomalies.append(f"health endpoint not ok: {h}")
    except Exception as e:
        anomalies.append(f"health endpoint unreachable ({HEALTH_URL}): {e}")
    return anomalies

def main():
    anomalies = check()
    stamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
    STATUS_FILE.write_text(json.dumps({"ts": stamp, "ok": not anomalies, "anomalies": anomalies}))
    if anomalies:
        print("WATCHDOG ANOMALY: " + " | ".join(anomalies), file=sys.stderr)
        sys.exit(1)

if __name__ == "__main__":
    main()
# src: ezra, contact: ezkru69@mail.com