chore: convert agent stats to use a table (#6374)

* chore: convert workspace agent stats from json to table

* chore: convert agent stats to use a table

Backwards compatibility becomes hard when all agent stats are in a JSON blob.
We also want to query this table for new agents that are failing health checks
so we can display it in the UI.

* Fix migration using default values
This commit is contained in:
Kyle Carberry
2023-02-28 13:33:33 -06:00
committed by GitHub
parent 7cf1e20aac
commit 05e449943d
22 changed files with 306 additions and 234 deletions
@@ -1,23 +1,28 @@
-- name: InsertAgentStat :one
-- name: InsertWorkspaceAgentStat :one
INSERT INTO
agent_stats (
workspace_agent_stats (
id,
created_at,
user_id,
workspace_id,
template_id,
agent_id,
payload
connections_by_proto,
connection_count,
rx_packets,
rx_bytes,
tx_packets,
tx_bytes
)
VALUES
($1, $2, $3, $4, $5, $6, $7) RETURNING *;
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING *;
-- name: GetTemplateDAUs :many
SELECT
(created_at at TIME ZONE 'UTC')::date as date,
user_id
FROM
agent_stats
workspace_agent_stats
WHERE
template_id = $1
GROUP BY
@@ -30,11 +35,11 @@ SELECT
(created_at at TIME ZONE 'UTC')::date as date,
user_id
FROM
agent_stats
workspace_agent_stats
GROUP BY
date, user_id
ORDER BY
date ASC;
-- name: DeleteOldAgentStats :exec
DELETE FROM agent_stats WHERE created_at < NOW() - INTERVAL '30 days';
-- name: DeleteOldWorkspaceAgentStats :exec
DELETE FROM workspace_agent_stats WHERE created_at < NOW() - INTERVAL '30 days';