From 1350b3b784c255b15897b93761ad4849bede1e88 Mon Sep 17 00:00:00 2001 From: Antonio Rivero <38889534+Antonio-RiveroMartnez@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:08:37 +0100 Subject: [PATCH] fix(plugins): Fix dashboard filter in Period Over Period KPI plugin (#27013) (cherry picked from commit 607301ad569bd251ec53dabfcb69f6da3ed958ea) --- .../src/plugin/buildQuery.ts | 16 ++++++++++++---- .../src/plugin/transformProps.ts | 12 +++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/plugin/buildQuery.ts b/superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/plugin/buildQuery.ts index 202063c13c5f8..aa0477e48f5fd 100644 --- a/superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/plugin/buildQuery.ts +++ b/superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/plugin/buildQuery.ts @@ -217,7 +217,11 @@ function calculatePrev( } export default function buildQuery(formData: QueryFormData) { - const { cols: groupby, time_comparison: timeComparison } = formData; + const { + cols: groupby, + time_comparison: timeComparison, + extra_form_data: extraFormData, + } = formData; const queryContextA = buildQueryContext(formData, baseQueryObject => [ { @@ -244,9 +248,11 @@ export default function buildQuery(formData: QueryFormData) { 'comparator' in timeFilter && typeof timeFilter.comparator === 'string' ) { - [testSince, testUntil] = getSinceUntil( - timeFilter.comparator.toLocaleLowerCase(), - ); + let timeRange = timeFilter.comparator.toLocaleLowerCase(); + if (extraFormData?.time_range) { + timeRange = extraFormData.time_range; + } + [testSince, testUntil] = getSinceUntil(timeRange); } let formDataB: QueryFormData; @@ -277,11 +283,13 @@ export default function buildQuery(formData: QueryFormData) { formDataB = { ...formData, adhoc_filters: queryBFilters, + extra_form_data: {}, }; } else { formDataB = { ...formData, adhoc_filters: formData.adhoc_custom, + extra_form_data: {}, }; } diff --git a/superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/plugin/transformProps.ts b/superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/plugin/transformProps.ts index 437641143cd3d..80737f6032feb 100644 --- a/superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/plugin/transformProps.ts +++ b/superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/plugin/transformProps.ts @@ -112,9 +112,15 @@ export default function transformProps(chartProps: ChartProps) { let valueDifference: number | string = bigNumber - prevNumber; - const percentDifferenceNum = prevNumber - ? (bigNumber - prevNumber) / Math.abs(prevNumber) - : 0; + let percentDifferenceNum; + + if (!bigNumber && !prevNumber) { + percentDifferenceNum = 0; + } else if (!bigNumber || !prevNumber) { + percentDifferenceNum = bigNumber ? 1 : -1; + } else { + percentDifferenceNum = (bigNumber - prevNumber) / Math.abs(prevNumber); + } const compType = compTitles[formData.timeComparison]; bigNumber = numberFormatter(bigNumber);