github.com/kiali/kiali@v1.84.0/tracing/tempo/traceQLBuilder.go (about)

     1  package tempo
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  )
     8  
     9  type operandType string
    10  
    11  const (
    12  	AND      operandType = "&&"
    13  	OR       operandType = "||"
    14  	EQUAL    operandType = "="
    15  	NOTEQUAL operandType = "!="
    16  	REGEX    operandType = "=~"
    17  )
    18  
    19  type TraceQL struct {
    20  	operator1 interface{}
    21  	operand   operandType
    22  	operator2 interface{}
    23  }
    24  
    25  // Groups are ()
    26  type Group struct {
    27  	group   []TraceQL
    28  	operand operandType
    29  }
    30  
    31  // Subqueries are {}
    32  type Subquery struct {
    33  	trace TraceQL
    34  }
    35  
    36  func printOperator(operator interface{}) string {
    37  
    38  	queryString := ""
    39  	valueType := reflect.TypeOf(operator)
    40  
    41  	switch valueType.String() {
    42  	case "tempo.Subquery":
    43  		queryString = fmt.Sprintf("{ %s }", printOperator(operator.(Subquery).trace))
    44  	case "tempo.Group":
    45  		queryString = "( "
    46  		for i, op := range operator.(Group).group {
    47  			queryString += fmt.Sprintf(" %s ", printOperator(op))
    48  			if i < len(operator.(Group).group)-1 {
    49  				queryString += fmt.Sprintf(" %s ", operator.(Group).operand)
    50  			}
    51  		}
    52  		queryString += ")"
    53  	case "tempo.TraceQL":
    54  		if operator.(TraceQL).operator1 != nil {
    55  			if reflect.TypeOf(operator.(TraceQL).operator2).String() == "string" {
    56  				queryString = fmt.Sprintf("%s %s \"%s\" ", operator.(TraceQL).operator1,
    57  					operator.(TraceQL).operand, operator.(TraceQL).operator2)
    58  			} else {
    59  				if reflect.TypeOf(operator.(TraceQL).operator2).String() == "tempo.unquoted" {
    60  					queryString = fmt.Sprintf("%s %s %s ", operator.(TraceQL).operator1,
    61  						operator.(TraceQL).operand, operator.(TraceQL).operator2)
    62  				} else {
    63  					queryString = fmt.Sprintf("%s %s %s ", printOperator(operator.(TraceQL).operator1),
    64  						operator.(TraceQL).operand, printOperator(operator.(TraceQL).operator2))
    65  				}
    66  
    67  			}
    68  
    69  		}
    70  	case "string":
    71  		queryString = fmt.Sprintf("%s", operator)
    72  	}
    73  	return queryString
    74  }
    75  
    76  func printSelect(fields []string) string {
    77  	selects := strings.Join(fields, ", ")
    78  	return fmt.Sprintf("select(%s)", selects)
    79  }