-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
series.go
54 lines (40 loc) · 1.23 KB
/
series.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package sketch
import (
"github.com/prometheus/prometheus/promql/parser"
"github.com/grafana/loki/v3/pkg/logproto"
)
const ValueTypeTopKMatrix = "topk_matrix"
type TopKVector struct {
topk *Topk
ts uint64
}
// TopkMatrix is `promql.Value` and `parser.Value`
type TopKMatrix []TopKVector
// Type implements `promql.Value` and `parser.Value`
func (TopKMatrix) Type() parser.ValueType { return ValueTypeTopKMatrix }
// String implements `promql.Value` and `parser.Value`
func (TopKMatrix) String() string {
return ""
}
func (s TopKMatrix) ToProto() (*logproto.TopKMatrix, error) {
points := make([]*logproto.TopKMatrix_Vector, 0, len(s))
for _, point := range s {
topk, err := point.topk.ToProto()
if err != nil {
return nil, err
}
points = append(points, &logproto.TopKMatrix_Vector{Topk: topk, TimestampMs: int64(point.ts)})
}
return &logproto.TopKMatrix{Values: points}, nil
}
func TopKMatrixFromProto(proto *logproto.TopKMatrix) (TopKMatrix, error) {
values := make(TopKMatrix, 0, len(proto.Values))
for _, vector := range proto.Values {
topk, err := TopkFromProto(vector.Topk)
if err != nil {
return nil, err
}
values = append(values, TopKVector{topk, uint64(vector.TimestampMs)})
}
return values, nil
}