@@ -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
"strconv"
15
16
"strings"
16
17
"time"
@@ -97,7 +98,7 @@ var RetentionStatements = []retentionStatement{{
97
98
}}
98
99
99
100
// RetentionOptions defines the non-default mapping of history categories with their retention period in days.
100
- type RetentionOptions map [string ]uint16
101
+ type RetentionOptions map [string ]float64
101
102
102
103
// UnmarshalText implements [encoding.TextUnmarshaler] to allow RetentionOptions to be parsed by env.
103
104
//
@@ -162,16 +163,16 @@ func (o *RetentionOptions) Validate() error {
162
163
type Retention struct {
163
164
db * database.DB
164
165
logger * logging.Logger
165
- historyDays uint16
166
- slaDays uint16
166
+ historyDays float64
167
+ slaDays float64
167
168
interval time.Duration
168
169
count uint64
169
170
options RetentionOptions
170
171
}
171
172
172
173
// NewRetention returns a new Retention.
173
174
func NewRetention (
174
- db * database.DB , historyDays , slaDays uint16 , interval time.Duration ,
175
+ db * database.DB , historyDays , slaDays float64 , interval time.Duration ,
175
176
count uint64 , options RetentionOptions , logger * logging.Logger ,
176
177
) * Retention {
177
178
return & Retention {
@@ -198,7 +199,7 @@ func (r *Retention) Start(ctx context.Context) error {
198
199
errs := make (chan error , 1 )
199
200
200
201
for _ , stmt := range RetentionStatements {
201
- var days uint16
202
+ var days float64
202
203
switch stmt .RetentionType {
203
204
case RetentionHistory :
204
205
if d , ok := r .options [stmt .Category ]; ok {
@@ -210,7 +211,7 @@ func (r *Retention) Start(ctx context.Context) error {
210
211
days = r .slaDays
211
212
}
212
213
213
- if days < 1 {
214
+ if days <= 0 {
214
215
r .logger .Debugf ("Skipping history retention for category %s" , stmt .Category )
215
216
continue
216
217
}
@@ -219,12 +220,21 @@ func (r *Retention) Start(ctx context.Context) error {
219
220
fmt .Sprintf ("Starting history retention for category %s" , stmt .Category ),
220
221
zap .Uint64 ("count" , r .count ),
221
222
zap .Duration ("interval" , r .interval ),
222
- zap .Uint16 ("retention-days" , days ),
223
+ zap .Float64 ("retention-days" , days ),
223
224
)
224
225
225
226
stmt := stmt
226
227
periodic .Start (ctx , r .interval , func (tick periodic.Tick ) {
227
- olderThan := tick .Time .AddDate (0 , 0 , - int (days ))
228
+ olderThan := tick .Time
229
+ wholeDays , dayFraction := math .Modf (float64 (days ))
230
+
231
+ if wholeDays > 0 {
232
+ olderThan = olderThan .AddDate (0 , 0 , - int (wholeDays ))
233
+ }
234
+
235
+ if dayFraction > 0 {
236
+ olderThan = olderThan .Add (- time .Duration (dayFraction * float64 (24 * time .Hour )))
237
+ }
228
238
229
239
r .logger .Debugf ("Cleaning up historical data for category %s from table %s older than %s" ,
230
240
stmt .Category , stmt .Table , olderThan )
0 commit comments