From a466c02304a9b051efbce8f52b86fe9b1b53de42 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 19 Nov 2023 14:00:27 +0700 Subject: [PATCH] [API] add playlist stats --- tubearchivist/api/src/aggs.py | 28 ++++++++++++++++++++++++++++ tubearchivist/api/urls.py | 5 +++++ tubearchivist/api/views.py | 13 +++++++++++++ 3 files changed, 46 insertions(+) diff --git a/tubearchivist/api/src/aggs.py b/tubearchivist/api/src/aggs.py index 1374842c..989c04ff 100644 --- a/tubearchivist/api/src/aggs.py +++ b/tubearchivist/api/src/aggs.py @@ -167,6 +167,34 @@ class Channel(AggBase): return response +class Playlist(AggBase): + """get playlist stats""" + + name = "playlist_stats" + path = "ta_playlist/_search" + data = { + "size": 0, + "aggs": { + "playlist_count": {"value_count": {"field": "playlist_id"}}, + "playlist_active": {"terms": {"field": "playlist_active"}}, + "playlist_subscribed": {"terms": {"field": "playlist_subscribed"}}, + }, + } + + def process(self): + """process aggregation""" + aggregations = self.get() + response = {"doc_count": aggregations["playlist_count"].get("value")} + for bucket in aggregations["playlist_active"]["buckets"]: + key = f"active_{bucket['key_as_string']}" + response.update({key: bucket.get("doc_count")}) + for bucket in aggregations["playlist_subscribed"]["buckets"]: + key = f"subscribed_{bucket['key_as_string']}" + response.update({key: bucket.get("doc_count")}) + + return response + + class WatchProgress(AggBase): """get watch progress""" diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index bc614603..84221810 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -161,6 +161,11 @@ urlpatterns = [ views.StatChannelView.as_view(), name="api-stats-channel", ), + path( + "stats/playlist/", + views.StatPlaylistView.as_view(), + name="api-stats-playlist", + ), path( "stats/watch/", views.StatWatchProgress.as_view(), diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index de090894..fbde8004 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -4,6 +4,7 @@ from api.src.aggs import ( BiggestChannel, Channel, DownloadHist, + Playlist, Video, WatchProgress, ) @@ -1171,6 +1172,18 @@ class StatChannelView(ApiBaseView): return Response(Channel().process()) +class StatPlaylistView(ApiBaseView): + """resolves to /api/stats/playlist/ + GET: return playlist stats + """ + + def get(self, request): + """get stats""" + # pylint: disable=unused-argument + + return Response(Playlist().process()) + + class StatWatchProgress(ApiBaseView): """resolves to /api/stats/watchprogress/ GET: return watch/unwatch progress stats