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

fix(annotations): time grain column #26140

Merged
merged 3 commits into from
Nov 30, 2023
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
9 changes: 6 additions & 3 deletions superset-frontend/src/components/Chart/chartAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,12 @@ export function runAnnotationQuery({
return Promise.resolve();
}

const granularity = fd.time_grain_sqla || fd.granularity;
fd.time_grain_sqla = granularity;
fd.granularity = granularity;
// In the original formData the `granularity` attribute represents the time grain (eg
// `P1D`), but in the request payload it corresponds to the name of the column where
// the time grain should be applied (eg, `Date`), so we need to move things around.
fd.time_grain_sqla = fd.time_grain_sqla || fd.granularity;
fd.granularity = fd.granularity_sqla;

const overridesKeys = Object.keys(annotation.overrides);
if (overridesKeys.includes('since') || overridesKeys.includes('until')) {
annotation.overrides = {
Expand Down
67 changes: 67 additions & 0 deletions superset-frontend/src/components/Chart/chartActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import fetchMock from 'fetch-mock';
import sinon from 'sinon';

import * as chartlib from '@superset-ui/core';
import { SupersetClient } from '@superset-ui/core';
import { LOG_EVENT } from 'src/logger/actions';
import * as exploreUtils from 'src/explore/exploreUtils';
import * as actions from 'src/components/Chart/chartAction';
Expand Down Expand Up @@ -233,4 +234,70 @@ describe('chart actions', () => {
expect(json.result[0].value.toString()).toEqual(expectedBigNumber);
});
});

describe('runAnnotationQuery', () => {
const mockDispatch = jest.fn();
const mockGetState = () => ({
charts: {
chartKey: {
latestQueryFormData: {
time_grain_sqla: 'P1D',
granularity_sqla: 'Date',
},
},
},
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should dispatch annotationQueryStarted and annotationQuerySuccess on successful query', async () => {
const annotation = {
name: 'Holidays',
annotationType: 'EVENT',
sourceType: 'NATIVE',
color: null,
opacity: '',
style: 'solid',
width: 1,
showMarkers: false,
hideLine: false,
value: 1,
overrides: {
time_range: null,
},
show: true,
showLabel: false,
titleColumn: '',
descriptionColumns: [],
timeColumn: '',
intervalEndColumn: '',
};
const key = undefined;

const postSpy = jest.spyOn(SupersetClient, 'post');
postSpy.mockImplementation(() =>
Promise.resolve({ json: { result: [] } }),
);
const buildV1ChartDataPayloadSpy = jest.spyOn(
exploreUtils,
'buildV1ChartDataPayload',
);

const queryFunc = actions.runAnnotationQuery({ annotation, key });
await queryFunc(mockDispatch, mockGetState);

expect(buildV1ChartDataPayloadSpy).toHaveBeenCalledWith({
formData: {
granularity: 'Date',
granularity_sqla: 'Date',
time_grain_sqla: 'P1D',
},
force: false,
resultFormat: 'json',
resultType: 'full',
});
});
});
});