Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: derived metrics use different line style #20242

Merged
merged 4 commits into from
Jun 5, 2022
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
Expand Up @@ -19,4 +19,5 @@
*/
export { getMetricOffsetsMap } from './getMetricOffsetsMap';
export { isTimeComparison } from './isTimeComparison';
export { isDerivedSeries } from './isDerivedSeries';
export { TIME_COMPARISON_SEPARATOR } from './constants';
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable camelcase */
/**
* 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 limitationsxw
* under the License.
*/
import {
ensureIsArray,
JsonObject,
QueryFormData,
ComparisionType,
} from '@superset-ui/core';
import { isString } from 'lodash';

export const isDerivedSeries = (
series: JsonObject,
formData: QueryFormData,
): boolean => {
const comparisonType = formData.comparison_type;
if (comparisonType !== ComparisionType.Values) {
return false;
}

const timeCompare: string[] = ensureIsArray(formData?.time_compare);
return isString(series.name)
? !!timeCompare.find(timeOffset => series.name.endsWith(timeOffset))
: false;
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import { QueryObject, SqlaFormData } from '@superset-ui/core';
import { boxplotOperator } from '../../../src';
import { boxplotOperator } from '@superset-ui/chart-controls';

const formData: SqlaFormData = {
metrics: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import { QueryObject, SqlaFormData } from '@superset-ui/core';
import { contributionOperator } from '../../../src';
import { contributionOperator } from '@superset-ui/chart-controls';

const formData: SqlaFormData = {
metrics: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import { QueryObject, SqlaFormData } from '@superset-ui/core';
import { pivotOperator } from '../../../src';
import { pivotOperator } from '@superset-ui/chart-controls';

const formData: SqlaFormData = {
metrics: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import { DTTM_ALIAS, QueryObject, SqlaFormData } from '@superset-ui/core';
import { prophetOperator } from '../../../src';
import { prophetOperator } from '@superset-ui/chart-controls';

const formData: SqlaFormData = {
metrics: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import { QueryObject, SqlaFormData } from '@superset-ui/core';
import { rollingWindowOperator } from '../../../src';
import { rollingWindowOperator } from '@superset-ui/chart-controls';

const formData: SqlaFormData = {
metrics: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import { QueryObject, SqlaFormData } from '@superset-ui/core';
import { sortOperator } from '../../../src';
import { sortOperator } from '@superset-ui/chart-controls';

const formData: SqlaFormData = {
metrics: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import { QueryObject, SqlaFormData } from '@superset-ui/core';
import { timeCompareOperator } from '../../../src';
import { timeCompareOperator } from '@superset-ui/chart-controls';

const formData: SqlaFormData = {
metrics: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
* under the License.
*/
import { QueryObject, SqlaFormData } from '@superset-ui/core';
import { timeCompareOperator, timeComparePivotOperator } from '../../../src';
import {
timeCompareOperator,
timeComparePivotOperator,
} from '@superset-ui/chart-controls';

const formData: SqlaFormData = {
metrics: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* 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 { isDerivedSeries } from '@superset-ui/chart-controls';
import { SqlaFormData, ComparisionType } from '@superset-ui/core';

const formData: SqlaFormData = {
datasource: 'foo',
viz_type: 'table',
};
const series = {
id: 'metric__1 month ago',
name: 'metric__1 month ago',
data: [100],
};

test('should be false if comparison type is not actual values', () => {
expect(isDerivedSeries(series, formData)).toEqual(false);
Object.keys(ComparisionType)
.filter(type => type === ComparisionType.Values)
.forEach(type => {
const formDataWithComparisionType = {
...formData,
comparison_type: type,
time_compare: ['1 month ago'],
};
expect(isDerivedSeries(series, formDataWithComparisionType)).toEqual(
false,
);
});
});

test('should be true if comparison type is values', () => {
const formDataWithActualTypes = {
...formData,
comparison_type: ComparisionType.Values,
time_compare: ['1 month ago', '1 month later'],
};
expect(isDerivedSeries(series, formDataWithActualTypes)).toEqual(true);
});

test('should be false if series name does not match time_compare', () => {
const arbitrary_series = {
id: 'arbitrary column',
name: 'arbitrary column',
data: [100],
};
const formDataWithActualTypes = {
...formData,
comparison_type: ComparisionType.Values,
time_compare: ['1 month ago', '1 month later'],
};
expect(isDerivedSeries(arbitrary_series, formDataWithActualTypes)).toEqual(
false,
);
});

test('should be false if time compare is not suffix', () => {
const series = {
id: '1 month ago__metric',
name: '1 month ago__metric',
data: [100],
};
const formDataWithActualTypes = {
...formData,
comparison_type: ComparisionType.Values,
time_compare: ['1 month ago', '1 month later'],
};
expect(isDerivedSeries(series, formDataWithActualTypes)).toEqual(false);
});

test('should be false if series name invalid', () => {
const series = {
id: 123,
name: 123,
data: [100],
};
const formDataWithActualTypes = {
...formData,
comparison_type: ComparisionType.Values,
time_compare: ['1 month ago', '1 month later'],
};
expect(isDerivedSeries(series, formDataWithActualTypes)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import {
isTimeseriesAnnotationLayer,
TimeseriesChartDataResponseResult,
} from '@superset-ui/core';
import { isDerivedSeries } from '@superset-ui/chart-controls';
import { EChartsCoreOption, SeriesOption } from 'echarts';
import { ZRLineType } from 'echarts/types/src/util/types';
import {
DEFAULT_FORM_DATA,
EchartsTimeseriesChartProps,
Expand Down Expand Up @@ -181,6 +183,9 @@ export default function transformProps(
}

rawSeries.forEach(entry => {
const lineStyle = isDerivedSeries(entry, chartProps.rawFormData)
? { type: 'dashed' as ZRLineType }
: {};
const transformedSeries = transformSeries(entry, colorScale, {
area,
filterState,
Expand All @@ -199,6 +204,7 @@ export default function transformProps(
richTooltip,
sliceId,
isHorizontal,
lineStyle,
});
if (transformedSeries) series.push(transformedSeries);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function transformSeries(
seriesKey?: OptionName;
sliceId?: number;
isHorizontal?: boolean;
lineStyle?: LineStyleOption;
},
): SeriesOption | undefined {
const { name } = series;
Expand Down Expand Up @@ -183,8 +184,8 @@ export function transformSeries(
}
}
const lineStyle = isConfidenceBand
? { opacity: OpacityEnum.Transparent }
: { opacity };
? { ...opts.lineStyle, opacity: OpacityEnum.Transparent }
: { ...opts.lineStyle, opacity };
return {
...series,
yAxisIndex,
Expand Down