diff --git a/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/index.ts b/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/index.ts new file mode 100644 index 0000000000..0d74952a88 --- /dev/null +++ b/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/index.ts @@ -0,0 +1,22 @@ +/* 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. + */ +export * from './resample'; +export * from './rollingWindow'; +export * from './timeCompare'; diff --git a/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/resample.ts b/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/resample.ts new file mode 100644 index 0000000000..46d5c3e5f8 --- /dev/null +++ b/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/resample.ts @@ -0,0 +1,26 @@ +/* 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 { getMetricLabel, ensureIsArray, QueryFormData, QueryObject } from '@superset-ui/core'; + +export function resampleTransform(formData: QueryFormData, queryObject: QueryObject): QueryObject { + return queryObject; +} + +export default {}; diff --git a/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/rollingWindow.ts b/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/rollingWindow.ts new file mode 100644 index 0000000000..40b1a82be2 --- /dev/null +++ b/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/rollingWindow.ts @@ -0,0 +1,69 @@ +/* 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, QueryFormData, QueryObject } from '@superset-ui/core'; + +function ensureIsInt(value: T, defaultValue: number): number { + return Number.isNaN(parseInt(String(value), 10)) ? defaultValue : parseInt(String(value), 10); +} + +export function rollingWindowTransform( + formData: QueryFormData, + queryObject: QueryObject, +): QueryObject { + if (formData.rolling_type === 'None' || !formData.rolling_type) { + return queryObject; + } + + const post_processing = ensureIsArray(queryObject.post_processing); + const columns = Object.fromEntries( + formData.metrics?.map(metric => { + if (typeof metric === 'string') { + return [metric, metric]; + } + return [metric.label, metric.label]; + }) || [], + ); + if (formData.rolling_type === 'cumsum') { + // rolling must be the first operation(before pivot or other transforms) + post_processing.unshift({ + operation: 'cum', + options: { + operator: 'sum', + columns, + }, + }); + } + + if (['sum', 'mean', 'std'].includes(formData.rolling_type)) { + // same before + post_processing.unshift({ + operation: 'rolling', + options: { + rolling_type: formData.rolling_type, + window: ensureIsInt(formData.rolling_periods, 1), + min_periods: ensureIsInt(formData.min_periods, 0), + columns, + }, + }); + } + return { ...queryObject, post_processing }; +} + +export default {}; diff --git a/packages/superset-ui-chart-controls/src/utils/AdvancedAnalyticsTransform.ts b/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/timeCompare.ts similarity index 64% rename from packages/superset-ui-chart-controls/src/utils/AdvancedAnalyticsTransform.ts rename to packages/superset-ui-chart-controls/src/utils/advancedAnalytics/timeCompare.ts index 0b685055af..9d082fb93c 100644 --- a/packages/superset-ui-chart-controls/src/utils/AdvancedAnalyticsTransform.ts +++ b/packages/superset-ui-chart-controls/src/utils/advancedAnalytics/timeCompare.ts @@ -21,53 +21,6 @@ import { getMetricLabel, ensureIsArray, QueryFormData, QueryObject } from '@supe const TIME_COMPARISION = '__'; -function ensureIsInt(value: T, defaultValue: number): number { - return Number.isNaN(parseInt(String(value), 10)) ? defaultValue : parseInt(String(value), 10); -} - -export function rollingWindowTransform( - formData: QueryFormData, - queryObject: QueryObject, -): QueryObject { - if (formData.rolling_type === 'None' || !formData.rolling_type) { - return queryObject; - } - - const post_processing = ensureIsArray(queryObject.post_processing); - const columns = Object.fromEntries( - formData.metrics?.map(metric => { - if (typeof metric === 'string') { - return [metric, metric]; - } - return [metric.label, metric.label]; - }) || [], - ); - if (formData.rolling_type === 'cumsum') { - // rolling must be the first operation(before pivot or other transforms) - post_processing.unshift({ - operation: 'cum', - options: { - operator: 'sum', - columns, - }, - }); - } - - if (['sum', 'mean', 'std'].includes(formData.rolling_type)) { - // same before - post_processing.unshift({ - operation: 'rolling', - options: { - rolling_type: formData.rolling_type, - window: ensureIsInt(formData.rolling_periods, 1), - min_periods: ensureIsInt(formData.min_periods, 0), - columns, - }, - }); - } - return { ...queryObject, post_processing }; -} - export function timeCompareTransform( formData: QueryFormData, queryObject: QueryObject, @@ -112,7 +65,7 @@ export function timeCompareTransform( }; } if (comparisonType === 'values') { - return { ...queryObject, post_processing, time_offset: timeCompare }; + return { ...queryObject, post_processing, time_offsets: timeCompare }; } const timeCompareProcessing = [ @@ -127,11 +80,7 @@ export function timeCompareTransform( }, ].concat(post_processing); - return { ...queryObject, post_processing: timeCompareProcessing, time_offset: timeCompare }; -} - -export function resampleTransform(formData: QueryFormData, queryObject: QueryObject): QueryObject { - return queryObject; + return { ...queryObject, post_processing: timeCompareProcessing, time_offsets: timeCompare }; } export default {}; diff --git a/packages/superset-ui-chart-controls/src/utils/index.ts b/packages/superset-ui-chart-controls/src/utils/index.ts index afccbda416..0451434152 100644 --- a/packages/superset-ui-chart-controls/src/utils/index.ts +++ b/packages/superset-ui-chart-controls/src/utils/index.ts @@ -19,6 +19,6 @@ export * from './selectOptions'; export * from './D3Formatting'; export * from './expandControlConfig'; -export * from './AdvancedAnalyticsTransform'; +export * from './advancedAnalytics'; export { default as mainMetric } from './mainMetric'; export { default as columnChoices } from './columnChoices';