Skip to content

Commit

Permalink
remove sort values on stacked totals
Browse files Browse the repository at this point in the history
  • Loading branch information
eschutho committed Dec 7, 2024
1 parent 77f3764 commit e685c5d
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/* eslint-disable camelcase */
import { invert } from 'lodash';
import {
AdhocMetric,
AnnotationLayer,
AxisType,
buildCustomFormatters,
Expand Down Expand Up @@ -290,12 +291,25 @@ export default function transformProps(
const showValueIndexesB = extractShowValueIndexes(rawSeriesB, {
stack,
});

const metricMapFunc = (metric: string | AdhocMetric) => {
if (metric.hasOwnProperty('label')) {
return (metric as AdhocMetric).label;
}
return verboseMap[metric as string] || metric;
};
const metricsLabels = metrics
.map(metricMapFunc)
.filter((label): label is string => label !== undefined);
const metricsLabelsB = metricsB.map(metricMapFunc);

const { totalStackedValues, thresholdValues } = extractDataTotalValues(
rebasedDataA,
{
stack,
percentageThreshold,
xAxisCol: xAxisLabel,
metricsLabels,
},
);
const {
Expand All @@ -305,6 +319,7 @@ export default function transformProps(
stack: Boolean(stackB),
percentageThreshold,
xAxisCol: xAxisLabel,
metricsLabels: metricsLabelsB,
});

annotationLayers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
t,
TimeseriesChartDataResponseResult,
NumberFormats,
AdhocMetric,
} from '@superset-ui/core';
import {
extractExtraMetrics,
Expand Down Expand Up @@ -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,
Expand All @@ -223,6 +232,7 @@ export default function transformProps(
percentageThreshold,
xAxisCol: xAxisLabel,
legendState,
metricsLabels,
},
);
const extraMetricLabels = extractExtraMetrics(chartProps.rawFormData).map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* under the License.
*/
import {
AdhocMetric,
AxisType,
ChartDataResponseResult,
DataRecord,
Expand All @@ -29,6 +30,7 @@ import {
normalizeTimestamp,
NumberFormats,
NumberFormatter,
QueryFormMetric,
SupersetTheme,
TimeFormatter,
ValueFormatter,
Expand Down Expand Up @@ -60,20 +62,20 @@ export function extractDataTotalValues(
opts: {
stack: StackType;
percentageThreshold: number;
xAxisCol: string;
legendState?: LegendState;
metricsLabels: string[];
},
): {
totalStackedValues: number[];
thresholdValues: number[];
} {
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]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
calculateLowerLogTick,
dedupSeries,
extractGroupbyLabel,
extractDataTotalValues,
extractSeries,
extractShowValueIndexes,
extractTooltipKeys,
Expand Down Expand Up @@ -1085,6 +1086,123 @@ 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 metricsLabels = ['metric1', 'metric2'];
const opts = {
stack: true,
percentageThreshold: 10,
metricsLabels,
};
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 metricsLabels = ['metric1', 'metric2'];
const opts = {
stack: true,
percentageThreshold: 10,
metricsLabels,
};
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 metricsLabels: string[] = [];
const opts = {
stack: true,
percentageThreshold: 10,
metricsLabels,
};
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 metricsLabels = ['metric1', 'metric2'];
const opts = {
stack: false,
percentageThreshold: 10,
metricsLabels,
};
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 metricsLabels = ['x', 'y', 'z'];
const opts = {
stack: true,
percentageThreshold: 10,
metricsLabels,
};
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 metricsLabels = ['metric1', 'metric2'];
const opts = {
stack: true,
percentageThreshold: 50,
metricsLabels,
};
const result = extractDataTotalValues(data, opts);
expect(result.totalStackedValues).toEqual([30, 40]);
expect(result.thresholdValues).toEqual([15, 20]);
});
it('should not add datum not in metrics 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 metricsLabels = ['val'];
const opts = {
stack: true,
percentageThreshold: 50,
metricsLabels,
};

const result = extractDataTotalValues(data, opts);

// Assuming extractDataTotalValues returns the total value
// without including the 'xAxisCol' category
expect(result.totalStackedValues).toEqual([345, 2432, 4543]); // 10 + 20, excluding the 'xAxisCol' category
});
});

test('extractTooltipKeys with rich tooltip', () => {
const result = extractTooltipKeys(forecastValue, 1, true, false);
expect(result).toEqual(['foo', 'bar']);
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
};
Expand Down

0 comments on commit e685c5d

Please sign in to comment.