github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/segment/retention.go (about) 1 package segment 2 3 import ( 4 "time" 5 ) 6 7 type RetentionPolicy struct { 8 now time.Time 9 10 AbsoluteTime time.Time 11 Levels map[int]time.Time 12 13 ExemplarsRetentionTime time.Time 14 } 15 16 func NewRetentionPolicy() *RetentionPolicy { 17 return &RetentionPolicy{now: time.Now()} 18 } 19 20 func (r RetentionPolicy) LowerTimeBoundary() time.Time { 21 if len(r.Levels) == 0 { 22 return r.AbsoluteTime 23 } 24 return r.Levels[0] 25 } 26 27 func (r *RetentionPolicy) SetAbsolutePeriod(period time.Duration) *RetentionPolicy { 28 r.AbsoluteTime = r.periodToTime(period) 29 return r 30 } 31 32 func (r *RetentionPolicy) SetExemplarsRetentionPeriod(period time.Duration) *RetentionPolicy { 33 r.ExemplarsRetentionTime = r.periodToTime(period) 34 return r 35 } 36 37 func (r *RetentionPolicy) SetLevelPeriod(level int, period time.Duration) *RetentionPolicy { 38 if r.Levels == nil { 39 r.Levels = make(map[int]time.Time) 40 } 41 r.Levels[level] = r.periodToTime(period) 42 return r 43 } 44 45 func (r *RetentionPolicy) SetLevels(levels ...time.Duration) *RetentionPolicy { 46 if r.Levels == nil { 47 r.Levels = make(map[int]time.Time) 48 } 49 for level, period := range levels { 50 if period != 0 { 51 r.Levels[level] = r.periodToTime(period) 52 } 53 } 54 return r 55 } 56 57 func (r RetentionPolicy) isToBeDeleted(sn *streeNode) bool { 58 return sn.isBefore(r.AbsoluteTime) || sn.isBefore(r.levelMaxTime(sn.depth)) 59 } 60 61 func (r RetentionPolicy) periodToTime(age time.Duration) time.Time { 62 if age == 0 { 63 return time.Time{} 64 } 65 return r.now.Add(-1 * age) 66 } 67 68 func (r *RetentionPolicy) normalize() *RetentionPolicy { 69 r.AbsoluteTime = normalizeTime(r.AbsoluteTime) 70 for k, v := range r.Levels { 71 r.Levels[k] = normalizeTime(v) 72 } 73 return r 74 } 75 76 func (r RetentionPolicy) levelMaxTime(depth int) time.Time { 77 if r.Levels == nil { 78 return time.Time{} 79 } 80 return r.Levels[depth] 81 }