Every state-changing API route is mapped to an action; a test fails for unmapped routes. Successful changes are recorded with actor, API token, target, IP, user agent and request ID; failed sign-ins too. Names are taken from the response, never secrets. Organizations read their log with audit.read under Settings, users their account activity on the security page, admins the platform log. Entries expire after UPTIME_RETENTION_AUDIT. Closes #65 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"slices"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/datapolicy"
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
|
||
)
|
||
|
||
func (s *Server) retention() datapolicy.Retention {
|
||
r := s.cfg.Retention
|
||
return datapolicy.Retention{Raw: r.Raw, Hourly: r.Hourly, Daily: r.Daily}
|
||
}
|
||
|
||
// RunHousekeeping aligns TimescaleDB's retention with the configuration and
|
||
// then purges rows TimescaleDB does not manage, hourly, until ctx ends.
|
||
// Every replica runs it; the deletes are idempotent.
|
||
func (s *Server) RunHousekeeping(ctx context.Context) {
|
||
if err := datapolicy.Apply(ctx, s.db.Pool(), s.retention(), s.log); err != nil {
|
||
// The API role may not own the tables; "api migrate up" applies the
|
||
// policies as owner.
|
||
s.log.WarnContext(ctx, "applying retention policies failed", "error", err)
|
||
}
|
||
tick := time.NewTicker(time.Hour)
|
||
defer tick.Stop()
|
||
for {
|
||
if err := s.Housekeep(ctx, time.Now()); err != nil && ctx.Err() == nil {
|
||
s.log.ErrorContext(ctx, "housekeeping failed", "error", err)
|
||
}
|
||
select {
|
||
case <-ctx.Done():
|
||
return
|
||
case <-tick.C:
|
||
}
|
||
}
|
||
}
|
||
|
||
// Housekeep deletes delivered notifications, stale invitations, expired API
|
||
// tokens and incidents older than the daily aggregate.
|
||
func (s *Server) Housekeep(ctx context.Context, now time.Time) error {
|
||
keep := s.cfg.Retention
|
||
var counts [5]int64
|
||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||
var err error
|
||
if keep.Housekeeping > 0 {
|
||
before := now.Add(-keep.Housekeeping)
|
||
if counts[0], err = q.PurgeNotifications(ctx, before); err != nil {
|
||
return err
|
||
}
|
||
if counts[1], err = q.PurgeInvitations(ctx, before); err != nil {
|
||
return err
|
||
}
|
||
if counts[2], err = q.PurgeExpiredTokens(ctx, &before); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if keep.Audit > 0 {
|
||
if counts[4], err = q.PurgeAuditEvents(ctx, now.Add(-keep.Audit)); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if keep.Daily > 0 {
|
||
before := now.Add(-keep.Daily)
|
||
counts[3], err = q.PurgeIncidents(ctx, &before)
|
||
}
|
||
return err
|
||
})
|
||
if err == nil && counts != [5]int64{} {
|
||
s.log.InfoContext(ctx, "housekeeping purged rows",
|
||
"notifications", counts[0], "invitations", counts[1], "api_tokens", counts[2], "incidents", counts[3], "audit_events", counts[4])
|
||
}
|
||
return err
|
||
}
|
||
|
||
func storageView(c *gin.Context, st datapolicy.Stats) web.StorageView {
|
||
ctx := c.Request.Context()
|
||
var v web.StorageView
|
||
for _, r := range st.Relations {
|
||
l := web.StorageLevel{
|
||
Key: strings.TrimPrefix(strings.TrimPrefix(r.Name, "check_results"), "_"),
|
||
Size: web.Bytes(r.TotalBytes),
|
||
Chunks: fmt.Sprintf("%d / %d", r.CompressedChunks, r.Chunks),
|
||
Retention: web.Days(ctx, r.Retention),
|
||
CompressAfter: "–",
|
||
}
|
||
if l.Key == "" {
|
||
l.Key = "raw"
|
||
}
|
||
if r.CompressAfter > 0 {
|
||
l.CompressAfter = web.Days(ctx, r.CompressAfter)
|
||
}
|
||
if r.AfterBytes > 0 {
|
||
l.Compressed = fmt.Sprintf("%s → %s", web.Bytes(r.BeforeBytes), web.Bytes(r.AfterBytes))
|
||
}
|
||
v.Levels = append(v.Levels, l)
|
||
}
|
||
const layout = "2006-01-02 15:04"
|
||
for _, j := range st.Jobs {
|
||
sj := web.StorageJob{
|
||
Name: strings.TrimPrefix(j.Proc, "policy_"), Relation: j.Relation,
|
||
OK: j.LastStatus != "Failed", Failures: j.Failures, LastRun: "–", NextRun: "–",
|
||
}
|
||
if j.LastRun != nil {
|
||
sj.LastRun = j.LastRun.Local().Format(layout)
|
||
}
|
||
if j.NextRun != nil {
|
||
sj.NextRun = j.NextRun.Local().Format(layout)
|
||
}
|
||
v.Jobs = append(v.Jobs, sj)
|
||
}
|
||
for name, size := range st.TableBytes {
|
||
v.Tables = append(v.Tables, web.StorageTable{Name: name, Size: web.Bytes(size)})
|
||
}
|
||
slices.SortFunc(v.Tables, func(a, b web.StorageTable) int { return strings.Compare(a.Name, b.Name) })
|
||
return v
|
||
}
|