@@ -207,3 +207,53 @@ def get(self, request):
207207 }
208208 )
209209 return Response (result .data )
210+
211+
212+ class PopularPortsAPIView (APIView ):
213+ @method_decorator (cache_page (60 * 60 )) # Cache for 1 hour
214+ def get (self , request ):
215+ days = int (request .GET .get ('days' , 30 ))
216+ limit = int (request .GET .get ('limit' , 10 ))
217+
218+ # Get submissions from the last X days
219+ end_date = datetime .datetime .now (tz = datetime .timezone .utc )
220+ start_date = end_date - datetime .timedelta (days = days )
221+
222+ submissions_unique = Submission .objects .filter (
223+ timestamp__range = [start_date , end_date ]
224+ ).order_by ('user' , '-timestamp' ).distinct ('user' )
225+
226+ # Get popular ports based on installation counts
227+ popular_ports = PortInstallation .objects .filter (
228+ submission_id__in = Subquery (submissions_unique .values ('id' )),
229+ requested = True
230+ ).exclude (
231+ port__icontains = 'mpstats'
232+ ).values ('port' ).annotate (
233+ total_count = Count ('port' ),
234+ req_count = Count (Case (When (requested = True , then = 1 ), output_field = IntegerField ()))
235+ ).order_by ('-total_count' )[:limit ]
236+
237+ return Response (list (popular_ports ))
238+
239+
240+ class EnhancedStatisticsAPIView (APIView ):
241+ @method_decorator (cache_page (60 * 60 )) # Cache for 1 hour
242+ def get (self , request ):
243+ from port .models import Port
244+
245+ # Get basic statistics
246+ current_week = datetime .datetime .today ().isocalendar ()[1 ]
247+ all_submissions = Submission .objects .all ().only ('id' )
248+ total_unique_users = all_submissions .distinct ('user' ).count ()
249+ current_week_unique = all_submissions .filter (timestamp__week = current_week ).distinct ('user' ).count ()
250+ last_week_unique = all_submissions .filter (timestamp__week = current_week - 1 ).distinct ('user' ).count ()
251+ total_ports = Port .objects .filter (active = True ).count ()
252+
253+ return Response ({
254+ 'current_week' : current_week_unique ,
255+ 'last_week' : last_week_unique ,
256+ 'total_submissions' : all_submissions .count (),
257+ 'total_unique_users' : total_unique_users ,
258+ 'total_ports' : total_ports ,
259+ })
0 commit comments