parseThresholdAggregationMethod will parse a threshold condition expression's method. It assumes the provided input argument is already trimmed and cleaned up. If it encounters a percentile method, it will parse it and verify it boils down to an expression of the form: `p(float64)`, but will return
(input string)
| 188 | // boils down to an expression of the form: `p(float64)`, but will return |
| 189 | // it verbatim, as a string. |
| 190 | func parseThresholdAggregationMethod(input string) (string, null.Float, error) { |
| 191 | // Is the input one of the methods keywords? |
| 192 | for _, m := range aggregationMethodTokens { |
| 193 | // Percentile expressions being of the form p(value), |
| 194 | // they won't be matched here. |
| 195 | if m == input { |
| 196 | return m, null.Float{}, nil |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Otherwise, attempt to parse a percentile expression |
| 201 | if strings.HasPrefix(input, tokenPercentile+"(") && strings.HasSuffix(input, ")") { |
| 202 | aggregationValue, err := strconv.ParseFloat(trimDelimited("p(", input, ")"), 64) |
| 203 | if err != nil { |
| 204 | return "", null.Float{}, fmt.Errorf("malformed percentile value; reason: %w", err) |
| 205 | } |
| 206 | if math.IsNaN(aggregationValue) || aggregationValue < 0 || aggregationValue > 100 { |
| 207 | return "", null.Float{}, fmt.Errorf("malformed percentile value, provide a number between 0 and 100") |
| 208 | } |
| 209 | |
| 210 | return tokenPercentile, null.FloatFrom(aggregationValue), nil |
| 211 | } |
| 212 | |
| 213 | return "", null.Float{}, fmt.Errorf("failed parsing method from expression") |
| 214 | } |
| 215 | |
| 216 | func trimDelimited(prefix, input, suffix string) string { |
| 217 | return strings.TrimSuffix(strings.TrimPrefix(input, prefix), suffix) |
searching dependent graphs…