Files
Sebastian UnterschützandClaude Opus 5.5 538e9358c3 feat(api): configurable retention, housekeeping and storage overview
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
2026-11-22 09:50:12 +01:00

131 lines
3.8 KiB
Templ

package web
import (
"context"
"fmt"
"time"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
)
// StorageView summarizes how check data is stored and cleaned up.
type StorageView struct {
Levels []StorageLevel
Jobs []StorageJob
Tables []StorageTable
}
// StorageLevel is raw results or one of the aggregates.
type StorageLevel struct {
Key string
Size string
Compressed string
Chunks string
Retention string
CompressAfter string
}
// StorageJob is a TimescaleDB background job.
type StorageJob struct {
Name string
Relation string
OK bool
LastRun string
NextRun string
Failures int64
}
// StorageTable is a regular table and its size.
type StorageTable struct {
Name string
Size string
}
// Bytes formats n in binary units.
func Bytes(n int64) string {
const unit = 1024
if n < unit {
return fmt.Sprintf("%d B", n)
}
div, exp := int64(unit), 0
for m := n / unit; m >= unit; m /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(n)/float64(div), "KMGTPE"[exp])
}
// Days formats a retention period; zero means forever.
func Days(ctx context.Context, d time.Duration) string {
if d == 0 {
return i18n.T(ctx, "storage.forever")
}
if d%(24*time.Hour) == 0 {
return i18n.T(ctx, "storage.days", "Count", int(d/(24*time.Hour)))
}
return d.String()
}
templ storageSection(v StorageView) {
<section class="mt-12">
@sectionHeading("storage.title")
<p class="mb-4 max-w-2xl text-sm text-muted">{ i18n.T(ctx, "storage.intro") }</p>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead class="text-left text-xs text-muted">
<tr class="border-b border-rule">
<th class="py-2 pr-4 font-normal">{ i18n.T(ctx, "storage.level") }</th>
<th class="py-2 pr-4 font-normal">{ i18n.T(ctx, "storage.size") }</th>
<th class="py-2 pr-4 font-normal">{ i18n.T(ctx, "storage.chunks") }</th>
<th class="py-2 pr-4 font-normal">{ i18n.T(ctx, "storage.compress_after") }</th>
<th class="py-2 font-normal">{ i18n.T(ctx, "storage.retention") }</th>
</tr>
</thead>
<tbody class="divide-y divide-rule">
for _, l := range v.Levels {
<tr>
<td class="py-2 pr-4">{ i18n.T(ctx, "storage.level_" + l.Key) }</td>
<td class="py-2 pr-4 font-mono text-xs">
{ l.Size }
if l.Compressed != "" {
<span class="text-muted">{ " · " + l.Compressed }</span>
}
</td>
<td class="py-2 pr-4 font-mono text-xs">{ l.Chunks }</td>
<td class="py-2 pr-4 font-mono text-xs">{ l.CompressAfter }</td>
<td class="py-2 font-mono text-xs">{ l.Retention }</td>
</tr>
}
</tbody>
</table>
</div>
if len(v.Tables) > 0 {
<p class="mt-3 font-mono text-xs text-muted">
for i, t := range v.Tables {
if i > 0 {
{ " · " }
}
{ t.Name + " " + t.Size }
}
</p>
}
<h3 class="mb-2 mt-8 text-xs text-muted">{ i18n.T(ctx, "storage.jobs") }</h3>
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, j := range v.Jobs {
<li class="grid grid-cols-[1rem_1fr_auto] items-center gap-3 py-2 sm:grid-cols-[1rem_1fr_10rem_10rem]">
<span class={ "h-2 w-2 rounded-full", templ.KV("bg-up", j.OK), templ.KV("bg-down", !j.OK) }></span>
<span>
{ i18n.T(ctx, "storage.job_" + j.Name) }
<span class="font-mono text-xs text-muted">{ j.Relation }</span>
if j.Failures > 0 {
<span class="text-xs text-down">{ i18n.T(ctx, "storage.failures", "Count", j.Failures) }</span>
}
</span>
<span class="font-mono text-xs text-muted" title={ i18n.T(ctx, "storage.last_run") }>{ j.LastRun }</span>
<span class="hidden font-mono text-xs text-muted sm:block" title={ i18n.T(ctx, "storage.next_run") }>{ j.NextRun }</span>
</li>
}
</ul>
</section>
}