From 02ac590caa87c55935657d9f71dd447bf22cadab Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 19 Nov 2023 14:42:16 +0700 Subject: [PATCH] [API] add download stats --- tubearchivist/api/src/aggs.py | 30 ++++++++++++++++++++++++++++++ tubearchivist/api/urls.py | 5 +++++ tubearchivist/api/views.py | 13 +++++++++++++ 3 files changed, 48 insertions(+) diff --git a/tubearchivist/api/src/aggs.py b/tubearchivist/api/src/aggs.py index 989c04f..0895bcb 100644 --- a/tubearchivist/api/src/aggs.py +++ b/tubearchivist/api/src/aggs.py @@ -195,6 +195,36 @@ class Playlist(AggBase): return response +class Download(AggBase): + """get downloads queue stats""" + + name = "download_queue_stats" + path = "ta_download/_search" + data = { + "size": 0, + "aggs": { + "status": {"terms": {"field": "status"}}, + "video_type": { + "filter": {"term": {"status": "pending"}}, + "aggs": {"type_pending": {"terms": {"field": "vid_type"}}}, + }, + }, + } + + def process(self): + """process aggregation""" + aggregations = self.get() + response = {} + for bucket in aggregations["status"]["buckets"]: + response.update({bucket["key"]: bucket.get("doc_count")}) + + for bucket in aggregations["video_type"]["type_pending"]["buckets"]: + key = f"pending_{bucket['key']}" + 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 8422181..b03547c 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -166,6 +166,11 @@ urlpatterns = [ views.StatPlaylistView.as_view(), name="api-stats-playlist", ), + path( + "stats/download/", + views.StatDownloadView.as_view(), + name="api-stats-download", + ), path( "stats/watch/", views.StatWatchProgress.as_view(), diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index fbde800..e026dba 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -3,6 +3,7 @@ from api.src.aggs import ( BiggestChannel, Channel, + Download, DownloadHist, Playlist, Video, @@ -1184,6 +1185,18 @@ class StatPlaylistView(ApiBaseView): return Response(Playlist().process()) +class StatDownloadView(ApiBaseView): + """resolves to /api/stats/download/ + GET: return download stats + """ + + def get(self, request): + """get stats""" + # pylint: disable=unused-argument + + return Response(Download().process()) + + class StatWatchProgress(ApiBaseView): """resolves to /api/stats/watchprogress/ GET: return watch/unwatch progress stats