fix(stats): enforce latest source endpoint only for projected daily mileage

This commit is contained in:
lingniu
2026-07-08 14:41:53 +08:00
parent 5f79df3fe2
commit bc2071ef42
3 changed files with 22 additions and 2 deletions

View File

@@ -66,3 +66,17 @@ cd go/vehicle-gateway && go test ./internal/stats -run TestProjectDailyMileageSe
cd go/vehicle-gateway && go test ./internal/stats -count=1 cd go/vehicle-gateway && go test ./internal/stats -count=1
# PASS: lingniu-vehicle-ingest/go/vehicle-gateway/internal/stats # PASS: lingniu-vehicle-ingest/go/vehicle-gateway/internal/stats
``` ```
## Fix (reviewer findings #2)
Corrected the remaining `projectDailyMileageSQL` contract issue so `trusted_source_endpoint` uses only `vehicle_data_source.latest_source_endpoint`:
- In `go/vehicle-gateway/internal/stats/source_mileage.go`, changed the projection from:
`COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint)`
to:
`ds.latest_source_endpoint`.
- In `go/vehicle-gateway/internal/stats/source_mileage_test.go`, extended `TestProjectDailyMileageSelectsCandidateAndMarksSource` assertions to:
- require `ds.latest_source_endpoint` is selected, and
- explicitly verify no fallback expression using `s.source_endpoint` remains.
This enforces the strict source-of-truth rule that endpoint data for final projection must come from latest connection metadata only, even when it is null/empty.

View File

@@ -192,7 +192,7 @@ SELECT
s.latest_total_mileage_km, s.latest_total_mileage_km,
s.source_key, s.source_key,
s.phone, s.phone,
COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint), ds.latest_source_endpoint,
s.sample_count s.sample_count
FROM vehicle_daily_mileage_source s FROM vehicle_daily_mileage_source s
LEFT JOIN vehicle_data_source ds LEFT JOIN vehicle_data_source ds

View File

@@ -104,12 +104,18 @@ func TestProjectDailyMileageSelectsCandidateAndMarksSource(t *testing.T) {
"ORDER BY COALESCE(ds.trust_priority, 100)", "ORDER BY COALESCE(ds.trust_priority, 100)",
"s.quality_status = '" + QualityOK + "'", "s.quality_status = '" + QualityOK + "'",
"s.daily_mileage_km BETWEEN 0 AND", "s.daily_mileage_km BETWEEN 0 AND",
"COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint)", "ds.latest_source_endpoint",
} { } {
if !strings.Contains(projectFinal, want) { if !strings.Contains(projectFinal, want) {
t.Fatalf("project query missing %q: %s", want, projectFinal) t.Fatalf("project query missing %q: %s", want, projectFinal)
} }
} }
if strings.Contains(projectFinal, "COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint)") {
t.Fatalf("project query should not fallback to source endpoint: %s", projectFinal)
}
if strings.Contains(projectFinal, ", s.source_endpoint") {
t.Fatalf("project query should only project latest_source_endpoint: %s", projectFinal)
}
if !strings.Contains(markSelected, "UPDATE vehicle_daily_mileage_source s") || !strings.Contains(markSelected, "SET s.is_selected = 1") { if !strings.Contains(markSelected, "UPDATE vehicle_daily_mileage_source s") || !strings.Contains(markSelected, "SET s.is_selected = 1") {
t.Fatalf("mark query should flag the elected source: %s", markSelected) t.Fatalf("mark query should flag the elected source: %s", markSelected)
} }