Skip to content

fix(coderd): humanize duration on notifications #14333

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

Merged
merged 4 commits into from
Aug 19, 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
3 changes: 2 additions & 1 deletion coderd/autobuild/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/coder/coder/v2/coderd/database/pubsub"
"github.com/coder/coder/v2/coderd/notifications"
"github.com/coder/coder/v2/coderd/schedule"
duration "github.com/coder/coder/v2/coderd/util/time"
"github.com/coder/coder/v2/coderd/wsbuilder"
)

Expand Down Expand Up @@ -330,7 +331,7 @@ func (e *Executor) runOnce(t time.Time) Stats {
map[string]string{
"name": ws.Name,
"reason": "inactivity exceeded the dormancy threshold",
"timeTilDormant": time.Duration(tmpl.TimeTilDormant).String(),
"timeTilDormant": duration.Humanize(time.Duration(tmpl.TimeTilDormant)),
},
"lifecycle_executor",
ws.ID,
Expand Down
4 changes: 2 additions & 2 deletions coderd/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
"reason": "breached the template's threshold for inactivity",
"initiator": "autobuild",
"dormancyHours": "24",
"timeTilDormant": "24h",
"timeTilDormant": "24 hours",
Copy link
Contributor

Choose a reason for hiding this comment

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

A test which includes not such a round number would also be helpful for illustrative purposes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added

},
},
},
Expand All @@ -716,7 +716,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
"name": "bobby-workspace",
"reason": "template updated to new dormancy policy",
"dormancyHours": "24",
"timeTilDormant": "24h",
"timeTilDormant": "24 hours",
},
},
},
Expand Down
44 changes: 44 additions & 0 deletions coderd/util/time/duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package duration

import (
"fmt"
"time"
)

type Unit struct {
value int
unit string
}

func Humanize(d time.Duration) string {
units := []Unit{
{int(d.Hours() / 24), "day"},
{int(d.Hours()) % 24, "hour"},
{int(d.Minutes()) % 60, "minute"},
{int(d.Seconds()) % 60, "second"},
}
nonZeroUnits := []Unit{}
for _, unit := range units {
if unit.value > 0 {
nonZeroUnits = append(nonZeroUnits, unit)
}
}
if len(nonZeroUnits) == 0 {
return "0 seconds"
}
var result string
for i, unit := range nonZeroUnits {
if i > 0 {
if i == len(nonZeroUnits)-1 {
result += " and "
} else {
result += ", "
}
}
result += fmt.Sprintf("%d %s", unit.value, unit.unit)
if unit.value > 1 {
result += "s"
}
}
return result
}
57 changes: 57 additions & 0 deletions coderd/util/time/duration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package duration_test

import (
"testing"
"time"

duration "github.com/coder/coder/v2/coderd/util/time"
)

func TestHumanize(t *testing.T) {
t.Parallel()

testCases := []struct {
duration time.Duration
expected string
}{
{
duration: time.Duration(0),
expected: "0 seconds",
},
{
duration: time.Duration(1 * time.Second),

Check failure on line 22 in coderd/util/time/duration_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
expected: "1 second",
},
{
duration: time.Duration(45 * time.Second),

Check failure on line 26 in coderd/util/time/duration_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
expected: "45 seconds",
},
{
duration: time.Duration(30 * time.Minute),

Check failure on line 30 in coderd/util/time/duration_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
expected: "30 minutes",
},
{
duration: time.Duration(30*time.Minute + 10*time.Second),

Check failure on line 34 in coderd/util/time/duration_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
expected: "30 minutes and 10 seconds",
},
{
duration: time.Duration(2*time.Hour + 25*time.Minute + 10*time.Second),

Check failure on line 38 in coderd/util/time/duration_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
expected: "2 hours, 25 minutes and 10 seconds",
},
{
duration: time.Duration(2400 * time.Hour),

Check failure on line 42 in coderd/util/time/duration_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
expected: "100 days",
},
}

for _, tc := range testCases {

Check failure on line 47 in coderd/util/time/duration_test.go

View workflow job for this annotation

GitHub Actions / lint

Range statement for test TestHumanize does not reinitialise the variable tc (paralleltest)
t.Run(tc.expected, func(t *testing.T) {
t.Parallel()

actual := duration.Humanize(tc.duration)
if actual != tc.expected {
t.Errorf("expected: %s, got: %s", tc.expected, actual)
}
})
}
}
3 changes: 2 additions & 1 deletion coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/coder/coder/v2/coderd/searchquery"
"github.com/coder/coder/v2/coderd/telemetry"
"github.com/coder/coder/v2/coderd/util/ptr"
duration "github.com/coder/coder/v2/coderd/util/time"
"github.com/coder/coder/v2/coderd/wsbuilder"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
Expand Down Expand Up @@ -1062,7 +1063,7 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) {
map[string]string{
"name": workspace.Name,
"reason": "a " + initiator.Username + " request",
"timeTilDormant": time.Duration(tmpl.TimeTilDormant).String(),
"timeTilDormant": duration.Humanize(time.Duration(tmpl.TimeTilDormant)),
},
"api",
workspace.ID,
Expand Down
3 changes: 2 additions & 1 deletion enterprise/coderd/schedule/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/coder/coder/v2/coderd/notifications"
agpl "github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/coderd/tracing"
duration "github.com/coder/coder/v2/coderd/util/time"
"github.com/coder/coder/v2/codersdk"
)

Expand Down Expand Up @@ -212,7 +213,7 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
map[string]string{
"name": ws.Name,
"reason": "an update to the template's dormancy",
"timeTilDormant": opts.TimeTilDormantAutoDelete.String(),
"timeTilDormant": duration.Humanize(opts.TimeTilDormantAutoDelete),
},
"scheduletemplate",
// Associate this notification with all the related entities.
Expand Down
Loading