feat: segment observation history by controller identity (closes #23)

This commit is contained in:
xavierk
2026-09-01 23:03:42 +05:30
parent 7d219c4697
commit 6a1841c447
3 changed files with 652 additions and 3 deletions
+40 -3
View File
@@ -124,6 +124,20 @@ def acquire_from_sysfs(sysfs_path: Path) -> Dict[str, Any]:
else:
identity["transport"] = None
# vid/ssvid from PCI node (optional, metadata only - never key components)
# PCI device directory is the sysfs_path itself (the controller dir is a symlink to PCI)
pci_device = sysfs_path
for attr, key in [("vendor", "vid"), ("subsystem_vendor", "ssvid")]:
filepath = pci_device / attr
if filepath.exists():
try:
value = filepath.read_text().strip()
identity[key] = value if value else None
except Exception:
identity[key] = None
else:
identity[key] = None
return identity
@@ -180,18 +194,34 @@ def write_sample(
identity: Dict[str, Any],
conn: sqlite3.Connection,
clock,
) -> None:
) -> Dict[str, Any]:
"""Write one sample to the observation store.
Identity normalization happens exactly once here.
Returns segment info for the caller.
"""
from .segment import find_current_segment, should_open_new_segment, open_segment
# Normalize identity exactly once at write time
identity_key = normalize_identity(identity)
identity_degraded = compute_identity_degraded(identity)
# TODO: Implement full sample writing with controller segment handling
# For now, just insert a basic sample with identity fields
# Find current segment
current_segment = find_current_segment(conn)
# Determine if we need a new segment
should_open, reason = should_open_new_segment(
current_segment, identity_key, sample["bytes_written"], conn
)
# Open new segment if needed
segment_opened = False
if should_open:
now = clock.utcnow()
open_segment(conn, now, identity, identity_key, identity_degraded)
segment_opened = True
# Insert sample
cursor = conn.execute(
"""
INSERT INTO samples (
@@ -226,6 +256,13 @@ def write_sample(
)
conn.commit()
return {
"segment_opened": segment_opened,
"segment_reason": reason,
"identity_key": identity_key,
"identity_degraded": identity_degraded,
}
def run_collection(