@@ -73,41 +73,6 @@ class PromQLBinaryExpression extends PromQLExpression {
73
73
}
74
74
}
75
75
76
- export enum ComparisonOperators {
77
- EQUALS = '==' ,
78
- NOT_EQUALS = '!=' ,
79
- GREATER_THAN = '>' ,
80
- LESS_THAN = '<' ,
81
- GREATER_THAN_OR_EQUALS = '>=' ,
82
- LESS_THAN_OR_EQUALS = '<=' ,
83
- }
84
-
85
- class PromQLComparisonExpression extends PromQLExpression {
86
-
87
- constructor ( private operator : ComparisonOperators , private left : PromQLExpression , private right : PromQLExpression ) {
88
- super ( ) ;
89
- }
90
-
91
- stringify ( ) {
92
- return `${ this . left . stringify ( ) } ${ this . operator } ${ this . right . stringify ( ) } ` ;
93
- }
94
- }
95
-
96
- enum LogicalOperators {
97
- AND = 'and' ,
98
- OR = 'or' ,
99
- }
100
-
101
- class PromQLLogicalExpression extends PromQLExpression {
102
- constructor ( private operator : LogicalOperators , private left : PromQLExpression , private right : PromQLExpression ) {
103
- super ( ) ;
104
- }
105
-
106
- stringify ( ) {
107
- return `${ this . left . stringify ( ) } ${ this . operator } (${ this . right . stringify ( ) } ) ` ;
108
- }
109
- }
110
-
111
76
112
77
export abstract class PromQLVectorExpression extends PromQLExpression {
113
78
@@ -148,6 +113,41 @@ export abstract class PromQLVectorExpression extends PromQLExpression {
148
113
}
149
114
}
150
115
116
+ enum LogicalOperators {
117
+ AND = 'and' ,
118
+ OR = 'or' ,
119
+ }
120
+
121
+ class PromQLLogicalExpression extends PromQLVectorExpression {
122
+ constructor ( private operator : LogicalOperators , private left : PromQLExpression , private right : PromQLExpression ) {
123
+ super ( ) ;
124
+ }
125
+
126
+ stringify ( ) {
127
+ return `${ this . left . stringify ( ) } ${ this . operator } (${ this . right . stringify ( ) } ) ` ;
128
+ }
129
+ }
130
+
131
+ export enum ComparisonOperators {
132
+ EQUALS = '==' ,
133
+ NOT_EQUALS = '!=' ,
134
+ GREATER_THAN = '>' ,
135
+ LESS_THAN = '<' ,
136
+ GREATER_THAN_OR_EQUALS = '>=' ,
137
+ LESS_THAN_OR_EQUALS = '<=' ,
138
+ }
139
+
140
+ class PromQLComparisonExpression extends PromQLVectorExpression {
141
+
142
+ constructor ( private operator : ComparisonOperators , private left : PromQLExpression , private right : PromQLExpression ) {
143
+ super ( ) ;
144
+ }
145
+
146
+ stringify ( ) {
147
+ return `${ this . left . stringify ( ) } ${ this . operator } ${ this . right . stringify ( ) } ` ;
148
+ }
149
+ }
150
+
151
151
class PromQLScalarExpression extends PromQLVectorExpression {
152
152
constructor ( private scalar : number , private left ?: PromQLExpression ) {
153
153
super ( ) ;
@@ -260,20 +260,58 @@ class PromQLWithoutExpression extends PromQLExpression {
260
260
}
261
261
}
262
262
263
+ class PromQLRangeVectorExpression extends PromQLVectorExpression {
264
+
265
+ constructor ( private vectorExpression : PromQLVectorExpression , private range : string ) {
266
+ super ( )
267
+ }
268
+
269
+ stringify ( ) : string {
270
+ return `${ this . vectorExpression . stringify ( ) } [${ this . range } ]`
271
+ }
272
+ }
273
+
274
+ abstract class PromQLFunctionArg {
275
+ abstract stringify ( ) : string
276
+ }
277
+
278
+ class PromQLVectorExpressionFunctionArg extends PromQLFunctionArg {
279
+ constructor ( private arg : PromQLVectorExpression ) {
280
+ super ( )
281
+ }
282
+
283
+ stringify ( ) : string {
284
+ return this . arg . stringify ( )
285
+ }
286
+ }
287
+
288
+ class PromQLStringFunctionArg extends PromQLFunctionArg {
289
+ constructor ( private arg : string ) {
290
+ super ( )
291
+ }
292
+
293
+ stringify ( ) : string {
294
+ return `"${ this . arg } "`
295
+ }
296
+ }
297
+
263
298
class PromQLFunction extends PromQLVectorExpression {
264
299
265
- constructor ( private name : string , private subExpression : PromQLExpression ) {
300
+ constructor ( private name : string , private args : PromQLFunctionArg [ ] ) {
266
301
super ( ) ;
267
302
}
268
303
269
304
stringify ( ) {
270
305
return `${ this . name } (
271
- ${ this . subExpression . stringify ( ) } ${ this . stringifyInner ( ) }
306
+ ${ this . serializeArgs ( ) }
272
307
)` ;
273
308
}
274
309
275
- stringifyInner ( ) {
276
- return ''
310
+ private serializeArgs ( ) {
311
+ return this . args . map (
312
+ arg => arg . stringify ( )
313
+ )
314
+ . join ( ', ' )
277
315
}
278
316
}
279
317
@@ -298,46 +336,75 @@ class PromQLAggregationFunction extends PromQLFunction {
298
336
299
337
class PromQLSumFunction extends PromQLAggregationFunction {
300
338
301
- constructor ( subExpression : PromQLExpression ) {
302
- super ( 'sum' , subExpression ) ;
339
+ constructor ( subExpression : PromQLVectorExpression ) {
340
+ super ( 'sum' , [
341
+ new PromQLVectorExpressionFunctionArg ( subExpression )
342
+ ] ) ;
303
343
}
304
344
}
305
345
306
346
class PromQLMaxFunction extends PromQLAggregationFunction {
307
347
308
- constructor ( subExpression : PromQLExpression ) {
309
- super ( 'max' , subExpression ) ;
348
+ constructor ( subExpression : PromQLVectorExpression ) {
349
+ super ( 'max' , [
350
+ new PromQLVectorExpressionFunctionArg ( subExpression )
351
+ ] ) ;
310
352
}
311
353
}
312
354
313
355
class PromQLGroupFunction extends PromQLAggregationFunction {
314
356
315
- constructor ( subExpression : PromQLExpression ) {
316
- super ( 'group' , subExpression ) ;
357
+ constructor ( subExpression : PromQLVectorExpression ) {
358
+ super ( 'group' , [
359
+ new PromQLVectorExpressionFunctionArg ( subExpression )
360
+ ] )
317
361
}
318
362
}
319
363
320
364
class PromQLCountFunction extends PromQLAggregationFunction {
321
365
322
- constructor ( subExpression : PromQLExpression ) {
323
- super ( 'count' , subExpression ) ;
366
+ constructor ( subExpression : PromQLVectorExpression ) {
367
+ super ( 'count' , [
368
+ new PromQLVectorExpressionFunctionArg ( subExpression )
369
+ ] )
324
370
}
325
371
}
326
372
327
373
class PromQLSortFunction extends PromQLFunction {
328
374
329
- constructor ( direction : 'asc' | 'desc' , subExpression : PromQLExpression ) {
330
- super ( `sort${ direction === 'desc' ? '_desc' : '' } ` , subExpression ) ;
375
+ constructor ( direction : 'asc' | 'desc' , subExpression : PromQLVectorExpression ) {
376
+ super ( `sort${ direction === 'desc' ? '_desc' : '' } ` , [
377
+ new PromQLVectorExpressionFunctionArg ( subExpression )
378
+ ] )
331
379
}
332
380
}
333
381
334
382
class PromQLRateFunction extends PromQLFunction {
335
- constructor ( private range : string , subExpression : PromQLExpression ) {
336
- super ( 'rate' , subExpression ) ;
383
+ constructor ( subExpression : PromQLRangeVectorExpression ) {
384
+ super ( 'rate' , [
385
+ new PromQLVectorExpressionFunctionArg ( subExpression )
386
+ ] )
337
387
}
388
+ }
389
+
390
+ class PromQLPresentOverTimeFunction extends PromQLFunction {
391
+
392
+ constructor ( subExpression : PromQLRangeVectorExpression ) {
393
+ super ( 'present_over_time' , [
394
+ new PromQLVectorExpressionFunctionArg ( subExpression )
395
+ ] )
396
+ }
397
+ }
338
398
339
- stringifyInner ( ) {
340
- return `[${ this . range } ]` ;
399
+ class PromQLLabelReplaceFunction extends PromQLFunction {
400
+ constructor ( exp : PromQLVectorExpression , dest : string , sourceLabel : string , replacement : string , regex : string ) {
401
+ super ( 'label_replace' , [
402
+ new PromQLVectorExpressionFunctionArg ( exp ) ,
403
+ new PromQLStringFunctionArg ( dest ) ,
404
+ new PromQLStringFunctionArg ( sourceLabel ) ,
405
+ new PromQLStringFunctionArg ( replacement ) ,
406
+ new PromQLStringFunctionArg ( regex ) ,
407
+ ] )
341
408
}
342
409
}
343
410
@@ -346,27 +413,39 @@ export class PromQL {
346
413
return new PromQLMetric ( name ) ;
347
414
}
348
415
349
- static sum ( subExpression : PromQLExpression ) {
416
+ static withRange ( subExpression : PromQLVectorExpression , range : string ) {
417
+ return new PromQLRangeVectorExpression ( subExpression , range )
418
+ }
419
+
420
+ static sum ( subExpression : PromQLVectorExpression ) {
350
421
return new PromQLSumFunction ( subExpression ) ;
351
422
}
352
423
353
- static sort ( direction : 'asc' | 'desc' , subExpression : PromQLExpression ) {
424
+ static sort ( direction : 'asc' | 'desc' , subExpression : PromQLVectorExpression ) {
354
425
return new PromQLSortFunction ( direction , subExpression ) ;
355
426
}
356
427
357
- static rate ( subExpression : PromQLExpression , range : string ) {
358
- return new PromQLRateFunction ( range , subExpression ) ;
428
+ static rate ( subExpression : PromQLRangeVectorExpression ) {
429
+ return new PromQLRateFunction ( subExpression ) ;
359
430
}
360
431
361
- static max ( subExpression : PromQLExpression ) {
432
+ static max ( subExpression : PromQLVectorExpression ) {
362
433
return new PromQLMaxFunction ( subExpression ) ;
363
434
}
364
435
365
- static group ( subExpression : PromQLExpression ) {
436
+ static group ( subExpression : PromQLVectorExpression ) {
366
437
return new PromQLGroupFunction ( subExpression ) ;
367
438
}
368
439
369
- static count ( subExpression : PromQLExpression ) {
440
+ static count ( subExpression : PromQLVectorExpression ) {
370
441
return new PromQLCountFunction ( subExpression ) ;
371
442
}
443
+
444
+ static presentOverTime ( subExpression : PromQLRangeVectorExpression ) {
445
+ return new PromQLPresentOverTimeFunction ( subExpression ) ;
446
+ }
447
+
448
+ static labelReplace ( exp : PromQLVectorExpression , dest : string , sourceLabel : string , replacement : string , regex : string ) {
449
+ return new PromQLLabelReplaceFunction ( exp , dest , sourceLabel , replacement , regex ) ;
450
+ }
372
451
}
0 commit comments