Update to return a float

Signed-off-by: David Leadbeater <dgl@dgl.cx>
pull/9716/head
David Leadbeater 3 years ago committed by Julien Pivotto
parent 89ebb3dcf2
commit c0c5f3227b

@ -74,7 +74,7 @@ versions.
| reReplaceAll | pattern, replacement, text | string | [Regexp.ReplaceAllString](https://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. | | reReplaceAll | pattern, replacement, text | string | [Regexp.ReplaceAllString](https://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
| graphLink | expr | string | Returns path to graph view in the [expression browser](https://prometheus.io/docs/visualization/browser/) for the expression. | | graphLink | expr | string | Returns path to graph view in the [expression browser](https://prometheus.io/docs/visualization/browser/) for the expression. |
| tableLink | expr | string | Returns path to tabular ("Table") view in the [expression browser](https://prometheus.io/docs/visualization/browser/) for the expression. | | tableLink | expr | string | Returns path to tabular ("Table") view in the [expression browser](https://prometheus.io/docs/visualization/browser/) for the expression. |
| parseDuration | string | string | Parses a duration string such as "1h" into the integer number of seconds it represents. | | parseDuration | string | float | Parses a duration string such as "1h" into the number of seconds it represents. |
### Others ### Others

@ -304,13 +304,12 @@ func NewTemplateExpander(
"externalURL": func() string { "externalURL": func() string {
return externalURL.String() return externalURL.String()
}, },
"parseDuration": func(d string) (string, error) { "parseDuration": func(d string) (float64, error) {
v, err := model.ParseDuration(d) v, err := model.ParseDuration(d)
if err != nil { if err != nil {
return "", err return 0, err
} }
dur := int64(time.Duration(v) / time.Second) return float64(time.Duration(v)) / float64(time.Second), nil
return fmt.Sprintf("%d", dur), nil
}, },
}, },
options: options, options: options,

@ -450,9 +450,9 @@ func TestTemplateExpansion(t *testing.T) {
output: "http://testhost:9090/path/prefix", output: "http://testhost:9090/path/prefix",
}, },
{ {
// parseDuration. // parseDuration (using printf to ensure the return is a string).
text: "{{ parseDuration \"1h2m10ms\" }}", text: "{{ printf \"%0.2f\" (parseDuration \"1h2m10ms\") }}",
output: "3720", output: "3720.01",
}, },
} }

Loading…
Cancel
Save