Include user activity stats in nodeinfo endpoints

merge-requests/552/head
Eliot Berriot 2019-01-21 10:16:50 +01:00
rodzic e8c81d734f
commit 0b66737181
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: DD6965E2476E5C27
4 zmienionych plików z 30 dodań i 9 usunięć

Wyświetl plik

@ -17,7 +17,7 @@ def get():
"protocols": ["activitypub"],
"services": {"inbound": [], "outbound": []},
"openRegistrations": preferences.get("users__registration_enabled"),
"usage": {"users": {"total": 0}},
"usage": {"users": {"total": 0, "activeHalfyear": 0, "activeMonth": 0}},
"metadata": {
"private": preferences.get("instance__nodeinfo_private"),
"shortDescription": preferences.get("instance__short_description"),
@ -37,7 +37,11 @@ def get():
if share_stats:
getter = memo(lambda: stats.get(), max_age=600)
statistics = getter()
data["usage"]["users"]["total"] = statistics["users"]
data["usage"]["users"]["total"] = statistics["users"]["total"]
data["usage"]["users"]["activeHalfyear"] = statistics["users"][
"active_halfyear"
]
data["usage"]["users"]["activeMonth"] = statistics["users"]["active_month"]
data["metadata"]["library"]["tracks"] = {"total": statistics["tracks"]}
data["metadata"]["library"]["artists"] = {"total": statistics["artists"]}
data["metadata"]["library"]["albums"] = {"total": statistics["albums"]}

Wyświetl plik

@ -1,4 +1,7 @@
import datetime
from django.db.models import Sum
from django.utils import timezone
from funkwhale_api.favorites.models import TrackFavorite
from funkwhale_api.history.models import Listening
@ -19,6 +22,15 @@ def get():
def get_users():
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(),
}
return User.objects.count()

Wyświetl plik

@ -5,7 +5,7 @@ from funkwhale_api.instance import nodeinfo
def test_nodeinfo_dump(preferences, mocker):
preferences["instance__nodeinfo_stats_enabled"] = True
stats = {
"users": 1,
"users": {"total": 1, "active_halfyear": 12, "active_month": 13},
"tracks": 2,
"albums": 3,
"artists": 4,
@ -21,7 +21,7 @@ def test_nodeinfo_dump(preferences, mocker):
"protocols": ["activitypub"],
"services": {"inbound": [], "outbound": []},
"openRegistrations": preferences["users__registration_enabled"],
"usage": {"users": {"total": stats["users"]}},
"usage": {"users": {"total": 1, "activeHalfyear": 12, "activeMonth": 13}},
"metadata": {
"private": preferences["instance__nodeinfo_private"],
"shortDescription": preferences["instance__short_description"],
@ -58,7 +58,7 @@ def test_nodeinfo_dump_stats_disabled(preferences, mocker):
"protocols": ["activitypub"],
"services": {"inbound": [], "outbound": []},
"openRegistrations": preferences["users__registration_enabled"],
"usage": {"users": {"total": 0}},
"usage": {"users": {"total": 0, "activeHalfyear": 0, "activeMonth": 0}},
"metadata": {
"private": preferences["instance__nodeinfo_private"],
"shortDescription": preferences["instance__short_description"],

Wyświetl plik

@ -1,10 +1,15 @@
import datetime
from funkwhale_api.instance import stats
def test_get_users(mocker):
mocker.patch("funkwhale_api.users.models.User.objects.count", return_value=42)
assert stats.get_users() == 42
def test_get_users(factories, now):
factories["users.User"](last_activity=now)
factories["users.User"](last_activity=now - datetime.timedelta(days=29))
factories["users.User"](last_activity=now - datetime.timedelta(days=31))
factories["users.User"](last_activity=now - datetime.timedelta(days=190))
factories["users.User"](is_active=False)
assert stats.get_users() == {"total": 4, "active_month": 2, "active_halfyear": 3}
def test_get_music_duration(factories):