fix(go): cache mileage after successful write

This commit is contained in:
lingniu
2026-07-03 08:43:58 +08:00
parent 8f5afd20a6
commit 1830798d2e
2 changed files with 40 additions and 1 deletions

View File

@@ -65,6 +65,7 @@ func (w *Writer) Append(ctx context.Context, env envelope.FrameEnvelope) error {
sample.TotalMileageKM); err != nil {
return err
}
w.markMileageWritten(sample)
}
return nil
}
@@ -77,13 +78,20 @@ func (w *Writer) seenSameMileage(sample MetricSample) bool {
if last, ok := w.lastTotalMileage[key]; ok && last == sample.TotalMileageKM {
return true
}
return false
}
func (w *Writer) markMileageWritten(sample MetricSample) {
prefix := fmt.Sprintf("%s|%s|", sample.VIN, sample.Protocol)
key := prefix + sample.StatDate
w.mu.Lock()
defer w.mu.Unlock()
for existing := range w.lastTotalMileage {
if strings.HasPrefix(existing, prefix) && existing != key {
delete(w.lastTotalMileage, existing)
}
}
w.lastTotalMileage[key] = sample.TotalMileageKM
return false
}
func SamplesFromEnvelope(env envelope.FrameEnvelope, loc *time.Location) ([]MetricSample, error) {