File size: 2,217 Bytes
a866fc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import importlib.util
from pathlib import Path

from app.events import COLUMNS

spec = importlib.util.spec_from_file_location("compact", Path(__file__).resolve().parent.parent / "scripts" / "compact.py")
compact = importlib.util.module_from_spec(spec)
spec.loader.exec_module(compact)


def test_plan_selects_only_that_month():
    files = [
        "README.md",
        "data/incoming/2026/09/01/a-1.parquet", "data/incoming/2026/09/30/b-2.parquet",
        "data/incoming/2026/10/01/c-3.parquet", "data/events/2026-08.parquet", "data/events/2026-09.parquet",
    ]
    incoming, existing, target = compact.plan(files, "2026-09")
    assert incoming == ["data/incoming/2026/09/01/a-1.parquet", "data/incoming/2026/09/30/b-2.parquet"]
    assert existing == "data/events/2026-09.parquet" and target == "data/events/2026-09.parquet"
    assert compact.plan(files, "2026-11") == ([], None, "data/events/2026-11.parquet")


def test_compact_rows_merges_aligns_and_dedupes():
    old_monthly = [{"event_type": "session", "session_id": "a" * 16, "timestamp": "2026-09-02T00:00:00Z",
                    "distinct_queries": 1}]
    shard1 = [{"event_type": "session", "session_id": "a" * 16, "timestamp": "2026-09-02T00:10:00Z",
               "distinct_queries": 3},
              {"event_type": "mlx_benchmark", "selected_model": "mlx-community/x-4bit", "timestamp": "2026-09-02T00:11:00Z"}]
    shard2 = [{"event_type": "session", "session_id": "b" * 16, "timestamp": "2026-09-03T00:00:00Z"}]
    rows = compact.compact_rows([old_monthly, shard1, shard2])
    assert len(rows) == 3 and all(set(r) == set(COLUMNS) for r in rows)
    assert [r["distinct_queries"] for r in rows if r["session_id"] == "a" * 16] == [3]
    assert [r["timestamp"] for r in rows] == sorted(r["timestamp"] for r in rows)


def test_previous_month_and_refuses_current():
    from datetime import datetime, timezone
    assert compact.previous_month(datetime(2026, 1, 15, tzinfo=timezone.utc)) == "2025-12"
    assert compact.previous_month(datetime(2026, 9, 14, tzinfo=timezone.utc)) == "2026-08"
    import pytest
    now = datetime.now(timezone.utc).strftime("%Y-%m")
    with pytest.raises(SystemExit):
        compact.main(["--month", now])