fix(stats): track data sources before mileage sampling

This commit is contained in:
lingniu
2026-07-08 16:46:06 +08:00
parent 44e119331d
commit da669fae94
2 changed files with 80 additions and 14 deletions

View File

@@ -211,6 +211,33 @@ func TestWriterAppendSkipsMileageWithoutSourceIdentity(t *testing.T) {
}
}
func TestWriterAppendTracksSourceWithoutMileageSample(t *testing.T) {
exec := &recordingExec{}
writer := NewWriter(exec, time.FixedZone("Asia/Shanghai", 8*3600))
event := envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
Phone: "13307795518",
SourceEndpoint: "222.66.200.68:43614",
EventTimeMS: time.Date(2026, 7, 8, 16, 30, 0, 0, time.FixedZone("Asia/Shanghai", 8*3600)).UnixMilli(),
Fields: map[string]any{
"jt808.location.speed_kmh": 38.5,
},
}
if err := writer.Append(context.Background(), event); err != nil {
t.Fatalf("Append() error = %v", err)
}
if len(exec.calls) != 1 {
t.Fatalf("exec calls = %d, want source only", len(exec.calls))
}
if !strings.Contains(exec.calls[0].query, "INSERT INTO vehicle_data_source") {
t.Fatalf("source upsert query missing: %s", exec.calls[0].query)
}
if got := exec.calls[0].args[1]; got != "222.66.200.68" {
t.Fatalf("source ip arg = %#v", got)
}
}
func TestWriterAppendDedupesRealtimeMileagePerSource(t *testing.T) {
exec := &recordingExec{}
writer := NewWriter(exec, time.FixedZone("Asia/Shanghai", 8*3600))