Skip to content

Commit

Permalink
Add v2 endpoint for new median pricing data
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptobench committed Feb 26, 2024
1 parent f4e63d7 commit 9c86362
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
32 changes: 32 additions & 0 deletions stats-backend/api2/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,35 @@ def create_pricing_snapshot():
snapshot.save()
except Exception as e:
print(e) # Replace with actual logging


@app.task
def median_pricing_past_hour():
try:
last_hour = timezone.now() - timedelta(hours=1)
cpu_median = median(
ProviderWithTask.objects.filter(created_at__gte=last_hour)
.values_list("cpu_per_hour", flat=True)
.exclude(cpu_per_hour__isnull=True)
)
env_median = median(
ProviderWithTask.objects.filter(created_at__gte=last_hour)
.values_list("env_per_hour", flat=True)
.exclude(env_per_hour__isnull=True)
)
start_median = median(
ProviderWithTask.objects.filter(created_at__gte=last_hour)
.values_list("start_price", flat=True)
.exclude(start_price__isnull=True)
)

pricing_data = {
"cpu_median": cpu_median,
"env_median": env_median,
"start_median": start_median,
}
print(f"Median pricing data: {pricing_data}")

r.set("pricing_median", json.dumps(pricing_data))
except Exception as e:
print(e) # Replace with proper logging mechanism
1 change: 1 addition & 0 deletions stats-backend/api2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
path("network/online", views.network_online),
path("network/online/flatmap", views.network_online_flatmap),
path("network/offers/cheapest/cores", views.cheapest_by_cores),
path("network/pricing/median/1h", views.get_median_pricing_1h),
path("provider/wallet/<wallet>", views.node_wallet),
path("provider/node/<yagna_id>", views.node),
path("provider/uptime/<yagna_id>", views.node_uptime),
Expand Down
13 changes: 13 additions & 0 deletions stats-backend/api2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
from .scoring import calculate_uptime_percentage


async def get_median_pricing_1h(request):
try:
pool = aioredis.ConnectionPool.from_url(
"redis://redis:6379/0", decode_responses=True
)
r = aioredis.Redis(connection_pool=pool)
pricing_data = json.loads(await r.get("pricing_median"))
pool.disconnect()
return JsonResponse(pricing_data)
except Exception as e:
return JsonResponse({"error": str(e)}, status=500)


async def list_ec2_instances_comparison(request):
if request.method == "GET":
pool = aioredis.ConnectionPool.from_url(
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 @@ -56,6 +56,7 @@ def setup_periodic_tasks(sender, **kwargs):
compare_ec2_and_golem,
providers_who_received_tasks,
create_pricing_snapshot,
median_pricing_past_hour,
)

# sender.add_periodic_task(
Expand All @@ -82,6 +83,12 @@ def setup_periodic_tasks(sender, **kwargs):
queue="default",
options={"queue": "default", "routing_key": "default"},
)
sender.add_periodic_task(
60,
median_pricing_past_hour.s(),
queue="default",
options={"queue": "default", "routing_key": "default"},
)
sender.add_periodic_task(
crontab(minute="*/10"),
providers_who_received_tasks.s(),
Expand Down

0 comments on commit 9c86362

Please sign in to comment.