You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2024/12/06 23:21:57 UTC

(superset) 01/03: remove sort values on stacked totals

This is an automated email from the ASF dual-hosted git repository.

elizabeth pushed a commit to branch elizabeth/add-indexes-perms
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 988ab61ff94e87087d23257b3a9f2ac24c7f0af0
Author: Elizabeth Thompson <es...@gmail.com>
AuthorDate: Thu Dec 5 16:45:24 2024 -0800

    remove sort values on stacked totals
---
 .../src/MixedTimeseries/transformProps.ts          |  18 ++++
 .../src/Timeseries/transformProps.ts               |  10 ++
 .../plugin-chart-echarts/src/utils/series.ts       |   7 +-
 .../plugin-chart-echarts/test/utils/series.test.ts | 112 +++++++++++++++++++++
 superset-frontend/webpack.config.js                |   2 +-
 5 files changed, 146 insertions(+), 3 deletions(-)

diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts
index d4d19f9c2f..78f808e700 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts
@@ -19,6 +19,7 @@
 /* eslint-disable camelcase */
 import { invert } from 'lodash';
 import {
+  AdhocMetric,
   AnnotationLayer,
   AxisType,
   buildCustomFormatters,
@@ -290,12 +291,28 @@ export default function transformProps(
   const showValueIndexesB = extractShowValueIndexes(rawSeriesB, {
     stack,
   });
+  const metricsLabels = metrics
+    .map(metric => {
+      if (metric.hasOwnProperty('label')) {
+        return (metric as AdhocMetric).label;
+      }
+      return verboseMap[metric as string] || metric;
+    })
+    .filter((label): label is string => label !== undefined);
+  const metricsLabelsB = metricsB.map((metric: string | AdhocMetric) => {
+    if (metric.hasOwnProperty('label')) {
+      return (metric as AdhocMetric).label;
+    }
+    return verboseMap[metric as string] || metric;
+  });
+
   const { totalStackedValues, thresholdValues } = extractDataTotalValues(
     rebasedDataA,
     {
       stack,
       percentageThreshold,
       xAxisCol: xAxisLabel,
+      metricsLabels,
     },
   );
   const {
@@ -305,6 +322,7 @@ export default function transformProps(
     stack: Boolean(stackB),
     percentageThreshold,
     xAxisCol: xAxisLabel,
+    metricsLabels: metricsLabelsB,
   });
 
   annotationLayers
diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts
index c62b7284b2..be3634aa1c 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts
@@ -40,6 +40,7 @@ import {
   t,
   TimeseriesChartDataResponseResult,
   NumberFormats,
+  AdhocMetric,
 } from '@superset-ui/core';
 import {
   extractExtraMetrics,
@@ -215,6 +216,14 @@ export default function transformProps(
   ) {
     xAxisLabel = verboseMap[xAxisLabel];
   }
+  const metricsLabels = metrics
+    .map(metric => {
+      if (metric.hasOwnProperty('label')) {
+        return (metric as AdhocMetric).label;
+      }
+      return verboseMap[metric as string] || metric;
+    })
+    .filter((label): label is string => label !== undefined);
   const isHorizontal = orientation === OrientationType.Horizontal;
   const { totalStackedValues, thresholdValues } = extractDataTotalValues(
     rebasedData,
@@ -223,6 +232,7 @@ export default function transformProps(
       percentageThreshold,
       xAxisCol: xAxisLabel,
       legendState,
+      metricsLabels,
     },
   );
   const extraMetricLabels = extractExtraMetrics(chartProps.rawFormData).map(
diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts b/superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts
index 0aa0ae988e..e636cc7087 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts
@@ -18,6 +18,7 @@
  * under the License.
  */
 import {
+  AdhocMetric,
   AxisType,
   ChartDataResponseResult,
   DataRecord,
@@ -29,6 +30,7 @@ import {
   normalizeTimestamp,
   NumberFormats,
   NumberFormatter,
+  QueryFormMetric,
   SupersetTheme,
   TimeFormatter,
   ValueFormatter,
@@ -62,6 +64,7 @@ export function extractDataTotalValues(
     percentageThreshold: number;
     xAxisCol: string;
     legendState?: LegendState;
+    metricsLabels: string[];
   },
 ): {
   totalStackedValues: number[];
@@ -69,11 +72,11 @@ export function extractDataTotalValues(
 } {
   const totalStackedValues: number[] = [];
   const thresholdValues: number[] = [];
-  const { stack, percentageThreshold, xAxisCol, legendState } = opts;
+  const { stack, percentageThreshold, legendState, metricsLabels } = opts;
   if (stack) {
     data.forEach(datum => {
       const values = Object.keys(datum).reduce((prev, curr) => {
-        if (curr === xAxisCol) {
+        if (!metricsLabels.includes(curr)) {
           return prev;
         }
         if (legendState && !legendState[curr]) {
diff --git a/superset-frontend/plugins/plugin-chart-echarts/test/utils/series.test.ts b/superset-frontend/plugins/plugin-chart-echarts/test/utils/series.test.ts
index 7054f6019a..bfc28bcc6b 100644
--- a/superset-frontend/plugins/plugin-chart-echarts/test/utils/series.test.ts
+++ b/superset-frontend/plugins/plugin-chart-echarts/test/utils/series.test.ts
@@ -29,6 +29,7 @@ import {
   calculateLowerLogTick,
   dedupSeries,
   extractGroupbyLabel,
+  extractDataTotalValues,
   extractSeries,
   extractShowValueIndexes,
   extractTooltipKeys,
@@ -1085,6 +1086,117 @@ const forecastValue = [
   },
 ];
 
+describe('extractDataTotalValues', () => {
+  it('test_extractDataTotalValues_withStack', () => {
+    const data: DataRecord[] = [
+      { metric1: 10, metric2: 20, xAxisCol: '2021-01-01' },
+      { metric1: 15, metric2: 25, xAxisCol: '2021-01-02' },
+    ];
+    const opts = {
+      stack: true,
+      percentageThreshold: 10,
+      xAxisCol: 'xAxisCol',
+    };
+    const result = extractDataTotalValues(data, opts);
+    expect(result.totalStackedValues).toEqual([30, 40]);
+    expect(result.thresholdValues).toEqual([3, 4]);
+  });
+
+  it('should calculate total and threshold values with stack option enabled', () => {
+    const data: DataRecord[] = [
+      { metric1: 10, metric2: 20, xAxisCol: '2021-01-01' },
+      { metric1: 15, metric2: 25, xAxisCol: '2021-01-02' },
+    ];
+    const opts = {
+      stack: true,
+      percentageThreshold: 10,
+      xAxisCol: 'xAxisCol',
+    };
+    const result = extractDataTotalValues(data, opts);
+    expect(result.totalStackedValues).toEqual([30, 40]);
+    expect(result.thresholdValues).toEqual([3, 4]);
+  });
+
+  it('should handle empty data array', () => {
+    const data: DataRecord[] = [];
+    const opts = {
+      stack: true,
+      percentageThreshold: 10,
+      xAxisCol: 'xAxisCol',
+    };
+    const result = extractDataTotalValues(data, opts);
+    expect(result.totalStackedValues).toEqual([]);
+    expect(result.thresholdValues).toEqual([]);
+  });
+
+  it('should calculate total and threshold values with stack option disabled', () => {
+    const data: DataRecord[] = [
+      { metric1: 10, metric2: 20, xAxisCol: '2021-01-01' },
+      { metric1: 15, metric2: 25, xAxisCol: '2021-01-02' },
+    ];
+    const opts = {
+      stack: false,
+      percentageThreshold: 10,
+      xAxisCol: 'xAxisCol',
+    };
+    const result = extractDataTotalValues(data, opts);
+    expect(result.totalStackedValues).toEqual([]);
+    expect(result.thresholdValues).toEqual([]);
+  });
+
+  it('should handle data with null or undefined values', () => {
+    const data: DataRecord[] = [
+      { my_x_axis: 'abc', x: 1, y: 0, z: 2 },
+      { my_x_axis: 'foo', x: null, y: 10, z: 5 },
+      { my_x_axis: null, x: 4, y: 3, z: 7 },
+    ];
+    const opts = {
+      stack: true,
+      percentageThreshold: 10,
+      xAxisCol: 'my_x_axis',
+    };
+    const result = extractDataTotalValues(data, opts);
+    expect(result.totalStackedValues).toEqual([3, 15, 14]);
+    expect(result.thresholdValues).toEqual([
+      0.30000000000000004, 1.5, 1.4000000000000001,
+    ]);
+  });
+
+  it('should handle different percentage thresholds', () => {
+    const data: DataRecord[] = [
+      { metric1: 10, metric2: 20, xAxisCol: '2021-01-01' },
+      { metric1: 15, metric2: 25, xAxisCol: '2021-01-02' },
+    ];
+    const opts = {
+      stack: true,
+      percentageThreshold: 50,
+      xAxisCol: 'xAxisCol',
+    };
+    const result = extractDataTotalValues(data, opts);
+    expect(result.totalStackedValues).toEqual([30, 40]);
+    expect(result.thresholdValues).toEqual([15, 20]);
+  });
+  it('should not add datum named the same as xAxisSort to the total value when stacked', () => {
+    const data: DataRecord[] = [
+      { xAxisCol: 'foo', xAxisSort: 10, val: 345 },
+      { xAxisCol: 'bar', xAxisSort: 20, val: 2432 },
+      { xAxisCol: 'baz', xAxisSort: 30, val: 4543 },
+    ];
+    const opts = {
+      stack: true,
+      percentageThreshold: 50,
+      xAxisCol: 'xAxisCol',
+      xAxisSort: 'xAxisSort',
+    };
+
+    const result = extractDataTotalValues(data, opts);
+
+    // Assuming extractDataTotalValues returns the total value
+    // without including the 'xAxisSort' category
+    expect(result.totalStackedValues).toEqual([345, 2432, 4543]); // 10 + 20, excluding the 'xAxisSort' category
+  });
+});
+
 test('extractTooltipKeys with rich tooltip', () => {
   const result = extractTooltipKeys(forecastValue, 1, true, false);
   expect(result).toEqual(['foo', 'bar']);
diff --git a/superset-frontend/webpack.config.js b/superset-frontend/webpack.config.js
index 10bf4d49ee..b02f0f80fd 100644
--- a/superset-frontend/webpack.config.js
+++ b/superset-frontend/webpack.config.js
@@ -568,7 +568,7 @@ if (isDevMode) {
         warnings: false,
         runtimeErrors: error => !/ResizeObserver/.test(error.message),
       },
-      logging: 'error',
+      logging: 'verbose',
     },
     static: path.join(process.cwd(), '../static/assets'),
   };