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
This commit is contained in:
Kyle Carberry
2023-03-07 08:25:04 -06:00
committed by GitHub
parent 66a6b590a1
commit 87ed7a7dba
6 changed files with 30 additions and 12 deletions

View File

@ -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)
@ -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]

View File

@ -5442,6 +5442,8 @@ SELECT
user_id
FROM
workspace_agent_stats
WHERE
connection_count > 0
GROUP BY
date, user_id
ORDER BY
@ -5483,7 +5485,8 @@ SELECT
FROM
workspace_agent_stats
WHERE
template_id = $1
template_id = $1 AND
connection_count > 0
GROUP BY
date, user_id
ORDER BY

View File

@ -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
@ -41,6 +42,8 @@ SELECT
user_id
FROM
workspace_agent_stats
WHERE
connection_count > 0
GROUP BY
date, user_id
ORDER BY

View File

@ -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)
}

View File

@ -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,
})
@ -935,7 +936,9 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
slog.F("payload", req),
)
if req.ConnectionCount > 0 {
activityBumpWorkspace(ctx, api.Logger.Named("activity_bump"), api.Database, workspace.ID)
}
payload, err := json.Marshal(req.ConnectionsByProto)
if err != nil {
@ -968,6 +971,7 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
return
}
if req.ConnectionCount > 0 {
err = api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
ID: workspace.ID,
LastUsedAt: now,
@ -976,6 +980,7 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
httpapi.InternalServerError(rw, err)
return
}
}
httpapi.Write(ctx, rw, http.StatusOK, agentsdk.StatsResponse{
ReportInterval: api.AgentStatsRefreshInterval,

View File

@ -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)