Skip to content

Commit

Permalink
Add endpoints to - return model_types, stats based on year, month and…
Browse files Browse the repository at this point in the history
… day
  • Loading branch information
DEENUU1 committed Dec 9, 2023
1 parent 090a8cb commit 06a6e0c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions fjob/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
path('api/company/', include('company.urls')),
path('api/candidate/', include('candidate.urls')),
path('api/payment/', include('payment.urls')),
path('api/stats/', include('stats.urls')),
path('docs/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
]
if settings.DEBUG:
Expand Down
8 changes: 8 additions & 0 deletions stats/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework.serializers import ModelSerializer
from .models import Statistics


class StatisticsSerializer(ModelSerializer):
class Meta:
model = Statistics
fields = '__all__'
16 changes: 16 additions & 0 deletions stats/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.urls import path
from .views import (
StatisticsDayView,
StatisticsMonthView,
StatisticsYearView,
StatisticsAggregatedView,
ModelTypeListView,
)

urlpatterns = [
path('day/<int:year>/<int:month>/<int:day>/', StatisticsDayView.as_view(), name='statistics-day'),
path('month/<int:year>/<int:month>/', StatisticsMonthView.as_view(), name='statistics-month'),
path('year/<int:year>/', StatisticsYearView.as_view(), name='statistics-year'),
path('aggregated/<str:scale>/', StatisticsAggregatedView.as_view(), name='statistics-aggregated'),
path('model/', ModelTypeListView.as_view(), name='model-type-list'),
]
84 changes: 84 additions & 0 deletions stats/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Statistics
from .serializers import StatisticsSerializer
from django.db.models import Count
from django.db.models.functions import TruncDay, TruncMonth, TruncYear
from rest_framework.permissions import IsAdminUser


class StatisticsDayView(APIView):
permission_classes = [IsAdminUser, ]

def get(self, request, year, month, day, model_type=None):
queryset = Statistics.objects.filter(
created_at__year=year, created_at__month=month, created_at__day=day
).filter(model_type=model_type) if model_type else Statistics.objects.filter(
created_at__year=year, created_at__month=month, created_at__day=day
)
serializer = StatisticsSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)


class StatisticsMonthView(APIView):
permission_classes = [IsAdminUser, ]

def get(self, request, year, month, model_type=None):
queryset = Statistics.objects.filter(
created_at__year=year, created_at__month=month
).filter(model_type=model_type) if model_type else Statistics.objects.filter(
created_at__year=year, created_at__month=month
)
serializer = StatisticsSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)


class StatisticsYearView(APIView):
permission_classes = [IsAdminUser, ]

def get(self, request, year, model_type=None):
queryset = Statistics.objects.filter(
created_at__year=year
).filter(model_type=model_type) if model_type else Statistics.objects.filter(
created_at__year=year
)
serializer = StatisticsSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)


class StatisticsAggregatedView(APIView):
permission_classes = [IsAdminUser, ]

def get(self, request, scale, model_type=None):
if scale == 'day':
queryset = Statistics.objects.annotate(date=TruncDay('created_at')).values('date').annotate(
count=Count('id')).filter(model_type=model_type) if model_type else Statistics.objects.annotate(
date=TruncDay('created_at')).values('date').annotate(count=Count('id'))
elif scale == 'month':
queryset = Statistics.objects.annotate(date=TruncMonth('created_at')).values('date').annotate(
count=Count('id')).filter(model_type=model_type) if model_type else Statistics.objects.annotate(
date=TruncMonth('created_at')).values('date').annotate(count=Count('id'))
elif scale == 'year':
queryset = Statistics.objects.annotate(date=TruncYear('created_at')).values('date').annotate(
count=Count('id')).filter(model_type=model_type) if model_type else Statistics.objects.annotate(
date=TruncYear('created_at')).values('date').annotate(count=Count('id'))
else:
return Response(
{
"error": "Invalid scale. Supported scales are 'day', 'month', and 'year'."
},
status=status.HTTP_400_BAD_REQUEST
)

serializer = StatisticsSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)


class ModelTypeListView(APIView):
permission_classes = [IsAdminUser, ]

def get(self, request):
queryset = Statistics.objects.values('model_type').distinct()
serializer = StatisticsSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

0 comments on commit 06a6e0c

Please sign in to comment.