[API] add video aggregation

This commit is contained in:
Simon 2023-11-19 13:01:27 +07:00
parent 4a145ee7cb
commit 91bb0ed9c0
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 60 additions and 8 deletions

View File

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

View File

@ -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/",

View File

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