github.com/thanos-io/thanos@v0.32.5/pkg/store/storepb/query_hints.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package storepb
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  )
    10  
    11  func (m *QueryHints) toPromQL(labelMatchers []LabelMatcher) string {
    12  	grouping := m.Grouping.toPromQL()
    13  	matchers := MatchersToString(labelMatchers...)
    14  	queryRange := m.Range.toPromQL()
    15  
    16  	query := fmt.Sprintf("%s %s (%s%s)", m.Func.Name, grouping, matchers, queryRange)
    17  	// Remove double spaces if some expressions are missing.
    18  	return strings.Join(strings.Fields(query), " ")
    19  }
    20  
    21  func (m *Grouping) toPromQL() string {
    22  	if m == nil {
    23  		return ""
    24  	}
    25  
    26  	if len(m.Labels) == 0 {
    27  		return ""
    28  	}
    29  	var op string
    30  	if m.By {
    31  		op = "by"
    32  	} else {
    33  		op = "without"
    34  	}
    35  
    36  	return fmt.Sprintf("%s (%s)", op, strings.Join(m.Labels, ","))
    37  }
    38  
    39  func (m *Range) toPromQL() string {
    40  	if m == nil {
    41  		return ""
    42  	}
    43  
    44  	if m.Millis == 0 {
    45  		return ""
    46  	}
    47  	return fmt.Sprintf("[%dms]", m.Millis)
    48  }