feat(platform): harden telemetry pipeline and unify Semi UI workspaces
This commit is contained in:
@@ -18,6 +18,7 @@ const (
|
||||
defaultCacheRetention = 72 * time.Hour
|
||||
defaultCacheCleanupInterval = 10 * time.Minute
|
||||
defaultBaselineMissTTL = time.Minute
|
||||
defaultBaselineHitTTL = 5 * time.Minute
|
||||
defaultMaxCacheEntries = 1000000
|
||||
)
|
||||
|
||||
@@ -34,6 +35,7 @@ type Writer struct {
|
||||
cacheRetention time.Duration
|
||||
cacheCleanupInterval time.Duration
|
||||
baselineMissTTL time.Duration
|
||||
baselineHitTTL time.Duration
|
||||
maxCacheEntries int
|
||||
lastCacheCleanup time.Time
|
||||
lastCacheCleanupStats cacheCleanupStats
|
||||
@@ -122,6 +124,7 @@ func NewWriter(exec Execer, loc *time.Location) *Writer {
|
||||
cacheRetention: defaultCacheRetention,
|
||||
cacheCleanupInterval: defaultCacheCleanupInterval,
|
||||
baselineMissTTL: defaultBaselineMissTTL,
|
||||
baselineHitTTL: defaultBaselineHitTTL,
|
||||
maxCacheEntries: defaultMaxCacheEntries,
|
||||
lastTotalMileage: map[string]float64{},
|
||||
lastSourceSeen: map[string]time.Time{},
|
||||
@@ -182,6 +185,15 @@ func (w *Writer) SetBaselineMissTTL(ttl time.Duration) {
|
||||
w.baselineMissTTL = ttl
|
||||
}
|
||||
|
||||
func (w *Writer) SetBaselineHitTTL(ttl time.Duration) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
if ttl < 0 {
|
||||
ttl = 0
|
||||
}
|
||||
w.baselineHitTTL = ttl
|
||||
}
|
||||
|
||||
func (w *Writer) SetMaxCacheEntries(maxEntries int) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
@@ -517,8 +529,12 @@ func (w *Writer) previousBaseline(ctx context.Context, candidate SourceMileageSa
|
||||
now := time.Now()
|
||||
w.mu.Lock()
|
||||
if cached, ok := w.baselineCache[cacheKey]; ok {
|
||||
missExpired := !cached.found && (w.baselineMissTTL == 0 || cached.cachedAt.IsZero() || now.Sub(cached.cachedAt) >= w.baselineMissTTL)
|
||||
if !missExpired {
|
||||
ttl := w.baselineMissTTL
|
||||
if cached.found {
|
||||
ttl = w.baselineHitTTL
|
||||
}
|
||||
expired := ttl == 0 || cached.cachedAt.IsZero() || now.Sub(cached.cachedAt) >= ttl
|
||||
if !expired {
|
||||
w.mu.Unlock()
|
||||
return cached.baseline, cached.found, nil
|
||||
}
|
||||
@@ -570,6 +586,16 @@ func (w *Writer) cacheBaseline(candidate SourceMileageSample, cacheKey string, e
|
||||
return
|
||||
}
|
||||
w.mu.Lock()
|
||||
if previous, ok := w.baselineCache[cacheKey]; ok &&
|
||||
previous.found && entry.found &&
|
||||
previous.baseline.LatestTotalKM == entry.baseline.LatestTotalKM &&
|
||||
previous.baseline.LatestEventTime.Equal(entry.baseline.LatestEventTime) &&
|
||||
!previous.cachedAt.IsZero() {
|
||||
// markBaselineWritten runs for every accepted sample. Preserve the
|
||||
// original cache age when the durable day-boundary baseline has not
|
||||
// changed, otherwise an active vehicle would keep stale history forever.
|
||||
entry.cachedAt = previous.cachedAt
|
||||
}
|
||||
if entry.cachedAt.IsZero() {
|
||||
entry.cachedAt = time.Now()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user