package web import ( "slices" "time" ) // Source names where a range reads its data. Raw results are kept for 90 // days, the aggregates much longer. type Source int // Sources. const ( SourceRaw Source = iota SourceHourly SourceDaily ) // TimeRange is a selectable chart window with Bucket as bar resolution. type TimeRange struct { Key string Window time.Duration Bucket time.Duration Source Source // Layout formats bar titles. Layout string } const day = 24 * time.Hour // 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: day, Bucket: 30 * time.Minute, Layout: "01-02 15:04"}, {Key: "7d", Window: 7 * day, Bucket: 4 * time.Hour, Source: SourceHourly, Layout: "01-02 15:04"}, {Key: "30d", Window: 30 * day, Bucket: 12 * time.Hour, Source: SourceHourly, Layout: "2006-01-02 15:04"}, {Key: "90d", Window: 90 * day, Bucket: 2 * day, Source: SourceDaily, Layout: "2006-01-02"}, {Key: "180d", Window: 180 * day, Bucket: 4 * day, Source: SourceDaily, Layout: "2006-01-02"}, {Key: "1y", Window: 364 * day, Bucket: 7 * day, Source: SourceDaily, Layout: "2006-01-02"}, } // 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", day}, {"7d", 7 * day}, {"30d", 30 * day}, {"90d", 90 * day}, {"180d", 180 * day}, {"1y", 365 * day}, } // HasEnoughData reports whether data since first covers at least half of // window, below which a chart of that window would be mostly empty. func HasEnoughData(first, now time.Time, window time.Duration) bool { return !first.IsZero() && now.Sub(first) >= window/2 } // bucketOrigin is TimescaleDB's default time_bucket origin. Bars are aligned // to it so they match buckets of several days, which Go's Truncate would // place differently. var bucketOrigin = time.Date(2000, 1, 3, 0, 0, 0, 0, time.UTC) // AlignBucket returns the start of the bucket of width step containing t. func AlignBucket(t time.Time, step time.Duration) time.Time { d := t.Sub(bucketOrigin) n := d / step if d < 0 && d%step != 0 { n-- } return bucketOrigin.Add(n * step) } // 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) }