Skip to content

Commit

Permalink
Merge pull request #47 from weni-ai/feature/return-full-number-in-rec…
Browse files Browse the repository at this point in the history
…urrence-chart

Feature/return full number in recurrence chart
  • Loading branch information
kallilsouza authored Dec 26, 2024
2 parents 721d294 + 77a2bb7 commit 61a002c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
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),
)

0 comments on commit 61a002c

Please sign in to comment.