Skip to content

Commit 9f1953b

Browse files
committed
chore: review fixes
Signed-off-by: Sergey <freak12techno@gmail.com>
1 parent 4f7cf6e commit 9f1953b

File tree

4 files changed

+164
-121
lines changed

4 files changed

+164
-121
lines changed

‎helpers/templates/convert_to_float.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

‎helpers/templates/humamize_duration_test.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

‎helpers/templates/humanize_duration.go renamed to ‎helpers/templates/time.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,31 @@ package templates
1616
import (
1717
"fmt"
1818
"math"
19+
"strconv"
20+
"time"
1921
)
2022

23+
func convertToFloat(i interface{}) (float64, error) {
24+
switch v := i.(type) {
25+
case float64:
26+
return v, nil
27+
case string:
28+
return strconv.ParseFloat(v, 64)
29+
case int:
30+
return float64(v), nil
31+
case uint:
32+
return float64(v), nil
33+
case int64:
34+
return float64(v), nil
35+
case uint64:
36+
return float64(v), nil
37+
case time.Duration:
38+
return v.Seconds(), nil
39+
default:
40+
return 0, fmt.Errorf("can't convert %T to float", v)
41+
}
42+
}
43+
2144
func HumanizeDuration(i interface{}) (string, error) {
2245
v, err := convertToFloat(i)
2346
if err != nil {

‎helpers/templates/time_test.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright 2024 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package templates
15+
16+
import (
17+
"testing"
18+
19+
"github.com/stretchr/testify/require"
20+
)
21+
22+
func TestHumanizeDurationSecondsFloat64(t *testing.T) {
23+
tc := []struct {
24+
name string
25+
input float64
26+
expected string
27+
}{
28+
{name: "zero", input: 0, expected: "0s"},
29+
{name: "one second", input: 1, expected: "1s"},
30+
{name: "one minute", input: 60, expected: "1m 0s"},
31+
{name: "one hour", input: 3600, expected: "1h 0m 0s"},
32+
{name: "one day", input: 86400, expected: "1d 0h 0m 0s"},
33+
{name: "one day and one hour", input: 86400 + 3600, expected: "1d 1h 0m 0s"},
34+
{name: "negative duration", input: -(86400*2 + 3600*3 + 60*4 + 5), expected: "-2d 3h 4m 5s"},
35+
{name: "using a float", input: 899.99, expected: "14m 59s"},
36+
}
37+
38+
for _, tt := range tc {
39+
t.Run(tt.name, func(t *testing.T) {
40+
result, err := HumanizeDuration(tt.input)
41+
require.NoError(t, err)
42+
require.Equal(t, tt.expected, result)
43+
})
44+
}
45+
}
46+
47+
func TestHumanizeDurationSubsecondAndFractionalSecondsFloat64(t *testing.T) {
48+
tc := []struct {
49+
name string
50+
input float64
51+
expected string
52+
}{
53+
{name: "millseconds", input: .1, expected: "100ms"},
54+
{name: "nanoseconds", input: .0001, expected: "100us"},
55+
{name: "milliseconds + nanoseconds", input: .12345, expected: "123.5ms"},
56+
{name: "minute + millisecond", input: 60.1, expected: "1m 0s"},
57+
{name: "minute + milliseconds", input: 60.5, expected: "1m 0s"},
58+
{name: "second + milliseconds", input: 1.2345, expected: "1.234s"},
59+
{name: "second + milliseconds rounded", input: 12.345, expected: "12.35s"},
60+
}
61+
62+
for _, tt := range tc {
63+
t.Run(tt.name, func(t *testing.T) {
64+
result, err := HumanizeDuration(tt.input)
65+
require.NoError(t, err)
66+
require.Equal(t, tt.expected, result)
67+
})
68+
}
69+
}
70+
71+
func TestHumanizeDurationErrorString(t *testing.T) {
72+
_, err := HumanizeDuration("one")
73+
require.Error(t, err)
74+
}
75+
76+
func TestHumanizeDurationSecondsString(t *testing.T) {
77+
tc := []struct {
78+
name string
79+
input string
80+
expected string
81+
}{
82+
{name: "zero", input: "0", expected: "0s"},
83+
{name: "second", input: "1", expected: "1s"},
84+
{name: "minute", input: "60", expected: "1m 0s"},
85+
{name: "hour", input: "3600", expected: "1h 0m 0s"},
86+
{name: "day", input: "86400", expected: "1d 0h 0m 0s"},
87+
}
88+
89+
for _, tt := range tc {
90+
t.Run(tt.name, func(t *testing.T) {
91+
result, err := HumanizeDuration(tt.input)
92+
require.NoError(t, err)
93+
require.Equal(t, tt.expected, result)
94+
})
95+
}
96+
}
97+
98+
func TestHumanizeDurationSubsecondAndFractionalSecondsString(t *testing.T) {
99+
tc := []struct {
100+
name string
101+
input string
102+
expected string
103+
}{
104+
{name: "millseconds", input: ".1", expected: "100ms"},
105+
{name: "nanoseconds", input: ".0001", expected: "100us"},
106+
{name: "milliseconds + nanoseconds", input: ".12345", expected: "123.5ms"},
107+
{name: "minute + millisecond", input: "60.1", expected: "1m 0s"},
108+
{name: "minute + milliseconds", input: "60.5", expected: "1m 0s"},
109+
{name: "second + milliseconds", input: "1.2345", expected: "1.234s"},
110+
{name: "second + milliseconds rounded", input: "12.345", expected: "12.35s"},
111+
}
112+
113+
for _, tt := range tc {
114+
t.Run(tt.name, func(t *testing.T) {
115+
result, err := HumanizeDuration(tt.input)
116+
require.NoError(t, err)
117+
require.Equal(t, tt.expected, result)
118+
})
119+
}
120+
}
121+
122+
func TestHumanizeDurationSecondsInt(t *testing.T) {
123+
tc := []struct {
124+
name string
125+
input int
126+
expected string
127+
}{
128+
{name: "zero", input: 0, expected: "0s"},
129+
{name: "negative", input: -1, expected: "-1s"},
130+
{name: "second", input: 1, expected: "1s"},
131+
{name: "days", input: 1234567, expected: "14d 6h 56m 7s"},
132+
}
133+
134+
for _, tt := range tc {
135+
t.Run(tt.name, func(t *testing.T) {
136+
result, err := HumanizeDuration(tt.input)
137+
require.NoError(t, err)
138+
require.Equal(t, tt.expected, result)
139+
})
140+
}
141+
}

0 commit comments

Comments
 (0)