sadpandajoe commented on code in PR #44276: URL: https://github.com/apache/superset/pull/44276#discussion_r4042732243
########## superset-frontend/src/core/explore/index.ts: ########## @@ -0,0 +1,109 @@ +/** + * 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 { explore as exploreApi } from '@apache-superset/core'; +import type { ChartDataResponseResult, QueryFormData } from '@superset-ui/core'; +import { setControlValue } from 'src/explore/actions/exploreActions'; +import { getChartDataRequest } from 'src/components/Chart/chartAction'; +import { store, RootState } from 'src/views/store'; +import { navigation } from '../navigation'; + +const getExploreState = () => (store.getState() as RootState).explore; + +// The Redux slice below is retained across an in-SPA navigation, so +// checking it alone can't tell a still-active chart from a stale one left +// over from before the user navigated to another page. +const isExploreActive = (): boolean => navigation.getPage() === 'explore'; + +const getChartId: typeof exploreApi.getChartId = () => + isExploreActive() + ? (getExploreState().slice?.slice_id ?? undefined) + : undefined; + +const getControlValues: typeof exploreApi.getControlValues = () => + isExploreActive() + ? { ...(getExploreState().form_data as Record<string, unknown>) } + : {}; + +const getControlValue: typeof exploreApi.getControlValue = (name: string) => + isExploreActive() + ? (getExploreState().form_data as Record<string, unknown>)[name] + : undefined; + +const requireFormData = (): QueryFormData => { + const { form_data } = getExploreState(); + if (!isExploreActive() || !form_data?.datasource) { + throw new Error('No chart is currently loaded in Explore'); + } + return form_data; +}; + +const setControlValues: typeof exploreApi.setControlValues = async ( + values: Record<string, unknown>, +) => { + requireFormData(); + Object.entries(values).forEach(([controlName, value]) => { + store.dispatch( + setControlValue(controlName, value, undefined, { programmatic: true }), + ); + }); +}; + +const getQuery: typeof exploreApi.getQuery = async () => { + const formData = requireFormData(); + const response = await getChartDataRequest({ + formData, + resultFormat: 'json', + resultType: 'query', + }); + const result = response.json?.result?.[0] as + | ChartDataResponseResult + | undefined; + if (!result || result.error) { + throw new Error(result?.error ?? 'Failed to retrieve the query'); + } + return result.query; +}; + +const getChartData: typeof exploreApi.getChartData = async () => { + const formData = requireFormData(); + const response = await getChartDataRequest({ + formData, + resultFormat: 'json', + resultType: 'full', + }); + const result = response.json?.result?.[0] as Review Comment: With `GLOBAL_ASYNC_QUERIES` enabled, this `resultType: 'full'` request can return the 202 job envelope rather than a `result` array, so every uncached `getChartData()` call throws instead of waiting for its data. Could this use the existing async-response handler before reading the result? ########## superset-frontend/src/core/explore/index.ts: ########## @@ -0,0 +1,109 @@ +/** + * 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 { explore as exploreApi } from '@apache-superset/core'; +import type { ChartDataResponseResult, QueryFormData } from '@superset-ui/core'; +import { setControlValue } from 'src/explore/actions/exploreActions'; +import { getChartDataRequest } from 'src/components/Chart/chartAction'; +import { store, RootState } from 'src/views/store'; +import { navigation } from '../navigation'; + +const getExploreState = () => (store.getState() as RootState).explore; + +// The Redux slice below is retained across an in-SPA navigation, so +// checking it alone can't tell a still-active chart from a stale one left +// over from before the user navigated to another page. +const isExploreActive = (): boolean => navigation.getPage() === 'explore'; + +const getChartId: typeof exploreApi.getChartId = () => + isExploreActive() + ? (getExploreState().slice?.slice_id ?? undefined) + : undefined; + +const getControlValues: typeof exploreApi.getControlValues = () => + isExploreActive() + ? { ...(getExploreState().form_data as Record<string, unknown>) } + : {}; + +const getControlValue: typeof exploreApi.getControlValue = (name: string) => + isExploreActive() + ? (getExploreState().form_data as Record<string, unknown>)[name] + : undefined; + +const requireFormData = (): QueryFormData => { + const { form_data } = getExploreState(); Review Comment: Explore renders from the normalized control state, but this reads the pre-normalization `form_data`; controls filled by defaults or `mapStateToProps` can therefore make `getQuery()` and `getChartData()` describe different data than the chart on screen. Could these APIs build their form data from the current controls, as Explore's query path does? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
