|
|
|
@ -23,7 +23,6 @@ import (
|
|
|
|
|
"net"
|
|
|
|
|
"net/url"
|
|
|
|
|
"sort"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
text_template "text/template"
|
|
|
|
|
"time"
|
|
|
|
@ -106,25 +105,6 @@ func query(ctx context.Context, q string, ts time.Time, queryFn QueryFunc) (quer
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func convertToFloat(i interface{}) (float64, error) {
|
|
|
|
|
switch v := i.(type) {
|
|
|
|
|
case float64:
|
|
|
|
|
return v, nil
|
|
|
|
|
case string:
|
|
|
|
|
return strconv.ParseFloat(v, 64)
|
|
|
|
|
case int:
|
|
|
|
|
return float64(v), nil
|
|
|
|
|
case uint:
|
|
|
|
|
return float64(v), nil
|
|
|
|
|
case int64:
|
|
|
|
|
return float64(v), nil
|
|
|
|
|
case uint64:
|
|
|
|
|
return float64(v), nil
|
|
|
|
|
default:
|
|
|
|
|
return 0, fmt.Errorf("can't convert %T to float", v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expander executes templates in text or HTML mode with a common set of Prometheus template functions.
|
|
|
|
|
type Expander struct {
|
|
|
|
|
text string
|
|
|
|
@ -219,7 +199,7 @@ func NewTemplateExpander(
|
|
|
|
|
return host
|
|
|
|
|
},
|
|
|
|
|
"humanize": func(i interface{}) (string, error) {
|
|
|
|
|
v, err := convertToFloat(i)
|
|
|
|
|
v, err := common_templates.ConvertToFloat(i)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
@ -248,7 +228,7 @@ func NewTemplateExpander(
|
|
|
|
|
return fmt.Sprintf("%.4g%s", v, prefix), nil
|
|
|
|
|
},
|
|
|
|
|
"humanize1024": func(i interface{}) (string, error) {
|
|
|
|
|
v, err := convertToFloat(i)
|
|
|
|
|
v, err := common_templates.ConvertToFloat(i)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
@ -267,30 +247,15 @@ func NewTemplateExpander(
|
|
|
|
|
},
|
|
|
|
|
"humanizeDuration": common_templates.HumanizeDuration,
|
|
|
|
|
"humanizePercentage": func(i interface{}) (string, error) {
|
|
|
|
|
v, err := convertToFloat(i)
|
|
|
|
|
v, err := common_templates.ConvertToFloat(i)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("%.4g%%", v*100), nil
|
|
|
|
|
},
|
|
|
|
|
"humanizeTimestamp": func(i interface{}) (string, error) {
|
|
|
|
|
v, err := convertToFloat(i)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tm, err := floatToTime(v)
|
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, errNaNOrInf):
|
|
|
|
|
return fmt.Sprintf("%.4g", v), nil
|
|
|
|
|
case err != nil:
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Sprint(tm), nil
|
|
|
|
|
},
|
|
|
|
|
"humanizeTimestamp": common_templates.HumanizeTimestamp,
|
|
|
|
|
"toTime": func(i interface{}) (*time.Time, error) {
|
|
|
|
|
v, err := convertToFloat(i)
|
|
|
|
|
v, err := common_templates.ConvertToFloat(i)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|