From 5f79df3fe29bd15b9b9ff143fa0a357d5d2c947e Mon Sep 17 00:00:00 2001 From: lingniu Date: Wed, 8 Jul 2026 14:39:40 +0800 Subject: [PATCH] fix(stats): project trusted mileage source endpoint from metadata --- .superpowers/sdd/task-3-report.md | 18 ++++++++++++++++++ .../internal/stats/source_mileage.go | 4 ++-- .../internal/stats/source_mileage_test.go | 3 ++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.superpowers/sdd/task-3-report.md b/.superpowers/sdd/task-3-report.md index e62f8629..eb74f4bb 100644 --- a/.superpowers/sdd/task-3-report.md +++ b/.superpowers/sdd/task-3-report.md @@ -48,3 +48,21 @@ Both passed. ## Commit `aa12317b5` - `feat(stats): project elected mileage source` + +## Fix (reviewer findings) + +Applied the reviewer correction for the trusted source endpoint contract: + +- In `go/vehicle-gateway/internal/stats/source_mileage.go`, `projectDailyMileageSQL` now writes `trusted_source_endpoint` from `vehicle_data_source.latest_source_endpoint` via `COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint)` so latest connection metadata is authoritative with a simple fallback. +- Replaced hardcoded quality predicate with the existing constant using `s.quality_status = '` + QualityOK + `'` in `projectDailyMileageSQL`. +- Updated `go/vehicle-gateway/internal/stats/source_mileage_test.go` to assert both the endpoint expression and constant-based quality predicate. + +Validation commands: + +```bash +cd go/vehicle-gateway && go test ./internal/stats -run TestProjectDailyMileageSelectsCandidateAndMarksSource -count=1 +# PASS: lingniu-vehicle-ingest/go/vehicle-gateway/internal/stats + +cd go/vehicle-gateway && go test ./internal/stats -count=1 +# PASS: lingniu-vehicle-ingest/go/vehicle-gateway/internal/stats +``` diff --git a/go/vehicle-gateway/internal/stats/source_mileage.go b/go/vehicle-gateway/internal/stats/source_mileage.go index 428669ac..ccf572a4 100644 --- a/go/vehicle-gateway/internal/stats/source_mileage.go +++ b/go/vehicle-gateway/internal/stats/source_mileage.go @@ -192,7 +192,7 @@ SELECT s.latest_total_mileage_km, s.source_key, s.phone, - s.source_endpoint, + COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint), s.sample_count FROM vehicle_daily_mileage_source s LEFT JOIN vehicle_data_source ds @@ -200,7 +200,7 @@ LEFT JOIN vehicle_data_source ds WHERE s.vin = ? AND s.stat_date = ? AND s.protocol = ? - AND s.quality_status = 'OK' + AND s.quality_status = '` + QualityOK + `' AND s.daily_mileage_km BETWEEN 0 AND ? AND COALESCE(ds.enabled, 1) = 1 ORDER BY COALESCE(ds.trust_priority, 100), diff --git a/go/vehicle-gateway/internal/stats/source_mileage_test.go b/go/vehicle-gateway/internal/stats/source_mileage_test.go index 66b0ad52..780cf50b 100644 --- a/go/vehicle-gateway/internal/stats/source_mileage_test.go +++ b/go/vehicle-gateway/internal/stats/source_mileage_test.go @@ -102,8 +102,9 @@ func TestProjectDailyMileageSelectsCandidateAndMarksSource(t *testing.T) { "FROM vehicle_daily_mileage_source s", "LEFT JOIN vehicle_data_source ds", "ORDER BY COALESCE(ds.trust_priority, 100)", - "s.quality_status = 'OK'", + "s.quality_status = '" + QualityOK + "'", "s.daily_mileage_km BETWEEN 0 AND", + "COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint)", } { if !strings.Contains(projectFinal, want) { t.Fatalf("project query missing %q: %s", want, projectFinal)