Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72d6731

Browse files
authored
fix: Only update workspace LastUsed when the connection payload has changed (#4115)
This was causing every workspace to update last used to time.Now() when coderd was restarted!
1 parent 153e96f commit 72d6731

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,33 @@ 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+
found := false
167+
latest := database.AgentStat{}
168+
for _, agentStat := range q.agentStats {
169+
if agentStat.AgentID != agentID {
170+
continue
171+
}
172+
if !found {
173+
latest = agentStat
174+
found = true
175+
continue
176+
}
177+
if agentStat.CreatedAt.After(latest.CreatedAt) {
178+
latest = agentStat
179+
found = true
180+
continue
181+
}
182+
}
183+
if !found {
184+
return database.AgentStat{}, sql.ErrNoRows
185+
}
186+
return latest, nil
187+
}
188+
162189
func (q *fakeQuerier) GetTemplateDAUs(_ context.Context, templateID uuid.UUID) ([]database.GetTemplateDAUsRow, error) {
163190
q.mutex.Lock()
164191
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)