Add status_alerts table and status_alert_after_hours setting
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3ac7111371
commit
d6480ef1b9
2 files changed
+34
No files matched your search
@@ -34,6 +34,12 @@ CREATE TABLE IF NOT EXISTS stale_alerts (
|
||||
host TEXT PRIMARY KEY REFERENCES hosts(name) ON DELETE CASCADE,
|
||||
last_seen INTEGER
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS status_alerts (
|
||||
host TEXT PRIMARY KEY REFERENCES hosts(name) ON DELETE CASCADE,
|
||||
level TEXT NOT NULL,
|
||||
since INTEGER NOT NULL,
|
||||
notified_level TEXT
|
||||
);
|
||||
"""
|
||||
|
||||
|
||||
@@ -164,6 +170,30 @@ def clear_stale_alert(conn: sqlite3.Connection, host: str) -> None:
|
||||
conn.execute("DELETE FROM stale_alerts WHERE host = ?", (host,))
|
||||
|
||||
|
||||
# --- pending reboot/update notifications ------------------------------------
|
||||
|
||||
def status_alerts(conn: sqlite3.Connection) -> dict[str, tuple[str, int, str | None]]:
|
||||
"""host -> (level, when it entered that level, level last notified for)."""
|
||||
return {r["host"]: (r["level"], r["since"], r["notified_level"])
|
||||
for r in conn.execute("SELECT * FROM status_alerts")}
|
||||
|
||||
|
||||
def save_status_alert(conn: sqlite3.Connection, host: str, level: str, since: int,
|
||||
notified_level: str | None) -> None:
|
||||
# The host may have been removed while a notification was in flight.
|
||||
conn.execute(
|
||||
"INSERT INTO status_alerts (host, level, since, notified_level) "
|
||||
"SELECT name, ?, ?, ? FROM hosts WHERE name = ? "
|
||||
"ON CONFLICT(host) DO UPDATE SET level = excluded.level, since = excluded.since, "
|
||||
"notified_level = excluded.notified_level",
|
||||
(level, since, notified_level, host),
|
||||
)
|
||||
|
||||
|
||||
def clear_status_alert(conn: sqlite3.Connection, host: str) -> None:
|
||||
conn.execute("DELETE FROM status_alerts WHERE host = ?", (host,))
|
||||
|
||||
|
||||
# --- users & sessions -------------------------------------------------------
|
||||
|
||||
def set_user(conn: sqlite3.Connection, name: str, pw_hash: str) -> None:
|
||||
|
||||
Reference in new issue
Block a user