package seheweit import "time" const ( ResourceEventID = "builtin.resource.sample" ResourceEventType = "resource.sample" RetentionDays = 7 MaxHistoryPoints = 361 ) // HistoryWindow describes the existing inclusive, epoch-aligned sample query. // Persistence chooses the most recently inserted sample in each bucket, not an // average, a peak or necessarily the largest timestamp in out-of-order data. type HistoryWindow struct { FromMS int64 ToMS int64 BucketMS int64 } func Window(hours int, now time.Time) HistoryWindow { if hours < 1 { hours = 1 } if hours > RetentionDays*24 { hours = RetentionDays * 24 } span := int64(time.Duration(hours) * time.Hour / time.Millisecond) bucket := span / (MaxHistoryPoints - 1) if bucket < int64(time.Minute/time.Millisecond) { bucket = int64(time.Minute / time.Millisecond) } return HistoryWindow{FromMS: now.UnixMilli() - span, ToMS: now.UnixMilli(), BucketMS: bucket} } // RetentionCutoff is inclusive: only samples strictly older are expired. func RetentionCutoff(atMS int64) int64 { return atMS - int64(RetentionDays*24*time.Hour/time.Millisecond) }