1 """Track recorder run history."""
3 from __future__
import annotations
6 from dataclasses
import dataclass
7 from datetime
import datetime
9 from sqlalchemy.orm.session
import Session
13 from ..db_schema
import RecorderRuns
14 from ..models
import process_timestamp
18 run_history: _RecorderRunsHistory, start: datetime
19 ) -> RecorderRuns |
None:
20 """Find the recorder run for a start time in _RecorderRunsHistory."""
21 run_timestamps = run_history.run_timestamps
22 runs_by_timestamp = run_history.runs_by_timestamp
31 if idx := bisect.bisect_left(run_timestamps, start.timestamp()):
32 return runs_by_timestamp[run_timestamps[idx - 1]]
36 @dataclass(frozen=True)
38 """Bisectable history of RecorderRuns."""
40 run_timestamps: list[int]
41 runs_by_timestamp: dict[int, RecorderRuns]
45 """Track recorder run history."""
48 """Track recorder run history."""
55 """Return the time the recorder started recording states."""
59 def first(self) -> RecorderRuns:
60 """Get the first run."""
61 if runs_by_timestamp := self.
_run_history_run_history.runs_by_timestamp:
62 return next(iter(runs_by_timestamp.values()))
67 """Get the current run."""
78 """Return if a run is active."""
81 def get(self, start: datetime) -> RecorderRuns |
None:
82 """Return the recorder run that started before or at start.
84 If the first run started after the start, return None
90 def start(self, session: Session) ->
None:
93 Must run in the recorder thread.
104 """Reset the run when the database is changed or fails.
106 Must run in the recorder thread.
111 def end(self, session: Session) ->
None:
112 """End the current run.
114 Must run in the recorder thread.
121 """Update the run cache.
123 Must run in the recorder thread.
125 run_timestamps: list[int] = []
126 runs_by_timestamp: dict[int, RecorderRuns] = {}
128 for run
in session.query(RecorderRuns).order_by(RecorderRuns.start.asc()).all():
132 timestamp =
int(run_dt.timestamp())
133 run_timestamps.append(timestamp)
134 runs_by_timestamp[timestamp] = run
147 """Clear the current run after ending it.
149 Must run in the recorder thread.
None start(self, Session session)
None end(self, Session session)
None load_from_db(self, Session session)
RecorderRuns current(self)
RecorderRuns|None get(self, datetime start)
datetime recording_start(self)
None process_timestamp(None ts)
RecorderRuns|None _find_recorder_run_for_start_time(_RecorderRunsHistory run_history, datetime start)