mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-06 06:29:55 +02:00
Fixed #82: Basic instance states are now available on /about
This commit is contained in:
parent
c89212379d
commit
d875f0d070
7 changed files with 259 additions and 0 deletions
51
api/funkwhale_api/instance/stats.py
Normal file
51
api/funkwhale_api/instance/stats.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
from django.db.models import Sum
|
||||
|
||||
from funkwhale_api.favorites.models import TrackFavorite
|
||||
from funkwhale_api.history.models import Listening
|
||||
from funkwhale_api.music import models
|
||||
from funkwhale_api.users.models import User
|
||||
|
||||
|
||||
def get():
|
||||
return {
|
||||
'users': get_users(),
|
||||
'tracks': get_tracks(),
|
||||
'albums': get_albums(),
|
||||
'artists': get_artists(),
|
||||
'track_favorites': get_track_favorites(),
|
||||
'listenings': get_listenings(),
|
||||
'music_duration': get_music_duration(),
|
||||
}
|
||||
|
||||
|
||||
def get_users():
|
||||
return User.objects.count()
|
||||
|
||||
|
||||
def get_listenings():
|
||||
return Listening.objects.count()
|
||||
|
||||
|
||||
def get_track_favorites():
|
||||
return TrackFavorite.objects.count()
|
||||
|
||||
|
||||
def get_tracks():
|
||||
return models.Track.objects.count()
|
||||
|
||||
|
||||
def get_albums():
|
||||
return models.Album.objects.count()
|
||||
|
||||
|
||||
def get_artists():
|
||||
return models.Artist.objects.count()
|
||||
|
||||
|
||||
def get_music_duration():
|
||||
seconds = models.TrackFile.objects.aggregate(
|
||||
d=Sum('duration'),
|
||||
)['d']
|
||||
if seconds:
|
||||
return seconds / 3600
|
||||
return 0
|
|
@ -1,7 +1,11 @@
|
|||
from django.conf.urls import url
|
||||
from django.views.decorators.cache import cache_page
|
||||
|
||||
from . import views
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^settings/$', views.InstanceSettings.as_view(), name='settings'),
|
||||
url(r'^stats/$',
|
||||
cache_page(60 * 5)(views.InstanceStats.as_view()), name='stats'),
|
||||
]
|
||||
|
|
|
@ -4,6 +4,8 @@ from rest_framework.response import Response
|
|||
from dynamic_preferences.api import serializers
|
||||
from dynamic_preferences.registries import global_preferences_registry
|
||||
|
||||
from . import stats
|
||||
|
||||
|
||||
class InstanceSettings(views.APIView):
|
||||
permission_classes = []
|
||||
|
@ -23,3 +25,12 @@ class InstanceSettings(views.APIView):
|
|||
data = serializers.GlobalPreferenceSerializer(
|
||||
api_preferences, many=True).data
|
||||
return Response(data, status=200)
|
||||
|
||||
|
||||
class InstanceStats(views.APIView):
|
||||
permission_classes = []
|
||||
authentication_classes = []
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
data = stats.get()
|
||||
return Response(data, status=200)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue