Skip to content

Commit d583aca

Browse files
authored
fix(coderd): workspaceapps: update last_used_at when workspace app reports stats (#11603)
- Adds a new query BatchUpdateLastUsedAt - Adds calls to BatchUpdateLastUsedAt in app stats handler upon flush - Passes a stats flush channel to apptest setup scaffolding and updates unit tests to assert modifications to LastUsedAt.
1 parent 5bfbf9f commit d583aca

File tree

16 files changed

+186
-15
lines changed

16 files changed

+186
-15
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,15 @@ func (q *querier) ArchiveUnusedTemplateVersions(ctx context.Context, arg databas
695695
return q.db.ArchiveUnusedTemplateVersions(ctx, arg)
696696
}
697697

698+
func (q *querier) BatchUpdateWorkspaceLastUsedAt(ctx context.Context, arg database.BatchUpdateWorkspaceLastUsedAtParams) error {
699+
// Could be any workspace and checking auth to each workspace is overkill for the purpose
700+
// of this function.
701+
if err := q.authorizeContext(ctx, rbac.ActionUpdate, rbac.ResourceWorkspace.All()); err != nil {
702+
return err
703+
}
704+
return q.db.BatchUpdateWorkspaceLastUsedAt(ctx, arg)
705+
}
706+
698707
func (q *querier) CleanTailnetCoordinators(ctx context.Context) error {
699708
if err := q.authorizeContext(ctx, rbac.ActionDelete, rbac.ResourceTailnetCoordinator); err != nil {
700709
return err

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,13 @@ func (s *MethodTestSuite) TestWorkspace() {
15491549
ID: ws.ID,
15501550
}).Asserts(ws, rbac.ActionUpdate).Returns()
15511551
}))
1552+
s.Run("BatchUpdateWorkspaceLastUsedAt", s.Subtest(func(db database.Store, check *expects) {
1553+
ws1 := dbgen.Workspace(s.T(), db, database.Workspace{})
1554+
ws2 := dbgen.Workspace(s.T(), db, database.Workspace{})
1555+
check.Args(database.BatchUpdateWorkspaceLastUsedAtParams{
1556+
IDs: []uuid.UUID{ws1.ID, ws2.ID},
1557+
}).Asserts(rbac.ResourceWorkspace.All(), rbac.ActionUpdate).Returns()
1558+
}))
15521559
s.Run("UpdateWorkspaceTTL", s.Subtest(func(db database.Store, check *expects) {
15531560
ws := dbgen.Workspace(s.T(), db, database.Workspace{})
15541561
check.Args(database.UpdateWorkspaceTTLParams{

coderd/database/dbmem/dbmem.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,31 @@ func (q *FakeQuerier) ArchiveUnusedTemplateVersions(_ context.Context, arg datab
963963
return archived, nil
964964
}
965965

966+
func (q *FakeQuerier) BatchUpdateWorkspaceLastUsedAt(_ context.Context, arg database.BatchUpdateWorkspaceLastUsedAtParams) error {
967+
err := validateDatabaseType(arg)
968+
if err != nil {
969+
return err
970+
}
971+
972+
q.mutex.Lock()
973+
defer q.mutex.Unlock()
974+
975+
// temporary map to avoid O(q.workspaces*arg.workspaceIds)
976+
m := make(map[uuid.UUID]struct{})
977+
for _, id := range arg.IDs {
978+
m[id] = struct{}{}
979+
}
980+
n := 0
981+
for i := 0; i < len(q.workspaces); i++ {
982+
if _, found := m[q.workspaces[i].ID]; !found {
983+
continue
984+
}
985+
q.workspaces[i].LastUsedAt = arg.LastUsedAt
986+
n++
987+
}
988+
return nil
989+
}
990+
966991
func (*FakeQuerier) CleanTailnetCoordinators(_ context.Context) error {
967992
return ErrUnimplemented
968993
}

coderd/database/dbmetrics/dbmetrics.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaces.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ SET
357357
WHERE
358358
id = $1;
359359

360+
-- name: BatchUpdateWorkspaceLastUsedAt :exec
361+
UPDATE
362+
workspaces
363+
SET
364+
last_used_at = @last_used_at
365+
WHERE
366+
id = ANY(@ids :: uuid[]);
367+
360368
-- name: GetDeploymentWorkspaceStats :one
361369
WITH workspaces_with_jobs AS (
362370
SELECT

coderd/httpapi/websocket.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"context"
55
"time"
66

7-
"cdr.dev/slog"
87
"nhooyr.io/websocket"
8+
9+
"cdr.dev/slog"
910
)
1011

1112
// Heartbeat loops to ping a WebSocket to keep it alive.

coderd/workspaceagents_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ func TestWorkspaceAgentExternalAuthListen(t *testing.T) {
16261626
cancel()
16271627
// We expect only 1
16281628
// In a failed test, you will likely see 9, as the last one
1629-
// gets cancelled.
1629+
// gets canceled.
16301630
require.Equal(t, 1, validateCalls, "validate calls duplicated on same token")
16311631
})
16321632
}

0 commit comments

Comments
 (0)