From 046a142f36ee9869d91a0ad8f609a776fe1b7883 Mon Sep 17 00:00:00 2001 From: clearml <> Date: Thu, 5 Dec 2024 22:36:33 +0200 Subject: [PATCH] Do not return the last incomplete interval for worker stats chart --- apiserver/bll/workers/stats.py | 10 ++++++---- apiserver/tests/automated/test_workers.py | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/apiserver/bll/workers/stats.py b/apiserver/bll/workers/stats.py index e7e630a..a1033a2 100644 --- a/apiserver/bll/workers/stats.py +++ b/apiserver/bll/workers/stats.py @@ -78,6 +78,8 @@ class WorkerStats: if from_date >= to_date: raise bad_request.FieldsValueError("from_date must be less than to_date") + interval = max(request.interval, self.min_chart_interval) + def get_dates_agg() -> dict: es_to_agg_types = ( ("avg", AggregationType.avg.value), @@ -85,7 +87,6 @@ class WorkerStats: ("max", AggregationType.max.value), ) - interval = max(request.interval, self.min_chart_interval) return { "dates": { "date_histogram": { @@ -136,16 +137,16 @@ class WorkerStats: with translate_errors_context(): data = self._search_company_stats(company_id, es_req) - return self._extract_results(data, request.items, request.split_by_variant) + cutoff_date = (to_date - 0.9 * interval) * 1000 # do not return the point for the incomplete last interval + return self._extract_results(data, request.items, request.split_by_variant, cutoff_date) @staticmethod def _extract_results( - data: dict, request_items: Sequence[StatItem], split_by_variant: bool + data: dict, request_items: Sequence[StatItem], split_by_variant: bool, cutoff_date ) -> dict: """ Clean results returned from elastic search (remove "aggregations", "buckets" etc.), leave only aggregation types requested by the user and return a clean dictionary - and return a "clean" dictionary of :param data: aggregation data retrieved from ES :param request_items: aggs types requested by the user :param split_by_variant: if False then aggregate by metric type, otherwise metric type + variant @@ -172,6 +173,7 @@ class WorkerStats: return [ extract_date_stats(date, metric_key) for date in metric_or_variant["dates"]["buckets"] + if date["key"] <= cutoff_date ] def extract_variant_results(metric: dict) -> dict: diff --git a/apiserver/tests/automated/test_workers.py b/apiserver/tests/automated/test_workers.py index ac271b3..cb05724 100644 --- a/apiserver/tests/automated/test_workers.py +++ b/apiserver/tests/automated/test_workers.py @@ -180,7 +180,7 @@ class TestWorkersService(TestService): self.assertEqual( set(stat.aggregation for stat in metric.stats), metric_stats ) - self.assertEqual(len(metric.dates), 11) + self.assertTrue(11 >= len(metric.dates) >= 10) # split by variants res = self.api.workers.get_stats( @@ -199,7 +199,7 @@ class TestWorkersService(TestService): set(metric.variant for metric in worker.metrics), {"0", "1"} if worker.worker == workers[0] else {"0"}, ) - self.assertEqual(len(metric.dates), 11) + self.assertTrue(11 >= len(metric.dates) >= 10) res = self.api.workers.get_stats( items=[dict(key="cpu_usage", aggregation="avg")],