added average song length to stats

This commit is contained in:
oslomp
2019-01-10 20:53:11 +01:00
parent 6a86cf1c0a
commit 274949c519
2 changed files with 10 additions and 5 deletions

View File

@ -27,9 +27,12 @@ def recache_user_stats():
caches['userstats'].set(cacheloc, new_stats, 48 * 3600)
return new_stats
def to_days(time):
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
def compute_stats():
@ -84,7 +87,7 @@ def compute_stats():
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')).order_by(
'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(
@ -108,7 +111,7 @@ def compute_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': to_days(list(time_requested)),
'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),