diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 17959e76a5..23d4f96d2d 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -19374,27 +19374,32 @@ func (q *sqlQuerier) GetDeploymentDAUs(ctx context.Context, tzOffset int32) ([]G } const getDeploymentWorkspaceAgentStats = `-- name: GetDeploymentWorkspaceAgentStats :one -WITH agent_stats AS ( - SELECT - coalesce(SUM(rx_bytes), 0)::bigint AS workspace_rx_bytes, - coalesce(SUM(tx_bytes), 0)::bigint AS workspace_tx_bytes, - coalesce((PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY connection_median_latency_ms)), -1)::FLOAT AS workspace_connection_latency_50, - coalesce((PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY connection_median_latency_ms)), -1)::FLOAT AS workspace_connection_latency_95 - FROM workspace_agent_stats - -- The greater than 0 is to support legacy agents that don't report connection_median_latency_ms. - WHERE workspace_agent_stats.created_at > $1 AND connection_median_latency_ms > 0 -), latest_agent_stats AS ( - SELECT - coalesce(SUM(session_count_vscode), 0)::bigint AS session_count_vscode, - coalesce(SUM(session_count_ssh), 0)::bigint AS session_count_ssh, - coalesce(SUM(session_count_jetbrains), 0)::bigint AS session_count_jetbrains, - coalesce(SUM(session_count_reconnecting_pty), 0)::bigint AS session_count_reconnecting_pty - FROM ( - SELECT id, created_at, user_id, agent_id, workspace_id, template_id, connections_by_proto, connection_count, rx_packets, rx_bytes, tx_packets, tx_bytes, connection_median_latency_ms, session_count_vscode, session_count_jetbrains, session_count_reconnecting_pty, session_count_ssh, usage, ROW_NUMBER() OVER(PARTITION BY agent_id ORDER BY created_at DESC) AS rn - FROM workspace_agent_stats WHERE created_at > $1 - ) AS a WHERE a.rn = 1 +WITH stats AS ( + SELECT + agent_id, + created_at, + rx_bytes, + tx_bytes, + connection_median_latency_ms, + session_count_vscode, + session_count_ssh, + session_count_jetbrains, + session_count_reconnecting_pty, + ROW_NUMBER() OVER (PARTITION BY agent_id ORDER BY created_at DESC) AS rn + FROM workspace_agent_stats + WHERE created_at > $1 ) -SELECT workspace_rx_bytes, workspace_tx_bytes, workspace_connection_latency_50, workspace_connection_latency_95, session_count_vscode, session_count_ssh, session_count_jetbrains, session_count_reconnecting_pty FROM agent_stats, latest_agent_stats +SELECT + coalesce(SUM(rx_bytes), 0)::bigint AS workspace_rx_bytes, + coalesce(SUM(tx_bytes), 0)::bigint AS workspace_tx_bytes, + -- The greater than 0 is to support legacy agents that don't report connection_median_latency_ms. + coalesce((PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY connection_median_latency_ms) FILTER (WHERE connection_median_latency_ms > 0)), -1)::FLOAT AS workspace_connection_latency_50, + coalesce((PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY connection_median_latency_ms) FILTER (WHERE connection_median_latency_ms > 0)), -1)::FLOAT AS workspace_connection_latency_95, + coalesce(SUM(session_count_vscode) FILTER (WHERE rn = 1), 0)::bigint AS session_count_vscode, + coalesce(SUM(session_count_ssh) FILTER (WHERE rn = 1), 0)::bigint AS session_count_ssh, + coalesce(SUM(session_count_jetbrains) FILTER (WHERE rn = 1), 0)::bigint AS session_count_jetbrains, + coalesce(SUM(session_count_reconnecting_pty) FILTER (WHERE rn = 1), 0)::bigint AS session_count_reconnecting_pty +FROM stats ` type GetDeploymentWorkspaceAgentStatsRow struct { diff --git a/coderd/database/queries/workspaceagentstats.sql b/coderd/database/queries/workspaceagentstats.sql index 9c49b281f6..ea4c14127d 100644 --- a/coderd/database/queries/workspaceagentstats.sql +++ b/coderd/database/queries/workspaceagentstats.sql @@ -99,27 +99,32 @@ WHERE ); -- name: GetDeploymentWorkspaceAgentStats :one -WITH agent_stats AS ( - SELECT - coalesce(SUM(rx_bytes), 0)::bigint AS workspace_rx_bytes, - coalesce(SUM(tx_bytes), 0)::bigint AS workspace_tx_bytes, - coalesce((PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY connection_median_latency_ms)), -1)::FLOAT AS workspace_connection_latency_50, - coalesce((PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY connection_median_latency_ms)), -1)::FLOAT AS workspace_connection_latency_95 - FROM workspace_agent_stats - -- The greater than 0 is to support legacy agents that don't report connection_median_latency_ms. - WHERE workspace_agent_stats.created_at > $1 AND connection_median_latency_ms > 0 -), latest_agent_stats AS ( - SELECT - coalesce(SUM(session_count_vscode), 0)::bigint AS session_count_vscode, - coalesce(SUM(session_count_ssh), 0)::bigint AS session_count_ssh, - coalesce(SUM(session_count_jetbrains), 0)::bigint AS session_count_jetbrains, - coalesce(SUM(session_count_reconnecting_pty), 0)::bigint AS session_count_reconnecting_pty - FROM ( - SELECT *, ROW_NUMBER() OVER(PARTITION BY agent_id ORDER BY created_at DESC) AS rn - FROM workspace_agent_stats WHERE created_at > $1 - ) AS a WHERE a.rn = 1 +WITH stats AS ( + SELECT + agent_id, + created_at, + rx_bytes, + tx_bytes, + connection_median_latency_ms, + session_count_vscode, + session_count_ssh, + session_count_jetbrains, + session_count_reconnecting_pty, + ROW_NUMBER() OVER (PARTITION BY agent_id ORDER BY created_at DESC) AS rn + FROM workspace_agent_stats + WHERE created_at > $1 ) -SELECT * FROM agent_stats, latest_agent_stats; +SELECT + coalesce(SUM(rx_bytes), 0)::bigint AS workspace_rx_bytes, + coalesce(SUM(tx_bytes), 0)::bigint AS workspace_tx_bytes, + -- The greater than 0 is to support legacy agents that don't report connection_median_latency_ms. + coalesce((PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY connection_median_latency_ms) FILTER (WHERE connection_median_latency_ms > 0)), -1)::FLOAT AS workspace_connection_latency_50, + coalesce((PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY connection_median_latency_ms) FILTER (WHERE connection_median_latency_ms > 0)), -1)::FLOAT AS workspace_connection_latency_95, + coalesce(SUM(session_count_vscode) FILTER (WHERE rn = 1), 0)::bigint AS session_count_vscode, + coalesce(SUM(session_count_ssh) FILTER (WHERE rn = 1), 0)::bigint AS session_count_ssh, + coalesce(SUM(session_count_jetbrains) FILTER (WHERE rn = 1), 0)::bigint AS session_count_jetbrains, + coalesce(SUM(session_count_reconnecting_pty) FILTER (WHERE rn = 1), 0)::bigint AS session_count_reconnecting_pty +FROM stats; -- name: GetDeploymentWorkspaceAgentUsageStats :one WITH agent_stats AS (