From 3379a413faec6698cbb751459189f9a7f7088628 Mon Sep 17 00:00:00 2001 From: Kallil Souza dos Santos Date: Tue, 24 Dec 2024 16:50:27 -0300 Subject: [PATCH 1/2] feat: add full number for each result in data transformation --- .../flowruns/usecases/query_execute.py | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/insights/sources/flowruns/usecases/query_execute.py b/insights/sources/flowruns/usecases/query_execute.py index b396ba1..ffbb2e4 100644 --- a/insights/sources/flowruns/usecases/query_execute.py +++ b/insights/sources/flowruns/usecases/query_execute.py @@ -9,17 +9,25 @@ ) -def transform_terms_count_to_percentage( +def transform_results_data( total: int, others: int, terms_agg_buckets: list[dict] ) -> list[dict]: transformed_results = [] + for term in terms_agg_buckets: - value = term.get("doc_count") - if value == 0 or value is None: - transformed_results.append({"label": term.get("key"), "value": 0}) + full_value = term.get("doc_count") + + if full_value == 0 or full_value is None: + transformed_results.append( + {"label": term.get("key"), "value": 0, full_value: 0} + ) continue - percent = round(((value / total) * 100), 2) - transformed_results.append({"label": term.get("key"), "value": percent}) + + percentage = round(((full_value / total) * 100), 2) + transformed_results.append( + {"label": term.get("key"), "value": percentage, "full_value": full_value} + ) + return transformed_results @@ -47,7 +55,7 @@ def execute( terms_agg = ( response.get("aggregations", {}).get("values", {}).get("agg_field") ) - transformed_terms = transform_terms_count_to_percentage( + transformed_terms = transform_results_data( total=terms_agg.get("doc_count", 0), others=terms_agg.get("agg_value", {}).get("sum_other_doc_count", 0), terms_agg_buckets=terms_agg.get("agg_value", {}).get("buckets", []), From 77a2bb7f384d48cabb48698b0f30bbeec9c2c490 Mon Sep 17 00:00:00 2001 From: Kallil Souza dos Santos Date: Tue, 24 Dec 2024 16:51:09 -0300 Subject: [PATCH 2/2] feat: add test for flowrun data transformation function --- .../tests/flowruns/test_query_execute.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 insights/sources/tests/flowruns/test_query_execute.py diff --git a/insights/sources/tests/flowruns/test_query_execute.py b/insights/sources/tests/flowruns/test_query_execute.py new file mode 100644 index 0000000..b676e37 --- /dev/null +++ b/insights/sources/tests/flowruns/test_query_execute.py @@ -0,0 +1,27 @@ +from random import randint +from django.test import TestCase +from django.utils.crypto import get_random_string + +from insights.sources.flowruns.usecases.query_execute import transform_results_data + + +class TestFlowRunDataTransformation(TestCase): + def setUp(self): + counts = [randint(1, 10) for i in range(2)] + + self.total = sum(counts) + self.terms_agg_buckets = [] + + for count in counts: + self.terms_agg_buckets.append( + {"key": get_random_string(10), "doc_count": count} + ) + + def test_flowrun_data_transformation(self): + results = transform_results_data(self.total, 0, self.terms_agg_buckets) + + for result in results: + self.assertEqual( + result["value"], + round(((result["full_value"] / self.total) * 100), 2), + )