feat(phase-8): implement operational web dashboard, rest api, and telemetry data provider
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
"""Unit tests for dashboard data provider and telemetry parser."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
from warehouse_twin.dashboard.data_provider import DashboardDataProvider
|
||||
|
||||
|
||||
def test_list_experiments(tmp_path: Path):
|
||||
"""Verify scanning and listing of experiment runs."""
|
||||
exp_dir = tmp_path / "experiments"
|
||||
exp_dir.mkdir(parents=True)
|
||||
|
||||
# Empty experiment dir
|
||||
dp = DashboardDataProvider(tmp_path)
|
||||
assert dp.list_experiments() == []
|
||||
|
||||
# Valid experiment 1
|
||||
e1 = exp_dir / "exp_alpha"
|
||||
e1.mkdir()
|
||||
(e1 / "config.json").write_text(json.dumps({
|
||||
"name": "Alpha Run", "duration": 1800.0, "seed": 10, "fleet_size": 2, "dispatch_policy": "nearest_idle"
|
||||
}), encoding="utf-8")
|
||||
(e1 / "kpi_summary.json").write_text(json.dumps({
|
||||
"throughput": {"completed_per_hour": 45.0}
|
||||
}), encoding="utf-8")
|
||||
|
||||
# Valid experiment 2
|
||||
e2 = exp_dir / "baseline_1hr"
|
||||
e2.mkdir()
|
||||
(e2 / "config.json").write_text(json.dumps({
|
||||
"name": "Baseline 1-Hour", "duration": 3600.0, "seed": 42, "fleet_size": 4
|
||||
}), encoding="utf-8")
|
||||
(e2 / "kpi_summary.json").write_text(json.dumps({
|
||||
"throughput": {"completed_per_hour": 85.0}
|
||||
}), encoding="utf-8")
|
||||
|
||||
exps = dp.list_experiments()
|
||||
assert len(exps) == 2
|
||||
# baseline_1hr is sorted first
|
||||
assert exps[0]["id"] == "baseline_1hr"
|
||||
assert exps[0]["throughput"] == 85.0
|
||||
assert exps[1]["id"] == "exp_alpha"
|
||||
assert exps[1]["throughput"] == 45.0
|
||||
|
||||
|
||||
def test_get_experiment_detail_and_csv_parsing(tmp_path: Path):
|
||||
"""Verify detailed telemetry parsing including robots and tasks CSVs."""
|
||||
exp_dir = tmp_path / "experiments" / "test_run"
|
||||
exp_dir.mkdir(parents=True)
|
||||
|
||||
(exp_dir / "config.json").write_text(json.dumps({"name": "Test Run"}), encoding="utf-8")
|
||||
(exp_dir / "kpi_summary.json").write_text(json.dumps({"fleet_size": 2, "turnaround_time": {"p50": 75.0}}), encoding="utf-8")
|
||||
|
||||
# Mock robots.csv
|
||||
robots_csv_content = (
|
||||
"robot_id,robot_type,current_state,battery_percent,utilization_pct,total_distance_m,loaded_distance_m,empty_distance_m,energy_consumed_kwh,charging_sessions\n"
|
||||
"amr_1,light_amr,IDLE,98.5,45.0,300.0,180.0,120.0,0.12,1\n"
|
||||
"amr_2,heavy_amr,LOADED_TRANSIT,95.0,60.0,400.0,250.0,150.0,0.25,2\n"
|
||||
)
|
||||
(exp_dir / "robots.csv").write_text(robots_csv_content, encoding="utf-8")
|
||||
|
||||
# Mock tasks.csv
|
||||
tasks_csv_content = (
|
||||
"task_id,task_type,status,is_overdue\n"
|
||||
"t1,ORDER_PICK,COMPLETED,False\n"
|
||||
"t2,ORDER_PICK,COMPLETED,True\n"
|
||||
"t3,PRODUCTION_LINE_FEED,IN_PROGRESS,False\n"
|
||||
"t4,REPLENISHMENT,PENDING,False\n"
|
||||
)
|
||||
(exp_dir / "tasks.csv").write_text(tasks_csv_content, encoding="utf-8")
|
||||
|
||||
# Mock aisle_congestion.csv
|
||||
aisle_csv_content = (
|
||||
"u,v,traversals,conflicts,wait_time_sec,congestion_score,recommendation\n"
|
||||
"N1,N2,50,5,12.5,0.75,Designate one-way corridor\n"
|
||||
"N3,N4,20,1,2.0,0.25,Normal flow\n"
|
||||
)
|
||||
(exp_dir / "aisle_congestion.csv").write_text(aisle_csv_content, encoding="utf-8")
|
||||
|
||||
dp = DashboardDataProvider(tmp_path)
|
||||
detail = dp.get_experiment_detail("test_run")
|
||||
assert detail is not None
|
||||
assert detail["id"] == "test_run"
|
||||
assert len(detail["robots"]) == 2
|
||||
assert detail["robots"][0]["robot_id"] == "amr_1"
|
||||
assert detail["robots"][0]["battery_percent"] == 98.5
|
||||
|
||||
sum_tasks = detail["tasks_summary"]
|
||||
assert sum_tasks["total"] == 4
|
||||
assert sum_tasks["completed"] == 2
|
||||
assert sum_tasks["on_time"] == 1
|
||||
assert sum_tasks["late"] == 1
|
||||
assert sum_tasks["in_progress"] == 1
|
||||
assert sum_tasks["pending"] == 1
|
||||
|
||||
assert len(detail["bottlenecks"]) == 2
|
||||
assert detail["bottlenecks"][0]["edge"] == "N1 -> N2"
|
||||
assert detail["bottlenecks"][0]["severity"] == "CRITICAL"
|
||||
assert detail["bottlenecks"][1]["severity"] == "INFO"
|
||||
|
||||
|
||||
def test_markdown_table_parsing():
|
||||
"""Verify parsing of Markdown comparison tables."""
|
||||
table_md = """
|
||||
# Scenario Comparison
|
||||
| Metric | 2 AMRs | 4 AMRs | 6 AMRs |
|
||||
| :--- | :---: | :---: | :---: |
|
||||
| **Throughput (tasks/hr)** | 77.00 | 85.00 | 85.00 |
|
||||
| **Fleet Utilization** | 95.6% | 50.9% | 32.7% |
|
||||
"""
|
||||
rows = DashboardDataProvider._parse_markdown_table(table_md)
|
||||
assert len(rows) == 2
|
||||
assert rows[0]["Metric"] == "Throughput (tasks/hr)"
|
||||
assert rows[0]["2 AMRs"] == "77.00"
|
||||
assert rows[0]["4 AMRs"] == "85.00"
|
||||
assert rows[1]["Metric"] == "Fleet Utilization"
|
||||
assert rows[1]["4 AMRs"] == "50.9%"
|
||||
Reference in New Issue
Block a user