feat: add go daily metric writer

This commit is contained in:
lingniu
2026-07-01 21:48:52 +08:00
parent 5d844701ff
commit bac8864e79
6 changed files with 414 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
package stats
import (
"context"
"database/sql"
"strings"
"testing"
"time"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
)
func TestSamplesFromEnvelopeDerivesDailyMileageAndTotal(t *testing.T) {
loc := time.FixedZone("Asia/Shanghai", 8*3600)
env := envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
VIN: "LNBVIN00000000001",
EventTimeMS: time.Date(2026, 7, 1, 8, 30, 0, 0, loc).UnixMilli(),
Fields: map[string]any{
envelope.FieldTotalMileageKM: 10241.2,
},
}
samples, err := SamplesFromEnvelope(env, loc)
if err != nil {
t.Fatalf("SamplesFromEnvelope() error = %v", err)
}
if len(samples) != 2 {
t.Fatalf("sample count = %d", len(samples))
}
if samples[0].MetricKey != MetricDailyMileageKM || samples[0].MetricValue != 0 {
t.Fatalf("unexpected daily mileage sample: %#v", samples[0])
}
if samples[1].MetricKey != MetricDailyTotalMileageKM || samples[1].MetricValue != 10241.2 {
t.Fatalf("unexpected daily total sample: %#v", samples[1])
}
if samples[0].StatDate != "2026-07-01" {
t.Fatalf("stat date = %q", samples[0].StatDate)
}
}
func TestSamplesFromEnvelopeSkipsMissingVINOrMileage(t *testing.T) {
samples, err := SamplesFromEnvelope(envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
Fields: map[string]any{envelope.FieldTotalMileageKM: 1.2},
}, nil)
if err != nil {
t.Fatalf("SamplesFromEnvelope() error = %v", err)
}
if len(samples) != 0 {
t.Fatalf("expected no samples without vin, got %#v", samples)
}
samples, err = SamplesFromEnvelope(envelope.FrameEnvelope{
Protocol: envelope.ProtocolGB32960,
VIN: "LNBVIN00000000002",
Fields: map[string]any{},
}, nil)
if err != nil {
t.Fatalf("SamplesFromEnvelope() error = %v", err)
}
if len(samples) != 0 {
t.Fatalf("expected no samples without mileage, got %#v", samples)
}
}
func TestWriterEnsuresSchemaAndUpsertsTwoMetrics(t *testing.T) {
exec := &recordingExec{}
writer := NewWriter(exec, time.FixedZone("Asia/Shanghai", 8*3600))
if err := writer.EnsureSchema(context.Background()); err != nil {
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(),
Fields: map[string]any{
envelope.FieldTotalMileageKM: "10000.0",
},
}); err != nil {
t.Fatalf("Append() error = %v", err)
}
if !strings.Contains(exec.calls[0].query, "CREATE TABLE IF NOT EXISTS vehicle_daily_metric") {
t.Fatalf("unexpected schema sql: %s", exec.calls[0].query)
}
if len(exec.calls) != 3 {
t.Fatalf("exec calls = %d", len(exec.calls))
}
if !strings.Contains(exec.calls[1].query, "ON DUPLICATE KEY UPDATE") {
t.Fatalf("unexpected upsert sql: %s", exec.calls[1].query)
}
}
type execCall struct {
query string
args []any
}
type recordingExec struct {
calls []execCall
}
func (e *recordingExec) ExecContext(_ context.Context, query string, args ...any) (sql.Result, error) {
e.calls = append(e.calls, execCall{query: query, args: args})
return nil, nil
}