diff --git a/tubearchivist/download/urls.py b/tubearchivist/download/urls.py index 2af17f17..94e96343 100644 --- a/tubearchivist/download/urls.py +++ b/tubearchivist/download/urls.py @@ -5,6 +5,11 @@ from download import views urlpatterns = [ path("", views.DownloadApiListView.as_view(), name="api-download-list"), + path( + "aggs/", + views.DownloadAggsApiView.as_view(), + name="api-download-aggs", + ), path( "/", views.DownloadApiView.as_view(), diff --git a/tubearchivist/download/views.py b/tubearchivist/download/views.py index 5db809fa..fd1fc7a2 100644 --- a/tubearchivist/download/views.py +++ b/tubearchivist/download/views.py @@ -75,6 +75,49 @@ class DownloadApiListView(ApiBaseView): return Response({"message": message}) +class DownloadAggsApiView(ApiBaseView): + """resolves to /api/download/aggs/ + GET: get download aggregations + """ + + search_base = "ta_download/_search" + valid_filter_view = ["ignore", "pending"] + + def get(self, request): + """get aggs""" + filter_view = request.GET.get("filter") + if filter_view: + if filter_view not in self.valid_filter_view: + message = f"invalid filter: {filter_view}" + return Response({"message": message}, status=400) + + self.data.update( + { + "query": {"term": {"status": {"value": filter_view}}}, + } + ) + + self.data.update( + { + "aggs": { + "channel_downloads": { + "multi_terms": { + "size": 30, + "terms": [ + {"field": "channel_name.keyword"}, + {"field": "channel_id"}, + ], + "order": {"_count": "desc"}, + } + } + } + } + ) + self.get_aggs() + + return Response(self.response) + + class DownloadApiView(ApiBaseView): """resolves to /api/download// GET: returns metadata dict of an item in the download queue