File tree Expand file tree Collapse file tree 1 file changed +29
-14
lines changed
indexer/src/kadena-server/repository/infra/repository Expand file tree Collapse file tree 1 file changed +29
-14
lines changed Original file line number Diff line number Diff line change @@ -44,26 +44,41 @@ export default class DexMetricsDbRepository implements DexMetricsRepository {
4444
4545 // Get TVL history
4646 const tvlHistoryQuery = `
47- SELECT
48- date_trunc('day', t.timestamp) AS timestamp,
49- SUM(CAST(t."tvlUsd" AS DECIMAL)) AS value
50- FROM (
47+ WITH ranked_tvl AS (
5148 SELECT
49+ date_trunc('day', d.day) AS day,
5250 pc."pairId",
5351 pc."tvlUsd",
54- pc."timestamp",
5552 ROW_NUMBER() OVER (
56- PARTITION BY pc."pairId", date_trunc(' day' , pc."timestamp")
53+ PARTITION BY d. day, pc."pairId"
5754 ORDER BY pc."timestamp" DESC
58- ) as rn
59- FROM "PoolCharts" pc
55+ ) AS rn
56+ FROM (
57+ SELECT generate_series(
58+ $1::timestamp,
59+ $2::timestamp,
60+ interval '1 day'
61+ ) AS day
62+ ) d
63+ JOIN "PoolCharts" pc
64+ ON pc."timestamp" < d.day + interval '1 day'
6065 JOIN "Pairs" p ON p.id = pc."pairId"
61- WHERE pc.timestamp BETWEEN $1 AND $2
62- AND p.address = $3
63- ) t
64- WHERE t.rn = 1
65- GROUP BY date_trunc('day', t.timestamp)
66- ORDER BY timestamp ASC;
66+ WHERE p.address = $3
67+ ),
68+ latest_tvl_per_day AS (
69+ SELECT
70+ day,
71+ "pairId",
72+ CAST("tvlUsd" AS DECIMAL) AS tvl_usd
73+ FROM ranked_tvl
74+ WHERE rn = 1
75+ )
76+ SELECT
77+ day AS timestamp,
78+ SUM(tvl_usd) AS value
79+ FROM latest_tvl_per_day
80+ GROUP BY day
81+ ORDER BY day ASC;
6782 ` ;
6883
6984 const tvlHistory = await sequelize . query ( tvlHistoryQuery , {
You can’t perform that action at this time.
0 commit comments