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.
- sensitivebox/__init__.py +10 -0
- sensitivebox/alerts/__init__.py +94 -0
- sensitivebox/alerts/config.py +85 -0
- sensitivebox/alerts/defaults.toml +126 -0
- sensitivebox/alerts/engine.py +624 -0
- sensitivebox/alerts/events.py +87 -0
- sensitivebox/alerts/globs.py +84 -0
- sensitivebox/alerts/model.py +215 -0
- sensitivebox/alerts/mute.py +55 -0
- sensitivebox/alerts/replay.py +99 -0
- sensitivebox/alerts/routing.py +247 -0
- sensitivebox/api/__init__.py +6 -0
- sensitivebox/api/app.py +225 -0
- sensitivebox/api/data.py +109 -0
- sensitivebox/api/host.py +27 -0
- sensitivebox/api/model.py +90 -0
- sensitivebox/api/security.py +89 -0
- sensitivebox/api/server.py +108 -0
- sensitivebox/api/state.py +220 -0
- sensitivebox/api/static/assets/archivo-latin-wdth-normal-DY7AcnAa.woff2 +0 -0
- sensitivebox/api/static/assets/ibm-plex-mono-medium-BpyVxOr_.woff2 +0 -0
- sensitivebox/api/static/assets/index-BH9EydfS.js +10 -0
- sensitivebox/api/static/assets/index-RMruxW54.css +1 -0
- sensitivebox/api/static/assets/mark-on-dark-CpSxozdM.png +0 -0
- sensitivebox/api/static/assets/mark-on-light-DgUAeHTp.png +0 -0
- sensitivebox/api/static/assets/mark-raised-Bg5z6PGs.png +0 -0
- sensitivebox/api/static/index.html +14 -0
- sensitivebox/browser.py +106 -0
- sensitivebox/cli.py +467 -0
- sensitivebox/collector.py +319 -0
- sensitivebox/completion.py +117 -0
- sensitivebox/daemon.py +181 -0
- sensitivebox/data/icons/128.png +0 -0
- sensitivebox/data/icons/256.png +0 -0
- sensitivebox/data/icons/512.png +0 -0
- sensitivebox/data/proart-p16.tar.gz +0 -0
- sensitivebox/data/sensitivebox.desktop.in +8 -0
- sensitivebox/data/sensitivebox.service.in +11 -0
- sensitivebox/doctor.py +322 -0
- sensitivebox/fixtures.py +177 -0
- sensitivebox/history.py +298 -0
- sensitivebox/hwmon/__init__.py +20 -0
- sensitivebox/hwmon/classify.py +74 -0
- sensitivebox/hwmon/discovery.py +139 -0
- sensitivebox/hwmon/ids.py +35 -0
- sensitivebox/hwmon/model.py +78 -0
- sensitivebox/hwmon/normalise.py +42 -0
- sensitivebox/hwmon/reader.py +65 -0
- sensitivebox/inspect.py +175 -0
- sensitivebox/live.py +56 -0
- sensitivebox/notify/__init__.py +7 -0
- sensitivebox/notify/base.py +60 -0
- sensitivebox/notify/desktop.py +84 -0
- sensitivebox/notify/log.py +30 -0
- sensitivebox/operations.py +44 -0
- sensitivebox/output.py +76 -0
- sensitivebox/process.py +25 -0
- sensitivebox/py.typed +0 -0
- sensitivebox/redact.py +85 -0
- sensitivebox/report.py +357 -0
- sensitivebox/runtime.py +313 -0
- sensitivebox/sample.py +84 -0
- sensitivebox/service.py +112 -0
- sensitivebox/simulate.py +220 -0
- sensitivebox/sources/__init__.py +83 -0
- sensitivebox/sources/apps.py +135 -0
- sensitivebox/sources/bluez.py +96 -0
- sensitivebox/sources/bluez_watch.py +214 -0
- sensitivebox/sources/changes.py +121 -0
- sensitivebox/sources/cpu.py +152 -0
- sensitivebox/sources/dbus.py +174 -0
- sensitivebox/sources/logind.py +88 -0
- sensitivebox/sources/memory.py +127 -0
- sensitivebox/sources/net.py +128 -0
- sensitivebox/sources/power_profiles.py +116 -0
- sensitivebox/sources/power_supply.py +70 -0
- sensitivebox/sources/psi.py +78 -0
- sensitivebox/sources/report.py +85 -0
- sensitivebox/sources/wifi.py +90 -0
- sensitivebox/sources/wifi_nm.py +269 -0
- sensitivebox/status.py +205 -0
- sensitivebox/store/__init__.py +5 -0
- sensitivebox/store/changes.py +58 -0
- sensitivebox/store/db.py +423 -0
- sensitivebox/store/events.py +337 -0
- sensitivebox/store/mutes.py +58 -0
- sensitivebox/store/quality.py +51 -0
- sensitivebox/store/read.py +40 -0
- sensitivebox/store/schema.py +194 -0
- sensitivebox/store/status.py +105 -0
- sensitivebox-0.1.0.dist-info/METADATA +424 -0
- sensitivebox-0.1.0.dist-info/RECORD +95 -0
- sensitivebox-0.1.0.dist-info/WHEEL +4 -0
- sensitivebox-0.1.0.dist-info/entry_points.txt +4 -0
- sensitivebox-0.1.0.dist-info/licenses/LICENSE +21 -0
sensitivebox/store/db.py
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
"""The SQLite store: a 1 h raw ring of input frames plus 1-minute and 15-minute rollups, in WAL
|
|
2
|
+
mode.
|
|
3
|
+
|
|
4
|
+
The alert engine reports every input event to ``journal`` (each poll's signals, rule and limit
|
|
5
|
+
changes, suspend and resume) and the daemon calls ``record`` once per poll with the extra metrics
|
|
6
|
+
and the rule evaluations. Both only touch memory; every ``FLUSH_EVERY_S`` the batch is written in
|
|
7
|
+
one transaction, and about once a minute the store prunes. ``inputs`` reads the input events back
|
|
8
|
+
for ``replay``. The wall clock is injected, so retention is tested without waiting. The
|
|
9
|
+
connection is used from the daemon's thread only.
|
|
10
|
+
|
|
11
|
+
A replay of the ring begins at one checkpoint advanced by the pruner. Session restart events
|
|
12
|
+
reproduce the fresh engine's Warming reset. There is no checkpoint per poll.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import logging
|
|
16
|
+
import math
|
|
17
|
+
import sqlite3
|
|
18
|
+
from collections.abc import Mapping
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from types import TracebackType
|
|
22
|
+
from typing import Self
|
|
23
|
+
|
|
24
|
+
import msgspec
|
|
25
|
+
|
|
26
|
+
from sensitivebox.alerts.engine import Clock, Evaluation, Step
|
|
27
|
+
from sensitivebox.alerts.events import (
|
|
28
|
+
Checkpoint,
|
|
29
|
+
EngineSettings,
|
|
30
|
+
InputEvent,
|
|
31
|
+
LimitsChanged,
|
|
32
|
+
OpenAlerts,
|
|
33
|
+
Restart,
|
|
34
|
+
Resume,
|
|
35
|
+
RulesLoaded,
|
|
36
|
+
Suspend,
|
|
37
|
+
Tick,
|
|
38
|
+
)
|
|
39
|
+
from sensitivebox.alerts.model import Missing, Rule, Value
|
|
40
|
+
from sensitivebox.alerts.replay import ReplayRunner
|
|
41
|
+
from sensitivebox.hwmon import Limits
|
|
42
|
+
from sensitivebox.store import schema
|
|
43
|
+
from sensitivebox.store.changes import Changes
|
|
44
|
+
from sensitivebox.store.events import Events
|
|
45
|
+
from sensitivebox.store.mutes import Mutes
|
|
46
|
+
|
|
47
|
+
log = logging.getLogger(__name__)
|
|
48
|
+
|
|
49
|
+
FLUSH_EVERY_S = 5.0
|
|
50
|
+
PRUNE_EVERY_S = 60.0
|
|
51
|
+
|
|
52
|
+
# frame, ts (wall), mono (engine clock), kind, signal, value, text, missing, payload
|
|
53
|
+
type _Input = tuple[
|
|
54
|
+
int, float, float, str, str | None, float | None, str | None, str | None, str | None
|
|
55
|
+
]
|
|
56
|
+
type _Raw = tuple[int, float, str, str | None, float | None, str | None, str | None, str | None]
|
|
57
|
+
|
|
58
|
+
_RULES = msgspec.json.Decoder(list[Rule])
|
|
59
|
+
_LIMITS = msgspec.json.Decoder(dict[str, Limits])
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass(slots=True)
|
|
63
|
+
class _Acc:
|
|
64
|
+
"""Count, sum, min and max of the values seen in one bucket since the last flush."""
|
|
65
|
+
|
|
66
|
+
n: int
|
|
67
|
+
total: float
|
|
68
|
+
lo: float
|
|
69
|
+
hi: float
|
|
70
|
+
|
|
71
|
+
def add(self, value: float) -> None:
|
|
72
|
+
self.n += 1
|
|
73
|
+
self.total += value
|
|
74
|
+
self.lo = min(self.lo, value)
|
|
75
|
+
self.hi = max(self.hi, value)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _split(value: Value | None) -> tuple[float | None, str | None, str | None]:
|
|
79
|
+
"""A signal value as the (number, text, missing reason) columns."""
|
|
80
|
+
if isinstance(value, Missing):
|
|
81
|
+
return None, None, value.reason
|
|
82
|
+
if isinstance(value, str):
|
|
83
|
+
return None, value, None
|
|
84
|
+
return value, None, None
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _join(number: float | None, text: str | None, missing: str | None) -> Value:
|
|
88
|
+
if missing is not None:
|
|
89
|
+
return Missing(missing)
|
|
90
|
+
if text is not None:
|
|
91
|
+
return text
|
|
92
|
+
return number if number is not None else Missing("not recorded")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class Store:
|
|
96
|
+
"""Open (creating if needed) the database at ``path``; ``None`` keeps it in memory."""
|
|
97
|
+
|
|
98
|
+
def __init__(self, path: Path | None, *, clock: Clock, start_session: bool = True) -> None:
|
|
99
|
+
if path is not None:
|
|
100
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
101
|
+
self._clock = clock
|
|
102
|
+
self._db = sqlite3.connect(":memory:" if path is None else str(path))
|
|
103
|
+
for pragma in schema.PRAGMAS:
|
|
104
|
+
self._db.execute(pragma)
|
|
105
|
+
with self._db:
|
|
106
|
+
version = int(self._db.execute(schema.GET_VERSION).fetchone()[0])
|
|
107
|
+
if version == 1:
|
|
108
|
+
for statement in schema.UPGRADE_FROM_1:
|
|
109
|
+
self._db.execute(statement)
|
|
110
|
+
for statement in schema.CREATE:
|
|
111
|
+
self._db.execute(statement)
|
|
112
|
+
if version == 2:
|
|
113
|
+
for statement in schema.UPGRADE_FROM_2:
|
|
114
|
+
self._db.execute(statement)
|
|
115
|
+
if version in (2, 3):
|
|
116
|
+
for statement in schema.UPGRADE_FROM_3:
|
|
117
|
+
self._db.execute(statement)
|
|
118
|
+
self._db.execute(schema.SET_VERSION)
|
|
119
|
+
self.events = Events(self._db, clock, writer=start_session)
|
|
120
|
+
self.mutes = Mutes(self._db)
|
|
121
|
+
self.changes = Changes(self._db, clock)
|
|
122
|
+
self._frame = int(self._db.execute(schema.LAST_FRAME).fetchone()[0])
|
|
123
|
+
checkpoint = self._db.execute("SELECT frame FROM replay_checkpoint").fetchone()
|
|
124
|
+
if checkpoint:
|
|
125
|
+
self._frame = max(self._frame, checkpoint[0] - 1)
|
|
126
|
+
self._ids: dict[str, int] = {}
|
|
127
|
+
self._inputs: list[_Input] = []
|
|
128
|
+
self._tick_wall: float | None = None
|
|
129
|
+
self._metrics: list[tuple[float, str, float]] = []
|
|
130
|
+
self._evaluations: list[tuple[float, Evaluation]] = []
|
|
131
|
+
self._acc_1m: dict[tuple[int, str], _Acc] = {}
|
|
132
|
+
self._acc_15m: dict[tuple[int, str], _Acc] = {}
|
|
133
|
+
now = clock()
|
|
134
|
+
self._last_flush = now
|
|
135
|
+
self._last_prune = now
|
|
136
|
+
|
|
137
|
+
def __enter__(self) -> Self:
|
|
138
|
+
return self
|
|
139
|
+
|
|
140
|
+
def __exit__(
|
|
141
|
+
self,
|
|
142
|
+
exc_type: type[BaseException] | None,
|
|
143
|
+
exc: BaseException | None,
|
|
144
|
+
tb: TracebackType | None,
|
|
145
|
+
) -> None:
|
|
146
|
+
self.close()
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def journal_mode(self) -> str:
|
|
150
|
+
row = self._db.execute("PRAGMA journal_mode").fetchone()
|
|
151
|
+
return str(row[0])
|
|
152
|
+
|
|
153
|
+
# -- writing ------------------------------------------------------------------------------
|
|
154
|
+
|
|
155
|
+
def journal(self, event: InputEvent) -> None:
|
|
156
|
+
"""The engine's journal: queue one input event (``Engine(..., journal=store.journal)``).
|
|
157
|
+
Signal values also feed the rollups."""
|
|
158
|
+
now = self._clock()
|
|
159
|
+
self._frame += 1
|
|
160
|
+
frame = self._frame
|
|
161
|
+
match event:
|
|
162
|
+
case Tick(at=at, signals=signals):
|
|
163
|
+
self._tick_wall = now
|
|
164
|
+
self._inputs.append((frame, now, at, "tick", None, None, None, None, None))
|
|
165
|
+
for name, value in signals.items():
|
|
166
|
+
number, text, missing = _split(value)
|
|
167
|
+
self._inputs.append(
|
|
168
|
+
(frame, now, at, "value", name, number, text, missing, None)
|
|
169
|
+
)
|
|
170
|
+
if number is not None:
|
|
171
|
+
self._accumulate(now, name, number)
|
|
172
|
+
case OpenAlerts(at=at, payload=payload):
|
|
173
|
+
self._inputs.append(
|
|
174
|
+
(frame, now, at, "open_alerts", None, None, None, None, payload)
|
|
175
|
+
)
|
|
176
|
+
case Restart(at=at):
|
|
177
|
+
self._inputs.append((frame, now, at, "restart", None, None, None, None, None))
|
|
178
|
+
case Checkpoint():
|
|
179
|
+
raise ValueError("checkpoint is a replay boundary, not a journal input")
|
|
180
|
+
case Suspend(at=at):
|
|
181
|
+
self._inputs.append((frame, now, at, "suspend", None, None, None, None, None))
|
|
182
|
+
case Resume(at=at):
|
|
183
|
+
self._inputs.append((frame, now, at, "resume", None, None, None, None, None))
|
|
184
|
+
case LimitsChanged(at=at, limits=limits):
|
|
185
|
+
payload = msgspec.json.encode(dict(limits)).decode()
|
|
186
|
+
self._inputs.append((frame, now, at, "limits", None, None, None, None, payload))
|
|
187
|
+
case RulesLoaded(at=at, rules=rules):
|
|
188
|
+
payload = msgspec.json.encode(list(rules)).decode()
|
|
189
|
+
self._inputs.append((frame, now, at, "rules", None, None, None, None, payload))
|
|
190
|
+
case EngineSettings(at=at, max_gap=max_gap):
|
|
191
|
+
self._inputs.append((frame, now, at, "settings", None, max_gap, None, None, None))
|
|
192
|
+
self._maintain(now)
|
|
193
|
+
|
|
194
|
+
def record(self, metrics: Mapping[str, float] | None = None, step: Step | None = None) -> None:
|
|
195
|
+
"""Queue one poll's extra metrics (stored, never alerted on) and its rule evaluations.
|
|
196
|
+
Flushes and prunes when due, so the caller only has to call this once per poll."""
|
|
197
|
+
now = self._clock() if self._tick_wall is None else self._tick_wall
|
|
198
|
+
self._tick_wall = None
|
|
199
|
+
for name, number in (metrics or {}).items():
|
|
200
|
+
self._metrics.append((now, name, number))
|
|
201
|
+
self._accumulate(now, name, number)
|
|
202
|
+
if step is not None:
|
|
203
|
+
self._evaluations.extend((now, e) for e in step.evaluations)
|
|
204
|
+
self._maintain(now)
|
|
205
|
+
|
|
206
|
+
def _maintain(self, now: float) -> None:
|
|
207
|
+
if now - self._last_flush >= FLUSH_EVERY_S:
|
|
208
|
+
self.flush()
|
|
209
|
+
if now - self._last_prune >= PRUNE_EVERY_S:
|
|
210
|
+
self.prune()
|
|
211
|
+
|
|
212
|
+
def _accumulate(self, ts: float, name: str, value: float) -> None:
|
|
213
|
+
if not math.isfinite(value):
|
|
214
|
+
return
|
|
215
|
+
for table, width in (
|
|
216
|
+
(self._acc_1m, schema.ROLLUP_1M_WIDTH_S),
|
|
217
|
+
(self._acc_15m, schema.ROLLUP_15M_WIDTH_S),
|
|
218
|
+
):
|
|
219
|
+
key = (int(ts // width) * width, name)
|
|
220
|
+
acc = table.get(key)
|
|
221
|
+
if acc is None:
|
|
222
|
+
table[key] = _Acc(1, value, value, value)
|
|
223
|
+
else:
|
|
224
|
+
acc.add(value)
|
|
225
|
+
|
|
226
|
+
def _signal_id(self, name: str) -> int:
|
|
227
|
+
known = self._ids.get(name)
|
|
228
|
+
if known is not None:
|
|
229
|
+
return known
|
|
230
|
+
self._db.execute(schema.INSERT_SIGNAL, (name,))
|
|
231
|
+
row = self._db.execute(schema.SELECT_SIGNAL, (name,)).fetchone()
|
|
232
|
+
self._ids[name] = int(row[0])
|
|
233
|
+
return self._ids[name]
|
|
234
|
+
|
|
235
|
+
def flush(self) -> None:
|
|
236
|
+
"""Write everything queued, in one transaction."""
|
|
237
|
+
self._last_flush = self._clock()
|
|
238
|
+
if not (
|
|
239
|
+
self._inputs or self._metrics or self._evaluations or self._acc_1m or self._acc_15m
|
|
240
|
+
):
|
|
241
|
+
return
|
|
242
|
+
try:
|
|
243
|
+
with self._db:
|
|
244
|
+
self._db.executemany(
|
|
245
|
+
schema.INSERT_INPUT,
|
|
246
|
+
[
|
|
247
|
+
(f, ts, mono, kind, None if n is None else self._signal_id(n), v, t, m, p)
|
|
248
|
+
for f, ts, mono, kind, n, v, t, m, p in self._inputs
|
|
249
|
+
],
|
|
250
|
+
)
|
|
251
|
+
self._db.executemany(
|
|
252
|
+
schema.INSERT_METRIC,
|
|
253
|
+
[(ts, self._signal_id(n), v) for ts, n, v in self._metrics],
|
|
254
|
+
)
|
|
255
|
+
self._db.executemany(
|
|
256
|
+
schema.INSERT_EVALUATION,
|
|
257
|
+
[self._evaluation_row(ts, e) for ts, e in self._evaluations],
|
|
258
|
+
)
|
|
259
|
+
self._upsert(schema.UPSERT_1M, self._acc_1m)
|
|
260
|
+
self._upsert(schema.UPSERT_15M, self._acc_15m)
|
|
261
|
+
except sqlite3.Error:
|
|
262
|
+
# Disk full, read-only, locked: the daemon keeps alerting; this batch is lost. Ids
|
|
263
|
+
# handed out inside the rolled-back transaction are no longer valid.
|
|
264
|
+
log.exception("store: could not write a batch, dropping it")
|
|
265
|
+
self._ids.clear()
|
|
266
|
+
self._inputs.clear()
|
|
267
|
+
self._metrics.clear()
|
|
268
|
+
self._evaluations.clear()
|
|
269
|
+
self._acc_1m.clear()
|
|
270
|
+
self._acc_15m.clear()
|
|
271
|
+
|
|
272
|
+
def _evaluation_row(
|
|
273
|
+
self, ts: float, e: Evaluation
|
|
274
|
+
) -> tuple[
|
|
275
|
+
float,
|
|
276
|
+
str,
|
|
277
|
+
int,
|
|
278
|
+
float | None,
|
|
279
|
+
str | None,
|
|
280
|
+
str | None,
|
|
281
|
+
str,
|
|
282
|
+
str,
|
|
283
|
+
float | None,
|
|
284
|
+
str | None,
|
|
285
|
+
int,
|
|
286
|
+
]:
|
|
287
|
+
number, text, _ = _split(e.value)
|
|
288
|
+
return (
|
|
289
|
+
ts,
|
|
290
|
+
e.rule_id,
|
|
291
|
+
self._signal_id(e.signal),
|
|
292
|
+
number,
|
|
293
|
+
text,
|
|
294
|
+
e.missing,
|
|
295
|
+
e.state.value,
|
|
296
|
+
e.reason,
|
|
297
|
+
e.threshold,
|
|
298
|
+
e.alert_severity,
|
|
299
|
+
int(e.no_data),
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
def _upsert(self, statement: str, table: Mapping[tuple[int, str], _Acc]) -> None:
|
|
303
|
+
self._db.executemany(
|
|
304
|
+
statement,
|
|
305
|
+
[
|
|
306
|
+
(bucket, self._signal_id(name), a.n, a.total, a.lo, a.hi)
|
|
307
|
+
for (bucket, name), a in table.items()
|
|
308
|
+
],
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
def prune(self) -> None:
|
|
312
|
+
"""Drop what is past its retention, as of the injected clock."""
|
|
313
|
+
now = self._clock()
|
|
314
|
+
self._last_prune = now
|
|
315
|
+
self.flush()
|
|
316
|
+
raw_cutoff = now - schema.RAW_RETENTION_S
|
|
317
|
+
try:
|
|
318
|
+
with self._db:
|
|
319
|
+
self._advance_checkpoint(raw_cutoff)
|
|
320
|
+
self.events.prune(now)
|
|
321
|
+
self.changes.prune(now)
|
|
322
|
+
boundary = self._db.execute("SELECT frame FROM replay_checkpoint").fetchone()
|
|
323
|
+
if boundary:
|
|
324
|
+
self._db.execute(
|
|
325
|
+
schema.PRUNE_CHECKPOINT_INPUTS,
|
|
326
|
+
(raw_cutoff, boundary[0], raw_cutoff, raw_cutoff, raw_cutoff),
|
|
327
|
+
)
|
|
328
|
+
else:
|
|
329
|
+
self._db.execute(schema.PRUNE_INPUTS, (raw_cutoff,) * 4)
|
|
330
|
+
self._db.execute(schema.PRUNE_METRICS, (raw_cutoff,))
|
|
331
|
+
self._db.execute(schema.PRUNE_EVALUATIONS, (raw_cutoff,))
|
|
332
|
+
self._db.execute(
|
|
333
|
+
schema.PRUNE_1M, (schema.ROLLUP_1M_WIDTH_S, now - schema.ROLLUP_1M_RETENTION_S)
|
|
334
|
+
)
|
|
335
|
+
self._db.execute(
|
|
336
|
+
schema.PRUNE_15M,
|
|
337
|
+
(schema.ROLLUP_15M_WIDTH_S, now - schema.ROLLUP_15M_RETENTION_S),
|
|
338
|
+
)
|
|
339
|
+
except sqlite3.Error:
|
|
340
|
+
log.exception("store: could not prune")
|
|
341
|
+
|
|
342
|
+
# -- reading ------------------------------------------------------------------------------
|
|
343
|
+
|
|
344
|
+
def inputs(self) -> list[InputEvent]:
|
|
345
|
+
"""Every input event in the ring, in the order the engine saw them, ready for
|
|
346
|
+
``replay``. Flushes first, so nothing queued is left out."""
|
|
347
|
+
self.flush()
|
|
348
|
+
checkpoint = self._db.execute("SELECT frame,mono,payload FROM replay_checkpoint").fetchone()
|
|
349
|
+
rows: list[_Raw] = self._db.execute(schema.SELECT_INPUTS).fetchall()
|
|
350
|
+
if checkpoint is None:
|
|
351
|
+
return self.decode_inputs(rows)
|
|
352
|
+
return [
|
|
353
|
+
Checkpoint(checkpoint[1], checkpoint[2]),
|
|
354
|
+
*self.decode_inputs([r for r in rows if r[0] >= checkpoint[0]]),
|
|
355
|
+
]
|
|
356
|
+
|
|
357
|
+
@property
|
|
358
|
+
def checkpoint_count(self) -> int:
|
|
359
|
+
return int(self._db.execute("SELECT count(*) FROM replay_checkpoint").fetchone()[0])
|
|
360
|
+
|
|
361
|
+
def _advance_checkpoint(self, cutoff: float) -> None:
|
|
362
|
+
prior = self._db.execute("SELECT frame,mono,payload FROM replay_checkpoint").fetchone()
|
|
363
|
+
# Legacy hand-written journals have no session identity; preserve their original API.
|
|
364
|
+
if (
|
|
365
|
+
prior is None
|
|
366
|
+
and self._db.execute("SELECT 1 FROM inputs_raw WHERE kind='restart' LIMIT 1").fetchone()
|
|
367
|
+
is None
|
|
368
|
+
):
|
|
369
|
+
return
|
|
370
|
+
boundary = self._db.execute(
|
|
371
|
+
"SELECT coalesce(min(frame), (SELECT coalesce(max(frame),0)+1 FROM inputs_raw))"
|
|
372
|
+
" FROM inputs_raw WHERE ts>=?",
|
|
373
|
+
(cutoff,),
|
|
374
|
+
).fetchone()[0]
|
|
375
|
+
runner = ReplayRunner()
|
|
376
|
+
if prior:
|
|
377
|
+
runner.feed(Checkpoint(prior[1], prior[2]))
|
|
378
|
+
rows: list[_Raw] = self._db.execute(schema.SELECT_INPUTS).fetchall()
|
|
379
|
+
for event in self.decode_inputs(
|
|
380
|
+
[r for r in rows if (prior is None or r[0] >= prior[0]) and r[0] < boundary]
|
|
381
|
+
):
|
|
382
|
+
runner.feed(event)
|
|
383
|
+
self._db.execute(
|
|
384
|
+
"INSERT OR REPLACE INTO replay_checkpoint VALUES(1,?,?,?)",
|
|
385
|
+
(boundary, runner.clock(), runner.engine.checkpoint()),
|
|
386
|
+
)
|
|
387
|
+
|
|
388
|
+
@staticmethod
|
|
389
|
+
def decode_inputs(rows: list[_Raw]) -> list[InputEvent]:
|
|
390
|
+
events: list[InputEvent] = []
|
|
391
|
+
tick: tuple[int, float, dict[str, Value]] | None = None
|
|
392
|
+
for frame, mono, kind, name, number, text, missing, payload in rows:
|
|
393
|
+
if tick is not None and (frame != tick[0] or kind != "value"):
|
|
394
|
+
events.append(Tick(tick[1], tick[2]))
|
|
395
|
+
tick = None
|
|
396
|
+
match kind:
|
|
397
|
+
case "tick":
|
|
398
|
+
tick = (frame, mono, {})
|
|
399
|
+
case "value" if tick is not None and name is not None:
|
|
400
|
+
tick[2][name] = _join(number, text, missing)
|
|
401
|
+
case "open_alerts" if payload is not None:
|
|
402
|
+
events.append(OpenAlerts(mono, payload))
|
|
403
|
+
case "restart":
|
|
404
|
+
events.append(Restart(mono))
|
|
405
|
+
case "suspend":
|
|
406
|
+
events.append(Suspend(mono))
|
|
407
|
+
case "resume":
|
|
408
|
+
events.append(Resume(mono))
|
|
409
|
+
case "limits" if payload is not None:
|
|
410
|
+
events.append(LimitsChanged(mono, _LIMITS.decode(payload)))
|
|
411
|
+
case "rules" if payload is not None:
|
|
412
|
+
events.append(RulesLoaded(mono, tuple(_RULES.decode(payload))))
|
|
413
|
+
case "settings":
|
|
414
|
+
events.append(EngineSettings(mono, number))
|
|
415
|
+
case _:
|
|
416
|
+
log.warning("store: skipping an unreadable input row (frame %d)", frame)
|
|
417
|
+
if tick is not None:
|
|
418
|
+
events.append(Tick(tick[1], tick[2]))
|
|
419
|
+
return events
|
|
420
|
+
|
|
421
|
+
def close(self) -> None:
|
|
422
|
+
self.flush()
|
|
423
|
+
self._db.close()
|