retention

package
v0.0.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2026 License: AGPL-3.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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 Policy

type Policy []Rule

Policy is a list of rules. An item is kept if any rule keeps it.

func (Policy) Validate

func (p Policy) Validate() error

Validate checks that the policy has at least one rule, and that every rule is valid. Requiring a rule means that applying a valid policy always keeps the newest item.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL