Bars use a resolution fitting the range and the latency line is placed by time. The SLA is the share of time a monitor was not down, counted from incidents and only since the monitor exists. Monitor pages list the SLA from one hour to 90 days. Closes #59 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
86 lines
2.9 KiB
Go
86 lines
2.9 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}
|
|
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%")
|
|
}
|
|
}
|