Skip to content

Commit ee4b934

Browse files
authored
Add Users Last Seen At (coder#4192)
1 parent b8ec5c7 commit ee4b934

File tree

20 files changed

+154
-17
lines changed

20 files changed

+154
-17
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,22 @@ func (q *fakeQuerier) UpdateUserStatus(_ context.Context, arg database.UpdateUse
19561956
return database.User{}, sql.ErrNoRows
19571957
}
19581958

1959+
func (q *fakeQuerier) UpdateUserLastSeenAt(_ context.Context, arg database.UpdateUserLastSeenAtParams) (database.User, error) {
1960+
q.mutex.Lock()
1961+
defer q.mutex.Unlock()
1962+
1963+
for index, user := range q.users {
1964+
if user.ID != arg.ID {
1965+
continue
1966+
}
1967+
user.LastSeenAt = arg.LastSeenAt
1968+
user.UpdatedAt = arg.UpdatedAt
1969+
q.users[index] = user
1970+
return user, nil
1971+
}
1972+
return database.User{}, sql.ErrNoRows
1973+
}
1974+
19591975
func (q *fakeQuerier) UpdateUserHashedPassword(_ context.Context, arg database.UpdateUserHashedPasswordParams) error {
19601976
q.mutex.Lock()
19611977
defer q.mutex.Unlock()

coderd/database/dump.sql

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE ONLY users
2+
DROP COLUMN last_seen_at;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE ONLY users
2+
ADD COLUMN last_seen_at timestamp NOT NULL DEFAULT '0001-01-01 00:00:00+00:00';

coderd/database/models.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/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: 52 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/users.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ SET
157157
WHERE
158158
id = $1 RETURNING *;
159159

160+
-- name: UpdateUserLastSeenAt :one
161+
UPDATE
162+
users
163+
SET
164+
last_seen_at = $2,
165+
updated_at = $3
166+
WHERE
167+
id = $1 RETURNING *;
168+
160169

161170
-- name: GetAuthorizationUserRoles :one
162171
-- This function returns roles for authorization purposes. Implied member roles

coderd/httpmw/apikey.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,22 @@ func ExtractAPIKey(cfg ExtractAPIKeyConfig) func(http.Handler) http.Handler {
317317
return
318318
}
319319
}
320+
321+
// We only want to update this occasionally to reduce DB write
322+
// load. We update alongside the UserLink and APIKey since it's
323+
// easier on the DB to colocate writes.
324+
_, err = cfg.DB.UpdateUserLastSeenAt(ctx, database.UpdateUserLastSeenAtParams{
325+
ID: key.UserID,
326+
LastSeenAt: database.Now(),
327+
UpdatedAt: database.Now(),
328+
})
329+
if err != nil {
330+
write(http.StatusInternalServerError, codersdk.Response{
331+
Message: internalErrorMessage,
332+
Detail: fmt.Sprintf("update user last_seen_at: %s", err.Error()),
333+
})
334+
return
335+
}
320336
}
321337

322338
// If the key is valid, we also fetch the user roles and status.

coderd/users.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,7 @@ func convertUser(user database.User, organizationIDs []uuid.UUID) codersdk.User
12101210
ID: user.ID,
12111211
Email: user.Email,
12121212
CreatedAt: user.CreatedAt,
1213+
LastSeenAt: user.LastSeenAt,
12131214
Username: user.Username,
12141215
Status: codersdk.UserStatus(user.Status),
12151216
OrganizationIDs: organizationIDs,

0 commit comments

Comments
 (0)