mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 21:52:21 +01:00
Cache the stats page
This commit is contained in:
@ -1,46 +1,28 @@
|
||||
from datetime import timedelta
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from django.conf import settings
|
||||
from django.db.models import Count, Q
|
||||
from django.core.cache import cache
|
||||
from django.shortcuts import render
|
||||
from django.utils import timezone
|
||||
|
||||
from queues.models import PlaylistSong
|
||||
from songs.models import Song
|
||||
|
||||
|
||||
def stats(request):
|
||||
total_uploads = Song.objects.filter(deleted=False).exclude(user_id=None).count()
|
||||
stats = cache.get('stats')
|
||||
status = 503
|
||||
current_age = None
|
||||
current_age_text = None
|
||||
|
||||
upload_stats = Song.objects.filter(deleted=False).exclude(user_id=None).values('user__name').annotate(
|
||||
total=Count('id')).order_by('-total', 'user__name')[:settings.STATS_TOP_COUNT]
|
||||
if stats:
|
||||
status = 200
|
||||
if 'last_updated' in stats:
|
||||
current_age = datetime.now() - stats['last_updated']
|
||||
if current_age < timedelta(minutes=1):
|
||||
current_age_text = 'Stats were updated less than a minute ago.'
|
||||
elif current_age < timedelta(minutes=2):
|
||||
current_age_text = 'Stats were updated one minute ago.'
|
||||
elif current_age < timedelta(minutes=60):
|
||||
minutes = current_age.seconds / 60
|
||||
current_age_text = 'Stats were updated {:.0f} minutes ago.'.format(minutes)
|
||||
else:
|
||||
current_age_text = 'Stats were updated more than an hour ago'
|
||||
|
||||
total_requests = PlaylistSong.objects.filter(state=2).exclude(
|
||||
Q(user_id=None) | Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).count()
|
||||
|
||||
request_stats = PlaylistSong.objects.filter(state=2).exclude(
|
||||
Q(user_id=None) | Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values('user__name').annotate(
|
||||
total=Count('id')).order_by('-total', 'user__name')[:settings.STATS_TOP_COUNT]
|
||||
|
||||
unique_request_stats = PlaylistSong.objects.filter(state=2).exclude(
|
||||
Q(user_id=None) | Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values(
|
||||
'user__name', 'user__name').annotate(
|
||||
total=Count('song_id', distinct=True), ratio=Count('song_id', distinct=True) / Count('id') * 100).order_by(
|
||||
'-total')[:settings.STATS_TOP_COUNT]
|
||||
|
||||
most_played_songs = PlaylistSong.objects.filter(state=2).exclude(
|
||||
Q(user_id=None) | Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values(
|
||||
'song__artist', 'song__title').annotate(total=Count('id')).order_by(
|
||||
'-total', 'song__artist')[:settings.STATS_TOP_COUNT]
|
||||
|
||||
most_played_songs_14_days = PlaylistSong.objects.filter(
|
||||
state=2, played_at__gte=timezone.now() - timedelta(days=14)).exclude(user_id=None).values(
|
||||
'song__artist', 'song__title').annotate(total=Count('id')).order_by(
|
||||
'-total', 'song__artist')[:settings.STATS_TOP_COUNT]
|
||||
|
||||
return render(request, 'stats/stats.html', {'total_uploads': total_uploads, 'upload_stats': upload_stats,
|
||||
'total_requests': total_requests, 'request_stats': request_stats,
|
||||
'unique_request_stats': unique_request_stats,
|
||||
'most_played_songs': most_played_songs,
|
||||
'most_played_songs_14_days': most_played_songs_14_days,
|
||||
'stats_top_count': settings.STATS_TOP_COUNT})
|
||||
data = {'stats': stats, 'current_age': current_age, 'current_age_text': current_age_text}
|
||||
return render(request, 'stats/stats.html', data, status=status)
|
||||
|
||||
Reference in New Issue
Block a user