| 1 | package metricsqlscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/ |
| 2 | |
| 3 | // Prettify returns prettified representation of MetricsQL query q. |
| 4 | func Prettifyscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Prettify().(q string) (string, error) { |
| 5 | e, err := parseInternalscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/parseInternal().(q) |
| 6 | if err != nil { |
| 7 | return "", err |
| 8 | } |
| 9 | e = removeParensExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/removeParensExpr().(e) |
| 10 | b := appendPrettifiedExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedExpr().(nil, e, 0, false) |
| 11 | return string(b), nil |
| 12 | } |
| 13 | |
| 14 | // maxPrettifiedLineLen is the maximum length of a single line returned by Prettify(). |
| 15 | // |
| 16 | // Actual lines may exceed the maximum length in some cases. |
| 17 | const maxPrettifiedLineLenscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/maxPrettifiedLineLen. = 80 |
| 18 | |
| 19 | func appendPrettifiedExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedExpr().(dst []byte, e Exprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Expr#, indent int, needParens bool) []byte { |
| 20 | dstLen := len(dst) |
| 21 | |
| 22 | // Try appending e to dst and check whether its length exceeds the maximum allowed line length. |
| 23 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 24 | if needParens { |
| 25 | dst = append(dst, '(') |
| 26 | } |
| 27 | dst = e.AppendStringscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Expr#AppendString.(dst) |
| 28 | if needParens { |
| 29 | dst = append(dst, ')') |
| 30 | } |
| 31 | if len(dst)-dstLen <= maxPrettifiedLineLenscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/maxPrettifiedLineLen. { |
| 32 | // There is no need in splitting the e string representation, since its' length doesn't exceed maxPrettifiedLineLen. |
| 33 | return dst |
| 34 | } |
| 35 | |
| 36 | // The e string representation exceeds maxPrettifiedLineLen. Split it into multiple lines. |
| 37 | dst = dst[:dstLen] |
| 38 | if needParens { |
| 39 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 40 | dst = append(dst, "(\n"...) |
| 41 | indent++ |
| 42 | } |
| 43 | switch t := e.(type) { |
| 44 | case *withExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/withExpr#: |
| 45 | // Put every WITH expression on a separate line |
| 46 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 47 | dst = append(dst, "WITH (\n"...) |
| 48 | indent++ |
| 49 | for _, wa := range t.Wasscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/withExpr#Was. { |
| 50 | dst = appendPrettifiedExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedExpr().(dst, wa, indent, false) |
| 51 | dst = append(dst, ",\n"...) |
| 52 | } |
| 53 | indent-- |
| 54 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 55 | dst = append(dst, ")\n"...) |
| 56 | dst = appendPrettifiedExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedExpr().(dst, t.Exprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/withExpr#Expr., indent, false) |
| 57 | case *withArgExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/withArgExpr#: |
| 58 | // Wrap long withArgExpr into `(...)` |
| 59 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 60 | dst = appendEscapedIdentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendEscapedIdent().(dst, t.Namescip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/withArgExpr#Name.) |
| 61 | if len(t.Argsscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/withArgExpr#Args.) > 0 { |
| 62 | dst = append(dst, '(') |
| 63 | dst = appendEscapedIdentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendEscapedIdent().(dst, t.Argsscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/withArgExpr#Args.[0]) |
| 64 | for _, arg := range t.Argsscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/withArgExpr#Args.[1:] { |
| 65 | dst = append(dst, ", "...) |
| 66 | dst = appendEscapedIdentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendEscapedIdent().(dst, arg) |
| 67 | } |
| 68 | dst = append(dst, ')') |
| 69 | } |
| 70 | dst = append(dst, " = (\n"...) |
| 71 | dst = appendPrettifiedExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedExpr().(dst, t.Exprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/withArgExpr#Expr., indent+1, false) |
| 72 | dst = append(dst, '\n') |
| 73 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 74 | dst = append(dst, ')') |
| 75 | case *BinaryOpExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/BinaryOpExpr#: |
| 76 | // Split: |
| 77 | // |
| 78 | // a op b |
| 79 | // |
| 80 | // into: |
| 81 | // |
| 82 | // foo |
| 83 | // op |
| 84 | // bar |
| 85 | if t.KeepMetricNamesscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/BinaryOpExpr#KeepMetricNames. { |
| 86 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 87 | dst = append(dst, "(\n"...) |
| 88 | indent++ |
| 89 | } |
| 90 | dst = appendPrettifiedExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedExpr().(dst, t.Leftscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/BinaryOpExpr#Left., indent, t.needLeftParensscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/BinaryOpExpr#needLeftParens().()) |
| 91 | dst = append(dst, '\n') |
| 92 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent+1) |
| 93 | dst = t.appendModifiersscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/BinaryOpExpr#appendModifiers().(dst) |
| 94 | dst = append(dst, '\n') |
| 95 | dst = appendPrettifiedExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedExpr().(dst, t.Rightscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/BinaryOpExpr#Right., indent, t.needRightParensscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/BinaryOpExpr#needRightParens().()) |
| 96 | if t.KeepMetricNamesscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/BinaryOpExpr#KeepMetricNames. { |
| 97 | indent-- |
| 98 | dst = append(dst, '\n') |
| 99 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 100 | dst = append(dst, ") keep_metric_names"...) |
| 101 | } |
| 102 | case *RollupExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/RollupExpr#: |
| 103 | // Split: |
| 104 | // |
| 105 | // q[d:s] offset off @ x |
| 106 | // |
| 107 | // into: |
| 108 | // |
| 109 | // ( |
| 110 | // q |
| 111 | // )[d:s] offset off @ x |
| 112 | dst = appendPrettifiedExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedExpr().(dst, t.Exprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/RollupExpr#Expr., indent, t.needParensscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/RollupExpr#needParens().()) |
| 113 | dst = t.appendModifiersscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/RollupExpr#appendModifiers().(dst) |
| 114 | case *AggrFuncExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/AggrFuncExpr#: |
| 115 | // Split: |
| 116 | // |
| 117 | // aggr_func(arg1, ..., argN) modifiers |
| 118 | // |
| 119 | // into: |
| 120 | // |
| 121 | // aggr_func( |
| 122 | // arg1, |
| 123 | // ... |
| 124 | // argN |
| 125 | // ) modifiers |
| 126 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 127 | dst = appendEscapedIdentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendEscapedIdent().(dst, t.Namescip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/AggrFuncExpr#Name.) |
| 128 | dst = appendPrettifiedFuncArgsscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedFuncArgs().(dst, indent, t.Argsscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/AggrFuncExpr#Args.) |
| 129 | dst = t.appendModifiersscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/AggrFuncExpr#appendModifiers().(dst) |
| 130 | case *FuncExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/FuncExpr#: |
| 131 | // Split: |
| 132 | // |
| 133 | // func(arg1, ..., argN) modifiers |
| 134 | // |
| 135 | // into: |
| 136 | // |
| 137 | // func( |
| 138 | // arg1, |
| 139 | // ... |
| 140 | // argN |
| 141 | // ) modifiers |
| 142 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 143 | dst = appendEscapedIdentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendEscapedIdent().(dst, t.Namescip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/FuncExpr#Name.) |
| 144 | dst = appendPrettifiedFuncArgsscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedFuncArgs().(dst, indent, t.Argsscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/FuncExpr#Args.) |
| 145 | dst = t.appendModifiersscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/FuncExpr#appendModifiers().(dst) |
| 146 | case *MetricExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/MetricExpr#: |
| 147 | // Split: |
| 148 | // |
| 149 | // metric{filters1 or ... or filtersN} |
| 150 | // |
| 151 | // into: |
| 152 | // |
| 153 | // metric{ |
| 154 | // filters1 |
| 155 | // or |
| 156 | // ... |
| 157 | // or |
| 158 | // filtersN |
| 159 | // } |
| 160 | lfss := t.labelFilterssscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/MetricExpr#labelFilterss. |
| 161 | offset := 0 |
| 162 | metricName := getMetricNameFromLabelFilterssscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/getMetricNameFromLabelFilterss().(lfss) |
| 163 | metricNamehasEscapedChars := hasEscapedCharsscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/hasEscapedChars().(metricName) |
| 164 | |
| 165 | if metricName != "" { |
| 166 | offset = 1 |
| 167 | } |
| 168 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 169 | if !metricNamehasEscapedChars { |
| 170 | dst = appendEscapedIdentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendEscapedIdent().(dst, metricName) |
| 171 | } |
| 172 | if isOnlyMetricNameInLabelFilterssscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/isOnlyMetricNameInLabelFilterss().(lfss) { |
| 173 | if metricNamehasEscapedChars { |
| 174 | dst = append(dst, '{') |
| 175 | dst = appendQuotedIdentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendQuotedIdent().(dst, metricName) |
| 176 | dst = append(dst, '}') |
| 177 | } |
| 178 | } else { |
| 179 | dst = append(dst, "{\n"...) |
| 180 | if metricNamehasEscapedChars { |
| 181 | dst = ifEscapedCharsAppendQuotedIdentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/ifEscapedCharsAppendQuotedIdent().(dst, metricName) |
| 182 | dst = append(dst, ',', ' ', '\n') |
| 183 | } |
| 184 | for i, lfs := range lfss { |
| 185 | lfs = lfs[offset:] |
| 186 | if len(lfs) == 0 { |
| 187 | continue |
| 188 | } |
| 189 | dst = appendPrettifiedLabelFiltersscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedLabelFilters().(dst, indent+1, lfs) |
| 190 | dst = append(dst, '\n') |
| 191 | if i+1 < len(lfss) && len(lfss[i+1]) > offset { |
| 192 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent+2) |
| 193 | dst = append(dst, "or\n"...) |
| 194 | } |
| 195 | } |
| 196 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 197 | dst = append(dst, '}') |
| 198 | } |
| 199 | default: |
| 200 | // marshal other expressions as is |
| 201 | dst = t.AppendStringscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Expr#AppendString.(dst) |
| 202 | } |
| 203 | if needParens { |
| 204 | indent-- |
| 205 | dst = append(dst, '\n') |
| 206 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 207 | dst = append(dst, ')') |
| 208 | } |
| 209 | return dst |
| 210 | } |
| 211 | |
| 212 | func appendPrettifiedFuncArgsscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedFuncArgs().(dst []byte, indent int, args []Exprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/Expr#) []byte { |
| 213 | dst = append(dst, "(\n"...) |
| 214 | for i, arg := range args { |
| 215 | dst = appendPrettifiedExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedExpr().(dst, arg, indent+1, false) |
| 216 | if i+1 < len(args) { |
| 217 | dst = append(dst, ',') |
| 218 | } |
| 219 | dst = append(dst, '\n') |
| 220 | } |
| 221 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 222 | dst = append(dst, ')') |
| 223 | return dst |
| 224 | } |
| 225 | |
| 226 | func appendPrettifiedLabelFiltersscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendPrettifiedLabelFilters().(dst []byte, indent int, lfs []*labelFilterExprscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/labelFilterExpr#) []byte { |
| 227 | dstLen := len(dst) |
| 228 | |
| 229 | // Try marshaling lfs into a single line |
| 230 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 231 | dst = appendLabelFilterExprsscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendLabelFilterExprs().(dst, lfs) |
| 232 | if len(dst)-dstLen <= maxPrettifiedLineLenscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/maxPrettifiedLineLen. { |
| 233 | return dst |
| 234 | } |
| 235 | |
| 236 | // Too long line - split it into multiple lines |
| 237 | dst = dst[:dstLen] |
| 238 | for i := range lfs { |
| 239 | dst = appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst, indent) |
| 240 | dst = lfs[i].AppendStringscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/labelFilterExpr#AppendString().(dst) |
| 241 | if i+1 < len(lfs) { |
| 242 | dst = append(dst, ",\n"...) |
| 243 | } |
| 244 | } |
| 245 | return dst |
| 246 | } |
| 247 | |
| 248 | func appendIndentscip-go gomod github.com/VictoriaMetrics/metricsql . `github.com/VictoriaMetrics/metricsql`/appendIndent().(dst []byte, indent int) []byte { |
| 249 | for range indent { |
| 250 | dst = append(dst, " "...) |
| 251 | } |
| 252 | return dst |
| 253 | } |