craig-rueda commented on a change in pull request #11499: URL: https://github.com/apache/incubator-superset/pull/11499#discussion_r540400826
########## File path: superset-frontend/src/middleware/asyncEvent.ts ########## @@ -0,0 +1,194 @@ +/** + * 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 { Middleware, MiddlewareAPI, Dispatch } from 'redux'; +import { makeApi, SupersetClient } from '@superset-ui/core'; +import { SupersetError } from 'src/components/ErrorMessage/types'; +import { getFeatureFlag, isFeatureEnabled, FeatureFlag } from '../featureFlags'; +import { + getClientErrorObject, + parseErrorJson, +} from '../utils/getClientErrorObject'; + +export type AsyncEvent = { + id: string; + channel_id: string; + job_id: string; + user_id: string; + status: string; + errors: SupersetError[]; + result_url: string; +}; + +type AsyncEventOptions = { + getPendingComponents: (state: any) => any[]; + successAction: (componentId: number, componentData: any) => { type: string }; + errorAction: (componentId: number, response: any) => { type: string }; + processEventsCallback?: (events: AsyncEvent[]) => void; // this is currently used only for tests +}; + +type CachedDataResponse = { + componentId: number; + status: string; + data: any; +}; + +const initAsyncEvents = (options: AsyncEventOptions) => { + // TODO: implement websocket support + const TRANSPORT_POLLING = 'polling'; + const config = getFeatureFlag(FeatureFlag.GLOBAL_ASYNC_QUERIES_OPTIONS) || {}; + const transport = config.transport || TRANSPORT_POLLING; + const polling_delay = config.polling_delay || 500; + const { + getPendingComponents, + successAction, + errorAction, + processEventsCallback, + } = options; + + const middleware: Middleware = <S>(store: MiddlewareAPI<S>) => ( + next: Dispatch<S>, + ) => { + const JOB_STATUS = { + PENDING: 'pending', + RUNNING: 'running', + ERROR: 'error', + DONE: 'done', + }; + const LOCALSTORAGE_KEY = 'last_async_event_id'; + const POLLING_URL = '/api/v1/async_event/'; + let lastReceivedEventId: string | null; + + try { + lastReceivedEventId = localStorage.getItem(LOCALSTORAGE_KEY); + } catch (err) { + console.warn('failed to fetch last event Id from localStorage'); + } + + const fetchEvents = makeApi< + { last_id?: string | null }, + { result: AsyncEvent[] } + >({ + method: 'GET', + endpoint: POLLING_URL, + }); + + const fetchCachedData = async ( + asyncEvent: AsyncEvent, + componentId: number, + ): Promise<CachedDataResponse> => { + let status = 'success'; + let data; + try { + const { json } = await SupersetClient.get({ + endpoint: asyncEvent.result_url, + }); + data = 'result' in json ? json.result[0] : json; + } catch (response) { + status = 'error'; + data = await getClientErrorObject(response); + } + + return { componentId, status, data }; + }; + + const setLastId = (asyncEvent: AsyncEvent) => { + lastReceivedEventId = asyncEvent.id; + try { + localStorage.setItem(LOCALSTORAGE_KEY, lastReceivedEventId as string); Review comment: Why use local storage here? Can't this just live in state? I foresee issues with users getting into odd states where a hard refresh of the browser won't clear things up. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
