add channel aggregation

This commit is contained in:
Simon 2023-08-30 18:42:03 +07:00
parent 988c2b8af7
commit 4ded8988c3
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 74 additions and 1 deletions

View File

@ -124,3 +124,52 @@ class DownloadHist(AggBase):
buckets = aggregations[self.name]["buckets"]
return {i.get("key_as_string"): i.get("doc_count") for i in buckets}
class BiggestChannel(AggBase):
"""get channel aggregations"""
name = "channel_stats"
path = "ta_video/_search"
data = {
"size": 0,
"aggs": {
name: {
"multi_terms": {
"terms": [
{"field": "channel.channel_name.keyword"},
{"field": "channel.channel_id"},
],
"order": {"doc_count": "desc"},
},
"aggs": {
"doc_count": {"value_count": {"field": "_index"}},
"duration": {"sum": {"field": "player.duration"}},
"media_size": {"sum": {"field": "media_size"}},
},
},
},
}
order_choices = ["doc_count", "duration", "media_size"]
def process(self, order_by=False):
"""process aggregation"""
if order_by and order_by in self.order_choices:
self.data["aggs"][self.name]["multi_terms"]["order"] = order_by
aggregations = self.get()
buckets = aggregations[self.name]["buckets"]
response = [
{
"id": i["key"][1],
"name": i["key"][0].title(),
"doc_count": i["doc_count"]["value"],
"duration": i["duration"]["value"],
"media_size": i["media_size"]["value"],
}
for i in buckets
]
return response

View File

@ -151,4 +151,9 @@ urlpatterns = [
views.StatDownloadHist.as_view(),
name="api-stats-downloadhist",
),
path(
"stats/biggestchannels/",
views.StatBiggestChannel.as_view(),
name="api-stats-biggestchannels",
),
]

View File

@ -1,6 +1,6 @@
"""all API views"""
from api.src.aggs import DownloadHist, Primary, WatchProgress
from api.src.aggs import BiggestChannel, DownloadHist, Primary, WatchProgress
from api.src.search_processor import SearchProcess
from home.src.download.queue import PendingInteract
from home.src.download.subscriptions import (
@ -1012,3 +1012,22 @@ class StatDownloadHist(ApiBaseView):
# pylint: disable=unused-argument
return Response(DownloadHist().process())
class StatBiggestChannel(ApiBaseView):
"""resolves to /api/stats/biggestchannels/
GET: return biggest channels
param: order
"""
order_choices = ["doc_count", "duration", "media_size"]
def get(self, request):
"""handle get request"""
order = request.GET.get("order")
if order and order not in self.order_choices:
message = {"message": f"invalid order parameter {order}"}
return Response(message, status=400)
return Response(BiggestChannel().process())