Skip to content

Commit 5091b58

Browse files
committed
fix: Only update workspace LastUsed when the connection payload has changed
This was causing every workspace to update last used to time.Now() when coderd was restarted!
1 parent 29d804e commit 5091b58

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ func (q *fakeQuerier) InsertAgentStat(_ context.Context, p database.InsertAgentS
159159
return stat, nil
160160
}
161161

162+
func (q *fakeQuerier) GetLatestAgentStat(_ context.Context, agentID uuid.UUID) (database.AgentStat, error) {
163+
q.mutex.RLock()
164+
defer q.mutex.RUnlock()
165+
166+
var latest *database.AgentStat
167+
for _, agentStat := range q.agentStats {
168+
if agentStat.AgentID != agentID {
169+
continue
170+
}
171+
if latest == nil {
172+
latest = &agentStat
173+
continue
174+
}
175+
if agentStat.CreatedAt.After(latest.CreatedAt) {
176+
latest = &agentStat
177+
continue
178+
}
179+
}
180+
if latest == nil {
181+
return database.AgentStat{}, sql.ErrNoRows
182+
}
183+
return *latest, nil
184+
}
185+
162186
func (q *fakeQuerier) GetTemplateDAUs(_ context.Context, templateID uuid.UUID) ([]database.GetTemplateDAUsRow, error) {
163187
q.mutex.Lock()
164188
defer q.mutex.Unlock()

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/agentstats.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ INSERT INTO
1212
VALUES
1313
($1, $2, $3, $4, $5, $6, $7) RETURNING *;
1414

15+
-- name: GetLatestAgentStat :one
16+
SELECT * FROM agent_stats WHERE agent_id = $1 ORDER BY created_at DESC LIMIT 1;
17+
1518
-- name: GetTemplateDAUs :many
1619
select
1720
(created_at at TIME ZONE 'UTC')::date as date,

coderd/workspaceagents.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,10 +795,22 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
795795
}
796796
defer conn.Close(websocket.StatusAbnormalClosure, "")
797797

798+
var lastReport codersdk.AgentStatsReportResponse
799+
latestStat, err := api.Database.GetLatestAgentStat(r.Context(), workspaceAgent.ID)
800+
if err == nil {
801+
err = json.Unmarshal(latestStat.Payload, &lastReport)
802+
if err != nil {
803+
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
804+
Message: "Failed to unmarshal stat payload.",
805+
Detail: err.Error(),
806+
})
807+
return
808+
}
809+
}
810+
798811
// Allow overriding the stat interval for debugging and testing purposes.
799812
ctx := r.Context()
800813
timer := time.NewTicker(api.AgentStatsRefreshInterval)
801-
var lastReport codersdk.AgentStatsReportResponse
802814
for {
803815
err := wsjson.Write(ctx, conn, codersdk.AgentStatsReportRequest{})
804816
if err != nil {

0 commit comments

Comments
 (0)