Skip to content

Commit

Permalink
Cache daily volume
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptobench committed Mar 28, 2024
1 parent 6fcd94e commit 61b4625
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
25 changes: 25 additions & 0 deletions stats-backend/api2/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1584,3 +1584,28 @@ def transaction_volume_over_time():
.order_by("date")
)
r.set("transaction_volume_over_time", json.dumps(list(data), cls=DjangoJSONEncoder))


from django.db.models.functions import TruncDay, Coalesce


@app.task
def daily_volume_golem_vs_chain():
data = (
GolemTransactions.objects.annotate(date=TruncDay("timestamp"))
.values("date")
.annotate(
on_golem=Coalesce(
Sum("amount", filter=Q(tx_from_golem=True), output_field=FloatField()),
0,
output_field=FloatField(),
),
not_golem=Coalesce(
Sum("amount", filter=Q(tx_from_golem=False), output_field=FloatField()),
0,
output_field=FloatField(),
),
)
.order_by("date")
)
r.set("daily_volume_golem_vs_chain", json.dumps(list(data), cls=DjangoJSONEncoder))
31 changes: 12 additions & 19 deletions stats-backend/api2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,25 +679,18 @@ def get_healthcheck_status(request):
from django.http import JsonResponse


def daily_volume_golem_vs_chain(request):
data = (
GolemTransactions.objects.annotate(date=TruncDay("timestamp"))
.values("date")
.annotate(
on_golem=Coalesce(
Sum("amount", filter=Q(tx_from_golem=True), output_field=FloatField()),
0,
output_field=FloatField(),
),
not_golem=Coalesce(
Sum("amount", filter=Q(tx_from_golem=False), output_field=FloatField()),
0,
output_field=FloatField(),
),
)
.order_by("date")
)
return JsonResponse(list(data), safe=False)
async def daily_volume_golem_vs_chain(request):
if request.method == "GET":
pool = aioredis.ConnectionPool.from_url(
"redis://redis:6379/0", decode_responses=True
)
r = aioredis.Redis(connection_pool=pool)
content = await r.get("daily_volume_golem_vs_chain")
data = json.loads(content)
pool.disconnect()
return JsonResponse(data, safe=False, json_dumps_params={"indent": 4})
else:
return HttpResponse(status=400)


from django.db.models import Count
Expand Down
7 changes: 7 additions & 0 deletions stats-backend/core/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def setup_periodic_tasks(sender, **kwargs):
transaction_type_comparison,
daily_transaction_type_counts,
average_transaction_value_over_time,
daily_volume_golem_vs_chain,
)

sender.add_periodic_task(
Expand All @@ -80,6 +81,12 @@ def setup_periodic_tasks(sender, **kwargs):
queue="default",
options={"queue": "default", "routing_key": "default"},
)
sender.add_periodic_task(
60,
daily_volume_golem_vs_chain.s(),
queue="default",
options={"queue": "default", "routing_key": "default"},
)
sender.add_periodic_task(
60,
network_earnings_overview_new.s(),
Expand Down

0 comments on commit 61b4625

Please sign in to comment.