general clean-up

This commit is contained in:
oslomp
2019-01-10 21:01:15 +01:00
parent 1cf4378316
commit 00d0a32ece
2 changed files with 32 additions and 44 deletions

View File

@ -1,9 +1,8 @@
from datetime import datetime, timedelta, time from datetime import datetime, timedelta
from django.core.cache import caches from django.core.cache import caches
from django.conf import settings from django.conf import settings
from django.db.models import Count, Q, Sum from django.db.models import Count, Q, Sum
from django.shortcuts import render
from django.utils import timezone from django.utils import timezone
from queues.models import PlaylistSong from queues.models import PlaylistSong
@ -30,49 +29,34 @@ def recache_user_stats():
return new_stats return new_stats
def time_convert(time): def best_uploaders_list(requests_uploader, most_requested_uploaders):
try: for requests in requests_uploader:
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) a = len(most_requested_uploaders)
b = 0 b = 0
while b <= a: while b <= a:
if b == a: if b == a:
adding_list_item(most_requested_uploaders, x) adding_list_item(most_requested_uploaders, requests)
elif x['song__user__id'] == most_requested_uploaders[b]['id']: elif requests['song__user__id'] == most_requested_uploaders[b]['id']:
if x['song__user__name'] == x['user__name']: if requests['song__user__name'] == requests['user__name']:
most_requested_uploaders[b]['own_total'] = x['total'] most_requested_uploaders[b]['own_total'] = requests['total']
else: else:
most_requested_uploaders[b]['total'] += x['total'] most_requested_uploaders[b]['total'] += requests['total']
break break
b += 1 b += 1
return
def adding_list_item(list, x): def adding_list_item(most_requested_list, requests):
list.append({ # adds a single item to the best_uploaders list
'id': x['song__user__id'], most_requested_list.append({
'name': x['song__user__name'], 'id': requests['song__user__id'],
'name': requests['song__user__name'],
'total': 0, 'total': 0,
'own_total': 0 'own_total': 0
}) })
if x['song__user__id'] == x['user__id']: if requests['song__user__id'] == requests['user__id']:
list[-1]['own_total']: x['total'] most_requested_list[-1]['own_total']: requests['total']
else: else:
list[-1]['_total']: x['total'] most_requested_list[-1]['_total']: requests['total']
return
def compute_stats(): def compute_stats():
@ -137,13 +121,10 @@ def compute_stats():
| Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).aggregate( | Q(user_id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).aggregate(
total=Sum('song__duration')) total=Sum('song__duration'))
total_time_overall = 0 total_time_overall = sum(x['avg_dur'] for x in time_requested)
count = 0 total_average = total_time_overall / len(time_requested)
for x in list(time_requested): avg_dur_min, avg_dur_sec = divmod(total_average, 60)
total_time_overall += x['avg_dur'] total_average = '{} minutes and {} seconds'.format(avg_dur_min, avg_dur_sec)
count += 1
total_average = total_time_overall / count
total_average = time_convert(total_average)
requests_uploader = PlaylistSong.objects.filter(state=2).exclude( requests_uploader = PlaylistSong.objects.filter(state=2).exclude(
Q(user_id=None) Q(user_id=None)
@ -152,7 +133,15 @@ def compute_stats():
'user__id').annotate(total=Count('song__user__name')) 'user__id').annotate(total=Count('song__user__name'))
most_requested_uploaders = [] most_requested_uploaders = []
calculate_uploaders(list(requests_uploader), most_requested_uploaders) best_uploaders_list(list(requests_uploader), most_requested_uploaders)
for time in list(time_requested):
# converts total time and average time in respectively days and minutes, seconds
time['duration'] = str(round(time['total'] / 86400, 2)) + ' days'
avg_dur_min, avg_dur_sec = divmod(time['avg_dur'], 60)
if avg_dur_sec < 10:
avg_dur_sec = '0' + str(avg_dur_sec)
time['avg_dur'] = '{}:{}'.format(avg_dur_min, avg_dur_sec)
return { return {
'last_updated': last_updated, 'last_updated': last_updated,
@ -164,10 +153,11 @@ def compute_stats():
'total_unique_requests': total_unique_requests, 'total_unique_requests': total_unique_requests,
'most_played_songs': list(most_played_songs), 'most_played_songs': list(most_played_songs),
'most_played_songs_14_days': list(most_played_songs_14_days), 'most_played_songs_14_days': list(most_played_songs_14_days),
'time_requested': time_convert(list(time_requested)), 'time_requested': 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, 'stats_top_count': settings.STATS_TOP_COUNT,
'most_requested_uploaders': list(most_requested_uploaders), 'most_requested_uploaders': list(most_requested_uploaders),
'total_average': total_average,
} }
@ -227,4 +217,4 @@ def user_stats(request):
'stats_top_count': settings.STATS_TOP_COUNT, 'stats_top_count': settings.STATS_TOP_COUNT,
'total_played_uploads': total_played_uploads, 'total_played_uploads': total_played_uploads,
'total_played_user_uploads': total_played_user_uploads, 'total_played_user_uploads': total_played_user_uploads,
} }

View File

@ -2,8 +2,6 @@ from datetime import datetime, timedelta
from django.core.cache import caches from django.core.cache import caches
from django.shortcuts import render from django.shortcuts import render
from stats.utils import user_stats
from django.http import JsonResponse, HttpResponseForbidden
def stats(request): def stats(request):