add download video histogram stats

This commit is contained in:
Simon 2023-08-30 17:42:10 +07:00
parent 58ef8f753f
commit 988c2b8af7
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 63 additions and 13 deletions

View File

@ -13,8 +13,7 @@ class AggBase:
def get(self):
"""make get call"""
data_size = {"size": 0, "aggs": self.data}
response, _ = ElasticWrap(self.path).get(data_size)
response, _ = ElasticWrap(self.path).get(self.data)
print(f"[agg][{self.name}] took {response.get('took')} ms to process")
return response.get("aggregations")
@ -29,7 +28,7 @@ class Primary(AggBase):
name = "primary"
path = "ta_video,ta_channel,ta_playlist,ta_subtitle,ta_download/_search"
data = {name: {"terms": {"field": "_index"}}}
data = {"size": 0, "aggs": {name: {"terms": {"field": "_index"}}}}
def process(self):
"""make the call"""
@ -45,18 +44,21 @@ class WatchProgress(AggBase):
name = "watch_progress"
path = "ta_video/_search"
data = {
name: {
"terms": {"field": "player.watched"},
"aggs": {
"watch_docs": {
"filter": {"terms": {"player.watched": [True, False]}},
"aggs": {
"true_count": {"value_count": {"field": "_index"}},
"duration": {"sum": {"field": "player.duration"}},
"size": 0,
"aggs": {
name: {
"terms": {"field": "player.watched"},
"aggs": {
"watch_docs": {
"filter": {"terms": {"player.watched": [True, False]}},
"aggs": {
"true_count": {"value_count": {"field": "_index"}},
"duration": {"sum": {"field": "player.duration"}},
},
},
},
},
}
},
}
def process(self):
@ -91,3 +93,34 @@ class WatchProgress(AggBase):
}
return bucket_parsed
class DownloadHist(AggBase):
"""get downloads histogram last week"""
name = "videos_last_week"
path = "ta_video/_search"
data = {
"size": 0,
"aggs": {
name: {
"date_histogram": {
"field": "date_downloaded",
"calendar_interval": "day",
"format": "yyyy-MM-dd",
"order": {"_key": "desc"},
},
"aggs": {
"total_videos": {"value_count": {"field": "youtube_id"}}
},
}
},
"query": {"range": {"date_downloaded": {"gte": "now-6d/d"}}},
}
def process(self):
"""process query"""
aggregations = self.get()
buckets = aggregations[self.name]["buckets"]
return {i.get("key_as_string"): i.get("doc_count") for i in buckets}

View File

@ -146,4 +146,9 @@ urlpatterns = [
views.StatWatchProgress.as_view(),
name="api-stats-watch",
),
path(
"stats/downloadhist/",
views.StatDownloadHist.as_view(),
name="api-stats-downloadhist",
),
]

View File

@ -1,6 +1,6 @@
"""all API views"""
from api.src.aggs import Primary, WatchProgress
from api.src.aggs import DownloadHist, Primary, WatchProgress
from api.src.search_processor import SearchProcess
from home.src.download.queue import PendingInteract
from home.src.download.subscriptions import (
@ -1000,3 +1000,15 @@ class StatWatchProgress(ApiBaseView):
# pylint: disable=unused-argument
return Response(WatchProgress().process())
class StatDownloadHist(ApiBaseView):
"""resolves to /api/stats/downloadhist/
GET: return download video count histogram for last days
"""
def get(self, request):
"""handle get request"""
# pylint: disable=unused-argument
return Response(DownloadHist().process())