@@ -52,17 +52,18 @@ DELETE FROM workspace_agent_stats WHERE created_at < NOW() - INTERVAL '30 days';
52
52
-- name: GetDeploymentWorkspaceAgentStats :one
53
53
WITH agent_stats AS (
54
54
SELECT * FROM workspace_agent_stats
55
- WHERE created_at > $1
55
+ WHERE workspace_agent_stats . created_at > $1
56
56
), latest_agent_stats AS (
57
- SELECT * FROM agent_stats GROUP BY agent_id ORDER BY created_at
57
+ SELECT * , ROW_NUMBER() OVER(PARTITION BY id ORDER BY created_at DESC ) AS rn
58
+ FROM agent_stats
58
59
)
59
60
SELECT
60
- SUM (latest_agent_stats .session_count_vscode ) AS session_count_vscode,
61
- SUM (latest_agent_stats .session_count_ssh ) AS session_count_ssh,
62
- SUM (latest_agent_stats .session_count_jetbrains ) AS session_count_jetbrains,
63
- SUM (latest_agent_stats .session_count_reconnecting_pty ) AS session_count_reconnecting_pty,
64
- SUM (agent_stats .rx_bytes ) AS workspace_rx_bytes,
65
- SUM (agent_stats .tx_bytes ) AS workspace_tx_bytes,
61
+ coalesce( SUM (latest_agent_stats .session_count_vscode ), 0 ):: bigint AS session_count_vscode,
62
+ coalesce( SUM (latest_agent_stats .session_count_ssh ), 0 ):: bigint AS session_count_ssh,
63
+ coalesce( SUM (latest_agent_stats .session_count_jetbrains ), 0 ):: bigint AS session_count_jetbrains,
64
+ coalesce( SUM (latest_agent_stats .session_count_reconnecting_pty ), 0 ):: bigint AS session_count_reconnecting_pty,
65
+ coalesce( SUM (agent_stats .rx_bytes ), 0 ):: bigint AS workspace_rx_bytes,
66
+ coalesce( SUM (agent_stats .tx_bytes ), 0 ):: bigint AS workspace_tx_bytes,
66
67
coalesce((PERCENTILE_DISC(0 .5 ) WITHIN GROUP(ORDER BY agent_stats .connection_median_latency_ms )), - 1 )::FLOAT AS workspace_connection_latency_50,
67
68
coalesce((PERCENTILE_DISC(0 .95 ) WITHIN GROUP(ORDER BY agent_stats .connection_median_latency_ms )), - 1 )::FLOAT AS workspace_connection_latency_95
68
- FROM agent_stats JOIN latest_agent_stats ON agent_stats .agent_id = latest_agent_stats .agent_id ;
69
+ FROM agent_stats JOIN latest_agent_stats ON agent_stats .agent_id = latest_agent_stats .agent_id AND rn = 1 ;
0 commit comments