feat(stats): write realtime mileage candidates
This commit is contained in:
@@ -156,6 +156,37 @@ func TestSamplesFromEnvelopeSkipsNonPositiveMileage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriterAppendWritesSourceCandidateAndProjection(t *testing.T) {
|
||||
exec := &recordingExec{}
|
||||
writer := NewWriter(exec, time.FixedZone("Asia/Shanghai", 8*3600))
|
||||
event := envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
VIN: "LA9GG64L7PBAF4001",
|
||||
Phone: "13307765812",
|
||||
SourceEndpoint: "115.231.168.135:20215",
|
||||
EventTimeMS: time.Date(2026, 7, 8, 13, 20, 0, 0, time.FixedZone("Asia/Shanghai", 8*3600)).UnixMilli(),
|
||||
Fields: map[string]any{
|
||||
"jt808.location.total_mileage_km": 4123.9,
|
||||
},
|
||||
}
|
||||
|
||||
if err := writer.Append(context.Background(), event); err != nil {
|
||||
t.Fatalf("Append() error = %v", err)
|
||||
}
|
||||
if len(exec.calls) != 5 {
|
||||
t.Fatalf("exec calls = %d, want source + candidate + clear + project + mark", len(exec.calls))
|
||||
}
|
||||
if !strings.Contains(exec.calls[0].query, "INSERT INTO vehicle_data_source") {
|
||||
t.Fatalf("first call should upsert source: %s", exec.calls[0].query)
|
||||
}
|
||||
if !strings.Contains(exec.calls[1].query, "INSERT INTO vehicle_daily_mileage_source") {
|
||||
t.Fatalf("second call should upsert candidate: %s", exec.calls[1].query)
|
||||
}
|
||||
if !strings.Contains(exec.calls[3].query, "INSERT INTO vehicle_daily_mileage") {
|
||||
t.Fatalf("fourth call should project final: %s", exec.calls[3].query)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriterEnsuresSchemaAndUpsertsDailyMileage(t *testing.T) {
|
||||
exec := &recordingExec{}
|
||||
writer := NewWriter(exec, time.FixedZone("Asia/Shanghai", 8*3600))
|
||||
@@ -163,9 +194,11 @@ func TestWriterEnsuresSchemaAndUpsertsDailyMileage(t *testing.T) {
|
||||
t.Fatalf("EnsureSchema() error = %v", err)
|
||||
}
|
||||
if err := writer.Append(context.Background(), envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolGB32960,
|
||||
VIN: "LNBVIN00000000002",
|
||||
EventTimeMS: time.Date(2026, 7, 1, 9, 0, 0, 0, time.FixedZone("Asia/Shanghai", 8*3600)).UnixMilli(),
|
||||
Protocol: envelope.ProtocolGB32960,
|
||||
VIN: "LNBVIN00000000002",
|
||||
Phone: "13307765812",
|
||||
SourceEndpoint: "115.231.168.135:20215",
|
||||
EventTimeMS: time.Date(2026, 7, 1, 9, 0, 0, 0, time.FixedZone("Asia/Shanghai", 8*3600)).UnixMilli(),
|
||||
Fields: map[string]any{
|
||||
envelope.FieldTotalMileageKM: "10000.0",
|
||||
},
|
||||
@@ -173,7 +206,7 @@ func TestWriterEnsuresSchemaAndUpsertsDailyMileage(t *testing.T) {
|
||||
t.Fatalf("Append() error = %v", err)
|
||||
}
|
||||
|
||||
if len(exec.calls) != 7 {
|
||||
if len(exec.calls) != 11 {
|
||||
t.Fatalf("exec calls = %d", len(exec.calls))
|
||||
}
|
||||
for i, want := range []string{
|
||||
@@ -196,22 +229,32 @@ func TestWriterEnsuresSchemaAndUpsertsDailyMileage(t *testing.T) {
|
||||
if strings.Contains(exec.calls[2].query, "KEY idx_vin (vin)") {
|
||||
t.Fatalf("daily mileage table should not keep redundant vin index covered by the primary key: %s", exec.calls[2].query)
|
||||
}
|
||||
upsertCall := exec.calls[6]
|
||||
if !strings.Contains(upsertCall.query, "ON DUPLICATE KEY UPDATE") {
|
||||
t.Fatalf("unexpected upsert sql: %s", upsertCall.query)
|
||||
projectCall := exec.calls[9]
|
||||
if !strings.Contains(projectCall.query, "INSERT INTO vehicle_daily_mileage") {
|
||||
t.Fatalf("unexpected project sql: %s", projectCall.query)
|
||||
}
|
||||
if strings.Contains(upsertCall.query, "metric_key") || strings.Contains(upsertCall.query, "metric_unit") {
|
||||
t.Fatalf("daily mileage upsert should not use generic metric columns: %s", upsertCall.query)
|
||||
if !strings.Contains(projectCall.query, "ON DUPLICATE KEY UPDATE") {
|
||||
t.Fatalf("unexpected upsert sql: %s", projectCall.query)
|
||||
}
|
||||
if !strings.Contains(upsertCall.query, "first_total_mileage_km <= 0") {
|
||||
t.Fatalf("upsert should ignore legacy zero first mileage: %s", upsertCall.query)
|
||||
if strings.Contains(projectCall.query, "metric_key") || strings.Contains(projectCall.query, "metric_unit") {
|
||||
t.Fatalf("daily mileage upsert should not use generic metric columns: %s", projectCall.query)
|
||||
}
|
||||
if !strings.Contains(upsertCall.query, "trusted_source_key") {
|
||||
t.Fatalf("upsert should track trusted source: %s", upsertCall.query)
|
||||
if !strings.Contains(projectCall.query, "LEFT JOIN vehicle_data_source ds") {
|
||||
t.Fatalf("project sql should join data source: %s", projectCall.query)
|
||||
}
|
||||
if got := upsertCall.args[0]; got != "LNBVIN00000000002" {
|
||||
if !strings.Contains(projectCall.query, "trusted_source_key") {
|
||||
t.Fatalf("upsert should track trusted source: %s", projectCall.query)
|
||||
}
|
||||
if got := projectCall.args[0]; got != "LNBVIN00000000002" {
|
||||
t.Fatalf("first upsert arg should be vin, got %#v", got)
|
||||
}
|
||||
|
||||
if !strings.Contains(exec.calls[6].query, "INSERT INTO vehicle_data_source") {
|
||||
t.Fatalf("source upsert query missing: %s", exec.calls[6].query)
|
||||
}
|
||||
if !strings.Contains(exec.calls[7].query, "INSERT INTO vehicle_daily_mileage_source") {
|
||||
t.Fatalf("candidate upsert query missing: %s", exec.calls[7].query)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriterSkipsConsecutiveDuplicateMileageSamples(t *testing.T) {
|
||||
@@ -234,8 +277,8 @@ func TestWriterSkipsConsecutiveDuplicateMileageSamples(t *testing.T) {
|
||||
t.Fatalf("duplicate Append() error = %v", err)
|
||||
}
|
||||
|
||||
if len(exec.calls) != 1 {
|
||||
t.Fatalf("exec calls = %d, want 1", len(exec.calls))
|
||||
if len(exec.calls) != 3 {
|
||||
t.Fatalf("exec calls = %d, want 3", len(exec.calls))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,8 +301,8 @@ func TestWriterDoesNotCacheMileageWhenUpsertFails(t *testing.T) {
|
||||
t.Fatalf("retry Append() error = %v", err)
|
||||
}
|
||||
|
||||
if len(exec.calls) != 2 {
|
||||
t.Fatalf("exec calls = %d, want 2", len(exec.calls))
|
||||
if len(exec.calls) != 4 {
|
||||
t.Fatalf("exec calls = %d, want 4", len(exec.calls))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,8 +326,8 @@ func TestWriterKeepsOnlyLatestDateInMileageCache(t *testing.T) {
|
||||
t.Fatalf("next-day Append() error = %v", err)
|
||||
}
|
||||
|
||||
if len(exec.calls) != 2 {
|
||||
t.Fatalf("exec calls = %d, want 2", len(exec.calls))
|
||||
if len(exec.calls) != 6 {
|
||||
t.Fatalf("exec calls = %d, want 6", len(exec.calls))
|
||||
}
|
||||
if len(writer.lastTotalMileage) != 1 {
|
||||
t.Fatalf("cache entries = %d, want 1", len(writer.lastTotalMileage))
|
||||
|
||||
Reference in New Issue
Block a user