fix(stats): elect freshest forwarding source per terminal

This commit is contained in:
lingniu
2026-07-19 17:33:53 +08:00
parent a13ee8fc30
commit 263107d47a
3 changed files with 48 additions and 4 deletions

View File

@@ -516,6 +516,9 @@ func TestProjectDailyMileageSelectsCandidateAndMarksSource(t *testing.T) {
"AND (ds.platform_name IS NULL OR TRIM(ds.platform_name) = '')",
"s.daily_mileage_km BETWEEN 0 AND",
"DATEDIFF(DATE(s.latest_event_time), DATE(s.first_event_time))",
"SUM(identity_source.sample_count)",
"identity_source.vin = s.vin",
"COALESCE(NULLIF(TRIM(identity_source.phone), ''), NULLIF(TRIM(identity_source.device_id), ''), identity_source.source_key)",
} {
if !strings.Contains(projectFinal, want) {
t.Fatalf("project query missing %q: %s", want, projectFinal)
@@ -526,7 +529,6 @@ func TestProjectDailyMileageSelectsCandidateAndMarksSource(t *testing.T) {
"trusted_phone",
"trusted_source_endpoint",
"first_total_mileage_km",
"sample_count)",
"ds.latest_source_endpoint",
} {
if strings.Contains(projectFinal, forbidden) {
@@ -556,6 +558,9 @@ func TestProjectDailyMileageSelectsCandidateAndMarksSource(t *testing.T) {
"WHEN 'DIRECT' THEN 1",
"WHEN 'UNKNOWN' THEN 2",
"ds.trust_priority",
"SUM(identity_source.sample_count)",
"identity_source.vin = s2.vin",
"COALESCE(NULLIF(TRIM(identity_source.phone), ''), NULLIF(TRIM(identity_source.device_id), ''), identity_source.source_key)",
"LIMIT 1",
} {
if !strings.Contains(markSelected, want) {
@@ -573,6 +578,17 @@ func TestProjectDailyMileageSelectsCandidateAndMarksSource(t *testing.T) {
t.Fatalf("source selection should prefer classified DIRECT before UNKNOWN, found %q", forbidden)
}
}
for queryName, query := range map[string]string{
"project": projectFinal,
"mark": markSelected,
} {
identityScore := strings.Index(query, "SUM(identity_source.sample_count)")
freshness := strings.Index(query, "latest_event_time DESC")
rowSamples := strings.LastIndex(query, "sample_count DESC")
if identityScore < 0 || freshness < identityScore || rowSamples < freshness {
t.Fatalf("%s query should rank terminal evidence before forwarding freshness and row samples: %s", queryName, query)
}
}
if got := exec.calls[1].args[3]; got != maxSelectedDailyMileageKM {
t.Fatalf("mark query max mileage arg = %#v, want %d", got, maxSelectedDailyMileageKM)
}