Skip to content

Commit bbdc400

Browse files
authored
chore(analytics): add daily wallet spent query (#895)
* feat(analytics): add daily wallet spent query * feat(analytics): added query to fetch provider revenue over time
1 parent 64f8fa0 commit bbdc400

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

doc/Example_Queries.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,145 @@ FROM daily_leases l
316316
GROUP BY date
317317
ORDER BY date DESC
318318
319+
```
320+
321+
## Query to fetch the daily spent for a wallet address (owner)
322+
323+
```
324+
WITH daily_leases AS (
325+
SELECT
326+
d.date,
327+
l.owner,
328+
l.denom,
329+
l.price,
330+
l."createdHeight",
331+
CASE
332+
WHEN l.id IS NULL THEN 0
333+
ELSE LEAST(d."lastBlockHeightYet", COALESCE(l."closedHeight", l."predictedClosedHeight")) -
334+
GREATEST(d."firstBlockHeight", l."createdHeight")
335+
END as blocks_in_day,
336+
d."aktPrice"
337+
FROM day d
338+
LEFT JOIN lease l ON
339+
l.owner = 'akash120y4k8s9zlpwtkdj94n9mdnd7czt0xuu5yrzes' AND -- change the owner here
340+
l."createdHeight" < d."lastBlockHeightYet" AND
341+
COALESCE(l."closedHeight", l."predictedClosedHeight") > d."firstBlockHeight"
342+
),
343+
daily_costs AS (
344+
SELECT
345+
date,
346+
ROUND(CAST(SUM(CASE
347+
WHEN denom = 'uakt'
348+
THEN blocks_in_day * price / 1000000.0
349+
ELSE 0
350+
END) as numeric), 2) as daily_akt_spent,
351+
ROUND(CAST(SUM(CASE
352+
WHEN denom = 'uusdc'
353+
THEN blocks_in_day * price / 1000000.0
354+
ELSE 0
355+
END) as numeric), 2) as daily_usdc_spent,
356+
ROUND(CAST(SUM(CASE
357+
WHEN denom = 'uakt'
358+
THEN blocks_in_day * price * "aktPrice" / 1000000.0
359+
WHEN denom = 'uusdc'
360+
THEN blocks_in_day * price / 1000000.0
361+
ELSE 0
362+
END) as numeric), 2) as daily_usd_spent
363+
FROM daily_leases
364+
GROUP BY date
365+
)
366+
SELECT
367+
d.date::DATE AS "Date",
368+
COUNT(dl.owner) AS "Active Leases",
369+
COALESCE(dc.daily_akt_spent, 0) as "Daily AKT Spent",
370+
COALESCE(SUM(dc.daily_akt_spent) OVER (ORDER BY d.date), 0) as "Total AKT Spent",
371+
COALESCE(dc.daily_usdc_spent, 0) as "Daily USDC Spent",
372+
COALESCE(SUM(dc.daily_usdc_spent) OVER (ORDER BY d.date), 0) as "Total USDC Spent",
373+
COALESCE(dc.daily_usd_spent, 0) as "Daily USD Spent",
374+
COALESCE(SUM(dc.daily_usd_spent) OVER (ORDER BY d.date), 0) as "Total USD Spent"
375+
FROM day d
376+
LEFT JOIN daily_leases dl ON dl.date = d.date
377+
LEFT JOIN daily_costs dc ON dc.date = d.date
378+
GROUP BY
379+
d.date,
380+
dc.daily_akt_spent,
381+
dc.daily_usdc_spent,
382+
dc.daily_usd_spent
383+
ORDER BY d.date DESC;
384+
```
385+
386+
## Query to fetch provider revenue over time
387+
388+
```
389+
WITH filtered_providers AS (
390+
SELECT owner, "hostUri"
391+
FROM provider
392+
WHERE "hostUri" = 'https://provider.europlots.com:8443' -- change the condition to filter providers
393+
),
394+
daily_leases AS (
395+
SELECT
396+
d.date,
397+
p."hostUri",
398+
l.id as lease_id,
399+
l.denom,
400+
l.price,
401+
l."createdHeight",
402+
CASE
403+
WHEN l.id IS NULL THEN 0
404+
ELSE LEAST(d."lastBlockHeightYet", COALESCE(l."closedHeight", l."predictedClosedHeight")) -
405+
GREATEST(d."firstBlockHeight", l."createdHeight")
406+
END as blocks_in_day,
407+
d."aktPrice"
408+
FROM day d
409+
CROSS JOIN filtered_providers p
410+
LEFT JOIN lease l ON
411+
p.owner = l."providerAddress" AND
412+
l."createdHeight" < d."lastBlockHeightYet" AND
413+
COALESCE(l."closedHeight", l."predictedClosedHeight") > d."firstBlockHeight"
414+
),
415+
daily_revenue AS (
416+
SELECT
417+
date,
418+
"hostUri",
419+
ROUND(CAST(SUM(CASE
420+
WHEN denom = 'uakt'
421+
THEN blocks_in_day * price / 1000000.0
422+
ELSE 0
423+
END) as numeric), 2) as daily_akt_revenue,
424+
ROUND(CAST(SUM(CASE
425+
WHEN denom = 'uusdc'
426+
THEN blocks_in_day * price / 1000000.0
427+
ELSE 0
428+
END) as numeric), 2) as daily_usdc_revenue,
429+
ROUND(CAST(SUM(CASE
430+
WHEN denom = 'uakt'
431+
THEN blocks_in_day * price * "aktPrice" / 1000000.0
432+
WHEN denom = 'uusdc'
433+
THEN blocks_in_day * price / 1000000.0
434+
ELSE 0
435+
END) as numeric), 2) as daily_usd_revenue
436+
FROM daily_leases
437+
WHERE lease_id IS NOT NULL
438+
GROUP BY date, "hostUri"
439+
)
440+
SELECT
441+
dl.date::DATE AS "Date",
442+
dl."hostUri",
443+
COUNT(DISTINCT dl.lease_id) AS "Active Leases",
444+
COALESCE(dr.daily_akt_revenue, 0) as "Daily AKT Revenue",
445+
COALESCE(SUM(dr.daily_akt_revenue) OVER (PARTITION BY dl."hostUri" ORDER BY dl.date), 0) as "Total AKT Revenue",
446+
COALESCE(dr.daily_usdc_revenue, 0) as "Daily USDC Revenue",
447+
COALESCE(SUM(dr.daily_usdc_revenue) OVER (PARTITION BY dl."hostUri" ORDER BY dl.date), 0) as "Total USDC Revenue",
448+
COALESCE(dr.daily_usd_revenue, 0) as "Daily USD Revenue",
449+
COALESCE(SUM(dr.daily_usd_revenue) OVER (PARTITION BY dl."hostUri" ORDER BY dl.date), 0) as "Total USD Revenue"
450+
FROM daily_leases dl
451+
LEFT JOIN daily_revenue dr ON dr.date = dl.date AND dr."hostUri" = dl."hostUri"
452+
WHERE dl.lease_id IS NOT NULL
453+
GROUP BY
454+
dl.date,
455+
dl."hostUri",
456+
dr.daily_akt_revenue,
457+
dr.daily_usdc_revenue,
458+
dr.daily_usd_revenue
459+
ORDER BY dl.date DESC, dl."hostUri";
319460
```

0 commit comments

Comments
 (0)