forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirst_last_over_time.go
253 lines (220 loc) · 6.55 KB
/
first_last_over_time.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package logql
import (
"math"
"time"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql"
"github.com/grafana/loki/v3/pkg/iter"
)
// newFirstWithTimestampIterator returns an iterator the returns the first value
// of a windowed aggregation.
func newFirstWithTimestampIterator(
it iter.PeekingSampleIterator,
selRange, step, start, end, offset int64,
) RangeVectorIterator {
inner := &batchRangeVectorIterator{
iter: it,
step: step,
end: end,
selRange: selRange,
metrics: map[string]labels.Labels{},
window: map[string]*promql.Series{},
agg: nil,
current: start - step, // first loop iteration will set it to start
offset: offset,
}
return &firstWithTimestampBatchRangeVectorIterator{
batchRangeVectorIterator: inner,
}
}
type firstWithTimestampBatchRangeVectorIterator struct {
*batchRangeVectorIterator
at []promql.Sample
}
// At aggregates the underlying window by picking the first sample with its
// timestamp.
func (r *firstWithTimestampBatchRangeVectorIterator) At() (int64, StepResult) {
if r.at == nil {
r.at = make([]promql.Sample, 0, len(r.window))
}
r.at = r.at[:0]
// convert ts from nano to milli seconds as the iterator work with nanoseconds
ts := r.current/1e+6 + r.offset/1e+6
for _, series := range r.window {
s := r.agg(series.Floats)
r.at = append(r.at, promql.Sample{
F: s.F,
T: s.T / int64(time.Millisecond),
Metric: series.Metric,
})
}
return ts, SampleVector(r.at)
}
// agg returns the first sample with its timestamp. The input is assumed to be
// in order.
func (r *firstWithTimestampBatchRangeVectorIterator) agg(samples []promql.FPoint) promql.FPoint {
if len(samples) == 0 {
return promql.FPoint{F: math.NaN(), T: 0}
}
return samples[0]
}
func newLastWithTimestampIterator(
it iter.PeekingSampleIterator,
selRange, step, start, end, offset int64,
) RangeVectorIterator {
inner := &batchRangeVectorIterator{
iter: it,
step: step,
end: end,
selRange: selRange,
metrics: map[string]labels.Labels{},
window: map[string]*promql.Series{},
agg: nil,
current: start - step, // first loop iteration will set it to start
offset: offset,
}
return &lastWithTimestampBatchRangeVectorIterator{
batchRangeVectorIterator: inner,
}
}
// lastWithTimestampBatchRangeVectorIterator returns an iterator that returns the
// last point in a windowed aggregation.
type lastWithTimestampBatchRangeVectorIterator struct {
*batchRangeVectorIterator
at []promql.Sample
}
// At aggregates the underlying window by picking the last sample with its
// timestamp.
func (r *lastWithTimestampBatchRangeVectorIterator) At() (int64, StepResult) {
if r.at == nil {
r.at = make([]promql.Sample, 0, len(r.window))
}
r.at = r.at[:0]
// convert ts from nano to milli seconds as the iterator work with nanoseconds
ts := r.current/1e+6 + r.offset/1e+6
for _, series := range r.window {
s := r.agg(series.Floats)
r.at = append(r.at, promql.Sample{
F: s.F,
T: s.T / int64(time.Millisecond),
Metric: series.Metric,
})
}
return ts, SampleVector(r.at)
}
// agg returns the last sample with its timestamp. The input is assumed to be
// in order.
func (r *lastWithTimestampBatchRangeVectorIterator) agg(samples []promql.FPoint) promql.FPoint {
if len(samples) == 0 {
return promql.FPoint{F: math.NaN(), T: 0}
}
return samples[len(samples)-1]
}
type mergeOverTimeStepEvaluator struct {
start, end, ts time.Time
step time.Duration
matrices []promql.Matrix
merge func(promql.Vector, int, int, promql.Series) promql.Vector
}
// Next returns the first or last element within one step of each matrix.
func (e *mergeOverTimeStepEvaluator) Next() (bool, int64, StepResult) {
var vec promql.Vector
e.ts = e.ts.Add(e.step)
if e.ts.After(e.end) {
return false, 0, nil
}
ts := e.ts.UnixNano() / int64(time.Millisecond)
// Merge other results
for i, m := range e.matrices {
for j, series := range m {
if len(series.Floats) == 0 || !e.inRange(series.Floats[0].T, ts) {
continue
}
vec = e.merge(vec, j, len(m), series)
e.pop(i, j)
}
}
// Align vector timestamps with step
for i := range vec {
vec[i].T = ts
}
return true, ts, SampleVector(vec)
}
// pop drops the float of the s'th series in the r'th matrix.
func (e *mergeOverTimeStepEvaluator) pop(r, s int) {
if len(e.matrices[r][s].Floats) <= 1 {
e.matrices[r][s].Floats = nil
return
}
e.matrices[r][s].Floats = e.matrices[r][s].Floats[1:]
}
// inRange returns true if t is in step range of ts.
func (e *mergeOverTimeStepEvaluator) inRange(t, ts int64) bool {
return (ts-e.step.Milliseconds()) <= t && t < ts
}
func (*mergeOverTimeStepEvaluator) Close() error { return nil }
func (*mergeOverTimeStepEvaluator) Error() error { return nil }
func NewMergeFirstOverTimeStepEvaluator(params Params, m []promql.Matrix) StepEvaluator {
if len(m) == 0 {
return EmptyEvaluator[SampleVector]{}
}
var (
start = params.Start()
end = params.End()
step = params.Step()
)
return &mergeOverTimeStepEvaluator{
start: start,
end: end,
ts: start.Add(-step), // will be corrected on first Next() call
step: step,
matrices: m,
merge: mergeFirstOverTime,
}
}
// mergeFirstOverTime selects the first sample by timestamp of each series.
func mergeFirstOverTime(vec promql.Vector, pos int, nSeries int, series promql.Series) promql.Vector {
if len(vec) < nSeries {
return append(vec, promql.Sample{
Metric: series.Metric,
T: series.Floats[0].T,
F: series.Floats[0].F,
})
} else if vec[pos].T > series.Floats[0].T {
vec[pos].F = series.Floats[0].F
vec[pos].T = series.Floats[0].T
}
return vec
}
func NewMergeLastOverTimeStepEvaluator(params Params, m []promql.Matrix) StepEvaluator {
if len(m) == 0 {
return EmptyEvaluator[SampleVector]{}
}
var (
start = params.Start()
end = params.End()
step = params.Step()
)
return &mergeOverTimeStepEvaluator{
start: start,
end: end,
ts: start.Add(-step), // will be corrected on first Next() call
step: step,
matrices: m,
merge: mergeLastOverTime,
}
}
// mergeLastOverTime selects the last sample by timestamp of each series.
func mergeLastOverTime(vec promql.Vector, pos int, nSeries int, series promql.Series) promql.Vector {
if len(vec) < nSeries {
return append(vec, promql.Sample{
Metric: series.Metric,
T: series.Floats[0].T,
F: series.Floats[0].F,
})
} else if vec[pos].T < series.Floats[0].T {
vec[pos].F = series.Floats[0].F
vec[pos].T = series.Floats[0].T
}
return vec
}