Include user activity stats in nodeinfo endpoints
This commit is contained in:
parent
e8c81d734f
commit
0b66737181
|
@ -17,7 +17,7 @@ def get():
|
||||||
"protocols": ["activitypub"],
|
"protocols": ["activitypub"],
|
||||||
"services": {"inbound": [], "outbound": []},
|
"services": {"inbound": [], "outbound": []},
|
||||||
"openRegistrations": preferences.get("users__registration_enabled"),
|
"openRegistrations": preferences.get("users__registration_enabled"),
|
||||||
"usage": {"users": {"total": 0}},
|
"usage": {"users": {"total": 0, "activeHalfyear": 0, "activeMonth": 0}},
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"private": preferences.get("instance__nodeinfo_private"),
|
"private": preferences.get("instance__nodeinfo_private"),
|
||||||
"shortDescription": preferences.get("instance__short_description"),
|
"shortDescription": preferences.get("instance__short_description"),
|
||||||
|
@ -37,7 +37,11 @@ def get():
|
||||||
if share_stats:
|
if share_stats:
|
||||||
getter = memo(lambda: stats.get(), max_age=600)
|
getter = memo(lambda: stats.get(), max_age=600)
|
||||||
statistics = getter()
|
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"]["tracks"] = {"total": statistics["tracks"]}
|
||||||
data["metadata"]["library"]["artists"] = {"total": statistics["artists"]}
|
data["metadata"]["library"]["artists"] = {"total": statistics["artists"]}
|
||||||
data["metadata"]["library"]["albums"] = {"total": statistics["albums"]}
|
data["metadata"]["library"]["albums"] = {"total": statistics["albums"]}
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
from django.db.models import Sum
|
from django.db.models import Sum
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
from funkwhale_api.favorites.models import TrackFavorite
|
from funkwhale_api.favorites.models import TrackFavorite
|
||||||
from funkwhale_api.history.models import Listening
|
from funkwhale_api.history.models import Listening
|
||||||
|
@ -19,6 +22,15 @@ def get():
|
||||||
|
|
||||||
|
|
||||||
def get_users():
|
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()
|
return User.objects.count()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ from funkwhale_api.instance import nodeinfo
|
||||||
def test_nodeinfo_dump(preferences, mocker):
|
def test_nodeinfo_dump(preferences, mocker):
|
||||||
preferences["instance__nodeinfo_stats_enabled"] = True
|
preferences["instance__nodeinfo_stats_enabled"] = True
|
||||||
stats = {
|
stats = {
|
||||||
"users": 1,
|
"users": {"total": 1, "active_halfyear": 12, "active_month": 13},
|
||||||
"tracks": 2,
|
"tracks": 2,
|
||||||
"albums": 3,
|
"albums": 3,
|
||||||
"artists": 4,
|
"artists": 4,
|
||||||
|
@ -21,7 +21,7 @@ def test_nodeinfo_dump(preferences, mocker):
|
||||||
"protocols": ["activitypub"],
|
"protocols": ["activitypub"],
|
||||||
"services": {"inbound": [], "outbound": []},
|
"services": {"inbound": [], "outbound": []},
|
||||||
"openRegistrations": preferences["users__registration_enabled"],
|
"openRegistrations": preferences["users__registration_enabled"],
|
||||||
"usage": {"users": {"total": stats["users"]}},
|
"usage": {"users": {"total": 1, "activeHalfyear": 12, "activeMonth": 13}},
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"private": preferences["instance__nodeinfo_private"],
|
"private": preferences["instance__nodeinfo_private"],
|
||||||
"shortDescription": preferences["instance__short_description"],
|
"shortDescription": preferences["instance__short_description"],
|
||||||
|
@ -58,7 +58,7 @@ def test_nodeinfo_dump_stats_disabled(preferences, mocker):
|
||||||
"protocols": ["activitypub"],
|
"protocols": ["activitypub"],
|
||||||
"services": {"inbound": [], "outbound": []},
|
"services": {"inbound": [], "outbound": []},
|
||||||
"openRegistrations": preferences["users__registration_enabled"],
|
"openRegistrations": preferences["users__registration_enabled"],
|
||||||
"usage": {"users": {"total": 0}},
|
"usage": {"users": {"total": 0, "activeHalfyear": 0, "activeMonth": 0}},
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"private": preferences["instance__nodeinfo_private"],
|
"private": preferences["instance__nodeinfo_private"],
|
||||||
"shortDescription": preferences["instance__short_description"],
|
"shortDescription": preferences["instance__short_description"],
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
from funkwhale_api.instance import stats
|
from funkwhale_api.instance import stats
|
||||||
|
|
||||||
|
|
||||||
def test_get_users(mocker):
|
def test_get_users(factories, now):
|
||||||
mocker.patch("funkwhale_api.users.models.User.objects.count", return_value=42)
|
factories["users.User"](last_activity=now)
|
||||||
|
factories["users.User"](last_activity=now - datetime.timedelta(days=29))
|
||||||
assert stats.get_users() == 42
|
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):
|
def test_get_music_duration(factories):
|
||||||
|
|
Loading…
Reference in New Issue