An open incident now shows "usually ~30m more (up to 1h 30m)", worked out
from how long earlier incidents with the same team and signature took.
GET /api/incidents/{id}/estimate returns the 50th and 90th percentile of
the time still to go, among earlier occurrences that lasted longer than this
one has so far. With fewer than five comparable occurrences it returns no
figures, and when this incident has outlived all of them it says
longer_than_usual instead of inventing a number.
Incidents closed by hand are left out: they record when someone gave up on
the incident, not when the problem ended. Archived incidents are left out
too, as in the stats. It is history, not a forecast; nothing about the
alert's current value is known to it, and the UI labels it as an estimate.
Added /similar to docs/api.md as well, which was missing from the table.
The stats page keeps its own median: it is the upper median, and switching
it to interpolated would change figures people already read.
107 lines
3.8 KiB
Go
107 lines
3.8 KiB
Go
package api_test
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type estimateResp struct {
|
|
SampleSize int `json:"sample_size"`
|
|
ElapsedSeconds int64 `json:"elapsed_seconds"`
|
|
P50Seconds *int64 `json:"p50_seconds"`
|
|
P90Seconds *int64 `json:"p90_seconds"`
|
|
LongerThanUsual bool `json:"longer_than_usual"`
|
|
}
|
|
|
|
func estimate(t *testing.T, s *ts, id int) estimateResp {
|
|
t.Helper()
|
|
var out estimateResp
|
|
decode(t, s.req(t, http.MethodGet, "/api/incidents/"+strconv.Itoa(id)+"/estimate", nil), &out)
|
|
return out
|
|
}
|
|
|
|
// history opens n incidents for one alert (job selects the signature), closes
|
|
// each after the given durations by the given source, and returns nothing: the
|
|
// ids are 1..n in order.
|
|
func history(t *testing.T, s *ts, job string, source string, durations ...int64) {
|
|
t.Helper()
|
|
base := time.Now().Add(-48 * time.Hour).Unix()
|
|
for i, d := range durations {
|
|
fp := "hist-" + job + "-" + source + "-" + strconv.Itoa(i)
|
|
postGrouped(t, s, fp, time.Unix(base+int64(i)*100, 0).UTC().Format(time.RFC3339),
|
|
map[string]string{"alertname": "Backlog", "job": job})
|
|
s.exec(t, `UPDATE incidents SET status = 'resolved', resolved_at = triggered_at + $1,
|
|
resolution_source = $2 WHERE group_key = $3`, d, source, fp)
|
|
}
|
|
}
|
|
|
|
// openNow opens a fresh incident for the alert that has been open for age.
|
|
func openNow(t *testing.T, s *ts, job string, age time.Duration) int {
|
|
t.Helper()
|
|
fp := "open-" + job
|
|
postGrouped(t, s, fp, time.Now().Add(-age).UTC().Format(time.RFC3339),
|
|
map[string]string{"alertname": "Backlog", "job": job})
|
|
// The incident opens when the webhook arrives, whatever startsAt says.
|
|
var id int
|
|
if err := s.db.QueryRow(`UPDATE incidents SET triggered_at = $2 WHERE group_key = $1 AND resolved_at IS NULL
|
|
RETURNING id`, fp, time.Now().Add(-age).Unix()).Scan(&id); err != nil {
|
|
t.Fatalf("backdate incident: %v", err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
func TestEstimate_TooFewSamplesGivesNoFigures(t *testing.T) {
|
|
s := newTS(t)
|
|
history(t, s, "a", "alerts", 600, 700, 800)
|
|
got := estimate(t, s, openNow(t, s, "a", time.Minute))
|
|
if got.SampleSize != 3 || got.P50Seconds != nil || got.P90Seconds != nil || got.LongerThanUsual {
|
|
t.Fatalf("expected a sample count and nothing else, got %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestEstimate_PercentilesAreTimeStillToGo(t *testing.T) {
|
|
s := newTS(t)
|
|
history(t, s, "a", "alerts", 1000, 2000, 3000, 4000, 5000)
|
|
got := estimate(t, s, openNow(t, s, "a", 500*time.Second))
|
|
if got.P50Seconds == nil || got.P90Seconds == nil {
|
|
t.Fatalf("expected figures, got %+v", got)
|
|
}
|
|
// p50 of the history is 3000 and p90 is 4600; about 500s have already passed.
|
|
if d := *got.P50Seconds - 2500; d < -5 || d > 5 {
|
|
t.Errorf("p50 remaining: want ~2500, got %d", *got.P50Seconds)
|
|
}
|
|
if d := *got.P90Seconds - 4100; d < -5 || d > 5 {
|
|
t.Errorf("p90 remaining: want ~4100, got %d", *got.P90Seconds)
|
|
}
|
|
}
|
|
|
|
func TestEstimate_ManualResolvesAndOtherSignaturesAreLeftOut(t *testing.T) {
|
|
s := newTS(t)
|
|
history(t, s, "a", "manual", 1000, 2000, 3000, 4000, 5000)
|
|
history(t, s, "b", "alerts", 1000, 2000, 3000, 4000, 5000)
|
|
got := estimate(t, s, openNow(t, s, "a", time.Minute))
|
|
if got.SampleSize != 0 || got.P50Seconds != nil {
|
|
t.Fatalf("expected no usable history, got %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestEstimate_OutlivingTheHistoryIsLongerThanUsual(t *testing.T) {
|
|
s := newTS(t)
|
|
history(t, s, "a", "alerts", 100, 200, 300, 400, 500)
|
|
got := estimate(t, s, openNow(t, s, "a", time.Hour))
|
|
if !got.LongerThanUsual || got.P50Seconds != nil {
|
|
t.Fatalf("expected longer_than_usual without figures, got %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestEstimate_ResolvedIncidentHasNone(t *testing.T) {
|
|
s := newTS(t)
|
|
history(t, s, "a", "alerts", 100, 200, 300, 400, 500)
|
|
got := estimate(t, s, 1)
|
|
if got.SampleSize != 0 || got.P50Seconds != nil || got.LongerThanUsual {
|
|
t.Fatalf("expected an empty estimate, got %+v", got)
|
|
}
|
|
}
|