Skip to content

Commit

Permalink
fix: custom SQL in the XAxis (#21847)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyongjie committed Oct 19, 2022
1 parent b773354 commit 0a4ecca
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
getColumnLabel,
getMetricLabel,
PostProcessingPivot,
getXAxis,
getXAxisLabel,
} from '@superset-ui/core';
import { PostProcessingFactory } from './types';

Expand All @@ -30,7 +30,7 @@ export const pivotOperator: PostProcessingFactory<PostProcessingPivot> = (
queryObject,
) => {
const metricLabels = ensureIsArray(queryObject.metrics).map(getMetricLabel);
const xAxis = getXAxis(formData);
const xAxis = getXAxisLabel(formData);

if (xAxis && metricLabels.length) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
* specific language governing permissions and limitationsxw
* under the License.
*/
import { PostProcessingProphet, getXAxis } from '@superset-ui/core';
import { PostProcessingProphet, getXAxisLabel } from '@superset-ui/core';
import { PostProcessingFactory } from './types';

/* eslint-disable @typescript-eslint/no-unused-vars */
export const prophetOperator: PostProcessingFactory<PostProcessingProphet> = (
formData,
queryObject,
) => {
const xAxis = getXAxis(formData);
const xAxis = getXAxisLabel(formData);
if (formData.forecastEnabled && xAxis) {
return {
operation: 'prophet',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
ensureIsArray,
getMetricLabel,
ComparisionType,
getXAxis,
getXAxisLabel,
} from '@superset-ui/core';
import { PostProcessingFactory } from './types';
import { getMetricOffsetsMap, isTimeComparison } from './utils';
Expand All @@ -34,7 +34,7 @@ export const renameOperator: PostProcessingFactory<PostProcessingRename> = (
const metrics = ensureIsArray(queryObject.metrics);
const columns = ensureIsArray(queryObject.columns);
const { truncate_metric } = formData;
const xAxis = getXAxis(formData);
const xAxis = getXAxisLabel(formData);
// remove or rename top level of column name(metric name) in the MultiIndex when
// 1) only 1 metric
// 2) exist dimentsion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import {
getColumnLabel,
NumpyFunction,
PostProcessingPivot,
getXAxis,
getXAxisLabel,
} from '@superset-ui/core';
import { getMetricOffsetsMap, isTimeComparison } from './utils';
import { PostProcessingFactory } from './types';

export const timeComparePivotOperator: PostProcessingFactory<PostProcessingPivot> =
(formData, queryObject) => {
const metricOffsetMap = getMetricOffsetsMap(formData, queryObject);
const xAxis = getXAxis(formData);
const xAxis = getXAxisLabel(formData);

if (isTimeComparison(formData, queryObject) && xAxis) {
const aggregates = Object.fromEntries(
Expand Down
16 changes: 14 additions & 2 deletions superset-frontend/packages/superset-ui-core/src/query/getXAxis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
getColumnLabel,
isQueryFormColumn,
QueryFormData,
QueryFormColumn,
Optional,
} from '@superset-ui/core';

export const isXAxisSet = (formData: QueryFormData) =>
Expand All @@ -32,14 +34,24 @@ export const hasGenericChartAxes = isFeatureEnabled(
FeatureFlag.GENERIC_CHART_AXES,
);

export const getXAxis = (formData: QueryFormData): string | undefined => {
export const getXAxisColumn = (
formData: QueryFormData,
): Optional<QueryFormColumn> => {
// The formData should be "raw form_data" -- the snake_case version of formData rather than camelCase.
if (!(formData.granularity_sqla || formData.x_axis)) {
return undefined;
}

if (isXAxisSet(formData)) {
return getColumnLabel(formData.x_axis);
return formData.x_axis;
}
return DTTM_ALIAS;
};

export const getXAxisLabel = (formData: QueryFormData): Optional<string> => {
const col = getXAxisColumn(formData);
if (col) {
return getColumnLabel(col);
}
return undefined;
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ export { default as getMetricLabel } from './getMetricLabel';
export { default as DatasourceKey } from './DatasourceKey';
export { default as normalizeOrderBy } from './normalizeOrderBy';
export { normalizeTimeColumn } from './normalizeTimeColumn';
export { getXAxis, isXAxisSet, hasGenericChartAxes } from './getXAxis';
export {
getXAxisLabel,
getXAxisColumn,
isXAxisSet,
hasGenericChartAxes,
} from './getXAxis';

export * from './types/AnnotationLayer';
export * from './types/QueryFormData';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ export * from '../query/types';

export type Maybe<T> = T | null;

export type Optional<T> = T | undefined;

export type ValueOf<T> = T[keyof T];
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import {
buildQueryContext,
ensureIsArray,
getXAxis,
getXAxisColumn,
isXAxisSet,
QueryFormData,
} from '@superset-ui/core';
Expand All @@ -35,7 +35,9 @@ export default function buildQuery(formData: QueryFormData) {
{
...baseQueryObject,
columns: [
...(isXAxisSet(formData) ? ensureIsArray(getXAxis(formData)) : []),
...(isXAxisSet(formData)
? ensureIsArray(getXAxisColumn(formData))
: []),
],
...(isXAxisSet(formData) ? {} : { is_timeseries: true }),
post_processing: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
smartDateVerboseFormatter,
NumberFormatter,
TimeFormatter,
getXAxis,
getXAxisLabel,
} from '@superset-ui/core';
import { EChartsCoreOption, graphic } from 'echarts';
import {
Expand Down Expand Up @@ -102,7 +102,7 @@ export default function transformProps(
const { r, g, b } = colorPicker;
const mainColor = `rgb(${r}, ${g}, ${b})`;

const timeColumn = getXAxis(rawFormData) as string;
const timeColumn = getXAxisLabel(rawFormData) as string;
let trendLineData;
let percentChange = 0;
let bigNumber = data.length === 0 ? null : data[0][metricName];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
PostProcessingPivot,
QueryFormData,
QueryObject,
getXAxis,
isXAxisSet,
getXAxisColumn,
} from '@superset-ui/core';
import {
pivotOperator,
Expand Down Expand Up @@ -54,7 +54,9 @@ export default function buildQuery(formData: QueryFormData) {
const queryObject = {
...baseQueryObject,
columns: [
...(isXAxisSet(formData) ? ensureIsArray(getXAxis(formData)) : []),
...(isXAxisSet(formData)
? ensureIsArray(getXAxisColumn(formData))
: []),
...ensureIsArray(fd.groupby),
],
series_columns: fd.groupby,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
QueryFormData,
TimeseriesChartDataResponseResult,
TimeseriesDataRecord,
getXAxis,
getXAxisLabel,
} from '@superset-ui/core';
import { EChartsCoreOption, SeriesOption } from 'echarts';
import {
Expand Down Expand Up @@ -152,7 +152,9 @@ export default function transformProps(

const colorScale = CategoricalColorNamespace.getScale(colorScheme as string);

const xAxisCol = getXAxis(chartProps.rawFormData as QueryFormData) as string;
const xAxisCol = getXAxisLabel(
chartProps.rawFormData as QueryFormData,
) as string;

const rebasedDataA = rebaseForecastDatum(data1, verboseMap);
const rawSeriesA = extractSeries(rebasedDataA, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
normalizeOrderBy,
PostProcessingPivot,
QueryFormData,
getXAxis,
getXAxisColumn,
isXAxisSet,
} from '@superset-ui/core';
import {
Expand Down Expand Up @@ -72,7 +72,9 @@ export default function buildQuery(formData: QueryFormData) {
{
...baseQueryObject,
columns: [
...(isXAxisSet(formData) ? ensureIsArray(getXAxis(formData)) : []),
...(isXAxisSet(formData)
? ensureIsArray(getXAxisColumn(formData))
: []),
...ensureIsArray(groupby),
],
series_columns: groupby,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import {
isTimeseriesAnnotationLayer,
TimeseriesChartDataResponseResult,
t,
getXAxis,
AxisType,
getXAxisLabel,
} from '@superset-ui/core';
import { isDerivedSeries } from '@superset-ui/chart-controls';
import { EChartsCoreOption, SeriesOption } from 'echarts';
Expand Down Expand Up @@ -148,7 +148,7 @@ export default function transformProps(

const colorScale = CategoricalColorNamespace.getScale(colorScheme as string);
const rebasedData = rebaseForecastDatum(data, verboseMap);
const xAxisCol = getXAxis(chartProps.rawFormData) as string;
const xAxisCol = getXAxisLabel(chartProps.rawFormData) as string;
const isHorizontal = orientation === OrientationType.horizontal;
const { totalStackedValues, thresholdValues } = extractDataTotalValues(
rebasedData,
Expand Down

0 comments on commit 0a4ecca

Please sign in to comment.