| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- /**
- * 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 {
- ComparisonType,
- FreeFormAdhocFilter,
- RollingType,
- TimeGranularity,
- } from '@superset-ui/core';
- import buildQuery from '../../src/MixedTimeseries/buildQuery';
- const formDataMixedChart = {
- datasource: 'dummy',
- viz_type: 'my_chart',
- // query
- // -- common
- time_range: '1980 : 2000',
- time_grain_sqla: TimeGranularity.WEEK,
- granularity_sqla: 'ds',
- // -- query a
- groupby: ['foo'],
- metrics: ['sum(sales)'],
- adhoc_filters: [
- {
- clause: 'WHERE',
- expressionType: 'SQL',
- sqlExpression: "foo in ('a', 'b')",
- } as FreeFormAdhocFilter,
- ],
- limit: 5,
- row_limit: 10,
- timeseries_limit_metric: 'count',
- order_desc: true,
- truncate_metric: true,
- show_empty_columns: true,
- // -- query b
- groupby_b: [],
- metrics_b: ['count'],
- adhoc_filters_b: [
- {
- clause: 'WHERE',
- expressionType: 'SQL',
- sqlExpression: "name in ('c', 'd')",
- } as FreeFormAdhocFilter,
- ],
- limit_b: undefined,
- row_limit_b: 100,
- timeseries_limit_metric_b: undefined,
- order_desc_b: false,
- truncate_metric_b: true,
- show_empty_columns_b: true,
- // chart configs
- show_value: false,
- show_valueB: undefined,
- };
- const formDataMixedChartWithAA = {
- ...formDataMixedChart,
- rolling_type: RollingType.Cumsum,
- time_compare: ['1 years ago'],
- comparison_type: ComparisonType.Values,
- resample_rule: '1AS',
- resample_method: 'zerofill',
- rolling_type_b: RollingType.Sum,
- rolling_periods_b: 1,
- min_periods_b: 1,
- comparison_type_b: ComparisonType.Difference,
- time_compare_b: ['3 years ago'],
- resample_rule_b: '1A',
- resample_method_b: 'asfreq',
- };
- test('should compile query object A', () => {
- const query = buildQuery(formDataMixedChart).queries[0];
- expect(query).toEqual({
- time_range: '1980 : 2000',
- since: undefined,
- until: undefined,
- granularity: 'ds',
- filters: [],
- extras: {
- having: '',
- time_grain_sqla: 'P1W',
- where: "(foo in ('a', 'b'))",
- },
- applied_time_extras: {},
- columns: ['foo'],
- metrics: ['sum(sales)'],
- annotation_layers: [],
- row_limit: 10,
- row_offset: undefined,
- series_columns: ['foo'],
- series_limit: 5,
- series_limit_metric: undefined,
- group_others_when_limit_reached: false,
- url_params: {},
- custom_params: {},
- custom_form_data: {},
- is_timeseries: true,
- time_offsets: [],
- post_processing: [
- {
- operation: 'pivot',
- options: {
- aggregates: {
- 'sum(sales)': {
- operator: 'mean',
- },
- },
- columns: ['foo'],
- drop_missing_columns: false,
- index: ['__timestamp'],
- },
- },
- {
- operation: 'rename',
- options: {
- columns: {
- 'sum(sales)': null,
- },
- inplace: true,
- level: 0,
- },
- },
- {
- operation: 'flatten',
- },
- ],
- orderby: [['count', false]],
- });
- });
- test('should compile query object B', () => {
- const query = buildQuery(formDataMixedChart).queries[1];
- expect(query).toEqual({
- time_range: '1980 : 2000',
- since: undefined,
- until: undefined,
- granularity: 'ds',
- filters: [],
- extras: {
- having: '',
- time_grain_sqla: 'P1W',
- where: "(name in ('c', 'd'))",
- },
- applied_time_extras: {},
- columns: [],
- metrics: ['count'],
- annotation_layers: [],
- row_limit: 100,
- row_offset: undefined,
- series_columns: [],
- series_limit: 0,
- series_limit_metric: undefined,
- group_others_when_limit_reached: false,
- url_params: {},
- custom_params: {},
- custom_form_data: {},
- is_timeseries: true,
- time_offsets: [],
- post_processing: [
- {
- operation: 'pivot',
- options: {
- aggregates: {
- count: {
- operator: 'mean',
- },
- },
- columns: [],
- drop_missing_columns: false,
- index: ['__timestamp'],
- },
- },
- {
- operation: 'flatten',
- },
- ],
- orderby: [['count', true]],
- });
- });
- test('should compile AA in query A', () => {
- const query = buildQuery(formDataMixedChartWithAA).queries[0];
- // time comparison
- expect(query.time_offsets).toEqual(['1 years ago']);
- // pivot
- expect(
- query.post_processing?.find(operator => operator?.operation === 'pivot'),
- ).toEqual({
- operation: 'pivot',
- options: {
- index: ['__timestamp'],
- columns: ['foo'],
- drop_missing_columns: false,
- aggregates: {
- 'sum(sales)': { operator: 'mean' },
- 'sum(sales)__1 years ago': { operator: 'mean' },
- },
- },
- });
- // cumsum
- expect(
- // prettier-ignore
- query
- .post_processing
- ?.find(operator => operator?.operation === 'cum')
- ?.operation,
- ).toEqual('cum');
- // resample
- expect(
- // prettier-ignore
- query
- .post_processing
- ?.find(operator => operator?.operation === 'resample'),
- ).toEqual({
- operation: 'resample',
- options: {
- method: 'asfreq',
- rule: '1AS',
- fill_value: 0,
- },
- });
- });
- test('should compile AA in query B', () => {
- const query = buildQuery(formDataMixedChartWithAA).queries[1];
- // time comparison
- expect(query.time_offsets).toEqual(['3 years ago']);
- // rolling total
- expect(
- // prettier-ignore
- query
- .post_processing
- ?.find(operator => operator?.operation === 'rolling'),
- ).toEqual({
- operation: 'rolling',
- options: {
- rolling_type: 'sum',
- window: 1,
- min_periods: 1,
- columns: {
- count: 'count',
- 'count__3 years ago': 'count__3 years ago',
- },
- },
- });
- // resample
- expect(
- // prettier-ignore
- query
- .post_processing
- ?.find(operator => operator?.operation === 'resample'),
- ).toEqual({
- operation: 'resample',
- options: {
- method: 'asfreq',
- rule: '1A',
- fill_value: null,
- },
- });
- });
- test("shouldn't convert a queryObject with axis", () => {
- const { queries } = buildQuery(formDataMixedChart);
- expect(queries[0]).toEqual(
- expect.objectContaining({
- granularity: 'ds',
- columns: ['foo'],
- series_columns: ['foo'],
- metrics: ['sum(sales)'],
- is_timeseries: true,
- extras: {
- time_grain_sqla: 'P1W',
- having: '',
- where: "(foo in ('a', 'b'))",
- },
- post_processing: [
- {
- operation: 'pivot',
- options: {
- aggregates: {
- 'sum(sales)': {
- operator: 'mean',
- },
- },
- columns: ['foo'],
- drop_missing_columns: false,
- index: ['__timestamp'],
- },
- },
- {
- operation: 'rename',
- options: { columns: { 'sum(sales)': null }, inplace: true, level: 0 },
- },
- {
- operation: 'flatten',
- },
- ],
- }),
- );
- expect(queries[1]).toEqual(
- expect.objectContaining({
- granularity: 'ds',
- columns: [],
- series_columns: [],
- metrics: ['count'],
- is_timeseries: true,
- extras: {
- time_grain_sqla: 'P1W',
- having: '',
- where: "(name in ('c', 'd'))",
- },
- post_processing: [
- {
- operation: 'pivot',
- options: {
- aggregates: {
- count: {
- operator: 'mean',
- },
- },
- columns: [],
- drop_missing_columns: false,
- index: ['__timestamp'],
- },
- },
- {
- operation: 'flatten',
- },
- ],
- }),
- );
- });
- test('ensure correct pivot columns', () => {
- const query = buildQuery({ ...formDataMixedChartWithAA, x_axis: 'ds' })
- .queries[0];
- expect(query.time_offsets).toEqual(['1 years ago']);
- // pivot
- expect(
- query.post_processing?.find(operator => operator?.operation === 'pivot'),
- ).toEqual({
- operation: 'pivot',
- options: {
- index: ['ds'],
- columns: ['foo'],
- drop_missing_columns: false,
- aggregates: {
- 'sum(sales)': { operator: 'mean' },
- 'sum(sales)__1 years ago': { operator: 'mean' },
- },
- },
- });
- });
|