From 0b66737181fcaf129cda834e42a371484a1c6ff8 Mon Sep 17 00:00:00 2001 From: Eliot Berriot <contact@eliotberriot.com> Date: Mon, 21 Jan 2019 10:16:50 +0100 Subject: [PATCH] Include user activity stats in nodeinfo endpoints --- api/funkwhale_api/instance/nodeinfo.py | 8 ++++++-- api/funkwhale_api/instance/stats.py | 12 ++++++++++++ api/tests/instance/test_nodeinfo.py | 6 +++--- api/tests/instance/test_stats.py | 13 +++++++++---- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/api/funkwhale_api/instance/nodeinfo.py b/api/funkwhale_api/instance/nodeinfo.py index 6519d791..286cb028 100644 --- a/api/funkwhale_api/instance/nodeinfo.py +++ b/api/funkwhale_api/instance/nodeinfo.py @@ -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"]} diff --git a/api/funkwhale_api/instance/stats.py b/api/funkwhale_api/instance/stats.py index 0cb1b979..50f69da8 100644 --- a/api/funkwhale_api/instance/stats.py +++ b/api/funkwhale_api/instance/stats.py @@ -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() diff --git a/api/tests/instance/test_nodeinfo.py b/api/tests/instance/test_nodeinfo.py index f37d6b79..a5bdc709 100644 --- a/api/tests/instance/test_nodeinfo.py +++ b/api/tests/instance/test_nodeinfo.py @@ -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"], diff --git a/api/tests/instance/test_stats.py b/api/tests/instance/test_stats.py index 4820735d..255e60d1 100644 --- a/api/tests/instance/test_stats.py +++ b/api/tests/instance/test_stats.py @@ -1,10 +1,15 @@ -from funkwhale_api.instance import stats +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): -- GitLab