You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by mi...@apache.org on 2023/12/04 17:09:40 UTC
(superset) 13/16: fix(annotations): time grain column (#26140)
This is an automated email from the ASF dual-hosted git repository.
michaelsmolina pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/superset.git
commit a4c5340c7e15563e1ef7e441eb300298b0222453
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Thu Nov 30 11:13:12 2023 -0500
fix(annotations): time grain column (#26140)
(cherry picked from commit cff473f825825a419eb544d56960ce3a8a541592)
---
.../src/components/Chart/chartAction.js | 9 ++-
.../src/components/Chart/chartActions.test.js | 67 ++++++++++++++++++++++
2 files changed, 73 insertions(+), 3 deletions(-)
diff --git a/superset-frontend/src/components/Chart/chartAction.js b/superset-frontend/src/components/Chart/chartAction.js
index 6db969ebb9..5dd4a4e04d 100644
--- a/superset-frontend/src/components/Chart/chartAction.js
+++ b/superset-frontend/src/components/Chart/chartAction.js
@@ -271,9 +271,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 = {
diff --git a/superset-frontend/src/components/Chart/chartActions.test.js b/superset-frontend/src/components/Chart/chartActions.test.js
index b44ca7c8d7..b3a6fed9f5 100644
--- a/superset-frontend/src/components/Chart/chartActions.test.js
+++ b/superset-frontend/src/components/Chart/chartActions.test.js
@@ -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';
@@ -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',
+ });
+ });
+ });
});