Files
nmon/internal/api/graph_p95.go

95 lines
3.2 KiB
Go

package api
import (
"context"
"errors"
"time"
"github.com/v10networks/nmon/internal/p95"
)
const (
p95Quantile = 0.95
p95Slot = 5 * time.Minute
// maxP95Span bounds the live computation to what one range query can return (about 25,000 slots).
maxP95Span = 85 * 24 * time.Hour
)
// graphP95 answers p95=true. A range that is one whole calendar month of the interface's site is the monthly
// figure and comes from p95.Service (materialised when the month is done and recorded); anything else is
// computed live over the drawn range.
func (s *server) graphP95(ctx context.Context, q *graphQuery, p *graphPlan, gs graphSite) (*GraphP95, error) {
if m, ok := s.wholeMonth(gs.site, p); ok && s.P95 != nil {
r, err := s.P95.P95(ctx, q.iface.ID, m.Label)
switch {
case err == nil:
return p95Graph(r), nil
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
return nil, err
case !errors.Is(err, p95.ErrNoData):
s.Logger.Warn("monthly p95 unavailable; computing live", "iface", q.iface.ID, "month", m.Label, "err", err)
}
}
return s.livep95(ctx, q, p, gs.hotStart)
}
// wholeMonth reports whether the planned range is one calendar month of the site's timezone, give or take a step.
func (s *server) wholeMonth(site string, p *graphPlan) (p95.Month, bool) {
tol := max(p.newest().step, p95Slot)
m := p95.MonthContaining(p.from.Add(tol), s.Zones.For(site))
near := func(a, b time.Time) bool { return a.Sub(b).Abs() <= tol }
return m, near(p.from, m.Start) && near(p.to, m.End)
}
// livep95 computes the 95th percentile of 5-minute average rates over the
// raw (hot) part of the drawn range, by nearest rank. M5 adds the materialised
// monthly value; until then the source is "live" and coverage says how much of
// the range's 5-minute slots had data, so a short history never passes for a full month.
func (s *server) livep95(ctx context.Context, q *graphQuery, p *graphPlan, hotStart time.Time) (*GraphP95, error) {
raw := p.newest()
if raw.tier != tierRaw {
return &GraphP95{Source: string(p95.SourceLive), Partial: true}, nil
}
start := ceilTo(latest(raw.from, hotStart, raw.bound.Add(-maxP95Span)), p95Slot)
end := floorTo(raw.bound, p95Slot)
expected := max(int64(p.to.Sub(p.from)/p95Slot), 1)
out := &GraphP95{Source: string(p95.SourceLive)}
if end.Before(start) {
return out, nil
}
w := tierWindow{tier: tierRaw, from: start, last: end, bound: end, step: p95Slot}
coverage := 1.0
for _, dir := range q.dirs {
var vals []float64
expr := `rate(` + q.octets(dir) + `{iface=` + quote(q.iface.ID) + `}[` + secs(p95Slot) + `])*8`
err := s.fetchOne(ctx, "hot", expr, w, func(_ int64, v float64) { vals = append(vals, v) })
if err != nil {
return nil, err
}
coverage = min(coverage, float64(len(vals))/float64(expected))
if len(vals) == 0 {
continue
}
v := nearestRank(vals, p95Quantile)
if dir == dirIn {
out.InBPS = &v
} else {
out.OutBPS = &v
}
}
out.Coverage = min(coverage, 1)
out.Partial = out.Coverage < p95.LowCoverage
return out, nil
}
// latest returns the latest of ts.
func latest(ts ...time.Time) time.Time {
out := ts[0]
for _, t := range ts[1:] {
if t.After(out) {
out = t
}
}
return out
}