From 91bb0ed9c03d44024c3fc2b0e0dd6b95525caf43 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 19 Nov 2023 13:01:27 +0700 Subject: [PATCH] [API] add video aggregation --- tubearchivist/api/src/aggs.py | 52 +++++++++++++++++++++++++++++++++++ tubearchivist/api/urls.py | 6 ++-- tubearchivist/api/views.py | 10 +++---- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/tubearchivist/api/src/aggs.py b/tubearchivist/api/src/aggs.py index 8921d86..f26e295 100644 --- a/tubearchivist/api/src/aggs.py +++ b/tubearchivist/api/src/aggs.py @@ -84,6 +84,58 @@ class Primary(AggBase): return response +class Video(AggBase): + """get video stats""" + + name = "video_stats" + path = "ta_video/_search" + data = { + "size": 0, + "aggs": { + "video_type": { + "terms": {"field": "vid_type"}, + "aggs": {"media_size": {"sum": {"field": "media_size"}}}, + }, + "video_active": { + "terms": {"field": "active"}, + "aggs": {"media_size": {"sum": {"field": "media_size"}}}, + }, + "video_media_size": {"sum": {"field": "media_size"}}, + "video_count": {"value_count": {"field": "youtube_id"}}, + }, + } + + def process(self): + """process aggregation""" + aggregations = self.get() + + response = { + "doc_count": aggregations["video_count"]["value"], + "media_size": int(aggregations["video_media_size"]["value"]), + } + for bucket in aggregations["video_type"]["buckets"]: + response.update( + { + f"type_{bucket['key']}": { + "doc_count": bucket.get("doc_count"), + "media_size": int(bucket["media_size"].get("value")), + } + } + ) + + for bucket in aggregations["video_active"]["buckets"]: + response.update( + { + f"active_{bucket['key_as_string']}": { + "doc_count": bucket.get("doc_count"), + "media_size": int(bucket["media_size"].get("value")), + } + } + ) + + return response + + class WatchProgress(AggBase): """get watch progress""" diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index 8b075f2..3f06802 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -152,9 +152,9 @@ urlpatterns = [ name="api-notification", ), path( - "stats/primary/", - views.StatPrimaryView.as_view(), - name="api-stats-primary", + "stats/video/", + views.StatVideoView.as_view(), + name="api-stats-video", ), path( "stats/watch/", diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 0e64d10..e2cfe69 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -1,6 +1,6 @@ """all API views""" -from api.src.aggs import BiggestChannel, DownloadHist, Primary, WatchProgress +from api.src.aggs import BiggestChannel, DownloadHist, Video, WatchProgress from api.src.search_processor import SearchProcess from home.src.download.queue import PendingInteract from home.src.download.subscriptions import ( @@ -1141,16 +1141,16 @@ class NotificationView(ApiBaseView): return Response(RedisArchivist().list_items(query)) -class StatPrimaryView(ApiBaseView): - """resolves to /api/stats/primary/ - GET: return document count +class StatVideoView(ApiBaseView): + """resolves to /api/stats/video/ + GET: return video stats """ def get(self, request): """get stats""" # pylint: disable=unused-argument - return Response(Primary().process()) + return Response(Video().process()) class StatWatchProgress(ApiBaseView):