Fix observed_by filter to use event_observers junction table

The observed_by filter on messages, advertisements, telemetry, and
trace_paths matched only the first observer (stored in observer_node_id),
silently excluding events whose secondary observers appear only in the
event_observers junction table. This caused filtered lists to appear
'several hours behind' when a dominant observer consistently won the
first-insert race for recent events.

Replace the ObserverNode.public_key predicate with an IN subquery against
the event_observers junction table (the canonical multi-observer source
already used for display). Add a shared observed_by_filter_clause() helper
in observer_utils.py to avoid duplication across all four routes.

Add regression tests proving a secondary observer (present only in
event_observers) sees events via the filter. Update existing fixtures and
inline test data to seed event_hash and EventObserver rows.

Fixes #239
This commit is contained in:
Louis King
2026-06-13 19:27:51 +01:00
parent a4d9513185
commit 888e193e09
12 changed files with 865 additions and 10 deletions
+57
View File
@@ -73,6 +73,63 @@ class TestListTelemetryFilters:
data = response.json()
assert len(data["items"]) == 1
def test_filter_by_observed_by_secondary_observer(
self,
client_no_auth,
api_db_session,
):
"""Secondary observer (only in event_observers) sees the telemetry."""
from meshcore_hub.common.hash_utils import compute_telemetry_hash
from meshcore_hub.common.models import EventObserver, Node, Telemetry
primary_node = Node(
public_key="p1telp1telp1telp1telp1telp1telp1",
name="PrimaryObserver",
first_seen=datetime.now(timezone.utc),
)
secondary_node = Node(
public_key="s1tels1tels1tels1tels1tels1tels1",
name="SecondaryObserver",
first_seen=datetime.now(timezone.utc),
)
api_db_session.add_all([primary_node, secondary_node])
api_db_session.commit()
now = datetime.now(timezone.utc)
node_pk = "secobstelsecobstelsecobsteltel"
event_hash = compute_telemetry_hash(
node_public_key=node_pk,
parsed_data={"temperature": 42.0},
received_at=now,
)
telemetry = Telemetry(
node_public_key=node_pk,
parsed_data={"temperature": 42.0},
received_at=now,
observer_node_id=primary_node.id,
event_hash=event_hash,
)
api_db_session.add(telemetry)
api_db_session.commit()
api_db_session.add(
EventObserver(
event_type="telemetry",
event_hash=event_hash,
observer_node_id=secondary_node.id,
observed_at=datetime.now(timezone.utc),
)
)
api_db_session.commit()
response = client_no_auth.get(
f"/api/v1/telemetry?observed_by={secondary_node.public_key}"
)
assert response.status_code == 200
data = response.json()
assert len(data["items"]) == 1
assert data["items"][0]["observed_by"] == primary_node.public_key
def test_filter_by_since(self, client_no_auth, api_db_session):
"""Test filtering telemetry by since timestamp."""
from meshcore_hub.common.models import Telemetry