mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix(coderd): humanize duration on notifications (#14333)
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"golang.org/x/xerrors"
|
||||
@@ -323,6 +324,7 @@ func (e *Executor) runOnce(t time.Time) Stats {
|
||||
}
|
||||
}
|
||||
if shouldNotifyDormancy {
|
||||
dormantTime := dbtime.Now().Add(time.Duration(tmpl.TimeTilDormant))
|
||||
_, err = e.notificationsEnqueuer.Enqueue(
|
||||
e.ctx,
|
||||
ws.OwnerID,
|
||||
@@ -330,7 +332,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": humanize.Time(dormantTime),
|
||||
},
|
||||
"lifecycle_executor",
|
||||
ws.ID,
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -716,7 +716,20 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
"name": "bobby-workspace",
|
||||
"reason": "template updated to new dormancy policy",
|
||||
"dormancyHours": "24",
|
||||
"timeTilDormant": "24h",
|
||||
"timeTilDormant": "24 hours",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "TemplateWorkspaceMarkedForDeletionInOneWeek",
|
||||
id: notifications.TemplateWorkspaceMarkedForDeletion,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
Labels: map[string]string{
|
||||
"name": "bobby-workspace",
|
||||
"reason": "template updated to new dormancy policy",
|
||||
"dormancyHours": "168", // 168 hours = 7 days = 1 week
|
||||
"timeTilDormant": "1 week",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/xerrors"
|
||||
@@ -1064,6 +1065,7 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if initiatorErr == nil && tmplErr == nil {
|
||||
dormantTime := dbtime.Now().Add(time.Duration(tmpl.TimeTilDormant))
|
||||
_, err = api.NotificationsEnqueuer.Enqueue(
|
||||
ctx,
|
||||
workspace.OwnerID,
|
||||
@@ -1071,7 +1073,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": humanize.Time(dormantTime),
|
||||
},
|
||||
"api",
|
||||
workspace.ID,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"cdr.dev/slog"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/google/uuid"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
@@ -205,6 +206,7 @@ func (s *EnterpriseTemplateScheduleStore) Set(ctx context.Context, db database.S
|
||||
}
|
||||
|
||||
for _, ws := range markedForDeletion {
|
||||
dormantTime := dbtime.Now().Add(opts.TimeTilDormantAutoDelete)
|
||||
_, err = s.enqueuer.Enqueue(
|
||||
ctx,
|
||||
ws.OwnerID,
|
||||
@@ -212,7 +214,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": humanize.Time(dormantTime),
|
||||
},
|
||||
"scheduletemplate",
|
||||
// Associate this notification with all the related entities.
|
||||
|
||||
Reference in New Issue
Block a user