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

perf(dashboard): Send chart requests before native filter requests #19077

Merged
merged 1 commit into from
Mar 10, 2022
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
17 changes: 15 additions & 2 deletions superset-frontend/src/dashboard/actions/dashboardState.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,16 @@ const refreshCharts = (chartList, force, interval, dashboardId, dispatch) =>
resolve();
});

export const ON_FILTERS_REFRESH = 'ON_FILTERS_REFRESH';
export function onFiltersRefresh() {
return { type: ON_FILTERS_REFRESH };
}

export const ON_FILTERS_REFRESH_SUCCESS = 'ON_FILTERS_REFRESH_SUCCESS';
export function onFiltersRefreshSuccess() {
return { type: ON_FILTERS_REFRESH_SUCCESS };
}

export const ON_REFRESH_SUCCESS = 'ON_REFRESH_SUCCESS';
export function onRefreshSuccess() {
return { type: ON_REFRESH_SUCCESS };
Expand All @@ -436,8 +446,11 @@ export function onRefresh(
) {
return dispatch => {
dispatch({ type: ON_REFRESH });
refreshCharts(chartList, force, interval, dashboardId, dispatch).then(() =>
dispatch({ type: ON_REFRESH_SUCCESS }),
refreshCharts(chartList, force, interval, dashboardId, dispatch).then(
() => {
dispatch(onRefreshSuccess());
dispatch(onFiltersRefresh());
},
);
};
}
Expand Down
1 change: 1 addition & 0 deletions superset-frontend/src/dashboard/actions/hydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export const hydrateDashboard =
maxUndoHistoryExceeded: false,
lastModifiedTime: dashboardData.changed_on,
isRefreshing: false,
isFiltersRefreshing: false,
activeTabs: dashboardState?.activeTabs || [],
filterboxMigrationState,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { FeatureFlag, isFeatureEnabled } from 'src/featureFlags';
import { waitForAsyncData } from 'src/middleware/asyncEvent';
import { ClientErrorObject } from 'src/utils/getClientErrorObject';
import { RootState } from 'src/dashboard/types';
import { onFiltersRefreshSuccess } from 'src/dashboard/actions/dashboardState';
import { dispatchFocusAction } from './utils';
import { FilterProps } from './types';
import { getFormData } from '../../utils';
Expand All @@ -62,6 +63,18 @@ const StyledDiv = styled.div`
const queriesDataPlaceholder = [{ data: [{}] }];
const behaviors = [Behavior.NATIVE_FILTER];

const useShouldFilterRefresh = () => {
const isDashboardRefreshing = useSelector<RootState, boolean>(
state => state.dashboardState.isRefreshing,
);
const isFilterRefreshing = useSelector<RootState, boolean>(
state => state.dashboardState.isFiltersRefreshing,
);

// trigger filter requests only after charts requests were triggered
return !isDashboardRefreshing && isFilterRefreshing;
};

const FilterValue: React.FC<FilterProps> = ({
dataMaskSelected,
filter,
Expand All @@ -75,9 +88,7 @@ const FilterValue: React.FC<FilterProps> = ({
const { id, targets, filterType, adhoc_filters, time_range } = filter;
const metadata = getChartMetadataRegistry().get(filterType);
const dependencies = useFilterDependencies(id, dataMaskSelected);
const isDashboardRefreshing = useSelector<RootState, boolean>(
state => state.dashboardState.isRefreshing,
);
const shouldRefresh = useShouldFilterRefresh();
const [state, setState] = useState<ChartDataResponseResult[]>([]);
const [error, setError] = useState<string>('');
const [formData, setFormData] = useState<Partial<QueryFormData>>({
Expand All @@ -97,6 +108,14 @@ const FilterValue: React.FC<FilterProps> = ({
const [isRefreshing, setIsRefreshing] = useState(false);
const dispatch = useDispatch();

const handleFilterLoadFinish = useCallback(() => {
setIsRefreshing(false);
setIsLoading(false);
if (shouldRefresh) {
dispatch(onFiltersRefreshSuccess());
}
}, [dispatch, shouldRefresh]);

useEffect(() => {
if (!inViewFirstTime && inView) {
setInViewFirstTime(true);
Expand Down Expand Up @@ -127,7 +146,7 @@ const FilterValue: React.FC<FilterProps> = ({
!isRefreshing &&
(!isEqualWith(formData, newFormData, customizer) ||
!isEqual(ownState, filterOwnState) ||
isDashboardRefreshing)
shouldRefresh)
) {
setFormData(newFormData);
setOwnState(filterOwnState);
Expand All @@ -147,22 +166,19 @@ const FilterValue: React.FC<FilterProps> = ({
const result = 'result' in json ? json.result[0] : json;

if (response.status === 200) {
setIsRefreshing(false);
setIsLoading(false);
setState([result]);
handleFilterLoadFinish();
} else if (response.status === 202) {
waitForAsyncData(result)
.then((asyncResult: ChartDataResponseResult[]) => {
setIsRefreshing(false);
setIsLoading(false);
setState(asyncResult);
handleFilterLoadFinish();
})
.catch((error: ClientErrorObject) => {
setError(
error.message || error.error || t('Check configuration'),
);
setIsRefreshing(false);
setIsLoading(false);
handleFilterLoadFinish();
});
} else {
throw new Error(
Expand All @@ -172,25 +188,24 @@ const FilterValue: React.FC<FilterProps> = ({
} else {
setState(json.result);
setError('');
setIsRefreshing(false);
setIsLoading(false);
handleFilterLoadFinish();
}
})
.catch((error: Response) => {
setError(error.statusText);
setIsRefreshing(false);
setIsLoading(false);
handleFilterLoadFinish();
});
}
}, [
inViewFirstTime,
dependencies,
datasetId,
groupby,
handleFilterLoadFinish,
JSON.stringify(filter),
hasDataSource,
isRefreshing,
isDashboardRefreshing,
shouldRefresh,
]);

useEffect(() => {
Expand Down
14 changes: 14 additions & 0 deletions superset-frontend/src/dashboard/reducers/dashboardState.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
UNSET_FOCUSED_FILTER_FIELD,
SET_ACTIVE_TABS,
SET_FULL_SIZE_CHART_ID,
ON_FILTERS_REFRESH,
ON_FILTERS_REFRESH_SUCCESS,
} from '../actions/dashboardState';
import { HYDRATE_DASHBOARD } from '../actions/hydrate';

Expand Down Expand Up @@ -136,6 +138,18 @@ export default function dashboardStateReducer(state = {}, action) {
isRefreshing: true,
};
},
[ON_FILTERS_REFRESH]() {
return {
...state,
isFiltersRefreshing: true,
};
},
[ON_FILTERS_REFRESH_SUCCESS]() {
return {
...state,
isFiltersRefreshing: false,
};
},
[ON_REFRESH_SUCCESS]() {
return {
...state,
Expand Down
1 change: 1 addition & 0 deletions superset-frontend/src/dashboard/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type DashboardState = {
activeTabs: ActiveTabs;
fullSizeChartId: number | null;
isRefreshing: boolean;
isFiltersRefreshing: boolean;
hasUnsavedChanges: boolean;
};
export type DashboardInfo = {
Expand Down