fix(go): cache mileage after successful write
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user