File size: 9,840 Bytes
d5fee4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""End-to-end tests for build_dataset.py, annotate.py, analyze.py and plot_figures.py.

A small raw run is synthesised in a temporary directory with the collector's
own schema so that the build pipeline, label rules, event join and figure
rendering can be exercised without network access. Nothing here is written
into the repository's dataset directory.
"""

import csv
import json
import math
import random
from datetime import datetime, timedelta, timezone
from pathlib import Path

import pytest

pd = pytest.importorskip("pandas")

import annotate  # noqa: E402
import build_dataset as bd  # noqa: E402
import collect_data as cd  # noqa: E402

START = datetime(2026, 9, 6, 22, 0, 0, tzinfo=timezone.utc)   # 18:00 local (UTC-4)
INTERVAL = 60
ROWS = 3 * 60 * 6                                             # 18 h of 1-minute samples
LOCAL_OFFSET = timedelta(hours=-4)


def write_synthetic_run(raw_dir: Path, run_id: str = "testhost_20260906_180000") -> Path:
    rng = random.Random(7)
    rows = []
    for i in range(ROWS):
        t = START + timedelta(seconds=i * INTERVAL)
        if 400 <= i < 405:            # simulated gap of five samples (machine asleep)
            continue
        s = cd.Sample(run_id=run_id, host_label="testhost", sample_index=i,
                      timestamp_utc=t.strftime("%Y-%m-%dT%H:%M:%SZ"),
                      timestamp_local=(t + LOCAL_OFFSET).strftime("%Y-%m-%dT%H:%M:%S-04:00"),
                      epoch_s=int(t.timestamp()), elapsed_s=float(i * INTERVAL),
                      link_type="wifi", gateway_ip="192.168.1.1", dns_server="192.168.1.1",
                      target="8.8.8.8")
        base = 14.0 + 4.0 * math.sin((i / 60.0) / 24.0 * 2 * math.pi)
        rtts = [max(1.0, rng.gauss(base, 1.2)) for _ in range(10)]
        if i in (700, 701, 702):     # simulated outage
            rtts = []
        elif i % 97 == 0:            # occasional single lost packet
            rtts = rtts[:9]
        elif i == 900:               # latency spike
            rtts = [r + 120 for r in rtts]
        st = cd.rtt_stats(rtts)
        s.rtt_min_ms, s.rtt_avg_ms, s.rtt_median_ms = st["min"], st["avg"], st["median"]
        s.rtt_max_ms, s.rtt_stddev_ms, s.jitter_ms = st["max"], st["stddev"], st["jitter"]
        s.packets_sent, s.packets_received = 10, len(rtts)
        s.packet_loss_pct = round(100.0 * (10 - len(rtts)) / 10, 1)
        g = [max(0.3, rng.gauss(2.0, 0.5)) for _ in range(5)]
        if i == 1000:
            g = [r + 60 for r in g]
        gst = cd.rtt_stats(g)
        s.gateway_rtt_min_ms, s.gateway_rtt_avg_ms = gst["min"], gst["avg"]
        s.gateway_rtt_max_ms, s.gateway_jitter_ms = gst["max"], gst["jitter"]
        s.gateway_packet_loss_pct = 0.0
        s.tcp_connect_min_ms = round(base - 0.5, 3)
        s.tcp_connect_median_ms = round(base, 3)
        s.dns_resolve_ms = round(rng.gauss(2.5, 0.4), 3)
        s.dns_recursive_ms = round(rng.gauss(40, 8), 3)
        s.http_ms, s.http_status = round(rng.gauss(60, 10), 3), 200
        s.wifi_signal_pct = 80 + (i % 5)
        s.wifi_rssi_dbm = cd.rssi_from_quality(s.wifi_signal_pct)
        s.wifi_rx_mbps, s.wifi_tx_mbps = 866.7, 866.7
        s.wifi_channel, s.wifi_band_ghz, s.wifi_phy = 36, 5.0, "802.11ax"
        if i % 30 == 0:
            s.download_mbps, s.upload_mbps = round(rng.gauss(450, 30), 2), round(rng.gauss(22, 2), 2)
            s.speedtest_ping_ms = 1800000.0 if i == 60 else 9.5     # one failed latency probe
            s.speedtest_server_id, s.speedtest_server = 1234, "Example ISP, Springfield"
        s.sample_duration_s = 10.4
        s.notes = "ping: no replies" if not rtts else ""
        rows.append(cd.asdict(s))
    raw_dir.mkdir(parents=True, exist_ok=True)
    csv_path = raw_dir / f"localnet_{run_id}.csv"
    with csv_path.open("w", newline="", encoding="utf-8") as f:
        w = csv.DictWriter(f, fieldnames=cd.CSV_FIELDS)
        w.writeheader()
        w.writerows(rows)
    meta = {"run_id": run_id, "collector_version": cd.COLLECTOR_VERSION, "status": "completed",
            "interval_s": float(INTERVAL), "sample_count": len(rows),
            "environment": {"os": "Windows-11-10.0.22631-SP0", "ping_resolution_ms": 1.0},
            "network": {"link_type": "wifi"}}
    csv_path.with_suffix(".json").write_text(json.dumps(meta), encoding="utf-8")
    return csv_path


@pytest.fixture(scope="module")
def built(tmp_path_factory):
    root = tmp_path_factory.mktemp("ds")
    raw = root / "raw"
    write_synthetic_run(raw)
    # a manual annotation covering the simulated outage
    annotate.main(["router rebooted", "--category", "maintenance",
                   "--start", "2026-09-07T09:39:30Z", "--end", "2026-09-07T09:43:00Z",
                   "--file", str(raw / "events.csv")])
    card = root / "DATASET_CARD.md"
    card.write_text("---\nsize_categories:\n  - n<1K\n---\n# card\n\n"
                    f"{bd.CARD_BEGIN}\nold\n{bd.CARD_END}\n", encoding="utf-8")
    bd.main(["--raw-dir", str(raw), "--out-dir", str(root), "--card", str(card)])
    df = pd.read_csv(root / "localnet_dynamics.csv")
    summary = json.loads((root / "summary.json").read_text(encoding="utf-8"))
    return root, df, summary, card


def test_rows_and_columns(built):
    root, df, summary, _ = built
    assert len(df) == ROWS - 5
    assert list(df.columns[:11]) == cd.CSV_FIELDS[:11]
    assert df.columns[-1] == "notes"
    for col in bd.RUN_COLUMNS + bd.TIME_COLUMNS + bd.LABEL_COLUMNS + bd.EVENT_COLUMNS:
        assert col in df.columns
    assert (root / "localnet_dynamics.parquet").exists()
    assert summary["rows"] == len(df) and summary["runs"][0]["interval_s"] == 60.0
    assert summary["runs"][0]["os"].startswith("Windows")
    assert df["collector_os"].iloc[0] == "Windows" and df["rtt_resolution_ms"].iloc[0] == 1.0


def test_time_features_and_gaps(built):
    _, df, summary, _ = built
    first = df.iloc[0]
    assert first["hour_local"] == 18 and first["date_local"] == "2026-09-06"
    assert first["weekday_local"] == 6 and first["is_weekend"] == 1       # Sunday
    gaps = df["gap_before_s"].dropna()
    assert gaps.max() == 6 * INTERVAL and (gaps == INTERVAL).sum() == len(gaps) - 1
    assert 99.0 < summary["coverage_pct"] < 100.0


def test_reference_labels(built):
    _, df, summary, _ = built
    outages = df[df["label_outage"] == 1]
    assert list(outages["sample_index"]) == [700, 701, 702]
    assert df.loc[df["sample_index"] == 900, "label_high_latency"].item() == 1
    assert df.loc[df["sample_index"] == 1000, "label_gateway_degraded"].item() == 1
    assert df.loc[df["sample_index"] == 97, "label_packet_loss"].item() == 1
    assert df.loc[df["sample_index"] == 97, "label_outage"].item() == 0
    assert df["label_high_latency"].sum() <= 5                 # rule is not over-sensitive
    assert (df["label_anomaly"] >= df["label_outage"]).all()
    thr = summary["runs"][0]["thresholds"]["rtt_avg_ms"]
    assert thr["threshold"] >= thr["median"] + 10.0
    assert summary["labels"]["label_outage"]["rows"] == 3


def test_failed_speedtest_latency_is_missing(built):
    _, df, summary, _ = built
    tests = df[df["download_mbps"].notna()]
    assert len(tests) == 36
    assert tests["speedtest_ping_ms"].isna().sum() == 1
    assert tests.loc[tests["sample_index"] == 60, "speedtest_ping_ms"].isna().all()
    assert summary["metrics"]["speedtest_ping_ms"]["max"] == 9.5


def test_event_join(built):
    _, df, summary, _ = built
    tagged = df[df["event_category"].fillna("") != ""]
    assert summary["events"] == 1
    assert set(tagged["sample_index"]) == {700, 701, 702, 703}
    assert (tagged["event_description"] == "router rebooted").all()


def test_card_updated(built):
    _, _, summary, card = built
    text = card.read_text(encoding="utf-8")
    assert "old" not in text
    assert f"| Rows | {summary['rows']:,} |" in text
    assert "size_categories:\n  - 1K<n<10K" in text
    assert text.count(bd.CARD_BEGIN) == 1 and text.count(bd.CARD_END) == 1


def test_size_category():
    assert bd.size_category(500) == "n<1K"
    assert bd.size_category(86_400) == "10K<n<100K"
    assert bd.size_category(2_000_000) == "1M<n<10M"


def test_analysis_json(built):
    import analyze
    root, df, summary, _ = built
    analyze.main(["--input", str(root / "localnet_dynamics.csv"), "--no-report"])
    a = json.loads((root / "analysis.json").read_text(encoding="utf-8"))
    assert a["overview"]["rows"] == len(df)
    assert len(a["hour_profile"]) == 18 and len(a["daily"]) == 2
    assert len(a["outages"]) == 1 and a["outages"][0]["samples"] == 3
    assert a["regimes"]["before"]["n"] + a["regimes"]["after"]["n"] == len(df)
    assert a["speedtest"]["tests"] == 36 and a["speedtest"]["failed_latency_probe"] == 1
    assert a["labels"]["counts"]["label_outage"] == 3
    assert set(a["autocorrelation"]["values"]) >= {"log_rtt_avg_ms", "loss_indicator", "http_ms"}


def test_figures(built, tmp_path):
    pytest.importorskip("matplotlib")
    import plot_figures
    root, _, _, _ = built
    out = tmp_path / "figures"
    plot_figures.main(["--input", str(root / "localnet_dynamics.csv"), "--out-dir", str(out)])
    for name in ("overview_timeline", "diurnal_profile", "calendar_heatmaps", "tcp_vs_icmp",
                 "loss_anatomy", "regimes", "autocorrelation", "speedtest", "labels"):
        assert (out / "png" / f"{name}.png").stat().st_size > 10_000, name
        assert (out / "pdf" / f"{name}.pdf").stat().st_size > 1_000, name


def test_annotate_parses_local_and_utc_times():
    assert annotate.parse_time("2026-09-12T16:00:00Z").isoformat() == "2026-09-12T16:00:00+00:00"
    assert annotate.parse_time("2026-09-12 16:00+02:00").isoformat() == "2026-09-12T14:00:00+00:00"
    local = annotate.parse_time("2026-09-12 16:00")
    assert local.tzinfo == timezone.utc