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

Skip to content

chore: use nil map on agent stats to check if report interval should be returned #6479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions coderd/database/dbfake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ func (q *fakeQuerier) GetTemplateDAUs(_ context.Context, templateID uuid.UUID) (
if as.TemplateID != templateID {
continue
}
if as.ConnectionCount == 0 {
continue
}

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

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

for _, as := range q.workspaceAgentStats {
if as.ConnectionCount == 0 {
continue
}
date := as.CreatedAt.Truncate(time.Hour * 24)

dateEntry := seens[date]
Expand Down
5 changes: 4 additions & 1 deletion coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion coderd/database/queries/workspaceagentstats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ SELECT
FROM
workspace_agent_stats
WHERE
template_id = $1
template_id = $1 AND
connection_count > 0
GROUP BY
date, user_id
ORDER BY
Expand All @@ -41,6 +42,8 @@ SELECT
user_id
FROM
workspace_agent_stats
WHERE
connection_count > 0
GROUP BY
date, user_id
ORDER BY
Expand Down
1 change: 1 addition & 0 deletions coderd/metricscache/metricscache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func TestCache_TemplateUsers(t *testing.T) {

for _, row := range tt.args.rows {
row.TemplateID = template.ID
row.ConnectionCount = 1
db.InsertWorkspaceAgentStat(context.Background(), row)
}

Expand Down
23 changes: 14 additions & 9 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,8 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
return
}

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

activityBumpWorkspace(ctx, api.Logger.Named("activity_bump"), api.Database, workspace.ID)
if req.ConnectionCount > 0 {
activityBumpWorkspace(ctx, api.Logger.Named("activity_bump"), api.Database, workspace.ID)
}

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

err = api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
ID: workspace.ID,
LastUsedAt: now,
})
if err != nil {
httpapi.InternalServerError(rw, err)
return
if req.ConnectionCount > 0 {
err = api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
ID: workspace.ID,
LastUsedAt: now,
})
if err != nil {
httpapi.InternalServerError(rw, err)
return
}
}

httpapi.Write(ctx, rw, http.StatusOK, agentsdk.StatsResponse{
Expand Down
2 changes: 1 addition & 1 deletion codersdk/agentsdk/agentsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func (c *Client) ReportStats(ctx context.Context, log slog.Logger, statsChan <-c
}

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

go func() {
defer close(exited)
Expand Down