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

Skip to content

Commit 87ed7a7

Browse files
authored
chore: use nil map on agent stats to check if report interval should be returned (#6479)
See https://github.com/coder/coder/actions/runs/4350638262/jobs/7601537088
1 parent 66a6b59 commit 87ed7a7

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

coderd/database/dbfake/databasefake.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ func (q *fakeQuerier) GetTemplateDAUs(_ context.Context, templateID uuid.UUID) (
304304
if as.TemplateID != templateID {
305305
continue
306306
}
307+
if as.ConnectionCount == 0 {
308+
continue
309+
}
307310

308311
date := as.CreatedAt.Truncate(time.Hour * 24)
309312

@@ -341,6 +344,9 @@ func (q *fakeQuerier) GetDeploymentDAUs(_ context.Context) ([]database.GetDeploy
341344
seens := make(map[time.Time]map[uuid.UUID]struct{})
342345

343346
for _, as := range q.workspaceAgentStats {
347+
if as.ConnectionCount == 0 {
348+
continue
349+
}
344350
date := as.CreatedAt.Truncate(time.Hour * 24)
345351

346352
dateEntry := seens[date]

coderd/database/queries.sql.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagentstats.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ SELECT
2929
FROM
3030
workspace_agent_stats
3131
WHERE
32-
template_id = $1
32+
template_id = $1 AND
33+
connection_count > 0
3334
GROUP BY
3435
date, user_id
3536
ORDER BY
@@ -41,6 +42,8 @@ SELECT
4142
user_id
4243
FROM
4344
workspace_agent_stats
45+
WHERE
46+
connection_count > 0
4447
GROUP BY
4548
date, user_id
4649
ORDER BY

coderd/metricscache/metricscache_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func TestCache_TemplateUsers(t *testing.T) {
175175

176176
for _, row := range tt.args.rows {
177177
row.TemplateID = template.ID
178+
row.ConnectionCount = 1
178179
db.InsertWorkspaceAgentStat(context.Background(), row)
179180
}
180181

coderd/workspaceagents.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,8 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
921921
return
922922
}
923923

924-
if req.RxBytes == 0 && req.TxBytes == 0 {
924+
// An empty stat means it's just looking for the report interval.
925+
if req.ConnectionsByProto == nil {
925926
httpapi.Write(ctx, rw, http.StatusOK, agentsdk.StatsResponse{
926927
ReportInterval: api.AgentStatsRefreshInterval,
927928
})
@@ -935,7 +936,9 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
935936
slog.F("payload", req),
936937
)
937938

938-
activityBumpWorkspace(ctx, api.Logger.Named("activity_bump"), api.Database, workspace.ID)
939+
if req.ConnectionCount > 0 {
940+
activityBumpWorkspace(ctx, api.Logger.Named("activity_bump"), api.Database, workspace.ID)
941+
}
939942

940943
payload, err := json.Marshal(req.ConnectionsByProto)
941944
if err != nil {
@@ -968,13 +971,15 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
968971
return
969972
}
970973

971-
err = api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
972-
ID: workspace.ID,
973-
LastUsedAt: now,
974-
})
975-
if err != nil {
976-
httpapi.InternalServerError(rw, err)
977-
return
974+
if req.ConnectionCount > 0 {
975+
err = api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
976+
ID: workspace.ID,
977+
LastUsedAt: now,
978+
})
979+
if err != nil {
980+
httpapi.InternalServerError(rw, err)
981+
return
982+
}
978983
}
979984

980985
httpapi.Write(ctx, rw, http.StatusOK, agentsdk.StatsResponse{

codersdk/agentsdk/agentsdk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ func (c *Client) ReportStats(ctx context.Context, log slog.Logger, statsChan <-c
399399
}
400400

401401
// Send an empty stat to get the interval.
402-
postStat(&Stats{ConnectionsByProto: map[string]int64{}})
402+
postStat(&Stats{})
403403

404404
go func() {
405405
defer close(exited)

0 commit comments

Comments
 (0)