cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.13.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/parser/utils/absent.go

63lines · modecode

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