Skip to content

Commit

Permalink
fix(fleet): finished list, retrieve & create fleet endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenepix committed Dec 24, 2023
1 parent e8dc1cf commit aa12309
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
22 changes: 7 additions & 15 deletions django_napse/api/fleets/serializers/cluster_serialisers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rest_framework import serializers

from django_napse.api.bots.serializers import BotSerializer
from django_napse.core.models import Bot, Cluster, Strategy
from django_napse.core.models import Bot, Cluster


class ClusterSerializerV1(serializers.ModelSerializer):
Expand All @@ -20,28 +20,20 @@ class Meta:
class ClusterFormatterSerializer(serializers.Serializer):
"""Format cluster dictionnary for fleet creation."""

# TODO: use bot UUID instead of bot name
bot_strategy = serializers.UUIDField(required=True)
bot_name = serializers.CharField(required=True)
template_bot = serializers.UUIDField(required=True)
share = serializers.FloatField(required=True)
breakpoint = serializers.IntegerField(required=True)
autoscale = serializers.BooleanField(required=True)

def validate(self, data):
data = super().validate(data)
try:
strategy = Strategy.objects.get(uuid=data["bot_strategy"])
except Strategy.DoesNotExist:
error_msg: str = "Strategy does not exist"
bot = Bot.objects.get(uuid=data.pop("template_bot"))
except Bot.DoesNotExist:
error_msg: str = "Template bot does not exist."
raise serializers.ValidationError(error_msg) from None
else:
data["bot_strategy"] = strategy
data["template_bot"] = bot
return data

def create(self, validated_data):
"""Return cluster dict for fleet creation."""
template_bot = Bot.objects.create(
name=validated_data.pop("bot_name"),
strategy=validated_data.pop("bot_strategy"),
)
return {"template_bot": template_bot, **validated_data}
return super().create(validated_data)
18 changes: 11 additions & 7 deletions django_napse/api/fleets/serializers/fleet_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, instance=None, data=empty, space=None, **kwargs):

def get_value(self, instance):
if self.space is None:
return instance.value()
return instance.value
return instance.space_frame_value(space=self.space)

def get_bot_count(self, instance):
Expand All @@ -45,15 +45,17 @@ def get_bot_count(self, instance):
return len(query_bot)
return len([bot for bot in query_bot if bot.space == self.space])

def validate(self, attrs):
data = super().validate(attrs)
data["exchange_account"] = self.space.exchange_account
return data

def create(self, validated_data):
clusters = validated_data.pop("clusters")
fleet = Fleet.objects.create(**validated_data)
for cluster in clusters:
fleet.clusters.create(**cluster)
return fleet
print(f"validated_data: {validated_data}")
return Fleet.objects.create(**validated_data)


class FleetDetailSerializer(serializers.Serializer):
class FleetDetailSerializer(serializers.ModelSerializer):
wallet = serializers.SerializerMethodField(read_only=True)
statistics = serializers.SerializerMethodField(read_only=True)
bots = BotSerializer(many=True, read_only=True)
Expand Down Expand Up @@ -97,6 +99,8 @@ def _update_merged_wallet(index: int, currency: str, merged_wallet) -> None:
else:
merged_wallet[index]["amount"] += currency.amount

if self.space is None:
return None
wallets = ConnectionWallet.objects.filter(owner__owner=self.space.wallet, owner__bot__in=instance.bots)
merged_wallet: list[dict[str, str | float]] = []

Expand Down
24 changes: 16 additions & 8 deletions django_napse/api/fleets/views/fleet_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_queryset(self):
self.get_space = NapseSpace.objects.get(uuid=space_uuid)
return self.get_space.fleets

def get_serialiser_class(self, *args, **kwargs):
def get_serializer_class(self, *args, **kwargs):
actions: dict = {
"list": FleetSerializer,
"retrieve": FleetDetailSerializer,
Expand All @@ -42,10 +42,16 @@ def get_permissions(self):
case _:
return super().get_permissions()

def get_object(self):
uuid = self.kwargs.get("pk", None)
if uuid is None:
return super().get_object()
return Fleet.objects.get(uuid=uuid)

def list(self, request):
space_containers = request.query_params.get("space_containers", True)
if not space_containers:
serializer = self.serializer_class(self.get_queryset(), many=True)
serializer = self.get_serializer(self.get_queryset(), many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

# Get spaces from API key
Expand All @@ -54,21 +60,23 @@ def list(self, request):
fleets = []
for space in spaces:
print("FLEET", space.fleets)
serializer = self.serializer_class(space.fleets, many=True)
serializer = self.get_serializer(space.fleets, many=True)
if serializer.data != []:
fleets.append(*serializer.data)
fleets += serializer.data
return Response(fleets, status=status.HTTP_200_OK)

def retrieve(self, request, pk=None):
pass
instance = self.get_object()
space = self.get_space(request)
serializer = self.get_serializer(instance, space=space)
return Response(serializer.data, status=status.HTTP_200_OK)

def create(self, request, *args, **kwargs):
return Response(status=status.HTTP_501_NOT_IMPLEMENTED)
space = self.get_space(request)
serializer = self.serializer_class(data=request.data, space=space)
serializer = self.get_serializer(data=request.data, space=space)
serializer.is_valid(raise_exception=True)
fleet = serializer.save()

fleet.invest(space, 0, "USDT")
return Response(status=status.HTTP_201_CREATED)

def delete(self):
Expand Down

0 comments on commit aa12309

Please sign in to comment.