Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/return full number in recurrence chart #47

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions insights/sources/flowruns/usecases/query_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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", []),
Expand Down
27 changes: 27 additions & 0 deletions insights/sources/tests/flowruns/test_query_execute.py
Original file line number Diff line number Diff line change
@@ -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),
)