From aadd1b44de04eebc84d2020a76f2a3ccf3dc454f Mon Sep 17 00:00:00 2001 From: VitaliStupin Date: Fri, 18 Oct 2024 16:34:59 +0300 Subject: [PATCH] Fix top 5 charts values in reports by calculating the correct average query duration --- .../opmon_reports/report_manager.py | 24 +++++++++++++++---- .../tests/test_report_manager.py | 12 +++++----- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/reports_module/opmon_reports/report_manager.py b/reports_module/opmon_reports/report_manager.py index 11efb12..eccff11 100644 --- a/reports_module/opmon_reports/report_manager.py +++ b/reports_module/opmon_reports/report_manager.py @@ -399,8 +399,10 @@ def get_succeeded_top(self, data, produced_service): if count <= 0: continue - if name not in result_dict or result_dict[name] < count: + if name not in result_dict: result_dict[name] = count + else: + result_dict[name] += count sorted_pairs = sorted(result_dict.items(), key=operator.itemgetter(1)) return sorted_pairs if len(sorted_pairs) < 6 else sorted_pairs[-5:] @@ -421,16 +423,28 @@ def create_duration_plot(self, data, produced_service, file_name): return self.create_plot(names, durations, translated_title, file_name) def get_duration_top(self, data, produced_service): + sum_dict = dict() + count_dict = dict() result_dict = dict() for key1 in data: for key2 in data[key1]: - duration = data[key1][key2].duration_avg.rounded_average + duration_sum = data[key1][key2].duration_avg.sum + query_count = data[key1][key2].duration_avg.count + duration_mean = data[key1][key2].duration_avg.rounded_average name = f"{self.target.subsystem_code}: {key1}" if produced_service else f"{key1}: {key2}" - if duration is None: + if duration_mean is None: continue - if name not in result_dict or result_dict[name] < duration: - result_dict[name] = duration + if name not in result_dict: + sum_dict[name] = duration_sum + count_dict[name] = query_count + result_dict[name] = duration_mean + else: + # Calculating the average duration of a produced service by summing + # the client report rows + sum_dict[name] += duration_sum + count_dict[name] += query_count + result_dict[name] = round(sum_dict[name] / count_dict[name]) sorted_pairs = sorted(result_dict.items(), key=operator.itemgetter(1)) return sorted_pairs if len(sorted_pairs) < 6 else sorted_pairs[-5:] diff --git a/reports_module/opmon_reports/tests/test_report_manager.py b/reports_module/opmon_reports/tests/test_report_manager.py index 5c14b9d..f638329 100644 --- a/reports_module/opmon_reports/tests/test_report_manager.py +++ b/reports_module/opmon_reports/tests/test_report_manager.py @@ -226,9 +226,9 @@ def test_get_succeeded_top(mocker, basic_args, target_subsystem): test_data = map_rows_to_producer_services(rows) succeeded_top = report_manager.get_succeeded_top(test_data, produced_service=True) assert succeeded_top == [ - ('TEST_SUB: service_2', 12), - ('TEST_SUB: service_3', 14), - ('TEST_SUB: service_1', 15) + ('TEST_SUB: service_1', 32), + ('TEST_SUB: service_2', 32), + ('TEST_SUB: service_3', 35) ] test_data = map_rows_to_consumer_services(rows) @@ -264,9 +264,9 @@ def test_get_duration(mocker, basic_args, target_subsystem): test_data = map_rows_to_producer_services(rows) duration_top = report_manager.get_duration_top(test_data, produced_service=True) assert duration_top == [ - ('TEST_SUB: service_2', 90), - ('TEST_SUB: service_1', 100), - ('TEST_SUB: service_3', 110) + ('TEST_SUB: service_2', 39), + ('TEST_SUB: service_1', 43), + ('TEST_SUB: service_3', 47) ] test_data = map_rows_to_consumer_services(rows)