mirror of
https://github.com/nprasad2077/nbaStats.git
synced 2026-11-08 02:50:32 +00:00
3 files changed
+151
-14
No files matched your search
@@ -6,3 +6,8 @@ class PlayerDataSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = PlayerData
|
||||
fields = '__all__'
|
||||
|
||||
class HistogramDataSerializer(serializers.Serializer):
|
||||
range = serializers.CharField()
|
||||
count = serializers.IntegerField()
|
||||
|
||||
+9
-1
@@ -1,5 +1,5 @@
|
||||
from django.urls import path
|
||||
from .views import PlayerDataList, PlayerDataByTeamList, PlayerDataBySeasonList, PlayerDataByNameList, TopScorersbySeasonList
|
||||
from .views import PlayerDataList, PlayerDataByTeamList, PlayerDataBySeasonList, PlayerDataByNameList, TopScorersbySeasonList, ThreeTwoTopPointTrends, TopAssistsBySeasonList, TopReboundsBySeasonList, TopBlocksBySeasonList, TopStealsBySeasonList, TopOffensiveReboundsBySeasonList, TopDefensiveReboundsBySeasonList, PointsPerGameHistogramBySeasonList
|
||||
|
||||
urlpatterns = [
|
||||
path('api/playerdata/', PlayerDataList.as_view(), name='playerdata_list'),
|
||||
@@ -7,4 +7,12 @@ urlpatterns = [
|
||||
path('api/playerdata/season/<str:season>/', PlayerDataBySeasonList.as_view(), name='playerdata_by_season'),
|
||||
path('api/playerdata/name/<str:name>/', PlayerDataByNameList.as_view(), name='playerdata_by_name'),
|
||||
path('api/playerdata/topscorers/season/<str:season>/', TopScorersbySeasonList.as_view(), name='top_scorers_by_season'),
|
||||
path('api/three_two_point_trends/', ThreeTwoTopPointTrends.as_view(), name='three_two_point_trends'),
|
||||
path('api/top_assists/<int:season>/', TopAssistsBySeasonList.as_view(), name='top_assists'),
|
||||
path('api/top_rebounds/<int:season>/', TopReboundsBySeasonList.as_view(), name='top_rebounds'),
|
||||
path('api/top_blocks/<int:season>/', TopBlocksBySeasonList.as_view(), name='top_blocks'),
|
||||
path('api/top_steals/<int:season>/', TopStealsBySeasonList.as_view(), name='top_steals'),
|
||||
path('api/top_rebounds_offensive/<int:season>/', TopOffensiveReboundsBySeasonList.as_view(), name='top_rebounds_ORB'),
|
||||
path('api/top_rebounds_defensive/<int:season>/', TopDefensiveReboundsBySeasonList.as_view(), name='top_rebounds_DRB'),
|
||||
path('api/points_per_game_histogram/<int:season>/', PointsPerGameHistogramBySeasonList.as_view(), name='points_per_game_histogram'),
|
||||
]
|
||||
+137
-13
@@ -3,7 +3,12 @@ from django.http import JsonResponse
|
||||
from django.core import serializers
|
||||
from .models import PlayerData
|
||||
from rest_framework import generics
|
||||
from .serializers import PlayerDataSerializer
|
||||
from .serializers import PlayerDataSerializer, HistogramDataSerializer
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from django.db.models import Avg
|
||||
from collections import Counter
|
||||
import math
|
||||
|
||||
|
||||
# Create your views here.
|
||||
@@ -13,40 +18,159 @@ class PlayerDataList(generics.ListCreateAPIView):
|
||||
queryset = PlayerData.objects.all().order_by('id')
|
||||
serializer_class = PlayerDataSerializer
|
||||
# pagination_class = None
|
||||
|
||||
|
||||
|
||||
|
||||
# Fetch player data by team
|
||||
class PlayerDataByTeamList(generics.ListAPIView):
|
||||
serializer_class = PlayerDataSerializer
|
||||
|
||||
|
||||
def get_queryset(self):
|
||||
team = self.kwargs['team']
|
||||
return PlayerData.objects.filter(team=team)
|
||||
|
||||
|
||||
|
||||
|
||||
# Fetch player data by season
|
||||
class PlayerDataBySeasonList(generics.ListAPIView):
|
||||
serializer_class = PlayerDataSerializer
|
||||
|
||||
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
return PlayerData.objects.filter(season=season)
|
||||
|
||||
|
||||
|
||||
|
||||
# Fetch player data by player name
|
||||
class PlayerDataByNameList(generics.ListAPIView):
|
||||
serializer_class = PlayerDataSerializer
|
||||
|
||||
|
||||
def get_queryset(self):
|
||||
name = self.kwargs['name']
|
||||
return PlayerData.objects.filter(name__icontains=name)
|
||||
|
||||
|
||||
|
||||
|
||||
# Fetch Top 20 players by PTS DESC for the season specified.
|
||||
class TopScorersbySeasonList(generics.ListAPIView):
|
||||
serializer_class = PlayerDataSerializer
|
||||
|
||||
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
return PlayerData.objects.filter(season=season).order_by('-PTS')[:20]
|
||||
|
||||
|
||||
# Fetch and calculate the average 3P made, 3P attemps, 2P made, 2P attempts for all players in each season.
|
||||
# Will use APIview here instead of generics.ListAPIView to avoid having to create a serializer to generate the queryset. avg_three_made and avg_two_made are new data points at the time of creation when compared to the per_game model.
|
||||
class ThreeTwoTopPointTrends(APIView):
|
||||
def get(self, request, format=None):
|
||||
seasons = PlayerData.objects.values_list(
|
||||
'season', flat=True).distinct().order_by('season')
|
||||
data = []
|
||||
|
||||
for season in seasons:
|
||||
season_data = PlayerData.objects.filter(season=season)
|
||||
avg_three_made = season_data.aggregate(Avg('three_fg'))[
|
||||
'three_fg__avg']
|
||||
avg_three_attempts = season_data.aggregate(Avg('three_attempts'))[
|
||||
'three_attempts__avg']
|
||||
avg_two_made = season_data.aggregate(Avg('two_fg'))['two_fg__avg']
|
||||
avg_two_attempts = season_data.aggregate(Avg('two_attempts'))[
|
||||
'two_attempts__avg']
|
||||
|
||||
data.append({
|
||||
'season': season,
|
||||
'avg_three_made': avg_three_made,
|
||||
'avg_three_attempts': avg_three_attempts,
|
||||
'avg_two_made': avg_two_made,
|
||||
'avg_two_attempts': avg_two_attempts,
|
||||
})
|
||||
|
||||
return Response(data)
|
||||
|
||||
# Fetch Top 20 players by AST DESC for season specified.
|
||||
|
||||
|
||||
class TopAssistsBySeasonList(generics.ListAPIView):
|
||||
serializer_class = PlayerDataSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
return PlayerData.objects.filter(season=season).order_by('-AST')[:20]
|
||||
|
||||
# Fetch Top 20 players by TRB DESC for season specified.
|
||||
|
||||
|
||||
class TopReboundsBySeasonList(generics.ListAPIView):
|
||||
serializer_class = PlayerDataSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
return PlayerData.objects.filter(season=season).order_by('-TRB')[:20]
|
||||
|
||||
# Fetch Top 20 players by BLK DESC for specified season.
|
||||
|
||||
|
||||
class TopBlocksBySeasonList(generics.ListAPIView):
|
||||
serializer_class = PlayerDataSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
return PlayerData.objects.filter(season=season).order_by('-BLK')[:20]
|
||||
|
||||
# Fetch Top 20 players by STL DESC for season specified.
|
||||
|
||||
|
||||
class TopStealsBySeasonList(generics.ListAPIView):
|
||||
serializer_class = PlayerDataSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
return PlayerData.objects.filter(season=season).order_by('-STL')[:20]
|
||||
|
||||
# Fetch Top 20 players by ORB DESC for season.
|
||||
|
||||
|
||||
class TopOffensiveReboundsBySeasonList(generics.ListAPIView):
|
||||
serializer_class = PlayerDataSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
return PlayerData.objects.filter(season=season).order_by('-ORB')[:20]
|
||||
|
||||
|
||||
# Fetch Top 20 players by DRB DESC for season.
|
||||
|
||||
class TopDefensiveReboundsBySeasonList(generics.ListAPIView):
|
||||
serializer_class = PlayerDataSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
return PlayerData.objects.filter(season=season).order_by('-DRB')[:20]
|
||||
|
||||
# PTS Histogram
|
||||
|
||||
|
||||
class PointsPerGameHistogramBySeasonList(generics.ListAPIView):
|
||||
serializer_class = HistogramDataSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
season = self.kwargs['season']
|
||||
queryset = PlayerData.objects.filter(season=season)
|
||||
|
||||
# Get the points per game for each player
|
||||
points_per_game_list = [float(player.PTS) for player in queryset]
|
||||
|
||||
# Define histogram bins (e.g., 0-5, 5-10, 10-15, etc.)
|
||||
bin_ranges = range(0, int(math.ceil(max(points_per_game_list))) + 1, 5)
|
||||
histogram_data = Counter()
|
||||
|
||||
# Fill the histogram with data
|
||||
for ppg in points_per_game_list:
|
||||
bin_index = next(
|
||||
(i for i, r in enumerate(bin_ranges) if r >= ppg), -1)
|
||||
if bin_index >= 1:
|
||||
bin_label = f"{bin_ranges[bin_index - 1]}-{bin_ranges[bin_index]}"
|
||||
histogram_data[bin_label] += 1
|
||||
|
||||
# Convert histogram_data to list of dicts for serialization
|
||||
histogram_list = [{"range": k, "count": v}
|
||||
for k, v in histogram_data.items()]
|
||||
|
||||
return histogram_list
|
||||
Reference in new issue
Block a user