Skip to content

Commit

Permalink
fix: Chart crashing if timeseries_limit_metric is an empty array (apa…
Browse files Browse the repository at this point in the history
…che#23480)

(cherry picked from commit 4530542)
  • Loading branch information
kgabryje authored and sadpandajoe committed Mar 24, 2023
1 parent 63dfa4d commit dd0f6d7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/
import {
ensureIsArray,
getMetricLabel,
QueryFormData,
QueryFormMetric,
Expand All @@ -25,14 +26,16 @@ import {
export function extractExtraMetrics(
formData: QueryFormData,
): QueryFormMetric[] {
const { groupby, timeseries_limit_metric, x_axis_sort } = formData;
const { groupby, timeseries_limit_metric, x_axis_sort, metrics } = formData;
const extra_metrics: QueryFormMetric[] = [];
const limitMetric = ensureIsArray(timeseries_limit_metric)[0];
if (
!(groupby || []).length &&
timeseries_limit_metric &&
getMetricLabel(timeseries_limit_metric) === x_axis_sort
limitMetric &&
getMetricLabel(limitMetric) === x_axis_sort &&
!metrics?.some(metric => getMetricLabel(metric) === x_axis_sort)
) {
extra_metrics.push(timeseries_limit_metric);
extra_metrics.push(limitMetric);
}
return extra_metrics;
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,45 @@ test('returns empty array if groupby populated', () => {
}),
).toEqual([]);
});

test('returns empty array if timeseries_limit_metric and x_axis_sort are included in main metrics array', () => {
expect(
extractExtraMetrics({
...baseFormData,
timeseries_limit_metric: 'a',
x_axis_sort: 'a',
}),
).toEqual([]);
});

test('returns empty array if timeseries_limit_metric and x_axis_sort are included in main metrics array with adhoc metrics', () => {
expect(
extractExtraMetrics({
...baseFormData,
metrics: [
'a',
{
expressionType: 'SIMPLE',
aggregate: 'SUM',
column: { column_name: 'num' },
},
],
timeseries_limit_metric: {
expressionType: 'SIMPLE',
aggregate: 'SUM',
column: { column_name: 'num' },
},
x_axis_sort: 'SUM(num)',
}),
).toEqual([]);
});

test('returns emoty array if timeseries_limit_metric is an empty array', () => {
expect(
extractExtraMetrics({
...baseFormData,
// @ts-ignore
timeseries_limit_metric: [],
}),
).toEqual([]);
});

0 comments on commit dd0f6d7

Please sign in to comment.