2019-01-21 09:16:50 +00:00
|
|
|
import datetime
|
|
|
|
|
2018-02-27 21:38:55 +00:00
|
|
|
from django.db.models import Sum
|
2019-01-21 09:16:50 +00:00
|
|
|
from django.utils import timezone
|
2018-02-27 21:38:55 +00:00
|
|
|
|
|
|
|
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 {
|
2018-06-09 13:36:16 +00:00
|
|
|
"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(),
|
2018-02-27 21:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_users():
|
2019-01-21 09:16:50 +00:00
|
|
|
qs = User.objects.filter(is_active=True)
|
|
|
|
now = timezone.now()
|
|
|
|
active_month = now - datetime.timedelta(days=30)
|
|
|
|
active_halfyear = now - datetime.timedelta(days=30 * 6)
|
|
|
|
return {
|
|
|
|
"total": qs.count(),
|
|
|
|
"active_month": qs.filter(last_activity__gte=active_month).count(),
|
|
|
|
"active_halfyear": qs.filter(last_activity__gte=active_halfyear).count(),
|
|
|
|
}
|
2018-02-27 21:38:55 +00:00
|
|
|
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():
|
2018-09-22 12:29:30 +00:00
|
|
|
seconds = models.Upload.objects.aggregate(d=Sum("duration"))["d"]
|
2018-02-27 21:38:55 +00:00
|
|
|
if seconds:
|
|
|
|
return seconds / 3600
|
|
|
|
return 0
|