Skip to content

Commit

Permalink
fix(fleet): export bout_count method to fleet's model
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenepix committed Jan 7, 2024
1 parent b8ca67a commit 1d99f34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
13 changes: 1 addition & 12 deletions django_napse/api/fleets/serializers/fleet_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,7 @@ def get_value(self, instance):
return instance.space_frame_value(space=self.space)

def get_bot_count(self, instance):
query_bot = instance.bots.all()
if self.space is None:
return len(query_bot)
result = []
for bot in query_bot:
try:
bot_space = bot.space
except BotError.InvalidSetting:
continue
if bot_space == self.space:
result.append(bot)
return len(result)
return instance.bot_count(space=self.space)

def get_delta(self, instance) -> float:
"""Delta on the last 30 days."""
Expand Down
22 changes: 19 additions & 3 deletions django_napse/core/models/fleets/fleet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django_napse.core.models.connections.connection import Connection
from django_napse.core.models.fleets.managers import FleetManager
from django_napse.core.models.orders.order import Order
from django_napse.utils.errors import BotError


class Fleet(models.Model):
Expand Down Expand Up @@ -90,13 +91,28 @@ def invest(self, space, amount, ticker):
connections += cluster.invest(space, amount * cluster.share, ticker)
return connections

def bot_count(self, space=None) -> int:
"""Count number of bots in fleet, depends on space frame."""
query_bot = self.bots.all()
if space is None:
return len(query_bot)
result = []
for bot in query_bot:
try:
bot_space = bot.space
except BotError.InvalidSetting:
continue
if bot_space == self.space:
result.append(bot)
return len(result)

def get_stats(self):
order_count = Order.objects.filter(
order_count = Order.objects.filter( # noqa: F841
connection__bot__in=self.bots,
created_at__gt=datetime.now(tz=get_default_timezone()) - timedelta(days=30),
).count()
return {
"value": self.value,
"order_count_30": order_count,
"change_30": 0, # TODO: Need history
"bot_count": self.bot_count(),
"delta_30": 0, # TODO: Need history
}

0 comments on commit 1d99f34

Please sign in to comment.