loop-common 0.0.1__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 (50) hide show
  1. loop_common/__init__.py +6 -0
  2. loop_common/base.py +98 -0
  3. loop_common/geometry/__init__.py +19 -0
  4. loop_common/geometry/_aabb.py +37 -0
  5. loop_common/geometry/_bounding_box.py +792 -0
  6. loop_common/geometry/_face_table.py +42 -0
  7. loop_common/geometry/_point.py +238 -0
  8. loop_common/geometry/_structured_grid.py +114 -0
  9. loop_common/geometry/_structured_grid_2d.py +146 -0
  10. loop_common/geometry/_structured_grid_3d.py +243 -0
  11. loop_common/geometry/_surface.py +288 -0
  12. loop_common/geometry/_unstructured_mesh.py +208 -0
  13. loop_common/interfaces/__init__.py +0 -0
  14. loop_common/interfaces/representation.py +45 -0
  15. loop_common/io/__init__.py +0 -0
  16. loop_common/logging/__init__.py +14 -0
  17. loop_common/logging/logger.py +123 -0
  18. loop_common/logging/sinks.py +221 -0
  19. loop_common/logging/timing.py +113 -0
  20. loop_common/math/__init__.py +5 -0
  21. loop_common/math/_maths.py +441 -0
  22. loop_common/math/_transformation.py +178 -0
  23. loop_common/math/finite_difference_stencil.py +38 -0
  24. loop_common/observations/__init__.py +3 -0
  25. loop_common/observations/lineset.py +41 -0
  26. loop_common/observations/orientation.py +122 -0
  27. loop_common/observations/pointset.py +8 -0
  28. loop_common/observer.py +284 -0
  29. loop_common/supports/_2d_base_unstructured.py +361 -0
  30. loop_common/supports/_2d_p1_unstructured.py +131 -0
  31. loop_common/supports/_2d_p2_unstructured.py +349 -0
  32. loop_common/supports/_2d_structured_grid.py +545 -0
  33. loop_common/supports/_3d_base_structured.py +546 -0
  34. loop_common/supports/_3d_p2_tetra.py +374 -0
  35. loop_common/supports/_3d_rectilinear_grid.py +315 -0
  36. loop_common/supports/_3d_structured_grid.py +510 -0
  37. loop_common/supports/_3d_structured_tetra.py +764 -0
  38. loop_common/supports/_3d_unstructured_tetra.py +656 -0
  39. loop_common/supports/__init__.py +69 -0
  40. loop_common/supports/_aabb.py +77 -0
  41. loop_common/supports/_base_support.py +118 -0
  42. loop_common/supports/_face_table.py +70 -0
  43. loop_common/supports/_p2_structured_tetra.py +627 -0
  44. loop_common/supports/_support_factory.py +78 -0
  45. loop_common/utils.py +74 -0
  46. loop_common-0.0.1.dist-info/METADATA +35 -0
  47. loop_common-0.0.1.dist-info/RECORD +50 -0
  48. loop_common-0.0.1.dist-info/WHEEL +5 -0
  49. loop_common-0.0.1.dist-info/licenses/LICENSE +21 -0
  50. loop_common-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,208 @@
1
+ """Pure unstructured mesh geometry: nodes/elements/neighbours containers."""
2
+
3
+ import numpy as np
4
+ from scipy import sparse
5
+
6
+ from ._aabb import _initialise_aabb
7
+ from ._face_table import _init_face_table
8
+ from ._structured_grid_2d import StructuredGrid2DGeometry
9
+ from ._structured_grid_3d import StructuredGrid3DGeometry
10
+
11
+
12
+ class UnstructuredMeshGeometry:
13
+ """An unstructured tetrahedral mesh defined by nodes, elements and neighbours."""
14
+
15
+ dimension = 3
16
+
17
+ def __init__(self, nodes: np.ndarray, elements: np.ndarray, neighbours: np.ndarray, aabb_nsteps=None):
18
+ self._nodes = np.array(nodes)
19
+ if self._nodes.shape[1] != 3:
20
+ raise ValueError("Nodes must be 3D")
21
+ self.neighbours = np.array(neighbours, dtype=np.int64)
22
+ if self.neighbours.shape[1] != 4:
23
+ raise ValueError("Neighbours array is too big")
24
+ self._elements = np.array(elements, dtype=np.int64)
25
+ if self.elements.shape[0] != self.neighbours.shape[0]:
26
+ raise ValueError("Number of elements and neighbours do not match")
27
+ self._barycentre = np.sum(self.nodes[self.elements[:, :4]][:, :, :], axis=1) / 4.0
28
+ self.minimum = np.min(self.nodes, axis=0)
29
+ self.maximum = np.max(self.nodes, axis=0)
30
+ length = self.maximum - self.minimum
31
+ self.minimum -= length * 0.1
32
+ self.maximum += length * 0.1
33
+ if self.elements.shape[0] < 2000:
34
+ self.aabb_grid = StructuredGrid3DGeometry(self.minimum, nsteps=[2, 2, 2], step_vector=[1, 1, 1])
35
+ else:
36
+ if aabb_nsteps is None:
37
+ box_vol = np.prod(self.maximum - self.minimum)
38
+ element_volume = box_vol / (len(self.elements) / 20)
39
+ step_vector = np.zeros(3)
40
+ step_vector[:] = element_volume ** (1.0 / 3.0)
41
+ aabb_nsteps = np.ceil((self.maximum - self.minimum) / step_vector).astype(int)
42
+ aabb_nsteps[aabb_nsteps < 2] = 2
43
+ aabb_nsteps = np.array(aabb_nsteps, dtype=int)
44
+ step_vector = (self.maximum - self.minimum) / (aabb_nsteps - 1)
45
+ self.aabb_grid = StructuredGrid3DGeometry(self.minimum, nsteps=aabb_nsteps, step_vector=step_vector)
46
+ self._aabb_table = sparse.csr_matrix((self.aabb_grid.n_elements, len(self.elements)), dtype=bool)
47
+ self._shared_element_relationships = np.zeros((self.neighbours[self.neighbours >= 0].flatten().shape[0], 2), dtype=int)
48
+ self._shared_elements = np.zeros((self.neighbours[self.neighbours >= 0].flatten().shape[0], 3), dtype=int)
49
+
50
+ @property
51
+ def nodes(self):
52
+ return self._nodes
53
+
54
+ @property
55
+ def elements(self):
56
+ return self._elements
57
+
58
+ @property
59
+ def barycentre(self):
60
+ return self._barycentre
61
+
62
+ @property
63
+ def n_nodes(self):
64
+ return self.nodes.shape[0]
65
+
66
+ @property
67
+ def n_elements(self):
68
+ return self.elements.shape[0]
69
+
70
+ @property
71
+ def aabb_table(self):
72
+ if np.sum(self._aabb_table) == 0:
73
+ _initialise_aabb(self)
74
+ return self._aabb_table
75
+
76
+ @property
77
+ def shared_elements(self):
78
+ if np.sum(self._shared_elements) == 0:
79
+ _init_face_table(self)
80
+ return self._shared_elements
81
+
82
+ @property
83
+ def shared_element_relationships(self):
84
+ if np.sum(self._shared_element_relationships) == 0:
85
+ _init_face_table(self)
86
+ return self._shared_element_relationships
87
+
88
+ def get_elements(self):
89
+ return self.elements
90
+
91
+ def get_neighbours(self):
92
+ return self.neighbours
93
+
94
+ @property
95
+ def shared_element_norm(self):
96
+ elements = self.shared_elements
97
+ v1 = self.nodes[elements[:, 1], :] - self.nodes[elements[:, 0], :]
98
+ v2 = self.nodes[elements[:, 2], :] - self.nodes[elements[:, 0], :]
99
+ return np.cross(v1, v2, axisa=1, axisb=1)
100
+
101
+ @property
102
+ def shared_element_size(self):
103
+ norm = self.shared_element_norm
104
+ return 0.5 * np.linalg.norm(norm, axis=1)
105
+
106
+ @property
107
+ def element_size(self):
108
+ vecs = (
109
+ self.nodes[self.elements[:, :4], :][:, 1:, :]
110
+ - self.nodes[self.elements[:, :4], :][:, 0, None, :]
111
+ )
112
+ return np.abs(np.linalg.det(vecs)) / 6
113
+
114
+ def inside(self, pos):
115
+ if pos.shape[1] > 3:
116
+ pos = pos[:, :3]
117
+ inside = np.ones(pos.shape[0]).astype(bool)
118
+ for i in range(3):
119
+ inside *= pos[:, i] > self.minimum[None, i]
120
+ inside *= pos[:, i] < self.maximum[None, i]
121
+ return inside
122
+
123
+
124
+ class UnstructuredMesh2DGeometry:
125
+ """An unstructured triangular mesh defined by vertices, elements and neighbours."""
126
+
127
+ dimension = 2
128
+
129
+ def __init__(self, elements, vertices, neighbours, aabb_nsteps=None):
130
+ self._elements = elements
131
+ self.vertices = vertices
132
+ if self.elements.shape[1] == 3:
133
+ self.order = 1
134
+ elif self.elements.shape[1] == 6:
135
+ self.order = 2
136
+ self.dof = self.vertices.shape[0]
137
+ self.neighbours = neighbours
138
+ self.minimum = np.min(self.nodes, axis=0)
139
+ self.maximum = np.max(self.nodes, axis=0)
140
+ length = self.maximum - self.minimum
141
+ self.minimum -= length * 0.1
142
+ self.maximum += length * 0.1
143
+ if aabb_nsteps is None:
144
+ box_vol = np.prod(self.maximum - self.minimum)
145
+ element_volume = box_vol / (len(self.elements) / 20)
146
+ step_vector = np.zeros(2)
147
+ step_vector[:] = element_volume ** (1.0 / 2.0)
148
+ aabb_nsteps = np.ceil((self.maximum - self.minimum) / step_vector).astype(int)
149
+ aabb_nsteps[aabb_nsteps < 2] = 2
150
+ step_vector = (self.maximum - self.minimum) / (aabb_nsteps - 1)
151
+ self.aabb_grid = StructuredGrid2DGeometry(self.minimum, nsteps=aabb_nsteps, step_vector=step_vector)
152
+ self._aabb_table = sparse.csr_matrix((self.aabb_grid.n_elements, len(self.elements)), dtype=bool)
153
+ self._shared_element_relationships = np.zeros((self.neighbours[self.neighbours >= 0].flatten().shape[0], 2), dtype=int)
154
+ self._shared_elements = np.zeros((self.neighbours[self.neighbours >= 0].flatten().shape[0], self.dimension), dtype=int)
155
+
156
+ @property
157
+ def aabb_table(self):
158
+ if np.sum(self._aabb_table) == 0:
159
+ _initialise_aabb(self)
160
+ return self._aabb_table
161
+
162
+ @property
163
+ def shared_elements(self):
164
+ if np.sum(self._shared_elements) == 0:
165
+ _init_face_table(self)
166
+ return self._shared_elements
167
+
168
+ @property
169
+ def shared_element_relationships(self):
170
+ if np.sum(self._shared_element_relationships) == 0:
171
+ _init_face_table(self)
172
+ return self._shared_element_relationships
173
+
174
+ @property
175
+ def elements(self):
176
+ return self._elements
177
+
178
+ @property
179
+ def n_elements(self):
180
+ return self.elements.shape[0]
181
+
182
+ @property
183
+ def n_nodes(self):
184
+ return self.vertices.shape[0]
185
+
186
+ @property
187
+ def ncps(self):
188
+ return self.elements.shape[1]
189
+
190
+ @property
191
+ def nodes(self):
192
+ return self.vertices
193
+
194
+ @property
195
+ def barycentre(self):
196
+ element_idx = np.arange(0, self.n_elements)
197
+ elements = self.elements[element_idx]
198
+ barycentre = np.sum(self.nodes[elements][:, :3, :], axis=1) / 3.0
199
+ return barycentre
200
+
201
+ @property
202
+ def shared_element_norm(self):
203
+ elements = self.shared_elements
204
+ v1 = self.nodes[elements[:, 1], :] - self.nodes[elements[:, 0], :]
205
+ norm = np.zeros_like(v1)
206
+ norm[:, 0] = v1[:, 1]
207
+ norm[:, 1] = -v1[:, 0]
208
+ return norm
File without changes
@@ -0,0 +1,45 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ import numpy as np
4
+
5
+
6
+ class BaseRepresentation(ABC):
7
+ @abstractmethod
8
+ def to_dict(self):
9
+ pass
10
+
11
+ @classmethod
12
+ @abstractmethod
13
+ def from_dict(cls, data):
14
+ pass
15
+
16
+ def __repr__(self):
17
+ return f"{self.__class__.__name__}({self.to_dict()})"
18
+
19
+ def __str__(self):
20
+ return self.__repr__()
21
+
22
+ def __eq__(self, other):
23
+ if not isinstance(other, BaseRepresentation):
24
+ return NotImplemented
25
+ return self.to_dict() == other.to_dict()
26
+
27
+ def __ne__(self, other):
28
+ eq_result = self.__eq__(other)
29
+ if eq_result is NotImplemented:
30
+ return NotImplemented
31
+ return not eq_result
32
+
33
+ def __hash__(self):
34
+ return hash(tuple(sorted(self.to_dict().items())))
35
+
36
+ @abstractmethod
37
+ def evaluate_value(self, position: np.ndarray):
38
+ raise NotImplementedError("Value evaluation not implemented for this representation")
39
+
40
+ @abstractmethod
41
+ def evaluate_gradient(self, position: np.ndarray):
42
+ raise NotImplementedError("Gradient evaluation not implemented for this representation")
43
+
44
+ def surfaces(self, value):
45
+ raise NotImplementedError("Surface extraction not implemented for this representation")
File without changes
@@ -0,0 +1,14 @@
1
+ from .logger import get_logger
2
+ from .sinks import FileSink, LogSink, SqliteSink, StreamSink, default_formatter
3
+ from .timing import timed, timed_stage
4
+
5
+ __all__ = [
6
+ "FileSink",
7
+ "LogSink",
8
+ "SqliteSink",
9
+ "StreamSink",
10
+ "default_formatter",
11
+ "get_logger",
12
+ "timed",
13
+ "timed_stage",
14
+ ]
@@ -0,0 +1,123 @@
1
+ """Zero-boilerplate logging interface.
2
+
3
+ Usage::
4
+
5
+ from lgutils.logging import get_logger
6
+
7
+ log = get_logger(__name__)
8
+ log.info("hello world")
9
+
10
+ # With a log file:
11
+ log = get_logger(__name__, log_file="run.log")
12
+ log.warning("this also goes to run.log")
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import logging
18
+ import sys
19
+ from pathlib import Path
20
+
21
+ # ---------------------------------------------------------------------------
22
+ # Optional loguru detection
23
+ # ---------------------------------------------------------------------------
24
+
25
+ try:
26
+ from loguru import logger as _loguru_logger # noqa: F401
27
+
28
+ _LOGURU_AVAILABLE = True
29
+ except ImportError:
30
+ _LOGURU_AVAILABLE = False
31
+
32
+ _DEFAULT_FMT = "%(asctime)s | %(name)-20s | %(levelname)-8s | %(message)s"
33
+ _LOGURU_FMT = "{time:YYYY-MM-DD HH:mm:ss} | {name:<20} | {level:<8} | {message}"
34
+
35
+
36
+ def get_logger(
37
+ name: str,
38
+ level: str | int = "INFO",
39
+ log_file: str | Path | None = None,
40
+ fmt: str | None = None,
41
+ use_loguru: bool | None = None,
42
+ ):
43
+ """Return a configured logger with no boilerplate required at the call site.
44
+
45
+ Parameters
46
+ ----------
47
+ name:
48
+ Logger name, typically ``__name__``.
49
+ level:
50
+ Log level string (``"DEBUG"``, ``"INFO"``, ``"WARNING"``, ``"ERROR"``)
51
+ or the corresponding integer constant.
52
+ log_file:
53
+ Optional path to a log file. Output is written to both stdout/stderr
54
+ **and** the file. Parent directories are created automatically.
55
+ fmt:
56
+ Custom format string. For stdlib loggers this is a ``%``-style
57
+ format; for loguru it is a loguru format string.
58
+ use_loguru:
59
+ Override auto-detection. ``True`` forces loguru (raises
60
+ ``RuntimeError`` if not installed). ``False`` forces stdlib.
61
+ ``None`` (default) uses loguru when available, stdlib otherwise.
62
+
63
+ Returns
64
+ -------
65
+ A logger object with ``.debug``, ``.info``, ``.warning``,
66
+ ``.error``, and ``.exception`` methods.
67
+ """
68
+ _use_loguru = _LOGURU_AVAILABLE if use_loguru is None else use_loguru
69
+ if _use_loguru:
70
+ return _build_loguru_logger(name, level, log_file, fmt)
71
+ return _build_stdlib_logger(name, level, log_file, fmt)
72
+
73
+
74
+ # ---------------------------------------------------------------------------
75
+ # Loguru backend
76
+ # ---------------------------------------------------------------------------
77
+
78
+
79
+ def _build_loguru_logger(name, level, log_file, fmt):
80
+ if not _LOGURU_AVAILABLE:
81
+ raise RuntimeError("loguru is not installed. Install it with: pip install loguru")
82
+ from loguru import logger
83
+
84
+ # Remove the default handler so we configure our own sinks.
85
+ logger.remove()
86
+ _fmt = fmt or _LOGURU_FMT
87
+ logger.add(sys.stderr, level=level, format=_fmt, colorize=True)
88
+ if log_file is not None:
89
+ Path(log_file).parent.mkdir(parents=True, exist_ok=True)
90
+ logger.add(str(log_file), level=level, format=_fmt, enqueue=True)
91
+
92
+ return logger.bind(name=name)
93
+
94
+
95
+ # ---------------------------------------------------------------------------
96
+ # Stdlib backend
97
+ # ---------------------------------------------------------------------------
98
+
99
+
100
+ def _build_stdlib_logger(name, level, log_file, fmt):
101
+ log = logging.getLogger(name)
102
+ log.setLevel(level)
103
+
104
+ # Guard against duplicate handlers on repeated calls with the same name.
105
+ if log.handlers:
106
+ return log
107
+
108
+ _fmt = fmt or _DEFAULT_FMT
109
+ formatter = logging.Formatter(_fmt)
110
+
111
+ stream_handler = logging.StreamHandler(sys.stdout)
112
+ stream_handler.setFormatter(formatter)
113
+ log.addHandler(stream_handler)
114
+
115
+ if log_file is not None:
116
+ log_file = Path(log_file)
117
+ log_file.parent.mkdir(parents=True, exist_ok=True)
118
+ file_handler = logging.FileHandler(log_file, encoding="utf-8")
119
+ file_handler.setFormatter(formatter)
120
+ log.addHandler(file_handler)
121
+
122
+ log.propagate = False
123
+ return log
@@ -0,0 +1,221 @@
1
+ """Pluggable log-sink infrastructure, shared across Loop packages.
2
+
3
+ :class:`LogSink` is the documented extension point host applications use to
4
+ route a package's log records into their own systems -- either by
5
+ subclassing it, or by passing a plain
6
+ ``Callable[[logging.LogRecord], None]`` straight to a package's
7
+ ``add_sink`` (e.g. ``LoopStructural.utils.add_sink``), no subclassing
8
+ required.
9
+
10
+ Attaching sinks to a specific logger registry (LoopStructural keeps its own
11
+ in ``LoopStructural.loggers``/``LoopStructural._extra_sinks``) is the
12
+ caller's responsibility -- these classes only build the
13
+ ``logging.Handler`` that gets attached.
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import logging
19
+ import sqlite3
20
+ import threading
21
+ from abc import ABC, abstractmethod
22
+ from datetime import datetime, timezone
23
+ from pathlib import Path
24
+ from typing import Callable
25
+
26
+ LogCallable = Callable[[logging.LogRecord], None]
27
+
28
+ __all__ = [
29
+ "FileSink",
30
+ "LogSink",
31
+ "SqliteSink",
32
+ "StreamSink",
33
+ "default_formatter",
34
+ ]
35
+
36
+
37
+ def default_formatter() -> logging.Formatter:
38
+ """Return the formatter used by the built-in sinks."""
39
+ return logging.Formatter("%(levelname)s: %(asctime)s: %(filename)s:%(lineno)d -- %(message)s")
40
+
41
+
42
+ class LogSink(ABC):
43
+ """Base class for a pluggable logging destination.
44
+
45
+ Subclass and implement :meth:`emit` to receive every
46
+ ``logging.LogRecord`` forwarded to a logger. A plain
47
+ ``Callable[[logging.LogRecord], None]`` works too and does not require
48
+ subclassing this class at all.
49
+ """
50
+
51
+ level: int = logging.NOTSET
52
+
53
+ @abstractmethod
54
+ def emit(self, record: logging.LogRecord) -> None:
55
+ """Handle a single log record."""
56
+
57
+ def handler(self) -> logging.Handler:
58
+ """Build the ``logging.Handler`` used to attach this sink to a logger."""
59
+ return _CallableHandler(self.emit, level=self.level)
60
+
61
+
62
+ class _CallableHandler(logging.Handler):
63
+ """Adapts a plain callable (or a `LogSink.emit`) to `logging.Handler`."""
64
+
65
+ def __init__(self, callback: LogCallable, *, level: int = logging.NOTSET):
66
+ super().__init__(level=level)
67
+ self._callback = callback
68
+
69
+ def emit(self, record: logging.LogRecord) -> None:
70
+ try:
71
+ self._callback(record)
72
+ except (AttributeError, TypeError, ValueError, RuntimeError):
73
+ self.handleError(record)
74
+
75
+
76
+ class StreamSink(LogSink):
77
+ """Writes formatted records to a stream, defaulting to stderr."""
78
+
79
+ def __init__(
80
+ self,
81
+ stream=None,
82
+ *,
83
+ formatter: logging.Formatter | None = None,
84
+ level: int = logging.WARNING,
85
+ ):
86
+ self.level = level
87
+ self._handler = logging.StreamHandler(stream)
88
+ self._handler.setFormatter(formatter or default_formatter())
89
+ self._handler.setLevel(level)
90
+
91
+ def emit(self, record: logging.LogRecord) -> None:
92
+ self._handler.emit(record)
93
+
94
+ def handler(self) -> logging.Handler:
95
+ return self._handler
96
+
97
+
98
+ class FileSink(LogSink):
99
+ """Writes formatted records to a log file, creating parent directories as needed."""
100
+
101
+ def __init__(
102
+ self,
103
+ path: str | Path,
104
+ *,
105
+ overwrite: bool = False,
106
+ formatter: logging.Formatter | None = None,
107
+ level: int = logging.INFO,
108
+ ):
109
+ self.path = Path(path)
110
+ self.level = level
111
+ if overwrite and self.path.exists():
112
+ self.path.unlink()
113
+ self.path.parent.mkdir(parents=True, exist_ok=True)
114
+ self._handler = logging.FileHandler(self.path)
115
+ self._handler.setFormatter(formatter or default_formatter())
116
+ self._handler.setLevel(level)
117
+
118
+ def emit(self, record: logging.LogRecord) -> None:
119
+ self._handler.emit(record)
120
+
121
+ def handler(self) -> logging.Handler:
122
+ return self._handler
123
+
124
+
125
+ class SqliteSink(LogSink):
126
+ """Writes structured log records to a SQLite database for querying run history.
127
+
128
+ Records produced by ``timed_stage``/``timed`` carry extra attributes
129
+ (``stage``, ``event``, ``duration_s``, ``run_id``) which are stored in
130
+ dedicated columns, so build/interpolation timings can be queried
131
+ directly (``sink.query(stage="update")``) instead of parsed out of
132
+ formatted log text.
133
+ """
134
+
135
+ _COLUMNS = (
136
+ "timestamp",
137
+ "logger_name",
138
+ "level",
139
+ "message",
140
+ "module",
141
+ "func_name",
142
+ "lineno",
143
+ "stage",
144
+ "event",
145
+ "duration_s",
146
+ "run_id",
147
+ )
148
+
149
+ def __init__(
150
+ self, path: str | Path, *, table: str = "log_records", level: int = logging.NOTSET
151
+ ):
152
+ self.path = Path(path)
153
+ self.level = level
154
+ self.table = table
155
+ self.path.parent.mkdir(parents=True, exist_ok=True)
156
+ self._lock = threading.Lock()
157
+ with self._lock, self._connect() as conn:
158
+ columns_sql = ", ".join(
159
+ f"{c} REAL" if c == "duration_s" else f"{c} TEXT" for c in self._COLUMNS
160
+ )
161
+ conn.execute(
162
+ f"CREATE TABLE IF NOT EXISTS {self.table} "
163
+ f"(id INTEGER PRIMARY KEY AUTOINCREMENT, {columns_sql})"
164
+ )
165
+
166
+ def _connect(self) -> sqlite3.Connection:
167
+ return sqlite3.connect(self.path, check_same_thread=False)
168
+
169
+ def emit(self, record: logging.LogRecord) -> None:
170
+ row = {
171
+ "timestamp": datetime.fromtimestamp(record.created, tz=timezone.utc).isoformat(),
172
+ "logger_name": record.name,
173
+ "level": record.levelname,
174
+ "message": record.getMessage(),
175
+ "module": record.module,
176
+ "func_name": record.funcName,
177
+ "lineno": record.lineno,
178
+ "stage": getattr(record, "stage", None),
179
+ "event": getattr(record, "event", None),
180
+ "duration_s": getattr(record, "duration_s", None),
181
+ "run_id": getattr(record, "run_id", None),
182
+ }
183
+ columns = ", ".join(row)
184
+ placeholders = ", ".join("?" for _ in row)
185
+ with self._lock, self._connect() as conn:
186
+ conn.execute(
187
+ f"INSERT INTO {self.table} ({columns}) VALUES ({placeholders})",
188
+ tuple(row.values()),
189
+ )
190
+
191
+ def query(
192
+ self,
193
+ *,
194
+ stage: str | None = None,
195
+ run_id: str | None = None,
196
+ logger_name: str | None = None,
197
+ level: str | None = None,
198
+ limit: int | None = None,
199
+ ) -> list[dict]:
200
+ """Query recorded log rows, optionally filtered. Returns dict rows, oldest first."""
201
+ clauses, params = [], []
202
+ for column, value in (
203
+ ("stage", stage),
204
+ ("run_id", run_id),
205
+ ("logger_name", logger_name),
206
+ ("level", level),
207
+ ):
208
+ if value is not None:
209
+ clauses.append(f"{column} = ?")
210
+ params.append(value)
211
+ sql = f"SELECT * FROM {self.table}"
212
+ if clauses:
213
+ sql += " WHERE " + " AND ".join(clauses)
214
+ sql += " ORDER BY id"
215
+ if limit is not None:
216
+ sql += " LIMIT ?"
217
+ params.append(int(limit))
218
+ with self._lock, self._connect() as conn:
219
+ conn.row_factory = sqlite3.Row
220
+ rows = conn.execute(sql, params).fetchall()
221
+ return [dict(row) for row in rows]