mirror of
https://github.com/prometheus/prometheus
synced 2024-12-26 08:33:06 +00:00
Merge pull request #14516 from freak12techno/use-humanize-duration-from-common
chore: use HumanizeDuration and ConvertToFloat from prometheus/common
This commit is contained in:
commit
f74d448c69
@ -23,7 +23,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
text_template "text/template"
|
text_template "text/template"
|
||||||
"time"
|
"time"
|
||||||
@ -106,25 +105,6 @@ func query(ctx context.Context, q string, ts time.Time, queryFn QueryFunc) (quer
|
|||||||
return result, nil
|
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.
|
// Expander executes templates in text or HTML mode with a common set of Prometheus template functions.
|
||||||
type Expander struct {
|
type Expander struct {
|
||||||
text string
|
text string
|
||||||
@ -219,7 +199,7 @@ func NewTemplateExpander(
|
|||||||
return host
|
return host
|
||||||
},
|
},
|
||||||
"humanize": func(i interface{}) (string, error) {
|
"humanize": func(i interface{}) (string, error) {
|
||||||
v, err := convertToFloat(i)
|
v, err := common_templates.ConvertToFloat(i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -248,7 +228,7 @@ func NewTemplateExpander(
|
|||||||
return fmt.Sprintf("%.4g%s", v, prefix), nil
|
return fmt.Sprintf("%.4g%s", v, prefix), nil
|
||||||
},
|
},
|
||||||
"humanize1024": func(i interface{}) (string, error) {
|
"humanize1024": func(i interface{}) (string, error) {
|
||||||
v, err := convertToFloat(i)
|
v, err := common_templates.ConvertToFloat(i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -267,30 +247,15 @@ func NewTemplateExpander(
|
|||||||
},
|
},
|
||||||
"humanizeDuration": common_templates.HumanizeDuration,
|
"humanizeDuration": common_templates.HumanizeDuration,
|
||||||
"humanizePercentage": func(i interface{}) (string, error) {
|
"humanizePercentage": func(i interface{}) (string, error) {
|
||||||
v, err := convertToFloat(i)
|
v, err := common_templates.ConvertToFloat(i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%.4g%%", v*100), nil
|
return fmt.Sprintf("%.4g%%", v*100), nil
|
||||||
},
|
},
|
||||||
"humanizeTimestamp": func(i interface{}) (string, error) {
|
"humanizeTimestamp": common_templates.HumanizeTimestamp,
|
||||||
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
|
|
||||||
},
|
|
||||||
"toTime": func(i interface{}) (*time.Time, error) {
|
"toTime": func(i interface{}) (*time.Time, error) {
|
||||||
v, err := convertToFloat(i)
|
v, err := common_templates.ConvertToFloat(i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user