Retrieve logs and measurements with explicit event time, observation time, window bounds and missing-data behavior.
Event-window retrieval: answer from complete time slices
Define the clock and window
Event-window retrieval finds records that occurred in a requested interval, not merely records indexed recently. A query about pump P-47 alarms between 09:00 and 09:30 must use event time; observation time describes when the collector received a record. Late arrivals and device clock drift can make those differ. Store both clocks, source ID, event ID and a normalized time zone. Use half-open windows so adjacent intervals do not count the same event twice. Temporal retrieval supplies the same boundary discipline for document validity, but event streams have additional lateness and completeness issues.
Join events by stable identity
A trace ID, asset ID or incident ID can join logs from several services. Free-text similarity between two error messages is not sufficient to claim they describe the same incident. Apply tenant and asset filters before joining, then retain each event's source and original timestamp. A repeated warning may be a retry of one operation or many separate failures; deduplicate only with a stable event or request identity. Duplicate evidence rules keep retries from inflating a count.
Treat gaps as unknown
No retrieved alarm is not proof that no alarm occurred. The collector may have been offline, the index may lag, or the query may cover a shard not searched. Record source watermarks and missing intervals. When a window is incomplete, report the covered interval and abstain from a global zero count. A late event can change a previously computed result; either revise the answer or pin it to a snapshot and explain the cutoff. Freshness controls should expose that cutoff in the trace.
Separate retrieval from aggregation
Retrieve the eligible events first, then compute counts, maxima or sequences deterministically. A p95 latency claim requires enough samples and an explicit percentile method; a single high value is not the percentile. For a sequence question, preserve ordering by event time and break ties with stable IDs. Apply units and sampling rates before combining sensor readings. Numeric context is as important for telemetry as it is for a table cell.
Evaluate late and partial data
Build cases with out-of-order arrivals, clock skew, duplicate retries, collector outages, overlapping windows and no matching events. Judge event recall, window-boundary correctness, join identity, completeness detection and final answer support separately. A high recall score on available records can still mask an incorrect 'nothing happened' answer when a source is missing. Keep both query time and data snapshot in the evaluation record so later backfills do not silently change the expected result.
Worked contract
from dataclasses import dataclass
from datetime import datetime, timezone
@dataclass(frozen=True)
class AlarmEvent:
event_id: str
tenant: str
asset_id: str
occurred_at: datetime
observed_at: datetime
def window_events(events: list[AlarmEvent], tenant: str, asset_id: str,
start: datetime, end: datetime) -> list[str]:
if start.tzinfo is None or end.tzinfo is None or end <= start:
raise ValueError("window needs ordered aware datetimes")
seen: set[str] = set()
selected: list[str] = []
for event in sorted(events, key=lambda item: (item.occurred_at, item.event_id)):
if event.tenant == tenant and event.asset_id == asset_id \
and start <= event.occurred_at < end and event.event_id not in seen:
seen.add(event.event_id)
selected.append(event.event_id)
return selected
utc = timezone.utc
event_time = datetime(2026, 10, 7, 9, 17, tzinfo=utc)
alarms = [AlarmEvent("alarm-47", "north", "P-47", event_time,
datetime(2026, 10, 7, 9, 39, tzinfo=utc)),
AlarmEvent("alarm-47", "north", "P-47", event_time,
datetime(2026, 10, 7, 9, 40, tzinfo=utc))]
assert window_events(alarms, "north", "P-47",
datetime(2026, 10, 7, 9, 0, tzinfo=utc),
datetime(2026, 10, 7, 9, 30, tzinfo=utc)) == ["alarm-47"]Performance and operating cost
Sorting N events costs O(N log N) time and O(N) space in the example; an index ordered by tenant, asset and event time narrows production windows. Watermarks and coverage records add modest metadata but are necessary for trustworthy zero-result answers. Wider windows increase scanned events and aggregation cost, so apply a query limit and stream large results.
Common Mistakes
- Filtering by collector arrival time when the question asks when an event occurred.
- Calling an incomplete window empty.
- Counting duplicate retries as independent alarms.
- Using naive local timestamps across a daylight-saving boundary.
Validation drill
Build 67 events over three assets with delayed arrivals and two collector gaps. Judge 31 window questions, requiring correct half-open boundaries and an explicit incomplete-data result where coverage is missing.
