package web import ( "slices" "time" ) // TimeRange is a selectable chart window. Bars use Bucket as resolution; // Hourly ranges are read from the hourly aggregate instead of raw results. type TimeRange struct { Key string Window time.Duration Bucket time.Duration Hourly bool // Layout formats bar titles. Layout string } // Ranges are offered on the dashboard and monitor pages, shortest first. var Ranges = []TimeRange{ {Key: "1h", Window: time.Hour, Bucket: time.Minute, Layout: "15:04"}, {Key: "3h", Window: 3 * time.Hour, Bucket: 5 * time.Minute, Layout: "15:04"}, {Key: "12h", Window: 12 * time.Hour, Bucket: 15 * time.Minute, Layout: "15:04"}, {Key: "24h", Window: 24 * time.Hour, Bucket: 30 * time.Minute, Layout: "01-02 15:04"}, {Key: "7d", Window: 7 * 24 * time.Hour, Bucket: 4 * time.Hour, Hourly: true, Layout: "01-02 15:04"}, } // SLAWindows are listed on monitor pages. var SLAWindows = []struct { Key string Window time.Duration }{ {"1h", time.Hour}, {"3h", 3 * time.Hour}, {"12h", 12 * time.Hour}, {"24h", 24 * time.Hour}, {"7d", 7 * 24 * time.Hour}, {"30d", 30 * 24 * time.Hour}, {"90d", 90 * 24 * time.Hour}, } // DefaultRange is used when the request names none. const DefaultRange = "24h" // RangeByKey returns the range for key, falling back to the default. func RangeByKey(key string) TimeRange { if i := slices.IndexFunc(Ranges, func(r TimeRange) bool { return r.Key == key }); i >= 0 { return Ranges[i] } return RangeByKey(DefaultRange) } // Bars returns the number of bars r shows. func (r TimeRange) Bars() int { return int(r.Window / r.Bucket) } // Span is a period during which a monitor was down; End is zero while it // still is. type Span struct { Start, End time.Time } // SLA returns the fraction of [from, to] not covered by down spans, or -1 // if the window is empty, e.g. because the monitor did not exist yet. // Overlapping spans count once. func SLA(down []Span, from, to time.Time) float64 { if !to.After(from) { return -1 } clipped := make([]Span, 0, len(down)) for _, s := range down { end := s.End if end.IsZero() || end.After(to) { end = to } start := s.Start if start.Before(from) { start = from } if end.After(start) { clipped = append(clipped, Span{start, end}) } } slices.SortFunc(clipped, func(a, b Span) int { return a.Start.Compare(b.Start) }) var downtime time.Duration var cur Span for i, s := range clipped { switch { case i == 0: cur = s case !s.Start.After(cur.End): if s.End.After(cur.End) { cur.End = s.End } default: downtime += cur.End.Sub(cur.Start) cur = s } } if len(clipped) > 0 { downtime += cur.End.Sub(cur.Start) } return 1 - float64(downtime)/float64(to.Sub(from)) } // SLAText formats an SLA for display. func SLAText(r float64) string { if r < 0 { return "–" } return PercentPrecise(r) } // PercentPrecise shows enough digits for SLA targets such as 99.95%. func PercentPrecise(r float64) string { switch { case r >= 1: return "100%" case r >= 0.999: return trimPercent(r*100, 3) case r >= 0.99: return trimPercent(r*100, 2) } return trimPercent(r*100, 1) }