Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add HumanizeTimestamp; make ConvertToFloat exportable #654

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions helpers/templates/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
package templates

import (
"errors"
"fmt"
"math"
"strconv"
"time"

"github.com/prometheus/common/model"
)

func convertToFloat(i interface{}) (float64, error) {
var errNaNOrInf = errors.New("value is NaN or Inf")

func ConvertToFloat(i interface{}) (float64, error) {
switch v := i.(type) {
case float64:
return v, nil
Expand All @@ -41,8 +46,20 @@ func convertToFloat(i interface{}) (float64, error) {
}
}

func FloatToTime(v float64) (*time.Time, error) {
freak12techno marked this conversation as resolved.
Show resolved Hide resolved
if math.IsNaN(v) || math.IsInf(v, 0) {
return nil, errNaNOrInf
}
timestamp := v * 1e9
if timestamp > math.MaxInt64 || timestamp < math.MinInt64 {
return nil, fmt.Errorf("%v cannot be represented as a nanoseconds timestamp since it overflows int64", v)
}
t := model.TimeFromUnixNano(int64(timestamp)).Time().UTC()
return &t, nil
}

func HumanizeDuration(i interface{}) (string, error) {
v, err := convertToFloat(i)
v, err := ConvertToFloat(i)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -87,3 +104,20 @@ func HumanizeDuration(i interface{}) (string, error) {
}
return fmt.Sprintf("%.4g%ss", v, prefix), nil
}

func HumanizeTimestamp(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
}
101 changes: 101 additions & 0 deletions helpers/templates/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package templates

import (
"math"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -139,3 +140,103 @@ func TestHumanizeDurationSecondsInt(t *testing.T) {
})
}
}

func TestHumanizeTimestampInt(t *testing.T) {
tc := []struct {
name string
input int
freak12techno marked this conversation as resolved.
Show resolved Hide resolved
expected string
}{
{name: "zero", input: 0, expected: "1970-01-01 00:00:00 +0000 UTC"},
{name: "negative", input: -1, expected: "1969-12-31 23:59:59 +0000 UTC"},
{name: "one", input: 1, expected: "1970-01-01 00:00:01 +0000 UTC"},
{name: "past", input: 1234567, expected: "1970-01-15 06:56:07 +0000 UTC"},
{name: "future", input: 9223372036, expected: "2262-04-11 23:47:16 +0000 UTC"},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
result, err := HumanizeTimestamp(tt.input)
require.NoError(t, err)
require.Equal(t, tt.expected, result)
})
}
}

func TestHumanizeTimestampUint(t *testing.T) {
tc := []struct {
name string
input uint64
expected string
}{
{name: "zero", input: 0, expected: "1970-01-01 00:00:00 +0000 UTC"},
{name: "one", input: 1, expected: "1970-01-01 00:00:01 +0000 UTC"},
{name: "past", input: 1234567, expected: "1970-01-15 06:56:07 +0000 UTC"},
{name: "future", input: 9223372036, expected: "2262-04-11 23:47:16 +0000 UTC"},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
result, err := HumanizeTimestamp(tt.input)
require.NoError(t, err)
require.Equal(t, tt.expected, result)
})
}
}

func TestHumanizeTimestampError(t *testing.T) {
_, err := HumanizeTimestamp(math.MaxInt64)
require.Error(t, err)
}

func TestHumanizeTimestampInfNanString(t *testing.T) {
tc := []struct {
name string
input string
expected string
}{
{name: "infinity", input: "+Inf", expected: "+Inf"},
{name: "minus infinity", input: "-Inf", expected: "-Inf"},
{name: "NaN", input: "NaN", expected: "NaN"},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
result, err := HumanizeTimestamp(tt.input)
require.NoError(t, err)
require.Equal(t, tt.expected, result)
})
}
}

func TestHumanizeTimestampInfNanFloat(t *testing.T) {
tc := []struct {
name string
input float64
expected string
}{
{name: "infinity", input: math.Inf(1), expected: "+Inf"},
{name: "minus infinity", input: math.Inf(-1), expected: "-Inf"},
{name: "NaN", input: math.NaN(), expected: "NaN"},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
result, err := HumanizeTimestamp(tt.input)
require.NoError(t, err)
require.Equal(t, tt.expected, result)
})
}
}

func TestHumanizeTimestampSampleFloat64(t *testing.T) {
result, err := HumanizeTimestamp(1435065584.128)
require.NoError(t, err)
require.Equal(t, "2015-06-23 13:19:44.128 +0000 UTC", result)
}

func TestHumanizeTimestampSampleString(t *testing.T) {
result, err := HumanizeTimestamp(1435065584.128)
require.NoError(t, err)
require.Equal(t, "2015-06-23 13:19:44.128 +0000 UTC", result)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be a single test, but that's just nitpicking :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually yeah, can fix right away

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also done, should be better now. Thanks for suggesting!
Also found a bug in one of these tests (float64 instead of string), fixed it there as well