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

DO NOT MERGE #852

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions superset-frontend/packages/superset-ui-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export * from './math-expression';
export * from './ui-overrides';
export * from './hooks';
export * from './currency-format';
export * from './time-comparison';
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export type QueryObjectExtras = Partial<{
time_grain_sqla?: TimeGranularity;
/** WHERE condition */
where?: string;
/** Instant Time Comparison */
instant_time_comparison_range?: string;
}>;

export type ResidualQueryObjectData = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!--
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.
-->

## @superset-ui/core/time-comparison

This is a collection of methods used to support Time Comparison in charts.

#### Example usage

```js
import { getComparisonTimeRangeInfo } from '@superset-ui/core';
const { since, until } = getComparisonTimeRangeInfo(
adhocFilters,
extraFormData,
);
console.log(adhocFilters, extraFormData);
```

or

```js
import { ComparisonTimeRangeType } from '@superset-ui/core';
ComparisonTimeRangeType.Custom; // 'c'
ComparisonTimeRangeType.InheritRange; // 'r'
```

#### API

`fn(args)`

- Do something
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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 { QueryFormData } from '../query';
import { AdhocFilter } from '../types';

/**
* This method is used to get the query filters to be applied to the comparison query after
* overriding the time range in case an extra form data is provided.
* For example when rendering a chart that uses time comparison in a dashboard with time filters.
* @param formData - the form data
* @param extraFormData - the extra form data
* @returns the query filters to be applied to the comparison query
*/
export const getComparisonFilters = (
formData: QueryFormData,
extraFormData: any,
): AdhocFilter[] => {
const timeFilterIndex: number =
formData.adhoc_filters?.findIndex(
filter => 'operator' in filter && filter.operator === 'TEMPORAL_RANGE',
) ?? -1;

const timeFilter: AdhocFilter | null =
timeFilterIndex !== -1 && formData.adhoc_filters
? formData.adhoc_filters[timeFilterIndex]
: null;

if (
timeFilter &&
'comparator' in timeFilter &&
typeof timeFilter.comparator === 'string'
) {
if (extraFormData?.time_range) {
timeFilter.comparator = extraFormData.time_range;
}
}

const comparisonQueryFilter = timeFilter ? [timeFilter] : [];

const otherFilters = formData.adhoc_filters?.filter(
(_value: any, index: number) => timeFilterIndex !== index,
);
const comparisonQueryFilters = otherFilters
? [...comparisonQueryFilter, ...otherFilters]
: comparisonQueryFilter;

return comparisonQueryFilters;
};

export default getComparisonFilters;
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* 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 { QueryFormData } from '../query';
import { getComparisonFilters } from './getComparisonFilters';
import { ComparisonTimeRangeType } from './types';

/**
* This is the main function to get the comparison info. It will return the formData
* that a viz can use to query the comparison data and the time shift text needed for
* the comparison time range based on the control value.
* @param formData
* @param timeComparison
* @param extraFormData
* @returns the processed formData
*/

export const getComparisonInfo = (
formData: QueryFormData,
timeComparison: string,
extraFormData: any,
): QueryFormData => {
let comparisonFormData;

if (timeComparison !== ComparisonTimeRangeType.Custom) {
comparisonFormData = {
...formData,
adhoc_filters: getComparisonFilters(formData, extraFormData),
extra_form_data: {
...extraFormData,
time_range: undefined,
},
};
} else {
// This is when user selects Custom as time comparison
comparisonFormData = {
...formData,
adhoc_filters: formData.adhoc_custom,
extra_form_data: {
...extraFormData,
time_range: undefined,
},
};
}

return comparisonFormData;
};

export default getComparisonInfo;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* 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
Expand All @@ -16,12 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
// eslint-disable-next-line import/prefer-default-export
export { default as PopKPIPlugin } from './plugin';
/**
* Note: this file exports the default export from PopKPI.tsx.
* If you want to export multiple visualization modules, you will need to
* either add additional plugin folders (similar in structure to ./plugin)
* OR export multiple instances of `ChartPlugin` extensions in ./plugin/index.ts
* which in turn load exports from CustomViz.tsx
*/

export * from './types';

export { default as getComparisonInfo } from './getComparisonInfo';
export { default as getComparisonFilters } from './getComparisonFilters';
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
* under the License.
*/

declare module '*.png' {
const value: any;
export default value;
/**
* Supported comparison time ranges
*/

export enum ComparisonTimeRangeType {
Custom = 'c',
InheritedRange = 'r',
Month = 'm',
Week = 'w',
Year = 'y',
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export { default as validateNumber } from './validateNumber';
export { default as validateNonEmpty } from './validateNonEmpty';
export { default as validateMaxValue } from './validateMaxValue';
export { default as validateMapboxStylesUrl } from './validateMapboxStylesUrl';
export { default as validateTimeComparisonRangeValues } from './validateTimeComparisonRangeValues';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 { ComparisonTimeRangeType } from '../time-comparison';
import { t } from '../translation';
import { ensureIsArray } from '../utils';

export const validateTimeComparisonRangeValues = (
timeRangeValue?: any,
controlValue?: any,
) => {
const isCustomTimeRange = timeRangeValue === ComparisonTimeRangeType.Custom;
const isCustomControlEmpty = controlValue?.every(
(val: any) => ensureIsArray(val).length === 0,
);
return isCustomTimeRange && isCustomControlEmpty
? [t('Filters for comparison must have a value')]
: [];
};

export default validateTimeComparisonRangeValues;
Loading
Loading