Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions metrics/1_create_mat_view.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
CREATE MATERIALIZED VIEW ${schema_name}.mv_pool_day_agg_2
TABLESPACE pg_default
AS WITH bounds AS (
SELECT EXTRACT(epoch FROM now() - '24:00:00'::interval)::bigint AS start_epoch,
EXTRACT(epoch FROM now())::bigint AS end_epoch
), recent AS (
SELECT f.pool,
f.minute_id,
f.volume_usd,
f.open,
f.close,
f.high,
f.low
FROM ${schema_name}.fifteen_minute_bucket_usd f
CROSS JOIN bounds b
WHERE f.minute_id >= b.start_epoch AND f.minute_id < b.end_epoch
), per_pool AS (
SELECT r.pool,
sum(r.volume_usd) AS volume,
max(r.high) AS high,
min(r.low) AS low
FROM recent r
GROUP BY r.pool
), fo AS (
SELECT DISTINCT ON (r.pool) r.pool,
r.open AS first_open
FROM recent r
ORDER BY r.pool, r.minute_id
), lc AS (
SELECT DISTINCT ON (r.pool) r.pool,
r.close AS last_close
FROM recent r
ORDER BY r.pool, r.minute_id DESC
), combined AS (
SELECT p.pool,
COALESCE(per_pool.volume, 0::numeric) AS volume,
fo.first_open,
lc.last_close,
per_pool.high,
per_pool.low
FROM ( SELECT pool.address AS pool
FROM ${schema_name}.pool) p
LEFT JOIN per_pool ON p.pool = per_pool.pool
LEFT JOIN fo ON p.pool = fo.pool
LEFT JOIN lc ON p.pool = lc.pool
)
SELECT pool,
volume,
first_open AS open,
last_close AS close,
high,
low,
CASE
WHEN first_open IS NOT NULL AND last_close IS NOT NULL THEN (last_close - first_open) / NULLIF(first_open, 0::numeric)::numeric * 100.0
ELSE NULL::numeric
END AS percent_day_change
FROM combined c
ORDER BY pool
WITH DATA;
4 changes: 4 additions & 0 deletions metrics/2_create_mat_view_indicies.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE INDEX pdc_idx ON ${schema_name}.mv_pool_day_agg_2 (percent_day_change);
CREATE UNIQUE INDEX u_pool_idx ON ${schema_name}.mv_pool_day_agg_2 (pool);
CREATE INDEX volume ON ${schema_name}.mv_pool_day_agg_2 (volume);
CREATE INDEX symbol_idx ON ${schema_name}.token (symbol);
21 changes: 21 additions & 0 deletions metrics/3_refresh_mat_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dotenv/config';
import * as pg from 'pg';


const client = new pg.Client({
connectionString: `postgres://${process.env.POSTGRES_USERNAME}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}:${process.env.POSTGRES_PORT}/${process.env.POSTGRES_DB}?sslmode=require`,
})

// Schema needs to be set to the schema targeting the correct deployment
export async function refreshViews() {
try {
await client.connect();
await client.query(`REFRESH MATERIALIZED VIEW CONCURRENTLY "${process.env.POSTGRES_SCHEMA}".mv_pool_day_agg_2;`);
} catch (error) {
await client.end();
console.error("Error refreshing views:", error);
return
} finally {
await client.end();
}
}
5 changes: 5 additions & 0 deletions metrics/refresh_mat_view_cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Run this from inside metrics directory
# Make sure postgres connection envs are set
# Make sure POSTGRES_SCHEMA is set to current deployment
# Change 15 to how many minutes you want to have between refreshes
(crontab -l 2>/dev/null; echo "*/15 * * * * node 3_refresh_mat_view.js") | crontab -