Skip to content

Commit f0c0418

Browse files
committed
db: GetWorkspaceAgentStatsAndLabels
1 parent d0b8398 commit f0c0418

File tree

7 files changed

+129
-53
lines changed

7 files changed

+129
-53
lines changed

coderd/database/dbauthz/system.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ func (q *querier) GetWorkspaceAgentStats(ctx context.Context, createdAfter time.
302302
return q.db.GetWorkspaceAgentStats(ctx, createdAfter)
303303
}
304304

305+
func (q *querier) GetWorkspaceAgentStatsAndLabels(ctx context.Context, createdAfter time.Time) ([]database.GetWorkspaceAgentStatsAndLabelsRow, error) {
306+
return q.db.GetWorkspaceAgentStatsAndLabels(ctx, createdAfter)
307+
}
308+
305309
func (q *querier) GetDeploymentWorkspaceStats(ctx context.Context) (database.GetDeploymentWorkspaceStatsRow, error) {
306310
return q.db.GetDeploymentWorkspaceStats(ctx)
307311
}

coderd/database/dbfake/databasefake.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3998,6 +3998,10 @@ func (q *fakeQuerier) GetWorkspaceAgentStats(_ context.Context, createdAfter tim
39983998
return stats, nil
39993999
}
40004000

4001+
func (q *fakeQuerier) GetWorkspaceAgentStatsAndLabels(_ context.Context, createdAfter time.Time) ([]database.GetWorkspaceAgentStatsAndLabelsRow, error) {
4002+
panic("not implemented yet")
4003+
}
4004+
40014005
func (q *fakeQuerier) GetWorkspacesEligibleForAutoStartStop(ctx context.Context, now time.Time) ([]database.Workspace, error) {
40024006
q.mutex.RLock()
40034007
defer q.mutex.RUnlock()

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: 85 additions & 0 deletions
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,38 @@ WITH agent_stats AS (
103103
) AS a WHERE a.rn = 1 GROUP BY a.user_id, a.agent_id, a.workspace_id, a.template_id
104104
)
105105
SELECT * FROM agent_stats JOIN latest_agent_stats ON agent_stats.agent_id = latest_agent_stats.agent_id;
106+
107+
-- name: GetWorkspaceAgentStatsAndLabels :many
108+
WITH agent_stats AS (
109+
SELECT
110+
user_id,
111+
agent_id,
112+
workspace_id,
113+
coalesce(SUM(rx_bytes), 0)::bigint AS workspace_rx_bytes,
114+
coalesce(SUM(tx_bytes), 0)::bigint AS workspace_tx_bytes
115+
FROM workspace_agent_stats
116+
WHERE workspace_agent_stats.created_at > $1
117+
GROUP BY user_id, agent_id, workspace_id
118+
), latest_agent_stats AS (
119+
SELECT
120+
a.agent_id,
121+
coalesce(SUM(session_count_vscode), 0)::bigint AS session_count_vscode,
122+
coalesce(SUM(session_count_ssh), 0)::bigint AS session_count_ssh,
123+
coalesce(SUM(session_count_jetbrains), 0)::bigint AS session_count_jetbrains,
124+
coalesce(SUM(session_count_reconnecting_pty), 0)::bigint AS session_count_reconnecting_pty,
125+
coalesce(SUM(connection_count), 0)::bigint AS connection_count,
126+
coalesce(SUM(connection_median_latency_ms), 0)::float AS connection_median_latency_ms
127+
FROM (
128+
SELECT *, ROW_NUMBER() OVER(PARTITION BY agent_id ORDER BY created_at DESC) AS rn
129+
FROM workspace_agent_stats
130+
-- The greater than 0 is to support legacy agents that don't report connection_median_latency_ms.
131+
WHERE created_at > $1 AND connection_median_latency_ms > 0
132+
) AS a
133+
WHERE a.rn = 1
134+
GROUP BY a.user_id, a.agent_id, a.workspace_id
135+
)
136+
SELECT users.username, workspace_agents.name AS agent_name, workspaces.name AS workspace_name, workspace_rx_bytes, workspace_tx_bytes, session_count_vscode, session_count_ssh, session_count_jetbrains, session_count_reconnecting_pty, connection_count, connection_median_latency_ms
137+
FROM agent_stats JOIN latest_agent_stats ON agent_stats.agent_id = latest_agent_stats.agent_id
138+
JOIN users ON users.id = agent_stats.user_id
139+
JOIN workspace_agents ON workspace_agents.id = agent_stats.agent_id
140+
JOIN workspaces ON workspaces.id = agent_stats.workspace_id;

coderd/prometheusmetrics/agentstats.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

coderd/prometheusmetrics/prometheusmetrics.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,6 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
329329
goto done
330330
}
331331

332-
db.GetWorkspAgents
333-
334332
done:
335333
logger.Debug(ctx, "Agent metrics collection is done")
336334
metricsCollectorAgentStats.Observe(timer.ObserveDuration().Seconds())

0 commit comments

Comments
 (0)