UPTIME_RETENTION_RAW, _HOURLY and _DAILY set TimescaleDB's retention policies, applied by "api migrate up" with owner rights and on start; periods too short for the charts are rejected. An hourly job deletes delivered notifications, stale invitations and expired API tokens after UPTIME_RETENTION_HOUSEKEEPING and resolved incidents together with the daily aggregate. The admin panel shows size, compression, retention and background job status of each storage level. Closes #63 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package datapolicy
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/migrations"
|
|
)
|
|
|
|
const day = 24 * time.Hour
|
|
|
|
func TestValidate(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
r Retention
|
|
ok bool
|
|
}{
|
|
{Retention{}, true},
|
|
{Retention{Raw: 90 * day, Hourly: 400 * day, Daily: 365 * day}, true},
|
|
{Retention{Raw: 6 * day}, false},
|
|
{Retention{Hourly: 30 * day}, false},
|
|
{Retention{Daily: 364 * day}, false},
|
|
} {
|
|
if err := tc.r.Validate(); (err == nil) != tc.ok {
|
|
t.Errorf("%+v.Validate() = %v, want ok=%t", tc.r, err, tc.ok)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testPool(t *testing.T) *pgxpool.Pool {
|
|
t.Helper()
|
|
dsn := os.Getenv("UPTIME_TEST_DATABASE_URL")
|
|
if dsn == "" {
|
|
t.Skip("UPTIME_TEST_DATABASE_URL must be set")
|
|
}
|
|
ctx := context.Background()
|
|
if _, err := migrations.Up(ctx, dsn); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pool, err := pgxpool.New(ctx, dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(pool.Close)
|
|
return pool
|
|
}
|
|
|
|
func TestApply(t *testing.T) {
|
|
pool := testPool(t)
|
|
ctx := context.Background()
|
|
log := slog.New(slog.DiscardHandler)
|
|
defaults := Retention{Raw: 90 * day, Hourly: 400 * day, Daily: 1095 * day}
|
|
t.Cleanup(func() { _ = Apply(ctx, pool, defaults, log) })
|
|
|
|
want := Retention{Raw: 30 * day, Hourly: 0, Daily: 730 * day}
|
|
for range 2 {
|
|
if err := Apply(ctx, pool, want, log); err != nil {
|
|
t.Fatalf("Apply() = %v", err)
|
|
}
|
|
}
|
|
got, err := retentionPolicies(ctx, pool)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got["check_results"] != want.Raw || got["check_results_1d"] != want.Daily {
|
|
t.Errorf("policies = %v, want raw %v and daily %v", got, want.Raw, want.Daily)
|
|
}
|
|
if _, ok := got["check_results_1h"]; ok {
|
|
t.Error("hourly aggregate still has a retention policy after setting it to forever")
|
|
}
|
|
if err := Apply(ctx, pool, Retention{Raw: day}, log); err == nil {
|
|
t.Error("Apply() accepted one day of raw results")
|
|
}
|
|
|
|
st, err := Collect(ctx, pool)
|
|
if err != nil {
|
|
t.Fatalf("Collect() = %v", err)
|
|
}
|
|
if len(st.Relations) != 3 || st.Relations[0].Name != "check_results" || st.Relations[0].Retention != want.Raw {
|
|
t.Errorf("Collect().Relations = %+v", st.Relations)
|
|
}
|
|
if len(st.Jobs) == 0 {
|
|
t.Error("Collect() found no background jobs")
|
|
}
|
|
}
|