add channel aggs

This commit is contained in:
Simon
2024-08-03 00:18:49 +02:00
parent 49cfbd5796
commit 38d74657ec
4 changed files with 70 additions and 30 deletions

View File

@@ -19,4 +19,9 @@ urlpatterns = [
views.ChannelApiView.as_view(),
name="api-channel",
),
path(
"<slug:channel_id>/aggs/",
views.ChannelAggsApiView.as_view(),
name="api-channel-aggs",
),
]

View File

@@ -8,35 +8,6 @@ from rest_framework.response import Response
from task.tasks import subscribe_to
class ChannelApiView(ApiBaseView):
"""resolves to /api/channel/<channel_id>/
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/<channel_id>/
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/<channel_id>/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

View File

@@ -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)

View File

@@ -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")