You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: analytics/SqlQueries.go
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -11,18 +11,18 @@ const (
11
11
12
12
GET_TOTAL_TimeAndQueries=`
13
13
SELECT
14
-
ROUND(SUM(total_exec_time)::numeric, 2) as total_time_ms,
15
-
SUM(calls) as total_queries
14
+
COALESCE(ROUND(SUM(total_exec_time)::numeric, 2), 0) as total_time_ms,
15
+
COALESCE(SUM(calls), 0) as total_queries
16
16
FROM pg_stat_statements pss
17
17
JOIN pg_database pd ON pss.dbid = pd.oid
18
18
WHERE pd.datname = $1
19
19
`
20
20
21
21
GET_READ_WRITE_CPU=`
22
22
SELECT
23
-
SUM(CASE WHEN query ILIKE 'SELECT%' THEN calls ELSE 0 END) as read_queries,
24
-
SUM(CASE WHEN query ILIKE ANY(ARRAY['INSERT%', 'UPDATE%', 'DELETE%']) THEN calls ELSE 0 END) as write_queries,
25
-
ROUND(SUM(total_exec_time)::numeric, 2) as total_cpu_time_ms
23
+
COALESCE(SUM(CASE WHEN query ILIKE 'SELECT%' THEN calls ELSE 0 END), 0) as read_queries,
24
+
COALESCE(SUM(CASE WHEN query ILIKE ANY(ARRAY['INSERT%', 'UPDATE%', 'DELETE%']) THEN calls ELSE 0 END), 0) as write_queries,
25
+
COALESCE(ROUND(SUM(total_exec_time)::numeric, 2), 0) as total_cpu_time_ms
26
26
FROM pg_stat_statements pss
27
27
JOIN pg_database pd ON pss.dbid = pd.oid
28
28
WHERE pd.datname = current_database()
@@ -31,13 +31,13 @@ const (
31
31
32
32
GET_ALL_CURRENT_STORAGE=`SELECT created_at::text, data->>'Management storage', data->>'Actual data' FROM analytics WHERE type = 'Storage' and "projectId" = $1 ORDER BY created_at DESC;`
33
33
34
-
GET_ALL_EXECUTION_TIME_STATS=`SELECT created_at::text, (data->>'total_time_ms')::numeric, (data->>'total_queries')::bigint FROM analytics WHERE type = 'ExecutionTimeStats' AND "projectId" = $1; `
34
+
GET_ALL_EXECUTION_TIME_STATS=`SELECT created_at::text, COALESCE((data->>'total_time_ms')::numeric, 0), COALESCE((data->>'total_queries')::bigint, 0) FROM analytics WHERE type = 'ExecutionTimeStats' AND "projectId" = $1; `
35
35
36
-
GET_ALL_DATABASE_USAGE_STATS=`SELECT created_at::text, (data->>'read_write_cost')::numeric, (data->>'cpu_cost')::numeric, (data->>'total_cost')::numeric FROM analytics WHERE type = 'DatabaseUsageStats' AND "projectId" = $1;`
36
+
GET_ALL_DATABASE_USAGE_STATS=`SELECT created_at::text, COALESCE((data->>'read_write_cost')::numeric, 0), COALESCE((data->>'cpu_cost')::numeric, 0), COALESCE((data->>'total_cost')::numeric, 0) FROM analytics WHERE type = 'DatabaseUsageStats' AND "projectId" = $1;`
37
37
38
38
// Queries to get the last records for each type of analytics
39
39
40
-
GET_LAST_EXECUTION_TIME_STATS=`SELECT created_at::text, (data->>'total_time_ms')::numeric, (data->>'total_queries')::bigint FROM analytics WHERE type = 'ExecutionTimeStats' AND "projectId" = $1 ORDER BY created_at DESC LIMIT 1;`
40
+
GET_LAST_EXECUTION_TIME_STATS=`SELECT created_at::text, COALESCE((data->>'total_time_ms')::numeric, 0), COALESCE((data->>'total_queries')::bigint, 0) FROM analytics WHERE type = 'ExecutionTimeStats' AND "projectId" = $1 ORDER BY created_at DESC LIMIT 1;`
41
41
42
-
GET_LAST_DATABASE_USAGE_STATS=`SELECT created_at::text, (data->>'read_write_cost')::numeric, (data->>'cpu_cost')::numeric, (data->>'total_cost')::numeric FROM analytics WHERE type = 'DatabaseUsageStats' AND "projectId" = $1 ORDER BY created_at DESC LIMIT 1;`
42
+
GET_LAST_DATABASE_USAGE_STATS=`SELECT created_at::text, COALESCE((data->>'read_write_cost')::numeric, 0), COALESCE((data->>'cpu_cost')::numeric, 0), COALESCE((data->>'total_cost')::numeric, 0) FROM analytics WHERE type = 'DatabaseUsageStats' AND "projectId" = $1 ORDER BY created_at DESC LIMIT 1;`
0 commit comments