diff --git a/funkwhale_network/cli.py b/funkwhale_network/cli.py index f24d9d20c3c5ad031f904e914417a878b23ad381..377d33d2af9dd5716e17580963c6927f30613e8b 100644 --- a/funkwhale_network/cli.py +++ b/funkwhale_network/cli.py @@ -229,6 +229,7 @@ async def crawl(domain, use_public, detail, passes, sort): "Active users (30d)", "Users", "Listenings", + "Downloads", "Open registrations", "Anonymous access", "Private", @@ -256,6 +257,7 @@ async def crawl(domain, use_public, detail, passes, sort): "Active users (180d)", "Users", "Listenings", + "Downloads", "Tracks", "Albums", "Artists", @@ -282,6 +284,7 @@ def aggregate_crawl_results(domains_info): "usage_users_active_half_year": permissive_sum, "usage_users_active_month": permissive_sum, "usage_listenings_total": permissive_sum, + "usage_downloads_total": permissive_sum, "library_tracks_total": permissive_sum, "library_albums_total": permissive_sum, "library_artists_total": permissive_sum, diff --git a/funkwhale_network/crawler.py b/funkwhale_network/crawler.py index 5a287903bc6fa5946d5d371428a99910391507a3..5800182f76f3d97f1a3f08877c4ceb074c07a4ce 100644 --- a/funkwhale_network/crawler.py +++ b/funkwhale_network/crawler.py @@ -169,6 +169,9 @@ def clean_check(check_data, nodeinfo_data): "usage_listenings_total": recursive_getattr( nodeinfo_data, "metadata.usage.listenings.total" ), + "usage_downloads_total": recursive_getattr( + nodeinfo_data, "metadata.usage.downloads.total" + ), "library_tracks_total": recursive_getattr( nodeinfo_data, "metadata.library.tracks.total" ), diff --git a/funkwhale_network/db.py b/funkwhale_network/db.py index 0e0d9fb9f8994e7286fab59d81162644ce1376e4..d6150defa0fcaf084f8ad7da630d200101ffc2c9 100644 --- a/funkwhale_network/db.py +++ b/funkwhale_network/db.py @@ -8,18 +8,20 @@ async def get_pool(db_dsn): async def create_domains_table(cursor): await cursor.execute( - """CREATE TABLE domains ( + """ + CREATE TABLE IF NOT EXISTS domains ( name VARCHAR(255) PRIMARY KEY, node_name VARCHAR(255) NULL, blocked BOOLEAN DEFAULT false, first_seen TIMESTAMP WITH TIME ZONE DEFAULT NOW() - )""" + ); + """ ) async def create_checks_table(cursor): sql = """ - CREATE TABLE checks ( + CREATE TABLE IF NOT EXISTS checks ( time TIMESTAMPTZ NOT NULL, domain VARCHAR(255) REFERENCES domains(name), up BOOLEAN NOT NULL, @@ -42,7 +44,8 @@ async def create_checks_table(cursor): software_prerelease VARCHAR(255) NULL, software_build VARCHAR(255) NULL ); - SELECT create_hypertable('checks', 'time'); + ALTER TABLE checks ADD COLUMN IF NOT EXISTS usage_downloads_total INTEGER NULL; + SELECT create_hypertable('checks', 'time', if_not_exists => TRUE); """ await cursor.execute(sql) @@ -94,6 +97,7 @@ async def get_stats(conn): "albums": {"total": 0}, "tracks": {"total": 0}, "listenings": {"total": 0}, + "downloads": {"total": 0}, } for check in checks: increment_stat(data["users"], "total", check["usage_users_total"]) @@ -114,6 +118,7 @@ async def get_stats(conn): increment_stat( data["listenings"], "total", int(check["usage_listenings_total"]) ) + increment_stat(data["downloads"], "total", int(check["usage_downloads_total"])) return data diff --git a/funkwhale_network/output.py b/funkwhale_network/output.py index 2d93dafa48a8d11990c73251bf50a1c7ccf43893..875a2425a3e79af2753c7ef4938ef465a976cf3b 100644 --- a/funkwhale_network/output.py +++ b/funkwhale_network/output.py @@ -7,6 +7,7 @@ FIELDS = { "Active users (180d)": {"field": "usage_users_active_half_year"}, "Users": {"field": "usage_users_total"}, "Listenings": {"field": "usage_listenings_total"}, + "Downloads": {"field": "usage_downloads_total"}, "Tracks": {"field": "library_tracks_total"}, "Albums": {"field": "library_albums_total"}, "Artists": {"field": "library_artists_total"}, @@ -22,6 +23,7 @@ FIELDS = { "Active users (180d)": {"field": "usage_users_active_half_year"}, "Users": {"field": "usage_users_total"}, "Listenings": {"field": "usage_listenings_total"}, + "Downloads": {"field": "usage_download_total"}, "Tracks": {"field": "library_tracks_total"}, "Albums": {"field": "library_albums_total"}, "Artists": {"field": "library_artists_total"}, diff --git a/funkwhale_network/schemas.py b/funkwhale_network/schemas.py index b9ef32e3085fe15f25fcadeccdb026859ba3e956..23fc281852f6f502dd7e331a854b59902294868c 100644 --- a/funkwhale_network/schemas.py +++ b/funkwhale_network/schemas.py @@ -102,6 +102,7 @@ class LibraryMetadataSchema(marshmallow.Schema): class MetadataUsageSchema(marshmallow.Schema): listenings = marshmallow.fields.Nested(StatisticsSchema, required=False) + downloads = marshmallow.fields.Nested(StatisticsSchema, required=False) class Meta: unknown = marshmallow.EXCLUDE diff --git a/funkwhale_network/serializers.py b/funkwhale_network/serializers.py index 03304fc335c9de0f498179cff93f25bc0986935f..6980c5a3a02262e07bbc92c8b4f175e49bd6c8c3 100644 --- a/funkwhale_network/serializers.py +++ b/funkwhale_network/serializers.py @@ -26,6 +26,7 @@ def serialize_check(raw): "activeMonth": raw["usage_users_active_month"], }, "listenings": {"total": raw["usage_listenings_total"]}, + "downloads": {"total": raw["usage_downloads_total"]}, }, "library": { "music": {"hours": raw["library_music_hours"]}, diff --git a/tests/factories.py b/tests/factories.py index 129098b9c2651c5f6723332eb9821717c1583c1e..1f3bf44da513ad878e1711503c5114540d92d540 100644 --- a/tests/factories.py +++ b/tests/factories.py @@ -58,6 +58,7 @@ class CheckFactory(DBFactory): usage_users_active_half_year = factory.Faker("random_int") usage_users_active_month = factory.Faker("random_int") usage_listenings_total = factory.Faker("random_int") + usage_downloads_total = factory.Faker("random_int") library_tracks_total = factory.Faker("random_int") library_albums_total = factory.Faker("random_int") library_artists_total = factory.Faker("random_int") diff --git a/tests/test_crawler.py b/tests/test_crawler.py index 1a0928f9b84043f9709597b606a179e9c4775996..6350983703848f1b4a19542a1482d5e19f11c81b 100644 --- a/tests/test_crawler.py +++ b/tests/test_crawler.py @@ -147,7 +147,7 @@ async def test_clean_check_result(): "albums": {"total": 10872}, "music": {"hours": 7650}, }, - "usage": {"listenings": {"total": 50294}}, + "usage": {"listenings": {"total": 50294}, "downloads": {"total": 91273}}, }, } @@ -163,6 +163,7 @@ async def test_clean_check_result(): "usage_users_active_half_year": 42, "usage_users_active_month": 23, "usage_listenings_total": 50294, + "usage_downloads_total": 91273, "library_tracks_total": 98552, "library_albums_total": 10872, "library_artists_total": 9831, @@ -193,6 +194,7 @@ async def test_save_check(populated_db, db_conn, factories): "usage_users_active_half_year": 42, "usage_users_active_month": 23, "usage_listenings_total": 50294, + "usage_downloads_total": 7092, "library_tracks_total": 98552, "library_albums_total": 10872, "library_artists_total": 9831, diff --git a/tests/test_db.py b/tests/test_db.py index 5386c7d9247c0bf2d14559c0b4b4988063f03e36..1db27b89f956042448b273c81ab59b4276ba8e5b 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -46,6 +46,7 @@ async def test_get_stats(factories, db_conn): usage_users_active_half_year=1, usage_users_active_month=2, usage_listenings_total=20, + usage_downloads_total=30, library_tracks_total=6, library_albums_total=30, library_artists_total=36, @@ -59,6 +60,7 @@ async def test_get_stats(factories, db_conn): usage_users_active_half_year=3, usage_users_active_month=1, usage_listenings_total=22, + usage_downloads_total=33, library_tracks_total=15, library_albums_total=13, library_artists_total=40, @@ -71,5 +73,6 @@ async def test_get_stats(factories, db_conn): "albums": {"total": 43}, "tracks": {"total": 21}, "listenings": {"total": 42}, + "downloads": {"total": 63}, } assert await db.get_stats(db_conn) == expected diff --git a/tests/test_serializers.py b/tests/test_serializers.py index 51627e3550cf4c2f31e37b2219beabdf0a355c72..7ab59347e604c75fb2b0b3c9ad358305ac0387e2 100644 --- a/tests/test_serializers.py +++ b/tests/test_serializers.py @@ -23,6 +23,7 @@ def test_serialize_check(): "usage_users_active_half_year": 2, "usage_users_active_month": 3, "usage_listenings_total": 4, + "usage_downloads_total": 44, "library_tracks_total": 5, "library_albums_total": 6, "library_artists_total": 7, @@ -52,6 +53,7 @@ def test_serialize_check(): "usage": { "users": {"total": 1, "activeHalfyear": 2, "activeMonth": 3}, "listenings": {"total": 4}, + "downloads": {"total": 44}, }, "software": { "name": "funkwhale",