From e09dcb80535f9dbaf214875905a3612ad74967dc Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Wed, 21 Dec 2022 08:45:35 -0300 Subject: [PATCH 1/3] chore: deprecate /superset/slice_json/ and /superset/annotation_json/ --- .../src/components/Chart/chartAction.js | 30 +++++++---- .../controls/AnnotationLayerControl/index.jsx | 2 +- .../exploreUtils/getAnnotationJsonUrl.test.ts | 51 ------------------- .../src/explore/exploreUtils/index.js | 10 ++-- superset/views/core.py | 2 + 5 files changed, 27 insertions(+), 68 deletions(-) delete mode 100644 superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts diff --git a/superset-frontend/src/components/Chart/chartAction.js b/superset-frontend/src/components/Chart/chartAction.js index f59c2da7199f2..4b605ae860964 100644 --- a/superset-frontend/src/components/Chart/chartAction.js +++ b/superset-frontend/src/components/Chart/chartAction.js @@ -291,25 +291,35 @@ export function runAnnotationQuery({ } const isNative = annotation.sourceType === ANNOTATION_SOURCE_TYPES.NATIVE; - const url = getAnnotationJsonUrl( - annotation.value, - sliceFormData, - isNative, - force, - ); + const url = getAnnotationJsonUrl(annotation.value, force); const controller = new AbortController(); const { signal } = controller; dispatch(annotationQueryStarted(annotation, controller, sliceKey)); - return SupersetClient.get({ + const annotationIndex = fd?.annotation_layers?.findIndex( + it => it.name === annotation.name, + ); + if (annotationIndex >= 0) { + fd.annotation_layers[annotationIndex].overrides = sliceFormData; + } + + SupersetClient.post({ url, signal, timeout: timeout * 1000, + headers: { 'Content-Type': 'application/json' }, + jsonPayload: buildV1ChartDataPayload({ + formData: fd, + force, + resultFormat: 'json', + resultType: 'full', + }), }) - .then(({ json }) => - dispatch(annotationQuerySuccess(annotation, json, sliceKey)), - ) + .then(({ json }) => { + const data = json?.result?.[0]?.annotation_data?.[annotation.name]; + return dispatch(annotationQuerySuccess(annotation, { data }, sliceKey)); + }) .catch(response => getClientErrorObject(response).then(err => { if (err.statusText === 'timeout') { diff --git a/superset-frontend/src/explore/components/controls/AnnotationLayerControl/index.jsx b/superset-frontend/src/explore/components/controls/AnnotationLayerControl/index.jsx index db3bbca272319..f70557170c0d8 100644 --- a/superset-frontend/src/explore/components/controls/AnnotationLayerControl/index.jsx +++ b/superset-frontend/src/explore/components/controls/AnnotationLayerControl/index.jsx @@ -71,7 +71,7 @@ class AnnotationLayerControl extends React.PureComponent { } componentDidMount() { - // preload the AnotationLayer component and dependent libraries i.e. mathjs + // preload the AnnotationLayer component and dependent libraries i.e. mathjs AnnotationLayer.preload(); } diff --git a/superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts b/superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts deleted file mode 100644 index 388c3b9a03093..0000000000000 --- a/superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts +++ /dev/null @@ -1,51 +0,0 @@ -/** - * 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 { getAnnotationJsonUrl } from '.'; - -let windowLocation: any; - -beforeAll(() => { - windowLocation = window.location; - // @ts-expect-error - delete window.location; -}); - -beforeEach(() => { - window.location = { - search: '?testA=0&testB=1', - } as any; -}); - -afterAll(() => { - window.location = windowLocation; -}); - -test('get correct annotation when isNative:true', () => { - const response = getAnnotationJsonUrl('slice_id', 'form_data', true); - expect(response).toBe( - '/superset/annotation_json/slice_id?form_data=%22form_data%22', - ); -}); - -test('get correct annotation when isNative:false', () => { - const response = getAnnotationJsonUrl('slice_id', { json: 'my-data' }, false); - expect(response).toBe( - '/superset/slice_json/slice_id?form_data=%7B%22json%22%3A%22my-data%22%7D', - ); -}); diff --git a/superset-frontend/src/explore/exploreUtils/index.js b/superset-frontend/src/explore/exploreUtils/index.js index 0e6a79ab91c9d..1d678427ff3cf 100644 --- a/superset-frontend/src/explore/exploreUtils/index.js +++ b/superset-frontend/src/explore/exploreUtils/index.js @@ -61,18 +61,16 @@ export function getHostName(allowDomainSharding = false) { return availableDomains[currentIndex]; } -export function getAnnotationJsonUrl(slice_id, form_data, isNative, force) { +export function getAnnotationJsonUrl(slice_id, force) { if (slice_id === null || slice_id === undefined) { return null; } + const uri = URI(window.location.search); - const endpoint = isNative ? 'annotation_json' : 'slice_json'; return uri - .pathname(`/superset/${endpoint}/${slice_id}`) + .pathname('/api/v1/chart/data') .search({ - form_data: safeStringify(form_data, (key, value) => - value === null ? undefined : value, - ), + form_data: safeStringify({ slice_id }), force, }) .toString(); diff --git a/superset/views/core.py b/superset/views/core.py index 534f8f667d707..3dc14b4afb4d3 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -505,6 +505,7 @@ def generate_json( @expose("/slice_json/") @etag_cache() @check_resource_permissions(check_slice_perms) + @deprecated() def slice_json(self, slice_id: int) -> FlaskResponse: form_data, slc = get_form_data(slice_id, use_slice_data=True) if not slc: @@ -528,6 +529,7 @@ def slice_json(self, slice_id: int) -> FlaskResponse: @has_access_api @event_logger.log_this @expose("/annotation_json/") + @deprecated() def annotation_json( # pylint: disable=no-self-use self, layer_id: int ) -> FlaskResponse: From 890fc6e157cc424bbc6b0c843b796cd27a8b05a1 Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Wed, 21 Dec 2022 08:48:30 -0300 Subject: [PATCH 2/3] cleanup --- superset-frontend/src/components/Chart/chartAction.js | 1 - 1 file changed, 1 deletion(-) diff --git a/superset-frontend/src/components/Chart/chartAction.js b/superset-frontend/src/components/Chart/chartAction.js index 4b605ae860964..99edf182ba69c 100644 --- a/superset-frontend/src/components/Chart/chartAction.js +++ b/superset-frontend/src/components/Chart/chartAction.js @@ -290,7 +290,6 @@ export function runAnnotationQuery({ : undefined; } - const isNative = annotation.sourceType === ANNOTATION_SOURCE_TYPES.NATIVE; const url = getAnnotationJsonUrl(annotation.value, force); const controller = new AbortController(); const { signal } = controller; From 90b356035aee69fb98736358aa156ab93fa83a7b Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Wed, 21 Dec 2022 09:14:45 -0300 Subject: [PATCH 3/3] cleanup --- superset-frontend/src/components/Chart/chartAction.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/superset-frontend/src/components/Chart/chartAction.js b/superset-frontend/src/components/Chart/chartAction.js index 99edf182ba69c..7418a8f8b72c9 100644 --- a/superset-frontend/src/components/Chart/chartAction.js +++ b/superset-frontend/src/components/Chart/chartAction.js @@ -30,10 +30,7 @@ import { shouldUseLegacyApi, getChartDataUri, } from 'src/explore/exploreUtils'; -import { - requiresQuery, - ANNOTATION_SOURCE_TYPES, -} from 'src/modules/AnnotationTypes'; +import { requiresQuery } from 'src/modules/AnnotationTypes'; import { addDangerToast } from 'src/components/MessageToasts/actions'; import { logEvent } from 'src/logger/actions'; @@ -303,7 +300,7 @@ export function runAnnotationQuery({ fd.annotation_layers[annotationIndex].overrides = sliceFormData; } - SupersetClient.post({ + return SupersetClient.post({ url, signal, timeout: timeout * 1000,