Skip to content

Commit

Permalink
[Visualize] Client Side caching (#105589) (#106072)
Browse files Browse the repository at this point in the history
* [Visualize] Client Side caching

Closes: #102622

* apply caching for returning to originatingApp

* fix case with 'save as' functiionality

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
alexwizp and kibanamachine authored Jul 19, 2021
1 parent c89f9d3 commit e6d944a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,27 @@ export const VisualizeByValueEditor = ({ onAppLeave }: VisualizeAppProps) => {
const [valueInput, setValueInput] = useState<VisualizeInput>();

useEffect(() => {
const { originatingApp: value, embeddableId: embeddableIdValue, valueInput: valueInputValue } =
services.stateTransferService.getIncomingEditorState(VisualizeConstants.APP_ID) || {};
const { stateTransferService, history, data } = services;
const {
originatingApp: value,
embeddableId: embeddableIdValue,
valueInput: valueInputValue,
searchSessionId,
} = stateTransferService.getIncomingEditorState(VisualizeConstants.APP_ID) || {};

setOriginatingApp(value);
setValueInput(valueInputValue);
setEmbeddableId(embeddableIdValue);

if (!valueInputValue) {
// if there is no value input to load, redirect to the visualize listing page.
services.history.replace(VisualizeConstants.LANDING_PAGE_PATH);
history.replace(VisualizeConstants.LANDING_PAGE_PATH);
}

if (searchSessionId) {
data.search.session.continue(searchSessionId);
} else {
data.search.session.start();
}
}, [services]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { VisualizeConstants } from '../..';
export const VisualizeEditor = ({ onAppLeave }: VisualizeAppProps) => {
const { id: visualizationIdFromUrl } = useParams<{ id: string }>();
const [originatingApp, setOriginatingApp] = useState<string>();
const [embeddableIdValue, setEmbeddableId] = useState<string>();
const { services } = useKibana<VisualizeServices>();
const [eventEmitter] = useState(new EventEmitter());
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(!visualizationIdFromUrl);
Expand Down Expand Up @@ -55,8 +56,17 @@ export const VisualizeEditor = ({ onAppLeave }: VisualizeAppProps) => {
useLinkedSearchUpdates(services, eventEmitter, appState, savedVisInstance);

useEffect(() => {
const { originatingApp: value } =
services.stateTransferService.getIncomingEditorState(VisualizeConstants.APP_ID) || {};
const { stateTransferService, data } = services;
const { originatingApp: value, searchSessionId, embeddableId } =
stateTransferService.getIncomingEditorState(VisualizeConstants.APP_ID) || {};

if (searchSessionId) {
data.search.session.continue(searchSessionId);
} else {
data.search.session.start();
}

setEmbeddableId(embeddableId);
setOriginatingApp(value);
}, [services]);

Expand All @@ -65,7 +75,7 @@ export const VisualizeEditor = ({ onAppLeave }: VisualizeAppProps) => {
return () => {
eventEmitter.removeAllListeners();
};
}, [eventEmitter]);
}, [eventEmitter, services]);

return (
<VisualizeEditorCommon
Expand All @@ -82,6 +92,7 @@ export const VisualizeEditor = ({ onAppLeave }: VisualizeAppProps) => {
setHasUnsavedChanges={setHasUnsavedChanges}
visEditorRef={visEditorRef}
onAppLeave={onAppLeave}
embeddableId={embeddableIdValue}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,21 @@ const TopNav = ({
const session = embeddableHandler.openInspector();
setInspectorSession(session);
}, [embeddableHandler]);

const doReload = useCallback(async () => {
// start a new session to make sure all data is up to date
services.data.search.session.start();

await visInstance.embeddableHandler.reload();
}, [visInstance.embeddableHandler, services.data.search.session]);

const handleRefresh = useCallback(
(_payload: any, isUpdate?: boolean) => {
if (isUpdate === false) {
visInstance.embeddableHandler.reload();
doReload();
}
},
[visInstance.embeddableHandler]
[doReload]
);

const config = useMemo(() => {
Expand Down Expand Up @@ -185,15 +193,15 @@ const TopNav = ({
.getAutoRefreshFetch$()
.subscribe(async (done) => {
try {
await visInstance.embeddableHandler.reload();
await doReload();
} finally {
done();
}
});
return () => {
autoRefreshFetchSub.unsubscribe();
};
}, [services.data.query.timefilter.timefilter, visInstance.embeddableHandler]);
}, [services.data.query.timefilter.timefilter, doReload]);

return isChromeVisible ? (
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const getTopNavConfig = (
embeddableId,
}: TopNavConfigParams,
{
data,
application,
chrome,
history,
Expand Down Expand Up @@ -154,17 +155,17 @@ export const getTopNavConfig = (
saveOptions.dashboardId === 'new' ? '#/create' : `#/view/${saveOptions.dashboardId}`;
}

if (newlyCreated && stateTransfer) {
if (stateTransfer) {
stateTransfer.navigateToWithEmbeddablePackage(app, {
state: {
type: VISUALIZE_EMBEDDABLE_TYPE,
input: { savedObjectId: id },
embeddableId,
embeddableId: savedVis.copyOnSave ? undefined : embeddableId,
searchSessionId: data.search.session.getSessionId(),
},
path,
});
} else {
// TODO: need the same thing here?
application.navigateToApp(app, { path });
}
} else {
Expand Down Expand Up @@ -214,6 +215,7 @@ export const getTopNavConfig = (
} as VisualizeInput,
embeddableId,
type: VISUALIZE_EMBEDDABLE_TYPE,
searchSessionId: data.search.session.getSessionId(),
};
stateTransfer.navigateToWithEmbeddablePackage(originatingApp, { state });
};
Expand Down Expand Up @@ -394,6 +396,7 @@ export const getTopNavConfig = (
} as VisualizeInput,
embeddableId,
type: VISUALIZE_EMBEDDABLE_TYPE,
searchSessionId: data.search.session.getSessionId(),
};

const path = dashboardId === 'new' ? '#/create' : `#/view/${dashboardId}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ const createVisualizeEmbeddableAndLinkSavedSearch = async (
savedObjectsPublic,
} = visualizeServices;
const embeddableHandler = (await createVisEmbeddableFromObject(vis, {
id: '',
timeRange: data.query.timefilter.timefilter.getTime(),
filters: data.query.filterManager.getFilters(),
id: '',
searchSessionId: data.search.session.getSessionId(),
})) as VisualizeEmbeddableContract;

embeddableHandler.getOutput$().subscribe((output) => {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/visualize/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export class VisualizePlugin
const { renderApp } = await import('./application');
const unmount = renderApp(params, services);
return () => {
data.search.session.clear();
params.element.classList.remove('visAppWrapper');
unlistenParentHistory();
unmount();
Expand Down

0 comments on commit e6d944a

Please sign in to comment.