diff --git a/tubearchivist/api/src/aggs.py b/tubearchivist/api/src/aggs.py index 7b3dcdb1..08259146 100644 --- a/tubearchivist/api/src/aggs.py +++ b/tubearchivist/api/src/aggs.py @@ -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} diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index 2b98a94a..1e12ad78 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -146,4 +146,9 @@ urlpatterns = [ views.StatWatchProgress.as_view(), name="api-stats-watch", ), + path( + "stats/downloadhist/", + views.StatDownloadHist.as_view(), + name="api-stats-downloadhist", + ), ] diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 008f8907..704e53a4 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -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())