Skip to content

Commit 5b92db9

Browse files
committed
feat: query from realtime view
1 parent 5dcfb06 commit 5b92db9

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
-- Your SQL goes here
2-
SELECT create_hypertable('entries', by_range('timestamp'));
3-
SELECT * FROM add_dimension('entries', by_hash('pair_id', 4))
4-
SELECT * FROM add_dimension('entries', by_hash('source', 4))
2+
SELECT create_hypertable('entries', 'timestamp');

pragma-entities/migrations/2024-01-11-123510_add_continuous_aggregates/up.sql

+28-6
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,45 @@
22
CREATE MATERIALIZED VIEW price_1_min_agg
33
WITH (timescaledb.continuous, timescaledb.materialized_only = true)
44
AS SELECT source,
5-
time_bucket('1 min'::interval, ts) as bucket,
5+
pair_id,
6+
time_bucket('1 min'::interval, timestamp) as bucket,
67
percentile_agg(price)
78
FROM entries
8-
GROUP BY source, bucket;
9+
GROUP BY source, pair_id, bucket
10+
WITH NO DATA;
11+
12+
SELECT add_continuous_aggregate_policy('price_1_min_agg',
13+
start_offset => INTERVAL '3 min',
14+
end_offset => INTERVAL '1 min',
15+
schedule_interval => INTERVAL '1 min');
916

1017
CREATE MATERIALIZED VIEW price_15_min_agg
1118
WITH (timescaledb.continuous, timescaledb.materialized_only = true)
1219
AS SELECT source,
13-
time_bucket('15 min'::interval, ts) as bucket,
20+
pair_id,
21+
time_bucket('15 min'::interval, timestamp) as bucket,
1422
percentile_agg(price)
1523
FROM entries
16-
GROUP BY source, bucket;
24+
GROUP BY source, pair_id, bucket
25+
WITH NO DATA;
26+
27+
SELECT add_continuous_aggregate_policy('price_15_min_agg',
28+
start_offset => INTERVAL '45 min',
29+
end_offset => INTERVAL '15 min',
30+
schedule_interval => INTERVAL '15 min');
1731

1832
CREATE MATERIALIZED VIEW price_1_h_agg
1933
WITH (timescaledb.continuous, timescaledb.materialized_only = true)
2034
AS SELECT source,
21-
time_bucket('1 h'::interval, ts) as bucket,
35+
pair_id,
36+
time_bucket('1 hour'::interval, timestamp) as bucket,
2237
percentile_agg(price)
2338
FROM entries
24-
GROUP BY source, bucket;
39+
GROUP BY source, pair_id, bucket
40+
WITH NO DATA;
41+
42+
SELECT add_continuous_aggregate_policy('price_1_h_agg',
43+
start_offset => INTERVAL '3 hours',
44+
end_offset => INTERVAL '1 hour',
45+
schedule_interval => INTERVAL '1 hour');
46+

pragma-node/src/infra/repositories/entry_repository.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,15 @@ pub async fn get_median_entries(
8686
let conn = pool.get().await.map_err(adapt_infra_error)?;
8787

8888
let raw_sql = r#"
89-
-- select the latest entry for every publisher,source combination
89+
-- query the materialized realtime view
9090
SELECT
9191
source,
92-
MAX(timestamp) AS time,
93-
(approx_percentile(0.5, uddsketch(100, 0.01, price)) WITHIN GROUP (ORDER BY price))::numeric AS median_price
92+
MAX(bucket) AS time,
93+
approx_percentile(0.5, percentile_agg) AS median_price
9494
FROM
95-
entries
95+
price_1_min_agg
9696
WHERE
9797
pair_id = $1
98-
GROUP BY
99-
source
10098
ORDER BY
10199
source;
102100
"#;

0 commit comments

Comments
 (0)