Skip to content

Commit cb29188

Browse files
committed
added episode count stats based on time elapsed
1 parent 6fdfb40 commit cb29188

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

tmh_registry/registry/api/viewsets.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin
1818
from rest_framework.permissions import IsAuthenticated
1919
from rest_framework.response import Response
20+
from rest_framework.views import APIView
2021
from rest_framework.viewsets import GenericViewSet, ReadOnlyModelViewSet
21-
from django.utils.timezone import now
22+
from django.utils.timezone import now, timedelta
2223

2324
from ..models import (
2425
Discharge,
@@ -306,6 +307,23 @@ def discharge(self, request, pk=None):
306307
),
307308
},
308309
)
310+
311+
@action(detail=False, methods=["get"])
312+
def stats(self, request):
313+
period = request.query_params.get("period") # e.g., "7d", "30d"
314+
episodes = Episode.objects.all()
315+
316+
if period:
317+
try:
318+
days = int(period.rstrip("d"))
319+
start_date = now() - timedelta(days=days)
320+
episodes = episodes.filter(surgery_date__gte=start_date)
321+
except ValueError:
322+
pass
323+
324+
total_count = episodes.count()
325+
return Response({"total_episodes": total_count})
326+
309327
@action(
310328
detail=True,
311329
serializer_class=FollowUpReadSerializer,

0 commit comments

Comments
 (0)