-
Notifications
You must be signed in to change notification settings - Fork 316
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
feat: add time template helpers #627
Merged
gotjosh
merged 9 commits into
prometheus:main
from
freak12techno:add-time-template-helpers
Jun 3, 2024
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9248aab
feat: add time template helpers
freak12techno 5bde41a
chore: probably fixed CI
freak12techno c5c1b1d
chore: revert gitignore
freak12techno 270171b
chore: fixed go.mod again
freak12techno 80e630e
Update helpers/templates/humamize_duration_test.go
freak12techno b9d8f7c
Update helpers/templates/humanize_duration.go
freak12techno 4f7cf6e
Update helpers/templates/convert_to_float.go
freak12techno 9f1953b
chore: review fixes
freak12techno b2fc541
Merge branch 'main' into add-time-template-helpers
freak12techno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2015 The Prometheus Authors | ||
freak12techno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package templates | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func convertToFloat(i interface{}) (float64, error) { | ||
freak12techno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
case time.Duration: | ||
return v.Seconds(), nil | ||
default: | ||
return 0, fmt.Errorf("can't convert %T to float", v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2015 The Prometheus Authors | ||
freak12techno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package templates | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHumanizeDurationSecondsFloat64(t *testing.T) { | ||
input := []float64{0, 1, 60, 3600, 86400, 86400 + 3600, -(86400*2 + 3600*3 + 60*4 + 5), 899.99} | ||
expected := []string{"0s", "1s", "1m 0s", "1h 0m 0s", "1d 0h 0m 0s", "1d 1h 0m 0s", "-2d 3h 4m 5s", "14m 59s"} | ||
|
||
for index, value := range input { | ||
result, err := HumanizeDuration(value) | ||
require.NoError(t, err) | ||
require.Equal(t, expected[index], result) | ||
} | ||
freak12techno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func TestHumanizeDurationSubsecondAndFractionalSecondsFloat64(t *testing.T) { | ||
input := []float64{.1, .0001, .12345, 60.1, 60.5, 1.2345, 12.345} | ||
expected := []string{"100ms", "100us", "123.5ms", "1m 0s", "1m 0s", "1.234s", "12.35s"} | ||
|
||
for index, value := range input { | ||
result, err := HumanizeDuration(value) | ||
require.NoError(t, err) | ||
require.Equal(t, expected[index], result) | ||
} | ||
} | ||
|
||
func TestHumanizeDurationErrorString(t *testing.T) { | ||
_, err := HumanizeDuration("one") | ||
require.Error(t, err) | ||
} | ||
|
||
func TestHumanizeDurationSecondsString(t *testing.T) { | ||
input := []string{"0", "1", "60", "3600", "86400"} | ||
expected := []string{"0s", "1s", "1m 0s", "1h 0m 0s", "1d 0h 0m 0s"} | ||
|
||
for index, value := range input { | ||
result, err := HumanizeDuration(value) | ||
require.NoError(t, err) | ||
require.Equal(t, expected[index], result) | ||
} | ||
} | ||
|
||
func TestHumanizeDurationSubsecondAndFractionalSecondsString(t *testing.T) { | ||
input := []string{".1", ".0001", ".12345", "60.1", "60.5", "1.2345", "12.345"} | ||
expected := []string{"100ms", "100us", "123.5ms", "1m 0s", "1m 0s", "1.234s", "12.35s"} | ||
|
||
for index, value := range input { | ||
result, err := HumanizeDuration(value) | ||
require.NoError(t, err) | ||
require.Equal(t, expected[index], result) | ||
} | ||
} | ||
|
||
func TestHumanizeDurationSecondsInt(t *testing.T) { | ||
input := []int{0, -1, 1, 1234567} | ||
expected := []string{"0s", "-1s", "1s", "14d 6h 56m 7s"} | ||
|
||
for index, value := range input { | ||
result, err := HumanizeDuration(value) | ||
require.NoError(t, err) | ||
require.Equal(t, expected[index], result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2015 The Prometheus Authors | ||
freak12techno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package templates | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
|
||
func HumanizeDuration(i interface{}) (string, error) { | ||
v, err := convertToFloat(i) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if math.IsNaN(v) || math.IsInf(v, 0) { | ||
return fmt.Sprintf("%.4g", v), nil | ||
} | ||
if v == 0 { | ||
return fmt.Sprintf("%.4gs", v), nil | ||
} | ||
if math.Abs(v) >= 1 { | ||
sign := "" | ||
if v < 0 { | ||
sign = "-" | ||
v = -v | ||
} | ||
duration := int64(v) | ||
seconds := duration % 60 | ||
minutes := (duration / 60) % 60 | ||
hours := (duration / 60 / 60) % 24 | ||
days := duration / 60 / 60 / 24 | ||
// For days to minutes, we display seconds as an integer. | ||
if days != 0 { | ||
return fmt.Sprintf("%s%dd %dh %dm %ds", sign, days, hours, minutes, seconds), nil | ||
} | ||
if hours != 0 { | ||
return fmt.Sprintf("%s%dh %dm %ds", sign, hours, minutes, seconds), nil | ||
} | ||
if minutes != 0 { | ||
return fmt.Sprintf("%s%dm %ds", sign, minutes, seconds), nil | ||
} | ||
// For seconds, we display 4 significant digits. | ||
return fmt.Sprintf("%s%.4gs", sign, v), nil | ||
} | ||
prefix := "" | ||
for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} { | ||
if math.Abs(v) >= 1 { | ||
break | ||
} | ||
prefix = p | ||
v *= 1000 | ||
} | ||
return fmt.Sprintf("%.4g%ss", v, prefix), nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should have a single file containing everything, we call it
time.go
underhelpers/template/time.go
then we can have the tests file adjusted to betime_test.go
.