@@ -11,6 +11,7 @@ import (
11
11
"github.com/icinga/icingadb/pkg/icingaredis/telemetry"
12
12
"github.com/pkg/errors"
13
13
"go.uber.org/zap"
14
+ "math"
14
15
"time"
15
16
)
16
17
@@ -95,7 +96,7 @@ var RetentionStatements = []retentionStatement{{
95
96
}}
96
97
97
98
// RetentionOptions defines the non-default mapping of history categories with their retention period in days.
98
- type RetentionOptions map [string ]uint16
99
+ type RetentionOptions map [string ]float32
99
100
100
101
// Validate checks constraints in the supplied retention options and
101
102
// returns an error if they are violated.
@@ -120,16 +121,16 @@ func (o RetentionOptions) Validate() error {
120
121
type Retention struct {
121
122
db * database.DB
122
123
logger * logging.Logger
123
- historyDays uint16
124
- slaDays uint16
124
+ historyDays float32
125
+ slaDays float32
125
126
interval time.Duration
126
127
count uint64
127
128
options RetentionOptions
128
129
}
129
130
130
131
// NewRetention returns a new Retention.
131
132
func NewRetention (
132
- db * database.DB , historyDays , slaDays uint16 , interval time.Duration ,
133
+ db * database.DB , historyDays , slaDays float32 , interval time.Duration ,
133
134
count uint64 , options RetentionOptions , logger * logging.Logger ,
134
135
) * Retention {
135
136
return & Retention {
@@ -156,7 +157,7 @@ func (r *Retention) Start(ctx context.Context) error {
156
157
errs := make (chan error , 1 )
157
158
158
159
for _ , stmt := range RetentionStatements {
159
- var days uint16
160
+ var days float32
160
161
switch stmt .RetentionType {
161
162
case RetentionHistory :
162
163
if d , ok := r .options [stmt .Category ]; ok {
@@ -177,12 +178,21 @@ func (r *Retention) Start(ctx context.Context) error {
177
178
fmt .Sprintf ("Starting history retention for category %s" , stmt .Category ),
178
179
zap .Uint64 ("count" , r .count ),
179
180
zap .Duration ("interval" , r .interval ),
180
- zap .Uint16 ("retention-days" , days ),
181
+ zap .Float32 ("retention-days" , days ),
181
182
)
182
183
183
184
stmt := stmt
184
185
periodic .Start (ctx , r .interval , func (tick periodic.Tick ) {
185
- olderThan := tick .Time .AddDate (0 , 0 , - int (days ))
186
+ olderThan := tick .Time
187
+ wholeDays , dayFraction := math .Modf (float64 (days ))
188
+
189
+ if wholeDays > 0 {
190
+ olderThan = olderThan .AddDate (0 , 0 , - int (wholeDays ))
191
+ }
192
+
193
+ if dayFraction > 0 {
194
+ olderThan = olderThan .Add (- time .Duration (dayFraction * float64 (24 * time .Hour )))
195
+ }
186
196
187
197
r .logger .Debugf ("Cleaning up historical data for category %s from table %s older than %s" ,
188
198
stmt .Category , stmt .Table , olderThan )
0 commit comments