From 04cdce9ad4f8c75f7553601a69292d90090f2325 Mon Sep 17 00:00:00 2001 From: Jim Driessen Date: Thu, 28 Sep 2017 17:25:21 +0200 Subject: [PATCH] Add more stats. --- marietje/marietje/settings.py | 3 + marietje/playerapi/views.py | 12 +-- .../migrations/0005_playlistsong_played_at.py | 20 +++++ marietje/queues/models.py | 1 + marietje/stats/templates/stats/stats.html | 82 ++++++++++++++++++- marietje/stats/views.py | 49 +++++++++-- 6 files changed, 148 insertions(+), 19 deletions(-) create mode 100644 marietje/queues/migrations/0005_playlistsong_played_at.py diff --git a/marietje/marietje/settings.py b/marietje/marietje/settings.py index 61c4291..1d39fa7 100644 --- a/marietje/marietje/settings.py +++ b/marietje/marietje/settings.py @@ -152,3 +152,6 @@ LIMIT_ALWAYS = False LIMIT_HOURS = (12, 16) CONTACT_EMAIL = 'marietje@science.ru.nl' + +STATS_TOP_COUNT = 50 +STATS_REQUEST_IGNORE_USER_IDS = (51, 515) diff --git a/marietje/playerapi/views.py b/marietje/playerapi/views.py index f3f3786..d81f105 100644 --- a/marietje/playerapi/views.py +++ b/marietje/playerapi/views.py @@ -34,6 +34,10 @@ def play(request): queue = get_object_or_404(Queue, id=request.POST.get('queue')) queue.started_at = timezone.now() queue.save() + current_song = queue.current_song() + current_song.played_at = queue.started_at + current_song.save() + return JsonResponse({}) @@ -46,14 +50,6 @@ def next(request): player_song.save() return JsonResponse({}) -@csrf_exempt -@token_required -def next(request): - queue = get_object_or_404(Queue, id=request.POST.get('queue')) - player_song = queue.current_song() - player_song.state = 2 - player_song.save() - return JsonResponse({}) @csrf_exempt @token_required diff --git a/marietje/queues/migrations/0005_playlistsong_played_at.py b/marietje/queues/migrations/0005_playlistsong_played_at.py new file mode 100644 index 0000000..e51bf7d --- /dev/null +++ b/marietje/queues/migrations/0005_playlistsong_played_at.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-09-26 13:56 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('queues', '0004_remove_playlistsong_order'), + ] + + operations = [ + migrations.AddField( + model_name='playlistsong', + name='played_at', + field=models.DateTimeField(blank=True, null=True), + ), + ] diff --git a/marietje/queues/models.py b/marietje/queues/models.py index f5a0a0e..a67ca89 100644 --- a/marietje/queues/models.py +++ b/marietje/queues/models.py @@ -29,6 +29,7 @@ class PlaylistSong(models.Model): blank=True, null=True ) + played_at = models.DateTimeField(blank=True, null=True) # 0: Queued. # 1: Playing. diff --git a/marietje/stats/templates/stats/stats.html b/marietje/stats/templates/stats/stats.html index eaa361c..8189cd8 100644 --- a/marietje/stats/templates/stats/stats.html +++ b/marietje/stats/templates/stats/stats.html @@ -9,13 +9,14 @@

Uploads

Total: {{ total_uploads }}

+

Top {{ stats_top_count }}:

- + @@ -33,13 +34,14 @@

Requests

Total: {{ total_requests }}

+

Top {{ stats_top_count }}:

# User# Songs# Songs
- + @@ -54,5 +56,81 @@
# User# Requests# Requests
+
+

Unique requests

+

Top {{ stats_top_count }}:

+
+ + + + + + + + + + {% for stat in unique_request_stats %} + + + + + + {% endfor %} + +
#User# Requests
{{ forloop.counter }}{{ stat.user__name }}{{ stat.total }} ({{stat.ratio }}%)
+
+
+
+

Most played songs

+

Top {{ stats_top_count }}:

+
+ + + + + + + + + + + {% for stat in most_played_songs %} + + + + + + + {% endfor %} + +
#ArtistTitle# Requests
{{ forloop.counter }}{{ stat.song__artist }}{{ stat.song__title }}{{ stat.total }}
+
+
+
+

Most played songs last 14 days

+

Top {{ stats_top_count }}:

+
+ + + + + + + + + + + {% for stat in most_played_songs_14_days %} + + + + + + + {% endfor %} + +
#ArtistTitle# Requests
{{ forloop.counter }}{{ stat.song__artist }}{{ stat.song__title }}{{ stat.total }}
+
+
{% endblock %} diff --git a/marietje/stats/views.py b/marietje/stats/views.py index 740c5f0..caba1fa 100644 --- a/marietje/stats/views.py +++ b/marietje/stats/views.py @@ -1,15 +1,46 @@ +from datetime import timedelta + +from django.conf import settings +from django.db.models import Count, Q from django.shortcuts import render -from django.db.models import Count -from songs.models import Song +from django.utils import timezone + from queues.models import PlaylistSong +from songs.models import Song def stats(request): - total_uploads = Song.objects.all().filter(deleted=False).exclude(user_id=None).count() - upload_stats = Song.objects.all().filter(deleted=False).exclude(user_id=None).values('user__name')\ - .annotate(total=Count('id')).order_by('-total') - total_requests = PlaylistSong.objects.all().filter(state=2).exclude(user_id=None).count() - request_stats = PlaylistSong.objects.all().filter(state=2).exclude(user_id=None).values('user__name')\ - .annotate(total=Count('id')).order_by('-total') + total_uploads = Song.objects.filter(deleted=False).exclude(user_id=None).count() + + 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] + + 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}) + '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})