#!/usr/bin/env python3 """Watch the gateway's alarm journal; export HF-1's preceding 15 minutes. Python standard library only, macOS/Linux. First run starts at the journal's end; subsequent runs resume from reports/alarm-snapshots/.cursor.json. """ import csv from datetime import datetime, timedelta, timezone import fcntl import json import math import os from pathlib import Path import signal import sys import time from urllib.parse import urlencode from urllib.request import urlopen ROOT = Path(__file__).resolve().parent.parent URL = os.getenv("THERMITE_PROJECT_URL", "http://localhost:8432/p/cast-house").rstrip("/") JOURNAL = Path(os.getenv("THERMITE_ALARM_JOURNAL", str(ROOT / "site/logs/alarms.jsonl"))) OUT = Path(os.getenv("THERMITE_SNAPSHOT_DIR", str(ROOT / "reports/alarm-snapshots"))) ALARM = "hf1/bath_temp:hi" TAGS = {"hf1/bath_temp": "degF", "hf1/level": "%"} def log(message): print(f"{datetime.now(timezone.utc).isoformat()} {message}", flush=True) def instant(text): return datetime.fromisoformat(text.replace("Z", "+00:00")) def get(path): with urlopen(URL + "/api/" + path, timeout=15) as response: return json.load(response) def atomic_write(path, write): temporary = path.with_name(path.name + ".tmp") with temporary.open("w", newline="", encoding="utf-8") as stream: write(stream) stream.flush() os.fsync(stream.fileno()) temporary.replace(path) def good(point): return (point["quality"] == "good" and isinstance(point["value"], (int, float)) and not isinstance(point["value"], bool) and math.isfinite(point["value"])) def snapshot(event): end = instant(event["timestamp"]) start = end - timedelta(minutes=15) stem = OUT / ("hf1-bath-temp-hi_" + end.strftime("%Y-%m-%dT%H-%M-%S.%fZ")) csv_path, text_path = Path(str(stem) + ".csv"), Path(str(stem) + ".txt") if csv_path.exists() and text_path.exists(): return # The summary is the completion marker; safe to replay a journal entry. # Let the existing historian recorder flush, keeping the window fixed at the alarm. time.sleep(max(0, end.timestamp() + 15 - time.time())) points = {} for tag in TAGS: query = urlencode(dict(start=start.isoformat(), end=end.isoformat(), mode="raw", limit=100000)) rows = get("history/" + tag + "?" + query)["points"] if len(rows) >= 100000: raise RuntimeError(f"History limit reached for {tag}; refusing a truncated export") points[tag] = sorted((p for p in rows if start <= instant(p["timestamp"]) <= end), key=lambda p: instant(p["timestamp"])) if not points[tag]: raise RuntimeError(f"No recorded history for {tag}; will retry") temperatures = [p for p in points["hf1/bath_temp"] if good(p)] peak = max(temperatures, key=lambda p: p["value"]) if temperatures else None level = points["hf1/level"][-1] summary = [f"HF-1 high-temperature alarm: {event['timestamp']} (UTC)", f"Occurrence: {event['occurrence']}; project: {event['project_id']}", f"Window: {start.isoformat()} through {end.isoformat()} (inclusive)", f"Trip value: {event['value']:.1f} degF (alarm activation event)"] summary.append(f"Peak recorded temperature: {peak['value']:.1f} degF at {peak['timestamp']}" if peak else "Peak recorded temperature: unavailable (no good samples)") summary.append(f"Level at alarm: {level['value']:.1f} %; last good sample {level['timestamp']} " f"({(end - instant(level['timestamp'])).total_seconds():.3f} s before alarm)" if good(level) else "Level at alarm: unavailable (latest sample is not good)") summary.append("History is recorded on change with deadbands; no interpolation. " "Summary uses good samples only; CSV retains every recorded quality.") summary.append("Samples: " + "; ".join( f"{tag}: {len(rows)}; first {rows[0]['timestamp']}; last {rows[-1]['timestamp']}" for tag, rows in points.items())) rows = sorted(((p["timestamp"], tag, p["value"], p["quality"], TAGS[tag]) for tag, samples in points.items() for p in samples), key=lambda row: instant(row[0])) def write_csv(stream): writer = csv.writer(stream) writer.writerow(["timestamp_utc", "tag", "value", "quality", "units"]) writer.writerows(rows) atomic_write(csv_path, write_csv) atomic_write(text_path, lambda stream: stream.write("\n".join(summary) + "\n")) log(f"Saved {csv_path.name} and {text_path.name}: {len(rows)} samples") def main(): OUT.mkdir(parents=True, exist_ok=True) lock = (OUT / ".watcher.lock").open("a") fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) # One writer per output directory. cursor = OUT / ".cursor.json" state = json.loads(cursor.read_text()) if cursor.exists() else None project_id = None log(f"Watching {JOURNAL} for {ALARM}; history from {URL}") while True: try: if project_id is None: project_id = get("status")["project_id"] with JOURNAL.open("rb") as journal: stat = os.fstat(journal.fileno()) identity = [stat.st_dev, stat.st_ino] if state is None: state = dict(file=identity, offset=stat.st_size) atomic_write(cursor, lambda stream: json.dump(state, stream)) log("First run: starting at journal end; waiting for a new activation") if state["file"] != identity or state["offset"] > stat.st_size: state = dict(file=identity, offset=0) journal.seek(state["offset"]) while line := journal.readline(): if not line.endswith(b"\n"): break # A writer may still be appending this record. event = json.loads(line) if (event.get("project_id") == project_id and event.get("alarm_id") == ALARM and event.get("transition") == "active"): log(f"Activation {event['timestamp']}, occurrence {event['occurrence']}") snapshot(event) # Advance only after both reports were successfully published. next_state = dict(file=identity, offset=journal.tell()) atomic_write(cursor, lambda stream: json.dump(next_state, stream)) state = next_state time.sleep(1) except (OSError, ValueError, KeyError, RuntimeError) as error: log(f"Retrying in 5 seconds: {error}") time.sleep(5) if __name__ == "__main__": signal.signal(signal.SIGTERM, lambda *_: sys.exit(0)) try: main() except KeyboardInterrupt: pass