mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
refactor(storage): simplify JSONL UTC bounds and regression tests
This commit is contained in:
+14
-13
@@ -5,7 +5,7 @@ from __future__ import annotations
|
||||
import heapq
|
||||
import json
|
||||
from collections.abc import Iterator
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@@ -14,6 +14,13 @@ from ..utils import format_timestamp, parse_timestamp
|
||||
from .base import Storage
|
||||
|
||||
|
||||
def _utc_bound(dt: datetime | None) -> datetime | None:
|
||||
"""Normalize a filter bound to the naive UTC used by stored timestamps."""
|
||||
if dt is not None and dt.tzinfo is not None:
|
||||
return dt.astimezone(timezone.utc).replace(tzinfo=None)
|
||||
return dt
|
||||
|
||||
|
||||
class JSONLStorage(Storage):
|
||||
"""JSONL file-based metrics storage."""
|
||||
|
||||
@@ -131,10 +138,8 @@ class JSONLStorage(Storage):
|
||||
return []
|
||||
|
||||
# Stored timestamps are naive UTC; apply the same normalization to filters.
|
||||
if start_time is not None:
|
||||
start_time = parse_timestamp(format_timestamp(start_time))
|
||||
if end_time is not None:
|
||||
end_time = parse_timestamp(format_timestamp(end_time))
|
||||
start_time = _utc_bound(start_time)
|
||||
end_time = _utc_bound(end_time)
|
||||
|
||||
def _matches(metrics: RequestMetrics) -> bool:
|
||||
if start_time is not None and metrics.timestamp < start_time:
|
||||
@@ -162,10 +167,8 @@ class JSONLStorage(Storage):
|
||||
mode: str | None = None,
|
||||
) -> int:
|
||||
"""Count metrics matching filters."""
|
||||
if start_time is not None:
|
||||
start_time = parse_timestamp(format_timestamp(start_time))
|
||||
if end_time is not None:
|
||||
end_time = parse_timestamp(format_timestamp(end_time))
|
||||
start_time = _utc_bound(start_time)
|
||||
end_time = _utc_bound(end_time)
|
||||
|
||||
count = 0
|
||||
|
||||
@@ -205,10 +208,8 @@ class JSONLStorage(Storage):
|
||||
end_time: datetime | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Get summary statistics."""
|
||||
if start_time is not None:
|
||||
start_time = parse_timestamp(format_timestamp(start_time))
|
||||
if end_time is not None:
|
||||
end_time = parse_timestamp(format_timestamp(end_time))
|
||||
start_time = _utc_bound(start_time)
|
||||
end_time = _utc_bound(end_time)
|
||||
|
||||
total_requests = 0
|
||||
total_tokens_before = 0
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Tests for JSONL storage query paging (shared newest-first contract)."""
|
||||
|
||||
from datetime import datetime, timedelta, timezone, tzinfo
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -65,50 +65,23 @@ def _ids(metrics: list[RequestMetrics]) -> list[str]:
|
||||
return [m.request_id for m in metrics]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("storage_cls", [JSONLStorage, SQLiteStorage])
|
||||
@pytest.mark.parametrize("method", ["query", "count", "get_summary_stats"])
|
||||
@pytest.mark.parametrize(
|
||||
"bound_tz",
|
||||
[None, timezone.utc, timezone(timedelta(hours=5, minutes=30)), timezone(timedelta(hours=-7))],
|
||||
ids=["naive-utc", "utc", "positive-offset", "negative-offset"],
|
||||
)
|
||||
@pytest.mark.parametrize("bounds", ["start", "end", "both", "mixed"])
|
||||
def test_storage_time_filters_use_utc(
|
||||
storage_cls: type[JSONLStorage] | type[SQLiteStorage],
|
||||
method: str,
|
||||
bound_tz: tzinfo | None,
|
||||
bounds: str,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Time filters include UTC boundary rows regardless of the caller's timezone."""
|
||||
boundary = datetime(2026, 4, 23, 23, 59, 59, 500000)
|
||||
aware_boundary = boundary.replace(tzinfo=timezone.utc)
|
||||
local_boundary = boundary if bound_tz is None else aware_boundary.astimezone(bound_tz)
|
||||
if bounds == "start":
|
||||
filters = {"start_time": local_boundary}
|
||||
expected = ["after", "boundary"]
|
||||
elif bounds == "end":
|
||||
filters = {"end_time": local_boundary}
|
||||
expected = ["boundary", "before"]
|
||||
elif bounds == "both":
|
||||
filters = {"start_time": local_boundary, "end_time": local_boundary}
|
||||
expected = ["boundary"]
|
||||
else:
|
||||
filters = {"start_time": local_boundary, "end_time": boundary}
|
||||
expected = ["boundary"]
|
||||
|
||||
with storage_cls(str(tmp_path / "metrics")) as storage:
|
||||
def test_jsonl_aware_time_bounds_are_inclusive(method: str, tmp_path: Path) -> None:
|
||||
"""Aware bounds preserve their UTC instant and include the exact boundary."""
|
||||
boundary = datetime(2026, 4, 23, 23, 59, 59, 500000, tzinfo=timezone.utc)
|
||||
local_boundary = boundary.astimezone(timezone(timedelta(hours=5, minutes=30)))
|
||||
with JSONLStorage(str(tmp_path / "metrics.jsonl")) as storage:
|
||||
for request_id, delta in [("before", -1), ("boundary", 0), ("after", 1)]:
|
||||
storage.save(_metrics(request_id, aware_boundary + timedelta(seconds=delta)))
|
||||
result = getattr(storage, method)(**filters)
|
||||
storage.save(_metrics(request_id, boundary + timedelta(microseconds=delta)))
|
||||
result = getattr(storage, method)(start_time=local_boundary, end_time=local_boundary)
|
||||
if method == "query":
|
||||
assert _ids(result) == expected
|
||||
assert _ids(result) == ["boundary"]
|
||||
elif method == "count":
|
||||
assert result == len(expected)
|
||||
assert result == 1
|
||||
else:
|
||||
assert result["total_requests"] == len(expected)
|
||||
assert result["total_tokens_before"] == 1000 * len(expected)
|
||||
assert result["total_tokens_after"] == 800 * len(expected)
|
||||
assert result["total_requests"] == 1
|
||||
assert result["total_tokens_before"] == 1000
|
||||
assert result["total_tokens_after"] == 800
|
||||
|
||||
|
||||
def _assert_timestamp_desc(metrics: list[RequestMetrics]) -> None:
|
||||
|
||||
Reference in New Issue
Block a user