Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
feat(schema/views): add hourly load aggregate, example union query
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Nov 27, 2019
1 parent e97211a commit 7618a3b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
2 changes: 2 additions & 0 deletions schema/grant/grafana.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ GRANT SELECT ON metrics TO :role_name;
-- crashing when it runs out of memory

GRANT SELECT ON agg_instance_load TO :role_name;
GRANT SELECT ON agg_instance_load_long TO :role_name;

GRANT SELECT ON agg_instance_pods TO :role_name;
52 changes: 43 additions & 9 deletions schema/query/history-load.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
SELECT
a.bucket AS time,
a.avg_load,
a.max_load,
REGEXP_REPLACE(l.labels->>'instance', '(.+):[0-9]+', '\1') AS instance
FROM
agg_instance_load AS a
JOIN
metric_labels AS l
ON l.lid = a.lid;
l.labels->>'instance' AS metric,
s.time,
s.value
FROM ((
SELECT
lid,
bucket AS time,
max_load AS value
FROM
agg_instance_load_long
WHERE bucket < NOW() - INTERVAL '1 day'
ORDER BY lid, bucket
) UNION ALL (
SELECT
lid,
bucket AS time,
max_load AS value
FROM
agg_instance_load
WHERE bucket > NOW() - INTERVAL '1 day'
ORDER BY lid, bucket
) UNION ALL (
SELECT
lid,
"tg" AS "time",
MAX("value") AS "value"
FROM (
SELECT
lid,
time_bucket(INTERVAL '1 minute', time) AS "tg",
value
FROM metrics
WHERE
time > NOW() - INTERVAL '15 minutes'
AND name = 'node_load1'
AND value != 'NaN'
) t
GROUP BY lid, time
ORDER BY lid, time
)) AS s
JOIN metric_labels AS l
ON l.lid = s.lid
ORDER BY s.time, s.lid;
22 changes: 19 additions & 3 deletions schema/views/instance-load.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@ CREATE VIEW agg_instance_load
WITH (
timescaledb.continuous,
timescaledb.refresh_interval = '5m',
timescaledb.refresh_lag = '5m')
AS SELECT
timescaledb.refresh_lag = '5m'
) AS SELECT
lid,
time_bucket('5 minutes', time) "bucket",
AVG(value) AS "avg_load",
MAX(value) AS "max_load"
FROM metric_samples
WHERE
name = 'node_load1'
GROUP BY lid, bucket;
GROUP BY lid, bucket;


CREATE VIEW agg_instance_load_long
WITH (
timescaledb.continuous,
timescaledb.refresh_interval = '1h',
timescaledb.refresh_lag = '1h'
) AS SELECT
lid,
time_bucket('1 hour', time) AS "bucket",
AVG(value) AS "avg_load",
MAX(value) AS "max_load"
FROM metric_samples
WHERE
name = 'node_load1'
GROUP BY lid, bucket;

0 comments on commit 7618a3b

Please sign in to comment.