From 943ed0c542fcfe033d218ae66db2035f5208d5df Mon Sep 17 00:00:00 2001 From: Maxime Beauchamp <15185355+baktun14@users.noreply.github.com> Date: Fri, 21 Feb 2025 15:07:59 -0500 Subject: [PATCH] feat(analytics): add daily wallet spent query --- doc/Example_Queries.md | 69 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/doc/Example_Queries.md b/doc/Example_Queries.md index 31a97475d..3ab400a7c 100644 --- a/doc/Example_Queries.md +++ b/doc/Example_Queries.md @@ -316,4 +316,73 @@ FROM daily_leases l GROUP BY date ORDER BY date DESC +``` + +## Query to fetch the daily spent for a wallet address (owner) + +``` +WITH current_block AS ( + SELECT MAX(height) as height + FROM block +), +daily_leases AS ( + SELECT + d.date, + l.owner, + l.denom, + l.price, + l."createdHeight", + CASE + WHEN l.id IS NULL THEN 0 + ELSE LEAST(d."lastBlockHeightYet", COALESCE(l."closedHeight", l."predictedClosedHeight")) - + GREATEST(d."firstBlockHeight", l."createdHeight") + END as blocks_in_day, + d."aktPrice" + FROM day d + LEFT JOIN lease l ON + l.owner = 'akash120y4k8s9zlpwtkdj94n9mdnd7czt0xuu5yrzes' AND -- change the owner here + l."createdHeight" < d."lastBlockHeightYet" AND + COALESCE(l."closedHeight", l."predictedClosedHeight") > d."firstBlockHeight" +), +daily_costs AS ( + SELECT + date, + ROUND(CAST(SUM(CASE + WHEN denom = 'uakt' + THEN blocks_in_day * price / 1000000.0 + ELSE 0 + END) as numeric), 2) as daily_akt_spent, + ROUND(CAST(SUM(CASE + WHEN denom = 'uusdc' + THEN blocks_in_day * price / 1000000.0 + ELSE 0 + END) as numeric), 2) as daily_usdc_spent, + ROUND(CAST(SUM(CASE + WHEN denom = 'uakt' + THEN blocks_in_day * price * "aktPrice" / 1000000.0 + WHEN denom = 'uusdc' + THEN blocks_in_day * price / 1000000.0 + ELSE 0 + END) as numeric), 2) as daily_usd_spent + FROM daily_leases + GROUP BY date +) +SELECT + d.date::DATE AS "Date", + COUNT(dl.owner) AS "Active Leases", + COALESCE(dc.daily_akt_spent, 0) as "Daily AKT Spent", + COALESCE(SUM(dc.daily_akt_spent) OVER (ORDER BY d.date), 0) as "Total AKT Spent", + COALESCE(dc.daily_usdc_spent, 0) as "Daily USDC Spent", + COALESCE(SUM(dc.daily_usdc_spent) OVER (ORDER BY d.date), 0) as "Total USDC Spent", + COALESCE(dc.daily_usd_spent, 0) as "Daily USD Spent", + COALESCE(SUM(dc.daily_usd_spent) OVER (ORDER BY d.date), 0) as "Total USD Spent" +FROM day d +LEFT JOIN daily_leases dl ON dl.date = d.date +LEFT JOIN daily_costs dc ON dc.date = d.date +GROUP BY + d.date, + dc.daily_akt_spent, + dc.daily_usdc_spent, + dc.daily_usd_spent +ORDER BY d.date DESC; ``` \ No newline at end of file