Add more stats.

This commit is contained in:
Jim Driessen
2017-09-28 17:25:21 +02:00
committed by Daan Sprenkels
parent cbc9284865
commit 04cdce9ad4
6 changed files with 148 additions and 19 deletions

View File

@ -152,3 +152,6 @@ LIMIT_ALWAYS = False
LIMIT_HOURS = (12, 16) LIMIT_HOURS = (12, 16)
CONTACT_EMAIL = 'marietje@science.ru.nl' CONTACT_EMAIL = 'marietje@science.ru.nl'
STATS_TOP_COUNT = 50
STATS_REQUEST_IGNORE_USER_IDS = (51, 515)

View File

@ -34,6 +34,10 @@ def play(request):
queue = get_object_or_404(Queue, id=request.POST.get('queue')) queue = get_object_or_404(Queue, id=request.POST.get('queue'))
queue.started_at = timezone.now() queue.started_at = timezone.now()
queue.save() queue.save()
current_song = queue.current_song()
current_song.played_at = queue.started_at
current_song.save()
return JsonResponse({}) return JsonResponse({})
@ -46,14 +50,6 @@ def next(request):
player_song.save() player_song.save()
return JsonResponse({}) 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 @csrf_exempt
@token_required @token_required

View File

@ -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),
),
]

View File

@ -29,6 +29,7 @@ class PlaylistSong(models.Model):
blank=True, blank=True,
null=True null=True
) )
played_at = models.DateTimeField(blank=True, null=True)
# 0: Queued. # 0: Queued.
# 1: Playing. # 1: Playing.

View File

@ -9,13 +9,14 @@
<div class="col-md-6"> <div class="col-md-6">
<h2>Uploads</h2> <h2>Uploads</h2>
<h4>Total: {{ total_uploads }}</h4> <h4>Total: {{ total_uploads }}</h4>
<h4>Top {{ stats_top_count }}:</h4>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<tr> <tr>
<th>#</th> <th>#</th>
<th>User</th> <th>User</th>
<th># Songs</th> <th>#&nbsp;Songs</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -33,13 +34,14 @@
<div class="col-md-6"> <div class="col-md-6">
<h2>Requests</h2> <h2>Requests</h2>
<h4>Total: {{ total_requests }}</h4> <h4>Total: {{ total_requests }}</h4>
<h4>Top {{ stats_top_count }}:</h4>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<tr> <tr>
<th>#</th> <th>#</th>
<th>User</th> <th>User</th>
<th># Requests</th> <th>#&nbsp;Requests</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -54,5 +56,81 @@
</table> </table>
</div> </div>
</div> </div>
<div class="col-md-6">
<h2>Unique requests</h2>
<h4>Top {{ stats_top_count }}:</h4>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>User</th>
<th>#&nbsp;Requests</th>
</tr>
</thead>
<tbody>
{% for stat in unique_request_stats %}
<tr>
<th>{{ forloop.counter }}</th>
<td>{{ stat.user__name }}</td>
<td>{{ stat.total }} ({{stat.ratio }}%)</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<h2>Most played songs</h2>
<h4>Top {{ stats_top_count }}:</h4>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Artist</th>
<th>Title</th>
<th>#&nbsp;Requests</th>
</tr>
</thead>
<tbody>
{% for stat in most_played_songs %}
<tr>
<th>{{ forloop.counter }}</th>
<td>{{ stat.song__artist }}</td>
<td>{{ stat.song__title }}</td>
<td>{{ stat.total }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<h2>Most played songs last 14 days</h2>
<h4>Top {{ stats_top_count }}:</h4>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Artist</th>
<th>Title</th>
<th>#&nbsp;Requests</th>
</tr>
</thead>
<tbody>
{% for stat in most_played_songs_14_days %}
<tr>
<th>{{ forloop.counter }}</th>
<td>{{ stat.song__artist }}</td>
<td>{{ stat.song__title }}</td>
<td>{{ stat.total }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div> </div>
{% endblock %} {% endblock %}

View File

@ -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.shortcuts import render
from django.db.models import Count from django.utils import timezone
from songs.models import Song
from queues.models import PlaylistSong from queues.models import PlaylistSong
from songs.models import Song
def stats(request): def stats(request):
total_uploads = Song.objects.all().filter(deleted=False).exclude(user_id=None).count() total_uploads = Song.objects.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') upload_stats = Song.objects.filter(deleted=False).exclude(user_id=None).values('user__name').annotate(
total_requests = PlaylistSong.objects.all().filter(state=2).exclude(user_id=None).count() total=Count('id')).order_by('-total', 'user__name')[:settings.STATS_TOP_COUNT]
request_stats = PlaylistSong.objects.all().filter(state=2).exclude(user_id=None).values('user__name')\
.annotate(total=Count('id')).order_by('-total') 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, 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})