Skip to content

Commit

Permalink
Remove space API in favor of pool_size
Browse files Browse the repository at this point in the history
  • Loading branch information
william-gr committed Aug 14, 2022
1 parent 1792e86 commit 07f12c4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
16 changes: 16 additions & 0 deletions openchiaapi/api/migrations/0046_delete_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 4.1 on 2022-08-14 17:32

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api', '0045_remove_payoutaddress_payout_round'),
]

operations = [
migrations.DeleteModel(
name='Space',
),
]
9 changes: 0 additions & 9 deletions openchiaapi/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,6 @@ class Meta:
transaction = models.ForeignKey(Transaction, null=True, on_delete=models.SET_NULL)


class Space(models.Model):

class Meta:
db_table = 'space'

date = models.DateTimeField()
size = models.BigIntegerField()


class SingletonModel(models.Model):

class Meta:
Expand Down
2 changes: 0 additions & 2 deletions openchiaapi/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
PoolSizeView,
QRCodeView,
StatsView,
SpaceView,
TransactionViewSet,
XCHPriceView,
XCHScanStatsView,
Expand Down Expand Up @@ -54,5 +53,4 @@
path('loggedin', LoggedInView.as_view()),
path('stats', StatsView.as_view()),
path('xchscan_stats', XCHScanStatsView.as_view()),
path('space', SpaceView.as_view()),
]
61 changes: 30 additions & 31 deletions openchiaapi/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from chia.util.byte_types import hexstr_to_bytes
from chia.util.hash import std_hash
from chia.util.ints import uint64
from datetime import datetime, timedelta
from datetime import datetime
from decimal import Decimal
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Avg, F, Sum, Q
Expand All @@ -30,7 +30,7 @@
from rest_framework.response import Response

from .models import (
Block, GlobalInfo, Launcher, Partial, Payout, PayoutAddress, Space,
Block, GlobalInfo, Launcher, Partial, Payout, PayoutAddress,
Notification,
Transaction,
)
Expand All @@ -45,7 +45,6 @@
PayoutAddressSerializer,
PayoutTransactionSerializer,
StatsSerializer,
SpaceSerializer,
TimeseriesSerializer,
TransactionSerializer,
XCHScanStatsSerializer,
Expand Down Expand Up @@ -162,9 +161,21 @@ def get(self, request, format=None):
farmers_total = farmers.count()
farmers_active = farmers.filter(points_pplns__gt=0).count()
pool_info = get_pool_info()

client = get_influxdb_client()
query_api = client.query_api()

try:
size = Space.objects.latest('id').size
except Space.DoesNotExist:
q = query_api.query(
textwrap.dedent('''from(bucket: "openchia")
|> range(start: duration(v: "-30m"))
|> filter(fn: (r) => r["_measurement"] == "pool_size")
|> filter(fn: (r) => r["_field"] == "global")
|> last()'''),
)
size = int(q[0].records[0]['_value'])
except Exception:
logger.error('Failed to get pool size', exc_info=True)
size = 0

globalinfo = GlobalInfo.load()
Expand All @@ -183,9 +194,6 @@ def get(self, request, format=None):
profitability += (b.amount / 1000000000000) / (b.pool_space / 1099511627776)
profitability /= 30

client = get_influxdb_client()
query_api = client.query_api()

try:
q = query_api.query(
textwrap.dedent('''from(bucket: "openchia")
Expand Down Expand Up @@ -237,9 +245,21 @@ def get(self, request, format=None):
block = Block.objects.order_by('-confirmed_block_index')
farmers = Launcher.objects.filter(is_pool_member=True).count()
pool_info = get_pool_info()

client = get_influxdb_client()
query_api = client.query_api()

try:
size = Space.objects.latest('id').size
except Space.DoesNotExist:
q = query_api.query(
textwrap.dedent('''from(bucket: "openchia")
|> range(start: duration(v: "-30m"))
|> filter(fn: (r) => r["_measurement"] == "pool_size")
|> filter(fn: (r) => r["_field"] == "global")
|> last()'''),
)
size = int(q[0].records[0]['_value'])
except Exception:
logger.error('Failed to get pool size', exc_info=True)
size = 0

pi = XCHScanStatsSerializer(data={
Expand Down Expand Up @@ -453,27 +473,6 @@ def get(self, request, format=None):
return paginator.get_paginated_response(serializer.data)


class SpaceView(APIView):
DEFAULT_DAYS = 30
days_param = openapi.Parameter(
'days',
openapi.IN_QUERY,
description=f'Number of days (default: {DEFAULT_DAYS})',
type=openapi.TYPE_INTEGER,
)

@swagger_auto_schema(
manual_parameters=[days_param],
responses={200: SpaceSerializer(many=True)}
)
def get(self, request, format=None):
days = self.request.query_params.get('days') or self.DEFAULT_DAYS
size = Space.objects.filter(
date__gte=datetime.now() - timedelta(days=int(days))
).order_by('date')
return Response([{'date': i.date, 'size': i.size} for i in size])


class LauncherSizeView(APIView):

days_param = openapi.Parameter(
Expand Down

0 comments on commit 07f12c4

Please sign in to comment.