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

Dashboard Performance: Debounce loadWidgets #4724

Closed
Closed
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
4 changes: 3 additions & 1 deletion client/app/components/dashboards/DashboardGrid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class DashboardGrid extends React.Component {
isPublic: PropTypes.bool,
dashboard: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
widgets: PropTypes.arrayOf(WidgetType).isRequired,
loadingWidgets: PropTypes.instanceOf(Set).isRequired,
filters: FiltersType,
onBreakpointChange: PropTypes.func,
onLoadWidget: PropTypes.func,
Expand Down Expand Up @@ -231,6 +232,7 @@ class DashboardGrid extends React.Component {
onParameterMappingsChange,
filters,
dashboard,
loadingWidgets,
isPublic,
widgets,
} = this.props;
Expand Down Expand Up @@ -264,7 +266,7 @@ class DashboardGrid extends React.Component {
widget={widget}
filters={filters}
isPublic={isPublic}
isLoading={widget.loading}
isLoading={loadingWidgets.has(widget.id)}
canEdit={dashboard.canEdit()}
onLoadWidget={onLoadWidget}
onRefreshWidget={onRefreshWidget}
Expand Down
2 changes: 2 additions & 0 deletions client/app/pages/dashboards/DashboardPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function DashboardComponent(props) {
const dashboardOptions = useDashboard(props.dashboard);
const {
dashboard,
loadingWidgets,
filters,
setFilters,
loadDashboard,
Expand Down Expand Up @@ -94,6 +95,7 @@ function DashboardComponent(props) {
<DashboardGrid
dashboard={dashboard}
widgets={dashboard.widgets}
loadingWidgets={loadingWidgets}
filters={filters}
isEditing={editingLayout}
onLayoutChange={editingLayout ? saveDashboardLayout : () => {}}
Expand Down
13 changes: 10 additions & 3 deletions client/app/pages/dashboards/PublicDashboardPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ import useDashboard from "./hooks/useDashboard";
import "./PublicDashboardPage.less";

function PublicDashboard({ dashboard }) {
const { globalParameters, filters, setFilters, refreshDashboard, loadWidget, refreshWidget } = useDashboard(
dashboard
);
const {
globalParameters,
filters,
loadingWidgets,
setFilters,
refreshDashboard,
loadWidget,
refreshWidget,
} = useDashboard(dashboard);

return (
<div className="container p-t-10 p-b-20">
Expand All @@ -35,6 +41,7 @@ function PublicDashboard({ dashboard }) {
<DashboardGrid
dashboard={dashboard}
widgets={dashboard.widgets}
loadingWidgets={loadingWidgets}
filters={filters}
isEditing={false}
isPublic
Expand Down
30 changes: 25 additions & 5 deletions client/app/pages/dashboards/hooks/useDashboard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState, useEffect, useMemo, useCallback, useRef } from "react";
import { useDebouncedCallback } from "use-debounce";
import { isEmpty, includes, compact, map, has, pick, keys, extend, every, get } from "lodash";
import notification from "@/services/notification";
import location from "@/services/location";
Expand All @@ -16,6 +17,8 @@ import useEditModeHandler from "./useEditModeHandler";

export { DashboardStatusEnum } from "./useEditModeHandler";

const LOADING_WIDGETS_DEBOUNCE_TIME = 100;

function getAffectedWidgets(widgets, updatedParameters = []) {
return !isEmpty(updatedParameters)
? widgets.filter(widget =>
Expand All @@ -38,6 +41,14 @@ function useDashboard(dashboardData) {
const [gridDisabled, setGridDisabled] = useState(false);
const globalParameters = useMemo(() => dashboard.getParametersDefs(), [dashboard]);
const canEditDashboard = !dashboard.is_archived && dashboard.can_edit;

const [loadingWidgets, setLoadingWidgets] = useState(new Set());
const loadingWidgetValueRef = useRef(loadingWidgets);
const [updateLoadingWidgets] = useDebouncedCallback(
() => setLoadingWidgets(new Set(loadingWidgetValueRef.current)),
LOADING_WIDGETS_DEBOUNCE_TIME
);

const isDashboardOwnerOrAdmin = useMemo(
() =>
!dashboard.is_archived &&
Expand Down Expand Up @@ -92,11 +103,18 @@ function useDashboard(dashboardData) {
updateDashboard({ is_draft: !dashboard.is_draft }, false);
}, [dashboard, updateDashboard]);

const loadWidget = useCallback((widget, forceRefresh = false) => {
widget.getParametersDefs(); // Force widget to read parameters values from URL
setDashboard(currentDashboard => extend({}, currentDashboard));
return widget.load(forceRefresh).finally(() => setDashboard(currentDashboard => extend({}, currentDashboard)));
}, []);
const loadWidget = useCallback(
(widget, forceRefresh = false) => {
widget.getParametersDefs(); // Force widget to read parameters values from URL
loadingWidgetValueRef.current.add(widget.id);
updateLoadingWidgets();
return widget.load(forceRefresh).finally(() => {
loadingWidgetValueRef.current.delete(widget.id);
updateLoadingWidgets();
});
},
[updateLoadingWidgets]
);

const refreshWidget = useCallback(widget => loadWidget(widget, true), [loadWidget]);

Expand All @@ -121,6 +139,7 @@ function useDashboard(dashboardData) {
return Promise.all(loadWidgetPromises).then(() => {
const queryResults = compact(map(dashboardRef.current.widgets, widget => widget.getQueryResult()));
const updatedFilters = collectDashboardFilters(dashboardRef.current, queryResults, location.search);
setDashboard(currentDashboard => extend({}, currentDashboard));
setFilters(updatedFilters);
});
},
Expand Down Expand Up @@ -203,6 +222,7 @@ function useDashboard(dashboardData) {

return {
dashboard,
loadingWidgets,
globalParameters,
refreshing,
filters,
Expand Down