-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add endpoints to - return model_types, stats based on year, month and…
… day
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |