diff --git a/tubearchivist/channel/urls.py b/tubearchivist/channel/urls.py index 8f992988..9b51671c 100644 --- a/tubearchivist/channel/urls.py +++ b/tubearchivist/channel/urls.py @@ -19,4 +19,9 @@ urlpatterns = [ views.ChannelApiView.as_view(), name="api-channel", ), + path( + "/aggs/", + views.ChannelAggsApiView.as_view(), + name="api-channel-aggs", + ), ] diff --git a/tubearchivist/channel/views.py b/tubearchivist/channel/views.py index 448dc535..5b28267f 100644 --- a/tubearchivist/channel/views.py +++ b/tubearchivist/channel/views.py @@ -8,35 +8,6 @@ from rest_framework.response import Response from task.tasks import subscribe_to -class ChannelApiView(ApiBaseView): - """resolves to /api/channel// - GET: returns metadata dict of channel - """ - - search_base = "ta_channel/_doc/" - permission_classes = [AdminWriteOnly] - - def get(self, request, channel_id): - # pylint: disable=unused-argument - """get request""" - self.get_document(channel_id) - return Response(self.response, status=self.status_code) - - def delete(self, request, channel_id): - # pylint: disable=unused-argument - """delete channel""" - message = {"channel": channel_id} - try: - YoutubeChannel(channel_id).delete_channel() - status_code = 200 - message.update({"state": "delete"}) - except FileNotFoundError: - status_code = 404 - message.update({"state": "not found"}) - - return Response(message, status=status_code) - - class ChannelApiListView(ApiBaseView): """resolves to /api/channel/ GET: returns list of channels @@ -101,6 +72,61 @@ class ChannelApiListView(ApiBaseView): ) +class ChannelApiView(ApiBaseView): + """resolves to /api/channel// + GET: returns metadata dict of channel + """ + + search_base = "ta_channel/_doc/" + permission_classes = [AdminWriteOnly] + + def get(self, request, channel_id): + # pylint: disable=unused-argument + """get request""" + self.get_document(channel_id) + return Response(self.response, status=self.status_code) + + def delete(self, request, channel_id): + # pylint: disable=unused-argument + """delete channel""" + message = {"channel": channel_id} + try: + YoutubeChannel(channel_id).delete_channel() + status_code = 200 + message.update({"state": "delete"}) + except FileNotFoundError: + status_code = 404 + message.update({"state": "not found"}) + + return Response(message, status=status_code) + + +class ChannelAggsApiView(ApiBaseView): + """resolves to /api/channel//aggs/ + GET: get channel aggregations + """ + + search_base = "ta_video/_search" + + def get(self, request, channel_id): + """get aggs""" + self.data.update( + { + "query": { + "term": {"channel.channel_id": {"value": channel_id}} + }, + "aggs": { + "total_items": {"value_count": {"field": "youtube_id"}}, + "total_size": {"sum": {"field": "media_size"}}, + "total_duration": {"sum": {"field": "player.duration"}}, + }, + } + ) + self.get_aggs() + + return Response(self.response) + + class ChannelApiSearchView(ApiBaseView): """resolves to /api/channel/search/ search for channel diff --git a/tubearchivist/common/src/search_processor.py b/tubearchivist/common/src/search_processor.py index 8a4b98fb..4450c276 100644 --- a/tubearchivist/common/src/search_processor.py +++ b/tubearchivist/common/src/search_processor.py @@ -20,6 +20,7 @@ class SearchProcess: def process(self): """detect type and process""" + print(self.response) if "_source" in self.response.keys(): # single self.processed = self._process_result(self.response) diff --git a/tubearchivist/common/views_base.py b/tubearchivist/common/views_base.py index fe1b7965..e913cccd 100644 --- a/tubearchivist/common/views_base.py +++ b/tubearchivist/common/views_base.py @@ -4,7 +4,7 @@ from appsettings.src.config import AppConfig from common.src.env_settings import EnvironmentSettings from common.src.es_connect import ElasticWrap from common.src.index_generic import Pagination -from common.src.search_processor import SearchProcess +from common.src.search_processor import SearchProcess, process_aggs from rest_framework import permissions from rest_framework.authentication import ( SessionAuthentication, @@ -96,3 +96,11 @@ class ApiBaseView(APIView): response["hits"]["total"]["value"] ) self.response["paginate"] = self.pagination_handler.pagination + + def get_aggs(self): + """get aggs alone""" + self.data["size"] = 0 + response, _ = ElasticWrap(self.search_base).get(data=self.data) + process_aggs(response) + + self.response = response.get("aggregations")