Dang Imperials

This commit is contained in:
firestar5683
2026-06-18 13:51:58 -05:00
parent 5eb6fe2dd9
commit 35e623c392
2 changed files with 63 additions and 6 deletions
@@ -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",
+15 -6
View File
@@ -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
}