-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunction.go
More file actions
385 lines (300 loc) · 10.2 KB
/
Copy pathfunction.go
File metadata and controls
385 lines (300 loc) · 10.2 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright The Perses Authors
// Licensed under the Apache License, Version 2.0 (the \"License\");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an \"AS IS\" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package promqlbuilder
import (
"github.com/perses/promql-builder/matrix"
"github.com/prometheus/prometheus/promql/parser"
)
func NewFunction(name string, args ...parser.Expr) *parser.Call {
fn, ok := parser.Functions[name]
if !ok {
fn = &parser.Function{Name: name}
}
return &parser.Call{
Func: fn,
Args: args,
}
}
func NewNumber(num float64) *parser.NumberLiteral {
return &parser.NumberLiteral{
Val: num,
}
}
func NewString(s string) *parser.StringLiteral {
return &parser.StringLiteral{
Val: s,
}
}
func Abs(vector parser.Expr) *parser.Call {
return NewFunction("abs", vector)
}
func Absent(vector parser.Expr) *parser.Call {
return NewFunction("absent", vector)
}
type RangeVectorBuilder interface {
*matrix.Builder | *parser.SubqueryExpr
}
func convertToExpr[T RangeVectorBuilder](input T) parser.Expr {
switch v := any(input).(type) {
case *matrix.Builder:
return v
case *parser.SubqueryExpr:
return v
default:
panic("unsupported type")
}
}
func AbsentOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("absent_over_time", convertToExpr(input))
}
func Acos(vector parser.Expr) *parser.Call {
return NewFunction("acos", vector)
}
func Acosh(vector parser.Expr) *parser.Call {
return NewFunction("acosh", vector)
}
func Asin(vector parser.Expr) *parser.Call {
return NewFunction("asin", vector)
}
func Asinh(vector parser.Expr) *parser.Call {
return NewFunction("asinh", vector)
}
func Atan(vector parser.Expr) *parser.Call {
return NewFunction("atan", vector)
}
func Atanh(vector parser.Expr) *parser.Call {
return NewFunction("atanh", vector)
}
func AvgOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("avg_over_time", convertToExpr(input))
}
func Ceil(vector parser.Expr) *parser.Call {
return NewFunction("ceil", vector)
}
func Changes[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("changes", convertToExpr(input))
}
func Clamp(vector parser.Expr, min float64, max float64) *parser.Call {
return NewFunction("clamp", vector, NewNumber(min), NewNumber(max))
}
func ClampMax(vector parser.Expr, max float64) *parser.Call {
return NewFunction("clamp_max", vector, NewNumber(max))
}
func ClampMin(vector parser.Expr, min float64) *parser.Call {
return NewFunction("clamp_min", vector, NewNumber(min))
}
func Cos(vector parser.Expr) *parser.Call {
return NewFunction("cos", vector)
}
func Cosh(vector parser.Expr) *parser.Call {
return NewFunction("cosh", vector)
}
func CountOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("count_over_time", convertToExpr(input))
}
func DaysInMonth(vector parser.Expr) *parser.Call {
return NewFunction("days_in_month", vector)
}
func DaysOfMonth(vector parser.Expr) *parser.Call {
return NewFunction("days_of_month", vector)
}
func DaysOfWeek(vector parser.Expr) *parser.Call {
return NewFunction("days_of_week", vector)
}
func DaysOfYear(vector parser.Expr) *parser.Call {
return NewFunction("days_of_year", vector)
}
func Deg(vector parser.Expr) *parser.Call {
return NewFunction("deg", vector)
}
func Delta[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("delta", convertToExpr(input))
}
func Deriv[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("deriv", convertToExpr(input))
}
func Exp(vector parser.Expr) *parser.Call {
return NewFunction("exp", vector)
}
func Floor(vector parser.Expr) *parser.Call {
return NewFunction("floor", vector)
}
func HistogramAvg(vector parser.Expr) *parser.Call {
return NewFunction("histogram_avg", vector)
}
func HistogramCount(vector parser.Expr) *parser.Call {
return NewFunction("histogram_count", vector)
}
func HistogramSum(vector parser.Expr) *parser.Call {
return NewFunction("histogram_sum", vector)
}
func HistogramStddev(vector parser.Expr) *parser.Call {
return NewFunction("histogram_stddev", vector)
}
func HistogramStdvar(vector parser.Expr) *parser.Call {
return NewFunction("histogram_stdvar", vector)
}
func HistogramFraction(lower float64, upper float64, vector parser.Expr) *parser.Call {
return NewFunction("histogram_fraction", NewNumber(lower), NewNumber(upper), vector)
}
func HistogramQuantile(quantile float64, vector parser.Expr) *parser.Call {
return NewFunction("histogram_quantile", NewNumber(quantile), vector)
}
func HistogramQuantiles(vector parser.Expr, labelName string, quantiles ...float64) *parser.Call {
args := []parser.Expr{vector, NewString(labelName)}
for _, q := range quantiles {
args = append(args, NewNumber(q))
}
return NewFunction("histogram_quantiles", args...)
}
func DoubleExponentialSmoothing[T RangeVectorBuilder](input T, smoothingFactor float64, trendFactor float64) *parser.Call {
return NewFunction("double_exponential_smoothing", convertToExpr(input), NewNumber(smoothingFactor), NewNumber(trendFactor))
}
func Hour(vector parser.Expr) *parser.Call {
return NewFunction("hour", vector)
}
func IDelta[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("idelta", convertToExpr(input))
}
func Increase[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("increase", convertToExpr(input))
}
func Info(vector parser.Expr, dataLabelSelector parser.Expr) *parser.Call {
return NewFunction("info", vector, dataLabelSelector)
}
func IRate[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("irate", convertToExpr(input))
}
func LabelReplace(vector parser.Expr, destinationLabel string, replacement string, sourceLabel string, regexp string) *parser.Call {
return NewFunction("label_replace", vector, NewString(destinationLabel), NewString(replacement), NewString(sourceLabel), NewString(regexp))
}
func LabelJoin(vector parser.Expr, destinationLabel string, replacement string, srcLabels ...string) *parser.Call {
args := []parser.Expr{vector, NewString(destinationLabel), NewString(replacement)}
for _, label := range srcLabels {
args = append(args, NewString(label))
}
return NewFunction("label_join", args...)
}
func LastOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("last_over_time", convertToExpr(input))
}
func Ln(vector parser.Expr) *parser.Call {
return NewFunction("ln", vector)
}
func Log10(vector parser.Expr) *parser.Call {
return NewFunction("log10", vector)
}
func Log2(vector parser.Expr) *parser.Call {
return NewFunction("log2", vector)
}
func MadOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("mad_over_time", convertToExpr(input))
}
func MaxOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("max_over_time", convertToExpr(input))
}
func MinOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("min_over_time", convertToExpr(input))
}
func Minute(vector parser.Expr) *parser.Call {
return NewFunction("minute", vector)
}
func Month(vector parser.Expr) *parser.Call {
return NewFunction("month", vector)
}
func PI() *parser.Call {
return NewFunction("pi")
}
func PredictLinear[T RangeVectorBuilder](input T, t float64) *parser.Call {
return NewFunction("predict_linear", convertToExpr(input), NewNumber(t))
}
func PresentOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("present_over_time", convertToExpr(input))
}
func QuantileOverTime[T RangeVectorBuilder](t float64, input T) *parser.Call {
return NewFunction("quantile_over_time", NewNumber(t), convertToExpr(input))
}
func Rad(vector parser.Expr) *parser.Call {
return NewFunction("rad", vector)
}
func Rate[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("rate", convertToExpr(input))
}
func Resets[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("resets", convertToExpr(input))
}
func Round(vector parser.Expr, t float64) *parser.Call {
return NewFunction("round", vector, NewNumber(t))
}
func Scalar(vector parser.Expr) *parser.Call {
return NewFunction("scalar", vector)
}
func Sgn(vector parser.Expr) *parser.Call {
return NewFunction("sgn", vector)
}
func Sin(vector parser.Expr) *parser.Call {
return NewFunction("sin", vector)
}
func Sinh(vector parser.Expr) *parser.Call {
return NewFunction("sinh", vector)
}
func Sort(vector parser.Expr) *parser.Call {
return NewFunction("sort", vector)
}
func SortDesc(vector parser.Expr) *parser.Call {
return NewFunction("sort_desc", vector)
}
func SortByLabel(vector parser.Expr, labels ...string) *parser.Call {
args := []parser.Expr{vector}
for _, label := range labels {
args = append(args, NewString(label))
}
return NewFunction("sort_by_label", args...)
}
func SortByLabelDesc(vector parser.Expr, labels ...string) *parser.Call {
args := []parser.Expr{vector}
for _, label := range labels {
args = append(args, NewString(label))
}
return NewFunction("sort_by_label_desc", args...)
}
func Sqrt(vector parser.Expr) *parser.Call {
return NewFunction("sqrt", vector)
}
func StddevOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("stddev_over_time", convertToExpr(input))
}
func StdvarOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("stdvar_over_time", convertToExpr(input))
}
func SumOverTime[T RangeVectorBuilder](input T) *parser.Call {
return NewFunction("sum_over_time", convertToExpr(input))
}
func Tan(vector parser.Expr) *parser.Call {
return NewFunction("tan", vector)
}
func Tanh(vector parser.Expr) *parser.Call {
return NewFunction("tanh", vector)
}
func Time() *parser.Call {
return NewFunction("time")
}
func Timestamp(vector parser.Expr) *parser.Call {
return NewFunction("timestamp", vector)
}
func Vector(scalar float64) *parser.Call {
return NewFunction("vector", NewNumber(scalar))
}
func Year(vector parser.Expr) *parser.Call {
return NewFunction("year", vector)
}