Skip to content

Commit 10327fb

Browse files
fix(coderd): humanize duration on notifications (coder#14333)
1 parent 755afa3 commit 10327fb

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

coderd/autobuild/lifecycle_executor.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync/atomic"
99
"time"
1010

11+
"github.com/dustin/go-humanize"
1112
"github.com/google/uuid"
1213
"golang.org/x/sync/errgroup"
1314
"golang.org/x/xerrors"
@@ -323,14 +324,15 @@ func (e *Executor) runOnce(t time.Time) Stats {
323324
}
324325
}
325326
if shouldNotifyDormancy {
327+
dormantTime := dbtime.Now().Add(time.Duration(tmpl.TimeTilDormant))
326328
_, err = e.notificationsEnqueuer.Enqueue(
327329
e.ctx,
328330
ws.OwnerID,
329331
notifications.TemplateWorkspaceDormant,
330332
map[string]string{
331333
"name": ws.Name,
332334
"reason": "inactivity exceeded the dormancy threshold",
333-
"timeTilDormant": time.Duration(tmpl.TimeTilDormant).String(),
335+
"timeTilDormant": humanize.Time(dormantTime),
334336
},
335337
"lifecycle_executor",
336338
ws.ID,

coderd/notifications/notifications_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
691691
"reason": "breached the template's threshold for inactivity",
692692
"initiator": "autobuild",
693693
"dormancyHours": "24",
694-
"timeTilDormant": "24h",
694+
"timeTilDormant": "24 hours",
695695
},
696696
},
697697
},
@@ -716,7 +716,20 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
716716
"name": "bobby-workspace",
717717
"reason": "template updated to new dormancy policy",
718718
"dormancyHours": "24",
719-
"timeTilDormant": "24h",
719+
"timeTilDormant": "24 hours",
720+
},
721+
},
722+
},
723+
{
724+
name: "TemplateWorkspaceMarkedForDeletionInOneWeek",
725+
id: notifications.TemplateWorkspaceMarkedForDeletion,
726+
payload: types.MessagePayload{
727+
UserName: "bobby",
728+
Labels: map[string]string{
729+
"name": "bobby-workspace",
730+
"reason": "template updated to new dormancy policy",
731+
"dormancyHours": "168", // 168 hours = 7 days = 1 week
732+
"timeTilDormant": "1 week",
720733
},
721734
},
722735
},

coderd/workspaces.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strconv"
1212
"time"
1313

14+
"github.com/dustin/go-humanize"
1415
"github.com/go-chi/chi/v5"
1516
"github.com/google/uuid"
1617
"golang.org/x/xerrors"
@@ -1064,14 +1065,15 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) {
10641065
}
10651066

10661067
if initiatorErr == nil && tmplErr == nil {
1068+
dormantTime := dbtime.Now().Add(time.Duration(tmpl.TimeTilDormant))
10671069
_, err = api.NotificationsEnqueuer.Enqueue(
10681070
ctx,
10691071
workspace.OwnerID,
10701072
notifications.TemplateWorkspaceDormant,
10711073
map[string]string{
10721074
"name": workspace.Name,
10731075
"reason": "a " + initiator.Username + " request",
1074-
"timeTilDormant": time.Duration(tmpl.TimeTilDormant).String(),
1076+
"timeTilDormant": humanize.Time(dormantTime),
10751077
},
10761078
"api",
10771079
workspace.ID,

enterprise/coderd/schedule/template.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"cdr.dev/slog"
1010

11+
"github.com/dustin/go-humanize"
1112
"github.com/google/uuid"
1213
"go.opentelemetry.io/otel/attribute"
1314
"go.opentelemetry.io/otel/trace"
@@ -205,14 +206,15 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
205206
}
206207

207208
for _, ws := range markedForDeletion {
209+
dormantTime := dbtime.Now().Add(opts.TimeTilDormantAutoDelete)
208210
_, err = s.enqueuer.Enqueue(
209211
ctx,
210212
ws.OwnerID,
211213
notifications.TemplateWorkspaceMarkedForDeletion,
212214
map[string]string{
213215
"name": ws.Name,
214216
"reason": "an update to the template's dormancy",
215-
"timeTilDormant": opts.TimeTilDormantAutoDelete.String(),
217+
"timeTilDormant": humanize.Time(dormantTime),
216218
},
217219
"scheduletemplate",
218220
// Associate this notification with all the related entities.

0 commit comments

Comments
 (0)