3.2 KiB
3.2 KiB
Task 3 Report: Final Mileage Election Projector
Outcome
Implemented ProjectDailyMileage in go/vehicle-gateway/internal/stats/source_mileage.go and added focused projector coverage in go/vehicle-gateway/internal/stats/source_mileage_test.go.
RED
Before implementation, the new projector test failed as expected because the function did not exist yet:
internal/stats/source_mileage_test.go:87:9: undefined: ProjectDailyMileage
That confirmed the test was exercising the missing task-3 surface rather than passing accidentally.
GREEN
After implementation:
go test ./internal/stats -run TestProjectDailyMileageSelectsCandidateAndMarksSource -count=1go test ./internal/stats -count=1
Both passed.
What Changed
- Added
maxSelectedDailyMileageKM = 1000. - Added
ProjectDailyMileage(...)with three SQL steps:- clear
is_selectedfor the VIN/date/protocol slice, - project the elected row from
vehicle_daily_mileage_sourceintovehicle_daily_mileage, - mark the winning source row as selected.
- clear
- Kept the candidate identity intact:
source_keystill carries protocol + phone/device + source IP, while the data-source join uses protocol + source IP. - Added a focused test that asserts the clear/project/mark sequence and the key SQL fragments.
Files Changed
go/vehicle-gateway/internal/stats/source_mileage.gogo/vehicle-gateway/internal/stats/source_mileage_test.go
Self-Review
- The projector stays aligned with the existing stats schema and helper conventions.
- The SQL ordering and join logic preserve the intended source identity split; there is no IP-only simplification.
- I did not add an unrelated schema migration because the current repository state already contained the needed
vehicle_daily_mileage_sourceandvehicle_daily_mileagecolumns/indexes for this projector. - The only notable follow-up risk is that the final selection query relies on MySQL-style
INSERT ... SELECT ... LIMIT 1 ON DUPLICATE KEY UPDATEbehavior, which matches the rest of the stats package but should still be exercised against the target DB in an integration run.
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,projectDailyMileageSQLnow writestrusted_source_endpointfromvehicle_data_source.latest_source_endpointviaCOALESCE(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 +'inprojectDailyMileageSQL. - Updated
go/vehicle-gateway/internal/stats/source_mileage_test.goto assert both the endpoint expression and constant-based quality predicate.
Validation commands:
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