Add logging for API endpoints

This commit is contained in:
Lars van Rhijn
2023-11-25 07:38:28 +01:00
parent 66ac1076d3
commit 11e8ff11b8
20 changed files with 400 additions and 109 deletions

View File

@ -1,3 +1,4 @@
from django.db.models import Q
from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404, CreateAPIView, DestroyAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
@ -8,7 +9,7 @@ from django.http import Http404
from queues.api.v1.serializers import PlaylistSerializer, QueueSerializer, PlaylistSongSerializer
from queues.exceptions import RequestException
from queues.models import Playlist, PlaylistSong, QueueCommand
from queues.models import Playlist, PlaylistSong, QueueCommand, Queue
from queues.services import get_user_or_default_queue
from songs.counters import request_counter
from songs.models import Song
@ -80,7 +81,7 @@ class QueueSkipAPIView(APIView):
if queue is None:
return Response(status=404)
playlist_song = request.user.queue.current_song()
playlist_song = queue.current_song()
if (
request.user is not None
and playlist_song.user != request.user
@ -90,6 +91,7 @@ class QueueSkipAPIView(APIView):
playlist_song.state = 2
playlist_song.save()
queue.log_action(request.user, "next", "Skipped to next song.")
return Response(status=200, data=QueueSerializer(queue).data)
@ -111,7 +113,18 @@ class PlaylistSongMoveDownAPIView(APIView):
and not request.user.has_perm("queues.can_move")
):
return Response(status=403)
playlist_song.move_down()
for queue in Queue.objects.filter(
Q(playlist=playlist_song.playlist) | Q(random_playlist=playlist_song.playlist)
):
queue.log_action(
request.user,
"down",
'Moved song "{}" of playlist "{}" down.'.format(playlist_song.song, playlist_song.playlist),
)
return Response(status=200, data=self.serializer_class(playlist_song).data)
@ -131,7 +144,18 @@ class PlaylistSongCancelAPIView(DestroyAPIView):
and not request.user.has_perm("queues.can_cancel")
):
return Response(status=403)
playlist_song.delete()
for queue in Queue.objects.filter(
Q(playlist=playlist_song.playlist) | Q(random_playlist=playlist_song.playlist)
):
queue.log_action(
request.user,
"cancel",
'Cancelled song "{}" of playlist "{}".'.format(playlist_song.song, playlist_song.playlist),
)
return Response(status=200, data=self.serializer_class(playlist_song).data)
@ -165,6 +189,8 @@ class QueueRequestAPIView(CreateAPIView):
except RequestException as e:
return Response(data={"success": False, "errorMessage": str(e)})
queue.log_action(request.user, "request_song", "Requested song {}.".format(song))
request_counter.labels(queue=queue.name).inc()
return Response(status=200, data=self.serializer_class(playlist_song).data)
@ -196,7 +222,11 @@ class QueueVolumeDownAPIView(APIView):
return Response(status=404)
if request.user is not None and not request.user.has_perm("queues.can_control_volume"):
return Response(status=403)
QueueCommand.objects.create(queue=queue, command="volume_down")
queue.log_action(request.user, "volume_down", "Reduced the volume of {}.".format(queue))
return Response(status=200, data=self.serializer_class(queue).data)
@ -227,7 +257,11 @@ class QueueVolumeUpAPIView(APIView):
return Response(status=404)
if request.user is not None and not request.user.has_perm("queues.can_control_volume"):
return Response(status=403)
QueueCommand.objects.create(queue=queue, command="volume_up")
queue.log_action(request.user, "volume_up", "Increased the volume of {}.".format(queue))
return Response(status=200, data=self.serializer_class(queue).data)
@ -258,5 +292,9 @@ class QueueMuteAPIView(APIView):
return Response(status=404)
if request.user is not None and not request.user.has_perm("queues.can_control_volume"):
return Response(status=403)
QueueCommand.objects.create(queue=queue, command="mute")
queue.log_action(request.user, "mute", "Muted the volume of {}.".format(queue))
return Response(status=200, data=self.serializer_class(queue).data)