From afb0886fcaf2655b3efc2c3bfb1bf4275e97c1b0 Mon Sep 17 00:00:00 2001 From: oslomp Date: Tue, 18 Dec 2018 18:53:49 +0100 Subject: [PATCH] Added explanation to stats and changed some details --- marietje/stats/templates/stats/stats.html | 33 ++--- marietje/stats/templates/stats/user.html | 15 ++- marietje/stats/utils.py | 143 ++++++++++++++++------ 3 files changed, 134 insertions(+), 57 deletions(-) diff --git a/marietje/stats/templates/stats/stats.html b/marietje/stats/templates/stats/stats.html index ae73f43..f6ed1f6 100644 --- a/marietje/stats/templates/stats/stats.html +++ b/marietje/stats/templates/stats/stats.html @@ -18,8 +18,8 @@

Uploads

-

Total: {{ stats.total_uploads }}

-

Top {{ stats.stats_top_count }}:

+

In total {{ stats.total_uploads }} songs have been uploaded. + These are the {{ stats.stats_top_count }} people who have uploaded the most.

@@ -43,8 +43,8 @@

Requests

-

Total: {{ stats.total_requests }}

-

Top {{ stats.stats_top_count }}:

+

In total {{ stats.total_requests }} songs have been requested. + These are the {{ stats.stats_top_count }} people who have requested the most songs.

@@ -68,8 +68,9 @@

Time requested

-

Total: {{stats.total_time_requested}}

-

Top {{ stats.stats_top_count }}:

+

In total {{ stats.total_time_requested }} of music have been requested, with an + average song length of {{ stats.total_average }}. + These are the {{ stats.stats_top_count }} people with the longest total time queued

@@ -95,8 +96,9 @@

Unique requests

-

Total: {{stats.total_unique_requests.total}}

-

Top {{ stats.stats_top_count }}:

+

In total {{stats.total_unique_requests.total}} different songs + have been requested. The {{ stats.stats_top_count }} people that have requested the largest number of + different songs are shown below.

@@ -120,7 +122,7 @@

Most played songs

-

Top {{ stats.stats_top_count }}:

+

These are the {{ stats.stats_top_count }} most played songs ever.

@@ -146,22 +148,25 @@

Most played uploaders

-

Top {{ stats.stats_top_count }}:

+

The left column shows the {{ stats.stats_top_count }} people whose songs are requested most often by other people + people. The right column shows how many times that person has queued his own songs.

- + + {% for stat in stats.most_requested_uploaders %} - - + + + {% endfor %} @@ -170,7 +175,7 @@

Most played songs last 14 days

-

Top {{ stats.stats_top_count }}:

+

These songs are played the {{ stats.stats_top_count }} most in the last two weeks.

# User# Songs# Others# Own
{{ forloop.counter }}{{ stat.song__user__name }}{{ stat.total }} ({% widthratio stat.total stats.total_requests 100 %}%){{ stat.name }}{{ stat.total }}{{ stat.own_total}}
diff --git a/marietje/stats/templates/stats/user.html b/marietje/stats/templates/stats/user.html index d6a877c..0b373a5 100644 --- a/marietje/stats/templates/stats/user.html +++ b/marietje/stats/templates/stats/user.html @@ -19,11 +19,11 @@ {% endif %} -

Total uploads: {{ stats.total_uploads }}

-

Unique requests: {{ stats.unique_requests }} ({% widthratio stats.unique_requests stats.total_requests 100 %}%)

Most played songs

-

Total: {{ stats.total_requests }}

+

You have requested {{ stats.unique_requests }} different + songs a total of {{ stats.total_requests }} times. This + means {% widthratio stats.unique_requests stats.total_requests 100 %}% of your requests have been unique.

Top {{ stats.stats_top_count }}:

@@ -51,8 +51,11 @@

Uploads requested

-

Total played by you: {{stats.total_played_user_uploads}}

-

Total played by others: {{stats.total_played_uploads}}

+

You have uploaded a total of {{stats.total_uploads }} songs. The left column + shows how many times these have been requested by other people. The right column shows + how many times you requested your own songs. In total your songs + have been queued {{stats.total_played_uploads }} times by others and + {{stats.total_played_user_uploads }} by yourself.

Top {{ stats.stats_top_count }}:

@@ -81,7 +84,7 @@

Most played uploaders

-

Top {{ stats.stats_top_count }}:

+

The people whose songs you have queued the most are:

diff --git a/marietje/stats/utils.py b/marietje/stats/utils.py index d5a68a4..20b95cd 100644 --- a/marietje/stats/utils.py +++ b/marietje/stats/utils.py @@ -16,9 +16,11 @@ def recache_stats(): caches['default'].delete('stats') caches['default'].set('stats', new_stats, 2 * 3600) return new_stats - + + def recache_user_stats(): - users = User.objects.exclude(Q(id=None) + 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']) @@ -27,13 +29,51 @@ def recache_user_stats(): caches['userstats'].set(cacheloc, new_stats, 48 * 3600) return new_stats + def time_convert(time): - for tr in time: - tr['duration'] = str(round(tr['total'] / 86400, 2)) + ' days' - avg_dur_sec = tr['avg_dur']%60 - avg_dur_min = int((tr['avg_dur']-avg_dur_sec)/60) - tr['avg_dur'] = '{}:{}'.format(avg_dur_min, avg_dur_sec) - return time + try: + for tr in time: + tr['duration'] = str(round(tr['total'] / 86400, 2)) + ' days' + avg_dur_sec = tr['avg_dur'] % 60 + avg_dur_min = int((tr['avg_dur'] - avg_dur_sec) / 60) + tr['avg_dur'] = '{}:{}'.format(avg_dur_min, avg_dur_sec) + return time + except: + avg_dur_sec = round(time % 60) + avg_dur_min = int(round((time - avg_dur_sec) / 60)) + return ('{} minutes and {} seconds'.format(avg_dur_min, avg_dur_sec)) + + +def calculate_uploaders(requests_uploader, most_requested_uploaders): + for x in requests_uploader: + a = len(most_requested_uploaders) + b = 0 + while b <= a: + if b == a: + adding_list_item(most_requested_uploaders, x) + elif x['song__user__id'] == most_requested_uploaders[b]['id']: + if x['song__user__name'] == x['user__name']: + most_requested_uploaders[b]['own_total'] = x['total'] + else: + most_requested_uploaders[b]['total'] += x['total'] + break + b += 1 + return + + +def adding_list_item(list, x): + list.append({ + 'id': x['song__user__id'], + 'name': x['song__user__name'], + 'total': 0, + 'own_total': 0 + }) + if x['song__user__id'] == x['user__id']: + list[-1]['own_total']: x['total'] + else: + list[-1]['_total']: x['total'] + return + def compute_stats(): # We want to grab the time now, because otherwise we would be reporting a minute too late @@ -78,45 +118,74 @@ def compute_stats(): '-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] + 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] time_requested = PlaylistSong.objects.filter(state=2).exclude( Q(user_id=None) | Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values( - 'user__id', 'user__name').annotate(total=Sum('song__duration'), avg_dur=Sum('song__duration')/Count('id')).order_by( - '-total')[:settings.STATS_TOP_COUNT] + 'user__id', 'user__name').annotate( + total=Sum('song__duration'), + avg_dur=Sum('song__duration') / + Count('id')).order_by('-total')[:settings.STATS_TOP_COUNT] total_time_requested = PlaylistSong.objects.all().filter(state=2).exclude( Q(user_id=None) | Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).aggregate( total=Sum('song__duration')) - most_requested_uploaders = PlaylistSong.objects.filter(state=2).exclude( + total_time_overall = 0 + count = 0 + for x in list(time_requested): + total_time_overall += x['avg_dur'] + count += 1 + total_average = total_time_overall / count + total_average = time_convert(total_average) + + requests_uploader = 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] + 'song__user__id', 'song__user__name', 'user__name', + 'user__id').annotate(total=Count('song__user__name')) + + most_requested_uploaders = [] + calculate_uploaders(list(requests_uploader), most_requested_uploaders) return { - 'last_updated': last_updated, - 'total_uploads': total_uploads, - 'upload_stats': list(upload_stats), - 'total_requests': total_requests, - 'request_stats': list(request_stats), - 'unique_request_stats': list(unique_request_stats), - 'total_unique_requests': total_unique_requests, - 'most_played_songs': list(most_played_songs), - 'most_played_songs_14_days': list(most_played_songs_14_days), - 'time_requested': time_convert(list(time_requested)), - '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), + 'last_updated': + last_updated, + 'total_uploads': + total_uploads, + 'upload_stats': + list(upload_stats), + 'total_requests': + total_requests, + 'request_stats': + list(request_stats), + 'unique_request_stats': + list(unique_request_stats), + 'total_unique_requests': + total_unique_requests, + 'most_played_songs': + list(most_played_songs), + 'most_played_songs_14_days': + list(most_played_songs_14_days), + 'time_requested': + time_convert(list(time_requested)), + '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), + 'total_average': + total_average, } + def user_stats(request): last_updated = datetime.now() @@ -148,11 +217,12 @@ def user_stats(request): '-total')[:settings.STATS_TOP_COUNT] most_played_uploads = PlaylistSong.objects.filter( - state=2, song_id__in=Song.objects.filter(user__id=request)).exclude(user__id=None).values( - 'song__artist', - 'song__title').annotate(total=Count('id', filter=~Q(user__id=request)), user_total=Count('id', filter=Q(user__id=request))).order_by( - '-total', 'song__artist', - 'song__title')[:settings.STATS_TOP_COUNT] + state=2, song_id__in=Song.objects.filter(user__id=request)).exclude( + user__id=None).values('song__artist', 'song__title').annotate( + total=Count('id', filter=~Q(user__id=request)), + user_total=Count('id', filter=Q(user__id=request))).order_by( + '-total', 'song__artist', + 'song__title')[:settings.STATS_TOP_COUNT] most_played = list(most_played_uploads) total_played_uploads = 0 @@ -173,4 +243,3 @@ def user_stats(request): 'total_played_uploads': total_played_uploads, 'total_played_user_uploads': total_played_user_uploads, } -