refactor(go): make daily mileage vin only

This commit is contained in:
lingniu
2026-07-02 21:02:02 +08:00
parent b23ae3410c
commit bec6326103
8 changed files with 43 additions and 106 deletions

View File

@@ -21,7 +21,6 @@ type Writer struct {
}
type MetricSample struct {
VehicleKey string
VIN string
Protocol envelope.Protocol
StatDate string
@@ -52,7 +51,6 @@ func (w *Writer) Append(ctx context.Context, env envelope.FrameEnvelope) error {
}
for _, sample := range samples {
if _, err := w.exec.ExecContext(ctx, upsertDailyMileageSQL,
sample.VehicleKey,
sample.VIN,
sample.StatDate,
string(sample.Protocol),
@@ -66,8 +64,7 @@ func (w *Writer) Append(ctx context.Context, env envelope.FrameEnvelope) error {
func SamplesFromEnvelope(env envelope.FrameEnvelope, loc *time.Location) ([]MetricSample, error) {
vin := strings.TrimSpace(env.VIN)
vehicleKey := strings.TrimSpace(env.VehicleKey())
if vehicleKey == "" || strings.HasSuffix(vehicleKey, ":unknown") {
if vin == "" {
return nil, nil
}
totalMileage, ok := floatField(env, envelope.FieldTotalMileageKM)
@@ -89,7 +86,6 @@ func SamplesFromEnvelope(env envelope.FrameEnvelope, loc *time.Location) ([]Metr
}
statDate := time.UnixMilli(eventMS).In(loc).Format("2006-01-02")
return []MetricSample{{
VehicleKey: vehicleKey,
VIN: vin,
Protocol: env.Protocol,
StatDate: statDate,
@@ -99,11 +95,10 @@ func SamplesFromEnvelope(env envelope.FrameEnvelope, loc *time.Location) ([]Metr
const upsertDailyMileageSQL = `
INSERT INTO vehicle_daily_mileage
(vehicle_key, vin, stat_date, protocol, daily_mileage_km,
(vin, stat_date, protocol, daily_mileage_km,
first_total_mileage_km, latest_total_mileage_km, sample_count)
VALUES (?, ?, ?, ?, 0, ?, ?, 1)
VALUES (?, ?, ?, 0, ?, ?, 1)
ON DUPLICATE KEY UPDATE
vin = IF(VALUES(vin) <> '', VALUES(vin), vin),
first_total_mileage_km = CASE
WHEN first_total_mileage_km IS NULL OR first_total_mileage_km <= 0
THEN VALUES(first_total_mileage_km)