Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lx9FNLG2CqeBbVGGd9bFsP
135 lines
3.6 KiB
Go
135 lines
3.6 KiB
Go
package p95
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math"
|
|
"regexp"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/v10networks/nmon/internal/longq"
|
|
)
|
|
|
|
// Querier is the slice of *vmsup.Querier this package reads through.
|
|
type Querier interface {
|
|
Query(ctx context.Context, store, q string, ts time.Time) ([]byte, error)
|
|
QueryRange(ctx context.Context, store, q string, from, to time.Time, step time.Duration) ([]byte, error)
|
|
}
|
|
|
|
const (
|
|
longStore = "long"
|
|
// readChunk bounds interfaces per range query, so a response (two directions
|
|
// of a 31-day month at 5 minutes) stays far below the Querier's size cap.
|
|
readChunk = 25
|
|
)
|
|
|
|
// Key identifies one directed interface series.
|
|
type Key struct{ Iface, Dir string }
|
|
|
|
// Reader reads 5-minute rates back from the long store.
|
|
type Reader struct{ q Querier }
|
|
|
|
// NewReader returns a Reader over q.
|
|
func NewReader(q Querier) *Reader { return &Reader{q: q} }
|
|
|
|
var idRE = regexp.MustCompile(`^[0-9A-Za-z]+$`)
|
|
|
|
// Rates returns, for each interface in ids and each direction, the 5-minute
|
|
// rates of the windows starting in [from, to), oldest first. from must be
|
|
// window-aligned. Interfaces without data are absent from the map.
|
|
func (r *Reader) Rates(ctx context.Context, ids []string, from, to time.Time) (map[Key][]Point, error) {
|
|
out := map[Key][]Point{}
|
|
for lo := 0; lo < len(ids); lo += readChunk {
|
|
chunk := ids[lo:min(lo+readChunk, len(ids))]
|
|
for _, id := range chunk {
|
|
if !idRE.MatchString(id) {
|
|
return nil, fmt.Errorf("p95: interface id %q is not a plain identifier", id)
|
|
}
|
|
}
|
|
sel := fmt.Sprintf(`{__name__=%q,iface=~"%s"}`, longq.MetricRate5m, strings.Join(chunk, "|"))
|
|
last := to.Add(-Window)
|
|
if last.Before(from) {
|
|
return out, nil
|
|
}
|
|
body, err := r.q.QueryRange(ctx, longStore, longq.LatestVersion(sel), from, last, Window)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("p95: read rates: %w", err)
|
|
}
|
|
rows, err := parseRows(body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, row := range rows {
|
|
k := Key{Iface: row.labels["iface"], Dir: row.labels["dir"]}
|
|
out[k] = append(out[k], row.pts...)
|
|
}
|
|
}
|
|
for k := range out {
|
|
pts := out[k]
|
|
sort.Slice(pts, func(a, b int) bool { return pts[a].TS < pts[b].TS })
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
type row struct {
|
|
labels map[string]string
|
|
pts []Point
|
|
}
|
|
|
|
// parseRows decodes an instant or range response. Non-finite values are
|
|
// dropped: NaN means no data, not zero.
|
|
func parseRows(body []byte) ([]row, error) {
|
|
var resp struct {
|
|
Status string `json:"status"`
|
|
Error string `json:"error"`
|
|
Data struct {
|
|
Result []struct {
|
|
Metric map[string]string `json:"metric"`
|
|
Value []any `json:"value"`
|
|
Values [][]any `json:"values"`
|
|
} `json:"result"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(body, &resp); err != nil {
|
|
return nil, fmt.Errorf("p95: decode store response: %w", err)
|
|
}
|
|
if resp.Status != "success" {
|
|
return nil, fmt.Errorf("p95: store query failed: %s", resp.Error)
|
|
}
|
|
rows := make([]row, 0, len(resp.Data.Result))
|
|
for _, res := range resp.Data.Result {
|
|
rw := row{labels: res.Metric}
|
|
raw := res.Values
|
|
if len(res.Value) == 2 {
|
|
raw = append(raw, res.Value)
|
|
}
|
|
for _, pair := range raw {
|
|
if p, ok := decodePoint(pair); ok {
|
|
rw.pts = append(rw.pts, p)
|
|
}
|
|
}
|
|
rows = append(rows, rw)
|
|
}
|
|
return rows, nil
|
|
}
|
|
|
|
func decodePoint(pair []any) (Point, bool) {
|
|
if len(pair) != 2 {
|
|
return Point{}, false
|
|
}
|
|
t, ok1 := pair[0].(float64)
|
|
s, ok2 := pair[1].(string)
|
|
if !ok1 || !ok2 {
|
|
return Point{}, false
|
|
}
|
|
v, err := strconv.ParseFloat(s, 64)
|
|
if err != nil || math.IsNaN(v) || math.IsInf(v, 0) {
|
|
return Point{}, false
|
|
}
|
|
return Point{TS: int64(math.Round(t * 1000)), V: v}, true
|
|
}
|