From 35e623c3924b81db3c6345ee66433dd6d4f6bbe4 Mon Sep 17 00:00:00 2001 From: firestar5683 <168790843+firestar5683@users.noreply.github.com> Date: Thu, 18 Jun 2026 13:51:58 -0500 Subject: [PATCH] Dang Imperials --- .../the_pond/tests/test_dashboard_stats.py | 48 +++++++++++++++++++ starpilot/system/the_pond/utilities.py | 21 +++++--- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/starpilot/system/the_pond/tests/test_dashboard_stats.py b/starpilot/system/the_pond/tests/test_dashboard_stats.py index ae3239582..7e2e42d33 100644 --- a/starpilot/system/the_pond/tests/test_dashboard_stats.py +++ b/starpilot/system/the_pond/tests/test_dashboard_stats.py @@ -230,6 +230,54 @@ def events(*names): return [SimpleNamespace(name=name) for name in names] +def test_drive_stats_cloud_distances_match_native_imperial_units(monkeypatch): + monkeypatch.setattr(utilities, "params", FakeParams({ + "IsMetric": False, + "ApiCache_DriveStats": { + "all": {"distance": 100.0, "routes": 12, "minutes": 360}, + "week": {"distance": 25.0, "routes": 3, "minutes": 90}, + }, + "StarPilotStats": { + "StarPilotMeters": 1609.344, + "StarPilotSeconds": 3600, + "StarPilotDrives": 2, + }, + })) + + stats = utilities.get_drive_stats() + + assert stats["all"]["distance"] == 100.0 + assert stats["all"]["drives"] == 12 + assert stats["all"]["hours"] == 6 + assert stats["week"]["distance"] == 25.0 + assert stats["starpilot"]["distance"] == 1.0 + assert stats["starpilot"]["hours"] == 1 + assert stats["starpilot"]["drives"] == 2 + + +def test_drive_stats_cloud_distances_match_native_metric_units(monkeypatch): + monkeypatch.setattr(utilities, "params", FakeParams({ + "IsMetric": True, + "ApiCache_DriveStats": { + "all": {"distance": 10.0, "routes": 2, "minutes": 60}, + "week": {"distance": 5.0, "routes": 1, "minutes": 30}, + }, + "StarPilotStats": { + "StarPilotMeters": 1000.0, + "StarPilotSeconds": 1800, + "StarPilotDrives": 1, + }, + })) + + stats = utilities.get_drive_stats() + + assert round(stats["all"]["distance"], 3) == 16.093 + assert round(stats["week"]["distance"], 3) == 8.047 + assert stats["starpilot"]["distance"] == 1.0 + assert stats["starpilot"]["hours"] == 0.5 + assert stats["starpilot"]["drives"] == 1 + + def test_route_metrics_count_selected_attention_events(): route = { "name": "route-a", diff --git a/starpilot/system/the_pond/utilities.py b/starpilot/system/the_pond/utilities.py index 1d62205f7..74de1d3b2 100644 --- a/starpilot/system/the_pond/utilities.py +++ b/starpilot/system/the_pond/utilities.py @@ -55,6 +55,7 @@ SEGMENT_RE = re.compile(r"^[0-9a-fA-F]{8}--[0-9a-fA-F]{10}--\d+$") TARGET_LOUDNESS = -15.0 METER_TO_MILE = 1.0 / 1609.344 METER_TO_KILOMETER = 0.001 +MILE_TO_KILOMETER = CV.MPH_TO_KPH METER_PER_SECOND_TO_MPH = CV.MS_TO_KPH * CV.KPH_TO_MPH DASHBOARD_CACHE_TTL_SECONDS = 5.0 @@ -575,21 +576,29 @@ def get_drive_stats(): is_metric = params.get_bool("IsMetric") unit = "kilometers" if is_metric else "miles" + def numeric(value, default=0.0): + try: + parsed = float(value) + except (TypeError, ValueError): + return default + return parsed if parsed == parsed else default + def process(timeframe): data = stats.get(timeframe, {}) + distance_miles = numeric(data.get("distance", 0)) return { - "distance": data.get("distance", 0) * (1 if is_metric else CV.KPH_TO_MPH), - "drives": data.get("routes", 0), - "hours": data.get("minutes", 0) / 60, + "distance": distance_miles * (MILE_TO_KILOMETER if is_metric else 1), + "drives": numeric(data.get("routes", 0)), + "hours": numeric(data.get("minutes", 0)) / 60, "unit": unit } stats["all"] = process("all") stats["week"] = process("week") stats["starpilot"] = { - "distance": starpilot_stats.get("StarPilotMeters", 0) * (0.001 if is_metric else METER_TO_MILE), - "hours": starpilot_stats.get("StarPilotSeconds", 0) / (60 * 60), - "drives": starpilot_stats.get("StarPilotDrives", 0), + "distance": numeric(starpilot_stats.get("StarPilotMeters", 0)) * (METER_TO_KILOMETER if is_metric else METER_TO_MILE), + "hours": numeric(starpilot_stats.get("StarPilotSeconds", 0)) / (60 * 60), + "drives": numeric(starpilot_stats.get("StarPilotDrives", 0)), "unit": unit }