Merge pull request #14516 from freak12techno/use-humanize-duration-from-common
Some checks are pending
buf.build / lint and publish (push) Waiting to run
CI / Go tests (push) Waiting to run
CI / More Go tests (push) Waiting to run
CI / Go tests with previous Go version (push) Waiting to run
CI / UI tests (push) Waiting to run
CI / Go tests on Windows (push) Waiting to run
CI / Mixins tests (push) Waiting to run
CI / Build Prometheus for common architectures (0) (push) Waiting to run
CI / Build Prometheus for common architectures (1) (push) Waiting to run
CI / Build Prometheus for common architectures (2) (push) Waiting to run
CI / Build Prometheus for all architectures (0) (push) Waiting to run
CI / Build Prometheus for all architectures (1) (push) Waiting to run
CI / Build Prometheus for all architectures (10) (push) Waiting to run
CI / Build Prometheus for all architectures (11) (push) Waiting to run
CI / Build Prometheus for all architectures (2) (push) Waiting to run
CI / Build Prometheus for all architectures (3) (push) Waiting to run
CI / Build Prometheus for all architectures (4) (push) Waiting to run
CI / Build Prometheus for all architectures (5) (push) Waiting to run
CI / Build Prometheus for all architectures (6) (push) Waiting to run
CI / Build Prometheus for all architectures (7) (push) Waiting to run
CI / Build Prometheus for all architectures (8) (push) Waiting to run
CI / Build Prometheus for all architectures (9) (push) Waiting to run
CI / Report status of build Prometheus for all architectures (push) Blocked by required conditions
CI / Check generated parser (push) Waiting to run
CI / golangci-lint (push) Waiting to run
CI / fuzzing (push) Waiting to run
CI / codeql (push) Waiting to run
CI / Publish main branch artifacts (push) Blocked by required conditions
CI / Publish release artefacts (push) Blocked by required conditions
CI / Publish UI on npm Registry (push) Blocked by required conditions
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run

chore: use HumanizeDuration and ConvertToFloat from prometheus/common
This commit is contained in:
Arthur Silva Sens 2024-08-07 14:01:03 -03:00 committed by GitHub
commit f74d448c69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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
} }