add all duration aggs

This commit is contained in:
Simon 2023-08-31 21:31:47 +07:00
parent 505f5b73c5
commit 5acc1ea718
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
1 changed files with 16 additions and 2 deletions

View File

@ -103,6 +103,8 @@ class WatchProgress(AggBase):
}, },
}, },
}, },
"total_duration": {"sum": {"field": "player.duration"}},
"total_vids": {"value_count": {"field": "_index"}},
}, },
} }
@ -112,13 +114,24 @@ class WatchProgress(AggBase):
buckets = aggregations[self.name]["buckets"] buckets = aggregations[self.name]["buckets"]
response = {} response = {}
all_duration = int(aggregations["total_duration"].get("value"))
response.update(
{
"all": {
"duration": all_duration,
"duration_str": DurationConverter().get_str(all_duration),
"items": aggregations["total_vids"].get("value"),
}
}
)
for bucket in buckets: for bucket in buckets:
response.update(self._build_bucket(bucket)) response.update(self._build_bucket(bucket, all_duration))
return response return response
@staticmethod @staticmethod
def _build_bucket(bucket): def _build_bucket(bucket, all_duration):
"""parse bucket""" """parse bucket"""
duration = int(bucket["watch_docs"]["duration"]["value"]) duration = int(bucket["watch_docs"]["duration"]["value"])
@ -133,6 +146,7 @@ class WatchProgress(AggBase):
key: { key: {
"duration": duration, "duration": duration,
"duration_str": duration_str, "duration_str": duration_str,
"progress": duration / all_duration if all_duration else 0,
"items": items, "items": items,
} }
} }