cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.50.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/parser/utils/rate.go

75lines · modecode

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