cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.31.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/parser/utils/rate.go

74lines · modecode

1package utils
2
3import (
4 promParser "github.com/prometheus/prometheus/promql/parser"
5 "github.com/rs/zerolog/log"
6
7 "github.com/cloudflare/pint/internal/parser"
8)
9
10func HasOuterRate(node *parser.PromQLNode) (calls []*promParser.Call) {
11 if n, ok := node.Node.(*promParser.Call); ok {
12 switch n.Func.Name {
13 case "rate", "irate", "deriv":
14 calls = append(calls, n)
15 return calls
16 case "ceil", "floor", "round":
17 return nil
18 }
19 }
20
21 if n, ok := node.Node.(*promParser.AggregateExpr); ok {
22 switch n.Op {
23 case promParser.COUNT:
24 return nil
25 case promParser.COUNT_VALUES:
26 return nil
27 }
28 }
29
30 if n, ok := node.Node.(*promParser.BinaryExpr); ok {
31 if n.VectorMatching != nil {
32 switch n.VectorMatching.Card {
33 case promParser.CardOneToOne:
34 case promParser.CardOneToMany:
35 for i, child := range node.Children {
36 if i == len(node.Children)-1 {
37 return HasOuterRate(child)
38 }
39 }
40 case promParser.CardManyToOne:
41 return HasOuterRate(node.Children[0])
42 case promParser.CardManyToMany:
43 default:
44 log.Warn().Str("matching", n.VectorMatching.Card.String()).Msg("Unsupported VectorMatching operation")
45 }
46 }
47
48 if n.Op.IsComparisonOperator() {
49 for i, child := range node.Children {
50 if i == 0 {
51 return HasOuterRate(child)
52 }
53 }
54 } else {
55 switch n.Op {
56 case promParser.LOR:
57 for _, child := range node.Children {
58 calls = append(calls, HasOuterRate(child)...)
59 }
60 return calls
61 case promParser.DIV, promParser.LUNLESS, promParser.LAND:
62 for _, child := range node.Children {
63 return HasOuterRate(child)
64 }
65 }
66 }
67 }
68
69 for _, child := range node.Children {
70 calls = append(calls, HasOuterRate(child)...)
71 }
72
73 return calls
74}
75