ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / test (pull_request) Successful in 9m5s
ci / lint (pull_request) Successful in 10m24s
ci / build (pull_request) Successful in 18m10s
ci / image (pull_request) Successful in 5m16s
Visitors can subscribe to a page's incidents by email where the organization allows it. They confirm their address first; confirmation and unsubscribe links only show a button, so mail scanners do not act on them, and mails carry RFC 8058 one-click unsubscribe headers. Subscribers get a mail when an incident opens or resolves and for every public update. Every page also has an RSS feed of incidents and planned maintenance and a read-only JSON API (status, summary, components, incidents), both served from the cached snapshot. Abuse protection: status page servers limit feed and API reads, subscription requests and link actions per visitor; the API limits subscription requests per visitor, mailbox and page, and a new limit caps the subscriptions an organization's pages gain in seven days (platform default and per-organization override, 1000 in the cloud). Unconfirmed subscriptions count towards it and are deleted after a week. The fixed cap of 200 monitors per status page is gone; the configurable limit alone decides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
145 lines
4.5 KiB
Go
145 lines
4.5 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 [9]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.Evidence > 0 {
|
||
if counts[5], err = q.PurgeEvidence(ctx, now.Add(-keep.Evidence)); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if keep.Audit > 0 {
|
||
if counts[4], err = q.PurgeAuditEvents(ctx, now.Add(-keep.Audit)); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if _, err = q.PurgeAlertmanagerAlerts(ctx, now.Add(-keepAlerts)); err != nil {
|
||
return err
|
||
}
|
||
if counts[6], err = q.PurgeUnverifiedUsers(ctx, now.Add(-unverifiedKeep)); err != nil {
|
||
return err
|
||
}
|
||
if counts[7], err = q.PurgeUnconfirmedSubscribers(ctx, now.Add(-subscriberWindow)); err != nil {
|
||
return err
|
||
}
|
||
if keep.Housekeeping > 0 {
|
||
if counts[8], err = q.PurgeSubscriberNotifications(ctx, now.Add(-keep.Housekeeping)); 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 != [9]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], "evidence", counts[5],
|
||
"unverified_accounts", counts[6], "unconfirmed_subscribers", counts[7], "subscriber_notifications", counts[8])
|
||
}
|
||
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
|
||
}
|