Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { buildQueryContext, QueryFormData } from '@superset-ui/core';

/**
* Mirrors the legacy BulletViz.query_obj: a single ungrouped metric.
*/
export default function buildQuery(formData: QueryFormData) {
const { metric } = formData;
return buildQueryContext(formData, baseQueryObject => [
{
...baseQueryObject,
columns: [],
metrics: metric ? [metric] : [],
},
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ const metadata = new ChartMetadata({
),
exampleGallery: [{ url: example, urlDark: exampleDark }],
name: t('Bullet Chart'),
tags: [t('Business'), t('Legacy'), t('Report'), t('nvd3')],
tags: [t('Business'), t('Report'), t('nvd3')],
thumbnail,
thumbnailDark,
useLegacyApi: true,
});

export default class BulletChartPlugin extends ChartPlugin {
constructor() {
super({
loadChart: () => import('../ReactNVD3'),
loadBuildQuery: () => import('./buildQuery'),
metadata,
transformProps,
controlPanel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
// @ts-nocheck -- legacy transformProps with loosely-typed formData from ChartProps
import { ChartProps, VizType } from '@superset-ui/core';
import { ChartProps, getMetricLabel, VizType } from '@superset-ui/core';
import isTruthy from './utils/isTruthy';
import {
tokenizeToNumericArray,
Expand Down Expand Up @@ -122,15 +122,24 @@ export default function transformProps(chartProps: ChartProps) {
} = formData;

const rawData = queriesData[0].data || [];
const data = Array.isArray(rawData)
? rawData.map(row => ({
...row,
values: Array.isArray(row.values)
? row.values.map(value => ({ ...value }))
: row.values,
key: formatLabel(row.key, datasource.verboseMap),
}))
: rawData;
let data;
if (vizType === VizType.Bullet && Array.isArray(rawData)) {
// The legacy explore_json endpoint reshaped the single-metric result
// into `{ measures: [...] }` server-side; the v1 chart data endpoint
// returns plain records, so the reshape happens here.
const metricLabel = getMetricLabel(metric);
data = { measures: rawData.map(row => row[metricLabel]) };
} else {
data = Array.isArray(rawData)
? rawData.map(row => ({
...row,
values: Array.isArray(row.values)
? row.values.map(value => ({ ...value }))
: row.values,
key: formatLabel(row.key, datasource.verboseMap),
}))
: rawData;
}

if (vizType === VizType.Pie) {
numberFormat = numberFormat || grabD3Format(datasource, metric);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { supersetTheme } from '@apache-superset/core/theme';
import { ChartProps, QueryFormData, VizType } from '@superset-ui/core';
import buildQuery from '../src/Bullet/buildQuery';
import transformProps from '../src/transformProps';

const formData: QueryFormData = {
datasource: '5__table',
granularity_sqla: 'ds',
time_range: 'No filter',
viz_type: VizType.Bullet,
metric: 'sum__num',
};

describe('Bullet buildQuery', () => {
test('selects the single ungrouped metric', () => {
const [query] = buildQuery(formData).queries;
expect(query.metrics).toEqual(['sum__num']);
expect(query.columns).toEqual([]);
});

test('maps legacy granularity_sqla saved form data', () => {
const [query] = buildQuery(formData).queries;
expect(query.granularity).toEqual('ds');
});
});

describe('Bullet transformProps', () => {
const baseChartProps = {
width: 800,
height: 600,
datasource: { verboseMap: {} },
theme: supersetTheme,
hooks: {},
initialValues: {},
};

test('reshapes v1 records into the measures shape', () => {
const chartProps = new ChartProps({
...baseChartProps,
formData: { vizType: VizType.Bullet, metric: 'sum__num' },
queriesData: [{ data: [{ sum__num: 42 }] }],
});
const { data } = transformProps(chartProps) as {
data: { measures: number[] };
};
expect(data).toEqual({ measures: [42] });
});

test('passes through legacy-shaped measures payloads', () => {
const chartProps = new ChartProps({
...baseChartProps,
formData: { vizType: VizType.Bullet, metric: 'sum__num' },
queriesData: [{ data: { measures: [42] } }],
});
const { data } = transformProps(chartProps) as {
data: { measures: number[] };
};
expect(data).toEqual({ measures: [42] });
});

test('reshapes using adhoc metric labels', () => {
const chartProps = new ChartProps({
...baseChartProps,
formData: {
vizType: VizType.Bullet,
metric: {
expressionType: 'SIMPLE',
aggregate: 'SUM',
column: { column_name: 'num' },
label: 'SUM(num)',
},
},
queriesData: [{ data: [{ 'SUM(num)': 7 }] }],
});
const { data } = transformProps(chartProps) as {
data: { measures: number[] };
};
expect(data).toEqual({ measures: [7] });
});
});
Loading