Documentation
¶
Overview ¶
Package retention decides which items in a time series to keep, under a policy made of cron schedules.
Each rule's schedule splits time into periods: every scheduled time starts a period that lasts until the next scheduled time. A rule keeps the first (oldest) item in each of its N most recent periods, so "@weekly" (Sunday at midnight) keeps the Sunday item, or the item that stood in for it if Sunday's is missing. An item is kept if any rule keeps it.
Periods are counted over the items themselves, not over the calendar: "keep 14 @daily" keeps an item from each of the 14 most recent days that have an item. If items stop being produced (e.g. backups start failing), nothing ages out until newer items replace it.
Index ¶
Constants ¶
const Unlimited = -1
Unlimited keeps an item from every period of the rule's schedule.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Decision ¶
type Decision[T any] struct { Item T Time time.Time Keep bool // Reasons lists the schedules of the rules that keep the item. It is empty when the item is removed. Reasons []string }
Decision is the outcome of applying a policy to one item.
func Apply ¶
func Apply[T any](p Policy, items []T, timeOf func(T) time.Time, loc *time.Location) ([]Decision[T], error)
Apply decides which items to keep. timeOf returns when an item was created, and schedules are evaluated in loc. Decisions are returned newest first. Items with identical times are ordered by their input order, so the result is deterministic for a given input.
type Rule ¶
type Rule struct {
// Schedule is a five-field cron expression (minute, hour, day of month, month, day of week) or a descriptor
// (@yearly, @monthly, @weekly, @daily, @hourly). A field prefixed with "!" matches every value that the rest
// of the field doesn't, e.g. "0 0 * * !sun" is every day but Sunday. Unlike cron, when both day of month and
// day of week are restricted, a day must match both.
Schedule string `yaml:"schedule" jsonschema:"required"`
// Keep is the number of periods to keep an item from, or -1 for every period.
Keep int `yaml:"keep" jsonschema:"required,minimum=-1"`
}
Rule keeps the first item in each of the Keep most recent periods of Schedule.