mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-10 00:22:21 +01:00
Song reporting and user stats
This commit is contained in:
committed by
Daan Sprenkels
parent
0c1f9cb08d
commit
f6fcc63450
@ -1,4 +1,4 @@
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime, timedelta, time
|
||||
|
||||
from django.core.cache import caches
|
||||
from django.conf import settings
|
||||
@ -8,19 +8,29 @@ from django.utils import timezone
|
||||
|
||||
from queues.models import PlaylistSong
|
||||
from songs.models import Song
|
||||
from marietje.models import User
|
||||
|
||||
|
||||
def recache_stats():
|
||||
new_stats = compute_stats()
|
||||
caches['default'].delete('stats')
|
||||
caches['default'].set('stats', new_stats, 7200)
|
||||
caches['default'].set('stats', new_stats, 2 * 3600)
|
||||
return new_stats
|
||||
|
||||
def recache_user_stats():
|
||||
users = User.objects.exclude(Q(id=None)
|
||||
| Q(id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values('id')
|
||||
for user in users:
|
||||
new_stats = user_stats(user['id'])
|
||||
cacheloc = 'userstats_{}'.format(user['id'])
|
||||
caches['default'].delete(cacheloc)
|
||||
caches['default'].set(cacheloc, new_stats, 48 * 3600)
|
||||
return new_stats
|
||||
|
||||
def to_days(time):
|
||||
for tr in time:
|
||||
tr['duration'] = str(round(tr['total']/86400, 2)) + ' days'
|
||||
return time
|
||||
|
||||
for tr in time:
|
||||
tr['duration'] = str(round(tr['total'] / 86400, 2)) + ' days'
|
||||
return time
|
||||
|
||||
def compute_stats():
|
||||
# We want to grab the time now, because otherwise we would be reporting a minute too late
|
||||
@ -48,13 +58,14 @@ def compute_stats():
|
||||
Q(user_id=None)
|
||||
| Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values(
|
||||
'user__id', '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]
|
||||
|
||||
total_requests=Count('id', distinct=True),
|
||||
unique_requests=Count('song__id', distinct=True)).order_by(
|
||||
'-total_requests')[:settings.STATS_TOP_COUNT]
|
||||
|
||||
total_unique_requests = PlaylistSong.objects.filter(state=2).exclude(
|
||||
Q(user_id=None)
|
||||
| Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).distinct().count()
|
||||
| Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).aggregate(
|
||||
total=Count('song__id', distinct=True))
|
||||
|
||||
most_played_songs = PlaylistSong.objects.filter(state=2).exclude(
|
||||
Q(user_id=None)
|
||||
@ -69,18 +80,23 @@ def compute_stats():
|
||||
'song__artist',
|
||||
'song__title').annotate(total=Count('id')).order_by(
|
||||
'-total', 'song__artist')[:settings.STATS_TOP_COUNT]
|
||||
|
||||
|
||||
time_requested = PlaylistSong.objects.filter(state=2).exclude(
|
||||
Q(user_id=None)
|
||||
Q(user_id=None)
|
||||
| Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values(
|
||||
'user__id', 'user__name').annotate(total=Sum('song__duration')).order_by(
|
||||
'-total')[:settings.STATS_TOP_COUNT]
|
||||
|
||||
'user__id', 'user__name').annotate(total=Sum('song__duration')).order_by(
|
||||
'-total')[:settings.STATS_TOP_COUNT]
|
||||
|
||||
total_time_requested = PlaylistSong.objects.all().filter(state=2).exclude(
|
||||
Q(user_id=None)
|
||||
Q(user_id=None)
|
||||
| Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).aggregate(
|
||||
total=Sum('song__duration'))
|
||||
|
||||
total=Sum('song__duration'))
|
||||
|
||||
most_requested_uploaders = PlaylistSong.objects.filter(state=2).exclude(
|
||||
Q(user_id=None)
|
||||
| Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values(
|
||||
'song__user__id', 'song__user__name').annotate(total=Count(
|
||||
'song__user__id')).order_by('-total')[:settings.STATS_TOP_COUNT]
|
||||
|
||||
return {
|
||||
'last_updated': last_updated,
|
||||
@ -93,7 +109,59 @@ def compute_stats():
|
||||
'most_played_songs': list(most_played_songs),
|
||||
'most_played_songs_14_days': list(most_played_songs_14_days),
|
||||
'time_requested': to_days(list(time_requested)),
|
||||
'total_time_requested': str(round(float(total_time_requested['total'])/86400, 2)) + ' days',
|
||||
'total_time_requested': str(round(float(total_time_requested['total']) / 86400, 2)) + ' days',
|
||||
'stats_top_count': settings.STATS_TOP_COUNT,
|
||||
|
||||
'most_requested_uploaders': list(most_requested_uploaders),
|
||||
}
|
||||
|
||||
def user_stats(request):
|
||||
last_updated = datetime.now()
|
||||
|
||||
total_uploads = Song.objects.filter(
|
||||
user__id=request, deleted=False).count()
|
||||
|
||||
total_requests = PlaylistSong.objects.filter(
|
||||
user__id=request, state=2).count()
|
||||
|
||||
unique_requests = PlaylistSong.objects.filter(
|
||||
user__id=request, state=2,
|
||||
song_id__isnull=False).values('song_id').distinct().count()
|
||||
|
||||
most_played_songs = PlaylistSong.objects.filter(
|
||||
user__id=request, 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', 'song__title')
|
||||
|
||||
most_played_uploaders = PlaylistSong.objects.filter(
|
||||
user__id=request, state=2).exclude(
|
||||
Q(user_id=None)
|
||||
| Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values(
|
||||
'song__user__id', 'song__user__name').annotate(
|
||||
total=Count('song__user__id')).order_by('-total')
|
||||
|
||||
most_played_uploads = PlaylistSong.objects.filter(
|
||||
state=2, song_id__in=Song.objects.filter(user__id=request)).exclude(
|
||||
Q(user_id=None)
|
||||
| Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values(
|
||||
'pk').values(
|
||||
'song__artist',
|
||||
'song__title').annotate(total=Count('id')).order_by(
|
||||
'-total', 'song__artist', 'song__title')
|
||||
|
||||
total_played_uploads = most_played_uploads.aggregate(newtotal=Sum('total'))
|
||||
|
||||
return {
|
||||
'last_updated': last_updated,
|
||||
'total_uploads': total_uploads,
|
||||
'total_requests': total_requests,
|
||||
'unique_requests': unique_requests,
|
||||
'most_played_songs': list(most_played_songs),
|
||||
'most_played_uploaders': list(most_played_uploaders),
|
||||
'most_played_uploads': list(most_played_uploads),
|
||||
'stats_top_count': settings.STATS_TOP_COUNT,
|
||||
'total_played_uploads': total_played_uploads['newtotal'],
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user