fix(stats): project trusted mileage source endpoint from metadata

This commit is contained in:
lingniu
2026-07-08 14:39:40 +08:00
parent 99e81a289e
commit 5f79df3fe2
3 changed files with 22 additions and 3 deletions

View File

@@ -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
```

View File

@@ -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),

View File

@@ -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)