feat(chart): add toggle for percentage metric calculation mode in Table chart (#33656)

This commit is contained in:
Levis Mbote
2025-06-13 21:00:58 +03:00
committed by GitHub
parent 0d3eebd221
commit 7deca8f2cd
6 changed files with 216 additions and 13 deletions

View File

@@ -741,6 +741,47 @@ class QueryContextProcessor:
return df.to_dict(orient="records")
def ensure_totals_available(self) -> None:
queries_needing_totals = []
totals_queries = []
for i, query in enumerate(self._query_context.queries):
needs_totals = any(
pp.get("operation") == "contribution"
for pp in getattr(query, "post_processing", []) or []
)
if needs_totals:
queries_needing_totals.append(i)
is_totals_query = (
not query.columns and query.metrics and not query.post_processing
)
if is_totals_query:
totals_queries.append(i)
if not queries_needing_totals or not totals_queries:
return
totals_idx = totals_queries[0]
totals_query = self._query_context.queries[totals_idx]
totals_query.row_limit = None
result = self._query_context.get_query_result(totals_query)
df = result.df
totals = {
col: df[col].sum() for col in df.columns if df[col].dtype.kind in "biufc"
}
for idx in queries_needing_totals:
query = self._query_context.queries[idx]
if hasattr(query, "post_processing") and query.post_processing:
for pp in query.post_processing:
if pp.get("operation") == "contribution":
pp["options"]["contribution_totals"] = totals
def get_payload(
self,
cache_query_context: bool | None = False,
@@ -748,7 +789,8 @@ class QueryContextProcessor:
) -> dict[str, Any]:
"""Returns the query results with both metadata and data"""
# Get all the payloads from the QueryObjects
self.ensure_totals_available()
query_results = [
get_query_results(
query_obj.result_type or self._query_context.result_type,
@@ -758,6 +800,7 @@ class QueryContextProcessor:
)
for query_obj in self._query_context.queries
]
return_value = {"queries": query_results}
if cache_query_context: