Ranges grow to 30d, 90d, 180d and 1y, read from the aggregates, and the SLA table to 180d and 1y. Ranges and SLA windows less than half covered by data are shown disabled. Monitor pages chart each region separately with its share of successful checks. Bars align to TimescaleDB's bucket origin so multi-day buckets match. Closes #61 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
139 lines
5.0 KiB
Go
139 lines
5.0 KiB
Go
package api_test
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestTimeRangesAndSLA(t *testing.T) {
|
|
e := newEnv(t)
|
|
_, region := e.platformChecker(t, uniq("eu-"))
|
|
owner := e.browser(t)
|
|
owner.signup("o@"+uniq("r")+".test", uniq("rng-"))
|
|
st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{
|
|
"name": "Shop", "type": "tcp", "target": "example.com:443", "interval_seconds": 3600, "regions": []string{region},
|
|
})
|
|
if st != http.StatusCreated {
|
|
t.Fatalf("create monitor = %d %v", st, body)
|
|
}
|
|
id := uuid.MustParse(body["id"].(string))
|
|
org := uuid.MustParse(owner.orgID())
|
|
|
|
// Two days old, down for six minutes half an hour ago.
|
|
ctx := context.Background()
|
|
now := time.Now()
|
|
conn, err := e.dbPool.Acquire(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Release()
|
|
if _, err := conn.Exec(ctx, `SELECT set_config('app.scope', 'system', false)`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := conn.Exec(ctx, `UPDATE monitors SET created_at = $2 WHERE id = $1`, id, now.Add(-48*time.Hour)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := conn.Exec(ctx, `INSERT INTO incidents (org_id, monitor_id, started_at, resolved_at) VALUES ($1, $2, $3, $4)`,
|
|
org, id, now.Add(-30*time.Minute), now.Add(-24*time.Minute)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _ = conn.Exec(ctx, `SELECT set_config('app.scope', '', false)`)
|
|
|
|
_, _, html := owner.page("/monitors/" + id.String() + "?range=1h")
|
|
// The monitor is two days old, so 7d and longer cover only those two
|
|
// days: 6 of 2880 minutes down.
|
|
for _, want := range []string{">1h<", "90.0%", ">7d<", "99.79%", ">90d<"} {
|
|
if !strings.Contains(html, want) {
|
|
t.Errorf("monitor page lacks %q", want)
|
|
}
|
|
}
|
|
|
|
bars := map[string]int{"1h": 60, "3h": 36, "12h": 48, "24h": 48, "7d": 42, "30d": 60, "90d": 45, "180d": 45, "1y": 52}
|
|
for key, n := range bars {
|
|
st, _, html := owner.page("/?range=" + key)
|
|
if st != http.StatusOK {
|
|
t.Errorf("GET /?range=%s = %d", key, st)
|
|
continue
|
|
}
|
|
// The dashboard has one monitor; its chart holds one rect per bar.
|
|
if got := strings.Count(html, "<rect "); got != n {
|
|
t.Errorf("range %s shows %d bars, want %d", key, got, n)
|
|
}
|
|
}
|
|
if _, _, html := owner.page("/?range=1h"); !strings.Contains(html, "90.0%") {
|
|
t.Error("dashboard 1h does not show the 1h SLA")
|
|
}
|
|
if _, _, html := owner.page("/?range=24h"); !strings.Contains(html, "99.58") {
|
|
t.Error("dashboard 24h does not show the 24h SLA (6 of 1440 minutes down)")
|
|
}
|
|
|
|
// A monitor created five minutes ago is measured only since then.
|
|
st, body = owner.do("POST", "/api/v1/org/monitors", map[string]any{
|
|
"name": "New", "type": "tcp", "target": "example.com:443", "interval_seconds": 3600, "regions": []string{region},
|
|
})
|
|
if st != http.StatusCreated {
|
|
t.Fatal(body)
|
|
}
|
|
if _, _, html := owner.page("/monitors/" + body["id"].(string)); !strings.Contains(html, "100%") {
|
|
t.Error("new monitor without incidents does not show 100%")
|
|
}
|
|
}
|
|
|
|
func TestRangeAvailabilityAndRegions(t *testing.T) {
|
|
e := newEnv(t)
|
|
_, eu := e.platformChecker(t, uniq("eu-"))
|
|
_, us := e.platformChecker(t, uniq("us-"))
|
|
owner := e.browser(t)
|
|
owner.signup("o@"+uniq("ra")+".test", uniq("ra-"))
|
|
st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{
|
|
"name": "API", "type": "tcp", "target": "example.com:443", "interval_seconds": 3600, "regions": []string{eu, us},
|
|
})
|
|
if st != http.StatusCreated {
|
|
t.Fatalf("create monitor = %d %v", st, body)
|
|
}
|
|
id := uuid.MustParse(body["id"].(string))
|
|
org := uuid.MustParse(owner.orgID())
|
|
|
|
// Two hours of results: eu always fine, us always failing.
|
|
ctx := context.Background()
|
|
now := time.Now()
|
|
checker := uuid.New()
|
|
for m := 0; m <= 120; m += 5 {
|
|
at := now.Add(-time.Duration(m) * time.Minute)
|
|
for region, ok := range map[string]bool{eu: true, us: false} {
|
|
if _, err := e.dbPool.Exec(ctx,
|
|
`INSERT INTO check_results (time, org_id, monitor_id, region, checker_id, ok, latency_ms) VALUES ($1, $2, $3, $4, $5, $6, 50)`,
|
|
at, org, id, region, checker, ok); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
_, _, html := owner.page("/monitors/" + id.String() + "?range=1h")
|
|
for key, disabled := range map[string]bool{"3h": false, "12h": true, "24h": true, "1y": true} {
|
|
got := strings.Contains(html, `aria-disabled="true">`+key+"<")
|
|
if got != disabled {
|
|
t.Errorf("range %s disabled = %t, want %t", key, got, disabled)
|
|
}
|
|
}
|
|
if !strings.Contains(html, `href="/monitors/`+id.String()+`?range=3h"`) {
|
|
t.Error("3h is not selectable with two hours of data")
|
|
}
|
|
if !strings.Contains(html, "By region") && !strings.Contains(html, "Nach Region") {
|
|
t.Fatal("monitor page lacks the per-region section")
|
|
}
|
|
if !strings.Contains(html, ">100%<") || !strings.Contains(html, ">0.0%<") {
|
|
t.Error("per-region availability does not show 100% for eu and 0.0% for us")
|
|
}
|
|
|
|
// The dashboard follows the monitor with the oldest data.
|
|
if _, _, dash := owner.page("/?range=1h"); !strings.Contains(dash, `aria-disabled="true">12h<`) || strings.Contains(dash, `aria-disabled="true">3h<`) {
|
|
t.Error("dashboard range availability does not follow the data")
|
|
}
|
|
}
|