sensitivebox 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. sensitivebox/__init__.py +10 -0
  2. sensitivebox/alerts/__init__.py +94 -0
  3. sensitivebox/alerts/config.py +85 -0
  4. sensitivebox/alerts/defaults.toml +126 -0
  5. sensitivebox/alerts/engine.py +624 -0
  6. sensitivebox/alerts/events.py +87 -0
  7. sensitivebox/alerts/globs.py +84 -0
  8. sensitivebox/alerts/model.py +215 -0
  9. sensitivebox/alerts/mute.py +55 -0
  10. sensitivebox/alerts/replay.py +99 -0
  11. sensitivebox/alerts/routing.py +247 -0
  12. sensitivebox/api/__init__.py +6 -0
  13. sensitivebox/api/app.py +225 -0
  14. sensitivebox/api/data.py +109 -0
  15. sensitivebox/api/host.py +27 -0
  16. sensitivebox/api/model.py +90 -0
  17. sensitivebox/api/security.py +89 -0
  18. sensitivebox/api/server.py +108 -0
  19. sensitivebox/api/state.py +220 -0
  20. sensitivebox/api/static/assets/archivo-latin-wdth-normal-DY7AcnAa.woff2 +0 -0
  21. sensitivebox/api/static/assets/ibm-plex-mono-medium-BpyVxOr_.woff2 +0 -0
  22. sensitivebox/api/static/assets/index-BH9EydfS.js +10 -0
  23. sensitivebox/api/static/assets/index-RMruxW54.css +1 -0
  24. sensitivebox/api/static/assets/mark-on-dark-CpSxozdM.png +0 -0
  25. sensitivebox/api/static/assets/mark-on-light-DgUAeHTp.png +0 -0
  26. sensitivebox/api/static/assets/mark-raised-Bg5z6PGs.png +0 -0
  27. sensitivebox/api/static/index.html +14 -0
  28. sensitivebox/browser.py +106 -0
  29. sensitivebox/cli.py +467 -0
  30. sensitivebox/collector.py +319 -0
  31. sensitivebox/completion.py +117 -0
  32. sensitivebox/daemon.py +181 -0
  33. sensitivebox/data/icons/128.png +0 -0
  34. sensitivebox/data/icons/256.png +0 -0
  35. sensitivebox/data/icons/512.png +0 -0
  36. sensitivebox/data/proart-p16.tar.gz +0 -0
  37. sensitivebox/data/sensitivebox.desktop.in +8 -0
  38. sensitivebox/data/sensitivebox.service.in +11 -0
  39. sensitivebox/doctor.py +322 -0
  40. sensitivebox/fixtures.py +177 -0
  41. sensitivebox/history.py +298 -0
  42. sensitivebox/hwmon/__init__.py +20 -0
  43. sensitivebox/hwmon/classify.py +74 -0
  44. sensitivebox/hwmon/discovery.py +139 -0
  45. sensitivebox/hwmon/ids.py +35 -0
  46. sensitivebox/hwmon/model.py +78 -0
  47. sensitivebox/hwmon/normalise.py +42 -0
  48. sensitivebox/hwmon/reader.py +65 -0
  49. sensitivebox/inspect.py +175 -0
  50. sensitivebox/live.py +56 -0
  51. sensitivebox/notify/__init__.py +7 -0
  52. sensitivebox/notify/base.py +60 -0
  53. sensitivebox/notify/desktop.py +84 -0
  54. sensitivebox/notify/log.py +30 -0
  55. sensitivebox/operations.py +44 -0
  56. sensitivebox/output.py +76 -0
  57. sensitivebox/process.py +25 -0
  58. sensitivebox/py.typed +0 -0
  59. sensitivebox/redact.py +85 -0
  60. sensitivebox/report.py +357 -0
  61. sensitivebox/runtime.py +313 -0
  62. sensitivebox/sample.py +84 -0
  63. sensitivebox/service.py +112 -0
  64. sensitivebox/simulate.py +220 -0
  65. sensitivebox/sources/__init__.py +83 -0
  66. sensitivebox/sources/apps.py +135 -0
  67. sensitivebox/sources/bluez.py +96 -0
  68. sensitivebox/sources/bluez_watch.py +214 -0
  69. sensitivebox/sources/changes.py +121 -0
  70. sensitivebox/sources/cpu.py +152 -0
  71. sensitivebox/sources/dbus.py +174 -0
  72. sensitivebox/sources/logind.py +88 -0
  73. sensitivebox/sources/memory.py +127 -0
  74. sensitivebox/sources/net.py +128 -0
  75. sensitivebox/sources/power_profiles.py +116 -0
  76. sensitivebox/sources/power_supply.py +70 -0
  77. sensitivebox/sources/psi.py +78 -0
  78. sensitivebox/sources/report.py +85 -0
  79. sensitivebox/sources/wifi.py +90 -0
  80. sensitivebox/sources/wifi_nm.py +269 -0
  81. sensitivebox/status.py +205 -0
  82. sensitivebox/store/__init__.py +5 -0
  83. sensitivebox/store/changes.py +58 -0
  84. sensitivebox/store/db.py +423 -0
  85. sensitivebox/store/events.py +337 -0
  86. sensitivebox/store/mutes.py +58 -0
  87. sensitivebox/store/quality.py +51 -0
  88. sensitivebox/store/read.py +40 -0
  89. sensitivebox/store/schema.py +194 -0
  90. sensitivebox/store/status.py +105 -0
  91. sensitivebox-0.1.0.dist-info/METADATA +424 -0
  92. sensitivebox-0.1.0.dist-info/RECORD +95 -0
  93. sensitivebox-0.1.0.dist-info/WHEEL +4 -0
  94. sensitivebox-0.1.0.dist-info/entry_points.txt +4 -0
  95. sensitivebox-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,337 @@
1
+ """Incident facts, routed events and quality counters. Only the daemon is a writer."""
2
+
3
+ import math
4
+ import sqlite3
5
+ import statistics
6
+ import uuid
7
+ from collections.abc import Callable, Iterable, Mapping
8
+ from dataclasses import dataclass
9
+ from datetime import tzinfo
10
+
11
+ import msgspec
12
+
13
+ from sensitivebox.alerts.engine import Clock, Evaluation, TransitionKind
14
+ from sensitivebox.alerts.model import Rule, Severity
15
+ from sensitivebox.alerts.routing import Notification, Routed
16
+ from sensitivebox.store.quality import Weekly, local_zone, week_at, week_spans
17
+
18
+ RETENTION_S = 400 * 86400
19
+ CREATE = (
20
+ """CREATE TABLE IF NOT EXISTS alert_incidents (
21
+ id TEXT PRIMARY KEY, rule_id TEXT NOT NULL, signal TEXT NOT NULL, no_data INTEGER NOT NULL,
22
+ opened REAL NOT NULL, closed REAL, active_s REAL NOT NULL DEFAULT 0,
23
+ incomplete INTEGER NOT NULL DEFAULT 0, muted INTEGER NOT NULL DEFAULT 0,
24
+ acknowledged INTEGER NOT NULL DEFAULT 0)""",
25
+ """CREATE UNIQUE INDEX IF NOT EXISTS alert_open ON alert_incidents(rule_id, signal, no_data)
26
+ WHERE closed IS NULL""",
27
+ """CREATE TABLE IF NOT EXISTS incident_summaries (
28
+ incident_id TEXT PRIMARY KEY REFERENCES alert_incidents(id) ON DELETE CASCADE,
29
+ name TEXT NOT NULL, severity TEXT NOT NULL, peak REAL, unit TEXT NOT NULL DEFAULT '')""",
30
+ """CREATE TABLE IF NOT EXISTS alert_events (
31
+ id INTEGER PRIMARY KEY AUTOINCREMENT, incident_id TEXT NOT NULL REFERENCES alert_incidents(id),
32
+ ts REAL NOT NULL, kind TEXT NOT NULL, reason TEXT NOT NULL, notification TEXT NOT NULL)""",
33
+ "CREATE INDEX IF NOT EXISTS alert_events_ts ON alert_events(ts)",
34
+ """CREATE TABLE IF NOT EXISTS incident_calendar (
35
+ incident_id TEXT PRIMARY KEY REFERENCES alert_incidents(id) ON DELETE CASCADE,
36
+ week TEXT NOT NULL, zone TEXT NOT NULL)""",
37
+ """CREATE TABLE IF NOT EXISTS weekly_quality (
38
+ rule_id TEXT NOT NULL, week TEXT NOT NULL, zone TEXT NOT NULL, updated REAL NOT NULL,
39
+ fires INTEGER NOT NULL DEFAULT 0, seconds REAL NOT NULL DEFAULT 0,
40
+ durations TEXT NOT NULL DEFAULT '[]', incomplete INTEGER NOT NULL DEFAULT 0,
41
+ muted INTEGER NOT NULL DEFAULT 0, acknowledged INTEGER NOT NULL DEFAULT 0,
42
+ PRIMARY KEY(rule_id, week, zone))""",
43
+ "CREATE TABLE IF NOT EXISTS operations_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL)",
44
+ )
45
+
46
+
47
+ @dataclass(frozen=True, slots=True)
48
+ class Incident:
49
+ id: str
50
+ rule_id: str
51
+ signal: str
52
+ no_data: bool
53
+ opened: float
54
+ closed: float | None
55
+ active_s: float
56
+ incomplete: bool
57
+ muted: bool
58
+ acknowledged: bool
59
+
60
+ def __post_init__(self) -> None:
61
+ # SQLite persists flags as 0/1; public JSON and typed consumers require booleans.
62
+ for name in ("no_data", "incomplete", "muted", "acknowledged"):
63
+ object.__setattr__(self, name, bool(getattr(self, name)))
64
+
65
+
66
+ @dataclass(frozen=True, slots=True)
67
+ class AlertEvent:
68
+ id: int
69
+ incident_id: str
70
+ ts: float
71
+ kind: str
72
+ reason: str
73
+ notification: Notification
74
+
75
+
76
+ @dataclass(frozen=True, slots=True)
77
+ class IncidentSummary:
78
+ name: str
79
+ severity: Severity
80
+ peak: float | None
81
+ unit: str
82
+
83
+
84
+ class Events:
85
+ def __init__(self, db: sqlite3.Connection, clock: Clock, *, writer: bool = True) -> None:
86
+ self.db, self.clock = db, clock
87
+ self.zone: Callable[[], tzinfo] = local_zone
88
+ self._last: tuple[float, float] | None = None
89
+ if writer:
90
+ with db:
91
+ db.execute("UPDATE alert_incidents SET incomplete=1 WHERE closed IS NULL")
92
+
93
+ def _week(self, rule: str, ts: float) -> tuple[str, str, str]:
94
+ week, zone = week_at(ts, self.zone())
95
+ self.db.execute(
96
+ "INSERT OR IGNORE INTO weekly_quality(rule_id,week,zone,updated) VALUES (?,?,?,?)",
97
+ (rule, week, zone, self.clock()),
98
+ )
99
+ return rule, week, zone
100
+
101
+ def pause(self) -> None:
102
+ self._last = None
103
+
104
+ def observe(self, mono: float, max_gap: float | None = None) -> None:
105
+ now = self.clock()
106
+ previous = self._last
107
+ if previous is not None:
108
+ mono = max(mono, previous[0])
109
+ self._last = (mono, now)
110
+ if previous is None:
111
+ return
112
+ elapsed = max(0, mono - previous[0])
113
+ with self.db:
114
+ if max_gap is not None and elapsed > max_gap:
115
+ self.db.execute("UPDATE alert_incidents SET incomplete=1 WHERE closed IS NULL")
116
+ return
117
+ for incident in self.incidents(open_only=True):
118
+ self.db.execute(
119
+ "UPDATE alert_incidents SET active_s=active_s+? WHERE id=?",
120
+ (elapsed, incident.id),
121
+ )
122
+ # Attribute elapsed monotonic time to the civil weeks occupied at this poll.
123
+ for week, zone, seconds in week_spans(now - elapsed, now, self.zone()):
124
+ self.db.execute(
125
+ "INSERT OR IGNORE INTO weekly_quality(rule_id,week,zone,updated)"
126
+ " VALUES (?,?,?,?)",
127
+ (incident.rule_id, week, zone, now),
128
+ )
129
+ self.db.execute(
130
+ "UPDATE weekly_quality SET seconds=seconds+?,updated=?"
131
+ " WHERE rule_id=? AND week=? AND zone=?",
132
+ (seconds, now, incident.rule_id, week, zone),
133
+ )
134
+
135
+ def record(self, routed: Routed) -> None:
136
+ with self.db:
137
+ for note in routed.delivered:
138
+ self._record(note, "delivered")
139
+ for held in routed.held:
140
+ self._record(held.notification, held.reason)
141
+
142
+ def sample(
143
+ self,
144
+ evaluations: Iterable[Evaluation],
145
+ rules: Mapping[str, Rule],
146
+ units: Mapping[str, str],
147
+ ) -> None:
148
+ """Keep the worst real value of each open incident beyond the raw ring's lifetime.
149
+
150
+ Above rules retain their maximum; below rules retain their minimum. Missing and
151
+ no-data readings never become a peak. Recovery readings cannot replace the extreme.
152
+ """
153
+ with self.db:
154
+ for evaluation in evaluations:
155
+ rule = rules.get(evaluation.rule_id)
156
+ if rule is None:
157
+ continue
158
+ row = self.db.execute(
159
+ "SELECT i.id,s.peak,s.unit FROM alert_incidents i"
160
+ " JOIN incident_summaries s ON s.incident_id=i.id"
161
+ " WHERE i.rule_id=? AND i.signal=? AND i.no_data=? AND i.closed IS NULL",
162
+ (evaluation.rule_id, evaluation.signal, evaluation.no_data),
163
+ ).fetchone()
164
+ if row is None:
165
+ continue
166
+ peak: float | None = row[1]
167
+ value = evaluation.value
168
+ if (
169
+ not evaluation.no_data
170
+ and evaluation.missing is None
171
+ and isinstance(value, (int, float))
172
+ and math.isfinite(value)
173
+ and (rule.when != "equals" or value == evaluation.threshold)
174
+ ):
175
+ peak = (
176
+ value
177
+ if peak is None
178
+ else min(peak, value)
179
+ if rule.when == "below"
180
+ else max(peak, value)
181
+ )
182
+ self.db.execute(
183
+ "UPDATE incident_summaries SET name=?,peak=?,unit=? WHERE incident_id=?",
184
+ (rule.title, peak, units.get(evaluation.signal, row[2]), row[0]),
185
+ )
186
+
187
+ def _record(self, note: Notification, reason: str) -> None:
188
+ now = self.clock()
189
+ row = self.db.execute(
190
+ "SELECT id FROM alert_incidents WHERE rule_id=? AND signal=? AND no_data=?"
191
+ " AND closed IS NULL",
192
+ (note.rule_id, note.signal, note.no_data),
193
+ ).fetchone()
194
+ if row is None:
195
+ if note.kind is not TransitionKind.FIRING:
196
+ return
197
+ incident = uuid.uuid4().hex
198
+ self.db.execute(
199
+ "INSERT INTO alert_incidents(id,rule_id,signal,no_data,opened) VALUES(?,?,?,?,?)",
200
+ (incident, note.rule_id, note.signal, note.no_data, now),
201
+ )
202
+ self.db.execute(
203
+ "INSERT INTO incident_summaries(incident_id,name,severity) VALUES(?,?,?)",
204
+ (incident, note.rule_id, note.severity),
205
+ )
206
+ key = self._week(note.rule_id, now)
207
+ self.db.execute(
208
+ "INSERT INTO incident_calendar VALUES(?,?,?)", (incident, key[1], key[2])
209
+ )
210
+ self.db.execute(
211
+ "UPDATE weekly_quality SET fires=fires+1 WHERE rule_id=? AND week=? AND zone=?",
212
+ key,
213
+ )
214
+ kind = "firing"
215
+ else:
216
+ incident = str(row[0])
217
+ kind = "renotify" if note.kind is TransitionKind.FIRING else "resolved"
218
+ self.db.execute(
219
+ "INSERT INTO alert_events(incident_id,ts,kind,reason,notification) VALUES(?,?,?,?,?)",
220
+ (incident, now, kind, reason, msgspec.json.encode(note).decode()),
221
+ )
222
+ if reason not in ("delivered", "cooldown", "delivery failed"):
223
+ self.fact(incident, "muted")
224
+ if note.kind is TransitionKind.RESOLVED:
225
+ current = next(i for i in self.incidents(open_only=True) if i.id == incident)
226
+ key = self._week(note.rule_id, now)
227
+ durations = msgspec.json.decode(
228
+ self.db.execute(
229
+ "SELECT durations FROM weekly_quality WHERE rule_id=? AND week=? AND zone=?",
230
+ key,
231
+ ).fetchone()[0],
232
+ type=list[float],
233
+ )
234
+ durations.append(current.active_s)
235
+ self.db.execute(
236
+ "UPDATE weekly_quality SET durations=?,incomplete=incomplete+?"
237
+ " WHERE rule_id=? AND week=? AND zone=?",
238
+ (msgspec.json.encode(durations).decode(), int(current.incomplete), *key),
239
+ )
240
+ self.db.execute("UPDATE alert_incidents SET closed=? WHERE id=?", (now, incident))
241
+
242
+ def fact(self, incident_id: str, fact: str) -> None:
243
+ if fact not in ("muted", "acknowledged"):
244
+ raise ValueError("unknown incident fact")
245
+ incident = next(i for i in self.incidents() if i.id == incident_id)
246
+ if incident.muted if fact == "muted" else incident.acknowledged:
247
+ return
248
+ calendar = self.db.execute(
249
+ "SELECT week,zone FROM incident_calendar WHERE incident_id=?", (incident_id,)
250
+ ).fetchone()
251
+ if calendar is None: # older writer data gets a calendar on its next fact
252
+ key = self._week(incident.rule_id, incident.opened)
253
+ self.db.execute(
254
+ "INSERT INTO incident_calendar VALUES(?,?,?)", (incident_id, key[1], key[2])
255
+ )
256
+ else:
257
+ key = (incident.rule_id, str(calendar[0]), str(calendar[1]))
258
+ if fact == "muted":
259
+ self.db.execute("UPDATE alert_incidents SET muted=1 WHERE id=?", (incident_id,))
260
+ self.db.execute(
261
+ "UPDATE weekly_quality SET muted=muted+1 WHERE rule_id=? AND week=? AND zone=?", key
262
+ )
263
+ else:
264
+ self.db.execute("UPDATE alert_incidents SET acknowledged=1 WHERE id=?", (incident_id,))
265
+ self.db.execute(
266
+ "INSERT OR REPLACE INTO operations_meta VALUES ('acknowledgement_producer','1')"
267
+ )
268
+ self.db.execute(
269
+ "UPDATE weekly_quality SET acknowledged=acknowledged+1"
270
+ " WHERE rule_id=? AND week=? AND zone=?",
271
+ key,
272
+ )
273
+
274
+ def incidents(self, *, open_only: bool = False) -> list[Incident]:
275
+ query = "SELECT * FROM alert_incidents"
276
+ if open_only:
277
+ query += " WHERE closed IS NULL"
278
+ return [Incident(*r) for r in self.db.execute(query + " ORDER BY opened DESC")]
279
+
280
+ def history(
281
+ self, since: float = 0, limit: int = 1001, after: int = 0, incident: str | None = None
282
+ ) -> list[AlertEvent]:
283
+ return [
284
+ AlertEvent(r[0], r[1], r[2], r[3], r[4], msgspec.json.decode(r[5], type=Notification))
285
+ for r in self.db.execute(
286
+ "SELECT * FROM alert_events WHERE ts>=? AND id>?"
287
+ " AND (? IS NULL OR incident_id=?) ORDER BY id DESC LIMIT ?",
288
+ (since, after, incident, incident, limit),
289
+ )
290
+ ]
291
+
292
+ def weekly(self) -> list[Weekly]:
293
+ ack = (
294
+ self.db.execute(
295
+ "SELECT 1 FROM operations_meta WHERE key='acknowledgement_producer'"
296
+ ).fetchone()
297
+ is not None
298
+ )
299
+ result: list[Weekly] = []
300
+ for (
301
+ rule,
302
+ week,
303
+ zone,
304
+ _,
305
+ fires,
306
+ seconds,
307
+ encoded,
308
+ incomplete,
309
+ muted,
310
+ acknowledged,
311
+ ) in self.db.execute("SELECT * FROM weekly_quality ORDER BY week DESC,rule_id,zone"):
312
+ durations = msgspec.json.decode(encoded, type=list[float])
313
+ result.append(
314
+ Weekly(
315
+ rule,
316
+ week,
317
+ zone,
318
+ fires,
319
+ seconds / 60,
320
+ statistics.median(durations) if durations else None,
321
+ 100 * sum(d < 300 for d in durations) / len(durations) if durations else None,
322
+ 100 * acknowledged / fires if ack and fires else None,
323
+ 100 * muted / fires if fires else None,
324
+ incomplete,
325
+ )
326
+ )
327
+ return result
328
+
329
+ def prune(self, now: float) -> None:
330
+ cutoff = now - RETENTION_S
331
+ self.db.execute(
332
+ "DELETE FROM alert_events WHERE ts<? AND incident_id NOT IN"
333
+ " (SELECT id FROM alert_incidents WHERE closed IS NULL)",
334
+ (cutoff,),
335
+ )
336
+ self.db.execute("DELETE FROM alert_incidents WHERE closed<?", (cutoff,))
337
+ self.db.execute("DELETE FROM weekly_quality WHERE updated<?", (cutoff,))
@@ -0,0 +1,58 @@
1
+ """Small SQLite control plane shared by the CLI and running daemon."""
2
+
3
+ import re
4
+ import sqlite3
5
+ from dataclasses import dataclass
6
+
7
+ CREATE = (
8
+ """CREATE TABLE IF NOT EXISTS mutes (
9
+ rule_id TEXT PRIMARY KEY, boot TEXT NOT NULL, expiry REAL, renotify INTEGER NOT NULL,
10
+ released INTEGER NOT NULL DEFAULT 0)""",
11
+ """CREATE TABLE IF NOT EXISTS quiet_hours (
12
+ singleton INTEGER PRIMARY KEY CHECK(singleton=1), schedule TEXT NOT NULL,
13
+ renotify INTEGER NOT NULL, released INTEGER NOT NULL DEFAULT 0)""",
14
+ )
15
+
16
+
17
+ @dataclass(frozen=True, slots=True)
18
+ class ActiveMute:
19
+ rule_id: str
20
+ remaining_s: float | None
21
+
22
+
23
+ class Mutes:
24
+ def __init__(self, db: sqlite3.Connection) -> None:
25
+ self.db = db
26
+
27
+ def set(self, rule: str, boot: str, expiry: float | None, renotify: bool) -> None:
28
+ with self.db:
29
+ self.db.execute(
30
+ "INSERT OR REPLACE INTO mutes VALUES(?,?,?,?,0)", (rule, boot, expiry, renotify)
31
+ )
32
+
33
+ def remove(self, rule: str, boot: str) -> None:
34
+ with self.db:
35
+ self.db.execute("INSERT OR REPLACE INTO mutes VALUES(?,?,0,1,1)", (rule, boot))
36
+
37
+ def active(self, boot: str, now: float) -> tuple[ActiveMute, ...]:
38
+ return tuple(
39
+ ActiveMute(rule, None if expiry is None else max(0, expiry - now))
40
+ for rule, expiry in self.db.execute(
41
+ "SELECT rule_id,expiry FROM mutes WHERE boot=? AND released=0"
42
+ " AND (expiry IS NULL OR expiry>?)",
43
+ (boot, now),
44
+ )
45
+ )
46
+
47
+ def quiet(self, schedule: str, renotify: bool) -> None:
48
+ if schedule != "off" and any(
49
+ not re.fullmatch(r"(?:[01]\d|2[0-3]):[0-5]\d-(?:[01]\d|2[0-3]):[0-5]\d", part)
50
+ or part[:5] == part[6:]
51
+ for part in schedule.split(",")
52
+ ):
53
+ raise ValueError("quiet hours require HH:MM-HH:MM (comma-separated) or off")
54
+ with self.db:
55
+ self.db.execute(
56
+ "INSERT OR REPLACE INTO quiet_hours VALUES(1,?,?,?)",
57
+ ("" if schedule == "off" else schedule, renotify, schedule == "off"),
58
+ )
@@ -0,0 +1,51 @@
1
+ """Local ISO weeks and persisted per-rule quality measurements."""
2
+
3
+ import os
4
+ from dataclasses import dataclass
5
+ from datetime import datetime, timedelta, tzinfo
6
+ from pathlib import Path
7
+ from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
8
+
9
+
10
+ def local_zone() -> tzinfo:
11
+ name = os.environ.get("TZ", "").removeprefix(":")
12
+ if not name:
13
+ name = str(Path("/etc/localtime").resolve()).partition("/zoneinfo/")[2]
14
+ try:
15
+ return ZoneInfo(name or "UTC")
16
+ except (ValueError, ZoneInfoNotFoundError):
17
+ return datetime.now().astimezone().tzinfo or ZoneInfo("UTC")
18
+
19
+
20
+ def week_at(ts: float, zone: tzinfo) -> tuple[str, str]:
21
+ local = datetime.fromtimestamp(ts, zone)
22
+ year, week, _ = local.isocalendar()
23
+ return f"{year}-W{week:02}", str(zone)
24
+
25
+
26
+ def week_spans(start: float, end: float, zone: tzinfo) -> list[tuple[str, str, float]]:
27
+ spans: list[tuple[str, str, float]] = []
28
+ while start < end:
29
+ local = datetime.fromtimestamp(start, zone)
30
+ monday = (local + timedelta(days=7 - local.weekday())).replace(
31
+ hour=0, minute=0, second=0, microsecond=0
32
+ )
33
+ stop = min(end, monday.timestamp())
34
+ week, name = week_at(start, zone)
35
+ spans.append((week, name, stop - start))
36
+ start = stop
37
+ return spans
38
+
39
+
40
+ @dataclass(frozen=True, slots=True)
41
+ class Weekly:
42
+ rule_id: str
43
+ week: str
44
+ zone: str
45
+ fires: int
46
+ firing_minutes: float
47
+ median_duration_s: float | None
48
+ percent_closed_under_5m: float | None
49
+ percent_acknowledged: float | None
50
+ percent_muted: float | None
51
+ incomplete_durations: int
@@ -0,0 +1,40 @@
1
+ """Read-only transactions for operations history and exact replay."""
2
+
3
+ import sqlite3
4
+ from collections.abc import Generator
5
+ from contextlib import contextmanager
6
+ from pathlib import Path
7
+
8
+ from sensitivebox.alerts.events import Checkpoint, InputEvent
9
+ from sensitivebox.store import schema
10
+ from sensitivebox.store.db import Store
11
+
12
+
13
+ @contextmanager
14
+ def reader(path: Path) -> Generator[sqlite3.Connection | None]:
15
+ if not path.exists():
16
+ yield None
17
+ return
18
+ db = sqlite3.connect(path.resolve().as_uri() + "?mode=ro", uri=True)
19
+ try:
20
+ db.execute("PRAGMA query_only=ON")
21
+ db.execute("BEGIN")
22
+ if db.execute("PRAGMA user_version").fetchone()[0] != schema.VERSION:
23
+ raise sqlite3.DatabaseError("history needs a compatible writer; start sbox watch")
24
+ yield db
25
+ finally:
26
+ db.close()
27
+
28
+
29
+ def inputs(path: Path) -> list[InputEvent]:
30
+ with reader(path) as db:
31
+ if db is None:
32
+ return []
33
+ rows = db.execute(schema.SELECT_INPUTS).fetchall()
34
+ checkpoint = db.execute("SELECT frame,mono,payload FROM replay_checkpoint").fetchone()
35
+ if checkpoint:
36
+ return [
37
+ Checkpoint(checkpoint[1], checkpoint[2]),
38
+ *Store.decode_inputs([r for r in rows if r[0] >= checkpoint[0]]),
39
+ ]
40
+ return Store.decode_inputs(rows)
@@ -0,0 +1,194 @@
1
+ """SQLite schema, retention and the SQL statements. All statements are static text with bound
2
+ parameters: nothing is ever formatted into SQL.
3
+
4
+ Retention (docs/BIBLE.md, "Storage" and "Alerting"):
5
+
6
+ * ``inputs_raw``: a 1 h ring of **input frames**, everything the alert engine saw, in order: each
7
+ poll's signal values or MISSING reasons (guard signals included), plus lifecycle events (rules
8
+ loaded or edited, chip limits changed, suspend, resume). Replaying it through the engine gives
9
+ exactly what the live daemon did (review ADV-005). One poll is a ``tick`` row followed by its
10
+ ``value`` rows, all sharing a ``frame`` number; a lifecycle event is one row whose ``payload``
11
+ is msgspec JSON. Pruning keeps the newest ``rules``, ``limits`` and ``settings`` rows from before
12
+ the cutoff,
13
+ so a replay of the ring knows the configuration in force at its start.
14
+ * ``metrics_raw``: values that are stored but never alerted on (network rates), 1 h.
15
+ * ``evaluations_raw``: every rule evaluation, derived from the frames and kept for display, 1 h.
16
+ * ``rollup_1m``: one-minute count/sum/min/max, kept 7 days.
17
+ * ``rollup_15m``: fifteen-minute count/sum/min/max, kept **400 days**. Insights depends on these;
18
+ never drop or shorten them (``tests/store/test_retention.py`` pins the number).
19
+
20
+ Rollups keep ``n`` and ``total`` (not an average) so a bucket written in two pieces, by a restart
21
+ or two flushes, merges exactly.
22
+ """
23
+
24
+ from sensitivebox.store.changes import CREATE as CHANGES_CREATE
25
+ from sensitivebox.store.events import CREATE as OPERATIONS_CREATE
26
+ from sensitivebox.store.mutes import CREATE as MUTES_CREATE
27
+
28
+ RAW_RETENTION_S = 3600
29
+ ROLLUP_1M_WIDTH_S = 60
30
+ ROLLUP_1M_RETENTION_S = 7 * 86_400
31
+ ROLLUP_15M_WIDTH_S = 15 * 60
32
+ ROLLUP_15M_RETENTION_S = 400 * 86_400
33
+
34
+ VERSION = 10 # bump with any schema change
35
+ GET_VERSION = "PRAGMA user_version"
36
+ SET_VERSION = f"PRAGMA user_version = {VERSION}"
37
+
38
+ # Version 1 kept signal values in samples_raw, without the engine's clock or lifecycle events, so
39
+ # it couldn't be replayed. It was only a 1 h ring: drop it. The rollups carry over unchanged.
40
+ UPGRADE_FROM_1 = ("DROP TABLE IF EXISTS samples_raw",)
41
+
42
+ PRAGMAS = (
43
+ "PRAGMA journal_mode = WAL",
44
+ "PRAGMA synchronous = NORMAL",
45
+ "PRAGMA foreign_keys = ON",
46
+ )
47
+
48
+ CREATE = (
49
+ *OPERATIONS_CREATE,
50
+ *MUTES_CREATE,
51
+ *CHANGES_CREATE,
52
+ """CREATE TABLE IF NOT EXISTS replay_checkpoint (
53
+ singleton INTEGER PRIMARY KEY CHECK(singleton=1), frame INTEGER NOT NULL,
54
+ mono REAL NOT NULL, payload TEXT NOT NULL)""",
55
+ """
56
+ CREATE TABLE IF NOT EXISTS signals (
57
+ id INTEGER PRIMARY KEY,
58
+ name TEXT NOT NULL UNIQUE
59
+ )
60
+ """,
61
+ """
62
+ CREATE TABLE IF NOT EXISTS inputs_raw (
63
+ frame INTEGER NOT NULL,
64
+ ts REAL NOT NULL,
65
+ mono REAL NOT NULL,
66
+ kind TEXT NOT NULL,
67
+ signal_id INTEGER REFERENCES signals(id),
68
+ value REAL,
69
+ text TEXT,
70
+ missing TEXT,
71
+ payload TEXT
72
+ )
73
+ """,
74
+ "CREATE INDEX IF NOT EXISTS inputs_raw_ts ON inputs_raw (ts)",
75
+ "CREATE INDEX IF NOT EXISTS inputs_raw_frame ON inputs_raw (frame)",
76
+ "CREATE INDEX IF NOT EXISTS inputs_raw_signal ON inputs_raw (signal_id, ts)",
77
+ """
78
+ CREATE TABLE IF NOT EXISTS metrics_raw (
79
+ ts REAL NOT NULL,
80
+ signal_id INTEGER NOT NULL REFERENCES signals(id),
81
+ value REAL NOT NULL
82
+ )
83
+ """,
84
+ "CREATE INDEX IF NOT EXISTS metrics_raw_ts ON metrics_raw (ts)",
85
+ """
86
+ CREATE TABLE IF NOT EXISTS evaluations_raw (
87
+ ts REAL NOT NULL,
88
+ rule_id TEXT NOT NULL,
89
+ signal_id INTEGER NOT NULL REFERENCES signals(id),
90
+ value REAL,
91
+ text TEXT,
92
+ missing TEXT,
93
+ state TEXT NOT NULL,
94
+ reason TEXT NOT NULL,
95
+ threshold REAL,
96
+ alert_severity TEXT,
97
+ no_data INTEGER NOT NULL DEFAULT 0,
98
+ metadata_known INTEGER NOT NULL DEFAULT 0
99
+ )
100
+ """,
101
+ "CREATE INDEX IF NOT EXISTS evaluations_raw_ts ON evaluations_raw (ts)",
102
+ "CREATE INDEX IF NOT EXISTS evaluations_raw_rule ON evaluations_raw (rule_id, ts)",
103
+ """
104
+ CREATE TABLE IF NOT EXISTS rollup_1m (
105
+ bucket INTEGER NOT NULL,
106
+ signal_id INTEGER NOT NULL REFERENCES signals(id),
107
+ n INTEGER NOT NULL,
108
+ total REAL NOT NULL,
109
+ lo REAL NOT NULL,
110
+ hi REAL NOT NULL,
111
+ PRIMARY KEY (bucket, signal_id)
112
+ ) WITHOUT ROWID
113
+ """,
114
+ """
115
+ CREATE TABLE IF NOT EXISTS rollup_15m (
116
+ bucket INTEGER NOT NULL,
117
+ signal_id INTEGER NOT NULL REFERENCES signals(id),
118
+ n INTEGER NOT NULL,
119
+ total REAL NOT NULL,
120
+ lo REAL NOT NULL,
121
+ hi REAL NOT NULL,
122
+ PRIMARY KEY (bucket, signal_id)
123
+ ) WITHOUT ROWID
124
+ """,
125
+ )
126
+
127
+ INSERT_SIGNAL = "INSERT OR IGNORE INTO signals (name) VALUES (?)"
128
+ SELECT_SIGNAL = "SELECT id FROM signals WHERE name = ?"
129
+
130
+ INSERT_INPUT = (
131
+ "INSERT INTO inputs_raw (frame, ts, mono, kind, signal_id, value, text, missing, payload)"
132
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
133
+ )
134
+ LAST_FRAME = "SELECT coalesce(max(frame), 0) FROM inputs_raw"
135
+ SELECT_INPUTS = """
136
+ SELECT i.frame, i.mono, i.kind, s.name, i.value, i.text, i.missing, i.payload
137
+ FROM inputs_raw AS i LEFT JOIN signals AS s ON s.id = i.signal_id
138
+ ORDER BY i.frame, i.rowid
139
+ """
140
+ INSERT_METRIC = "INSERT INTO metrics_raw (ts, signal_id, value) VALUES (?, ?, ?)"
141
+ INSERT_EVALUATION = (
142
+ "INSERT INTO evaluations_raw"
143
+ " (ts, rule_id, signal_id, value, text, missing, state, reason, threshold,"
144
+ " alert_severity, no_data, metadata_known)"
145
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)"
146
+ )
147
+
148
+ UPSERT_1M = """
149
+ INSERT INTO rollup_1m (bucket, signal_id, n, total, lo, hi) VALUES (?, ?, ?, ?, ?, ?)
150
+ ON CONFLICT (bucket, signal_id) DO UPDATE SET
151
+ n = n + excluded.n,
152
+ total = total + excluded.total,
153
+ lo = min(lo, excluded.lo),
154
+ hi = max(hi, excluded.hi)
155
+ """
156
+ UPSERT_15M = """
157
+ INSERT INTO rollup_15m (bucket, signal_id, n, total, lo, hi) VALUES (?, ?, ?, ?, ?, ?)
158
+ ON CONFLICT (bucket, signal_id) DO UPDATE SET
159
+ n = n + excluded.n,
160
+ total = total + excluded.total,
161
+ lo = min(lo, excluded.lo),
162
+ hi = max(hi, excluded.hi)
163
+ """
164
+
165
+ # Raw rows older than the ring go, except the newest rules and limits events before the cutoff.
166
+ # A rollup bucket goes only once the *whole* bucket is older than its retention
167
+ # (bucket start + width <= cutoff), so retention is never cut short. PRUNE_INPUTS takes the
168
+ # cutoff four times.
169
+ PRUNE_INPUTS = """
170
+ DELETE FROM inputs_raw WHERE ts < ? AND frame NOT IN (
171
+ SELECT coalesce(max(frame), -1) FROM inputs_raw WHERE kind = 'rules' AND ts < ?
172
+ UNION ALL
173
+ SELECT coalesce(max(frame), -1) FROM inputs_raw WHERE kind = 'limits' AND ts < ?
174
+ UNION ALL
175
+ SELECT coalesce(max(frame), -1) FROM inputs_raw WHERE kind = 'settings' AND ts < ?
176
+ )
177
+ """
178
+ PRUNE_CHECKPOINT_INPUTS = PRUNE_INPUTS.replace(
179
+ "WHERE ts < ? AND frame NOT IN", "WHERE ts < ? AND frame < ? AND frame NOT IN"
180
+ )
181
+ PRUNE_METRICS = "DELETE FROM metrics_raw WHERE ts < ?"
182
+ PRUNE_EVALUATIONS = "DELETE FROM evaluations_raw WHERE ts < ?"
183
+ PRUNE_1M = "DELETE FROM rollup_1m WHERE bucket + ? <= ?"
184
+ PRUNE_15M = "DELETE FROM rollup_15m WHERE bucket + ? <= ?"
185
+
186
+ # Only the writer migrates: status readers never execute these statements.
187
+ UPGRADE_FROM_2 = (
188
+ "ALTER TABLE evaluations_raw ADD COLUMN alert_severity TEXT",
189
+ "ALTER TABLE evaluations_raw ADD COLUMN no_data INTEGER NOT NULL DEFAULT 0",
190
+ )
191
+
192
+ UPGRADE_FROM_3 = (
193
+ "ALTER TABLE evaluations_raw ADD COLUMN metadata_known INTEGER NOT NULL DEFAULT 0",
194
+ )