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

[Lens] Move dataViews cache into main Lens state #137309

Merged
merged 32 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
57d814d
Move lens dataViews state into main state
dej611 Jul 27, 2022
751c2a2
:fire: Remove some old cruft from the code
dej611 Jul 27, 2022
c7a9ce8
:bug: Fix dataViews layer change
dej611 Jul 27, 2022
79566af
:bug: Fix datasourceLayers refs
dej611 Jul 27, 2022
4822b36
:fire: Remove more old cruft
dej611 Jul 27, 2022
220aa39
:bug: Fix bug when loading SO
dej611 Jul 28, 2022
759f19a
:bug: Fix initial existence flag
dej611 Jul 28, 2022
7f590da
:label: Fix type issues
dej611 Jul 28, 2022
12b91fc
:label: Fix types and tests
dej611 Jul 28, 2022
2ee6f6d
:label: Fix types issues
dej611 Jul 29, 2022
ce7bddd
:white_check_mark: Fix more tests
dej611 Jul 29, 2022
80647be
:white_check_mark: Fix with new dataViews structure
dej611 Aug 3, 2022
63bf020
:white_check_mark: Fix more test mocks
dej611 Aug 3, 2022
3a7f9fb
:white_check_mark: More tests fixed
dej611 Aug 3, 2022
2fc1f0f
:fire: Removed unused prop
dej611 Aug 4, 2022
e9abe5a
:white_check_mark: Down to single broken test suite
dej611 Aug 4, 2022
ca51c4f
Merge remote-tracking branch 'upstream/main' into feature/dataview-state
dej611 Aug 4, 2022
13a669e
:label: Fix type issue
dej611 Aug 4, 2022
120e52a
Merge branch 'main' into feature/dataview-state
flash1293 Aug 8, 2022
58b873a
:ok_hand: Integrate selector feedback
dej611 Aug 10, 2022
70cab64
:white_check_mark: Fix remaining unit tests
dej611 Aug 10, 2022
2301d19
Merge remote-tracking branch 'upstream/main' into feature/dataview-state
dej611 Aug 10, 2022
e68a013
:label: fix type issues
dej611 Aug 10, 2022
d594e09
:bug: Fix bug when creating dataview in place
dej611 Aug 10, 2022
d32f9dd
Merge remote-tracking branch 'upstream/main' into feature/dataview-state
dej611 Aug 11, 2022
5a5d3b9
:bug: Fix edit + remove field flow
dej611 Aug 11, 2022
0ed3fdf
Update x-pack/plugins/lens/public/visualizations/xy/types.ts
dej611 Aug 11, 2022
07e3136
:camera_flash: Fix snapshot
dej611 Aug 11, 2022
d317513
:bug: Fix the dataViews switch bug
dej611 Aug 11, 2022
8510724
Merge remote-tracking branch 'upstream/main' into feature/dataview-state
dej611 Aug 12, 2022
1b64dda
:fire: remove old cruft
dej611 Aug 12, 2022
958ae77
Merge remote-tracking branch 'upstream/main' into feature/dataview-state
dej611 Aug 12, 2022
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ describe('Lens App', () => {
expect(services.navigation.ui.TopNavMenu).toHaveBeenCalledWith(
expect.objectContaining({
query: 'fake query',
indexPatterns: [{ id: 'mockip', isTimeBased: expect.any(Function) }],
indexPatterns: [{ id: 'mockip', isTimeBased: expect.any(Function), fields: [] }],
}),
{}
);
Expand Down
29 changes: 28 additions & 1 deletion x-pack/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import {
LensAppState,
DispatchSetState,
selectSavedObjectFormat,
updateIndexPatterns,
} from '../state_management';
import { SaveModalContainer, runSaveLensVisualization } from './save_modal_container';
import { LensInspector } from '../lens_inspector_service';
import { getEditPath } from '../../common';
import { isLensEqual } from './lens_document_equality';
import { IndexPatternServiceAPI, createIndexPatternService } from '../indexpattern_service/service';

export type SaveProps = Omit<OnSaveProps, 'onTitleDuplicate' | 'newDescription'> & {
returnToOrigin: boolean;
Expand Down Expand Up @@ -66,6 +68,7 @@ export function App({
getOriginatingAppName,
spaces,
http,
notifications,
executionContext,
// Temporarily required until the 'by value' paradigm is default.
dashboardFeatureFlag,
Expand Down Expand Up @@ -360,6 +363,22 @@ export function App({
);
}, [initialContext]);

const indexPatternService = useMemo(
() =>
createIndexPatternService({
dataViews: lensAppServices.dataViews,
uiSettings: lensAppServices.uiSettings,
core: { http, notifications },
updateIndexPatterns: (newIndexPatternsState, options) => {
dispatch(updateIndexPatterns(newIndexPatternsState));
if (options?.applyImmediately) {
dispatch(applyChanges());
}
},
}),
[dispatch, http, notifications, lensAppServices]
);

return (
<>
<div className="lnsApp" data-test-subj="lnsApp">
Expand All @@ -381,13 +400,15 @@ export function App({
topNavMenuEntryGenerators={topNavMenuEntryGenerators}
initialContext={initialContext}
theme$={theme$}
indexPatternService={indexPatternService}
/>
{getLegacyUrlConflictCallout()}
{(!isLoading || persistedDoc) && (
<MemoizedEditorFrameWrapper
editorFrame={editorFrame}
showNoDataPopover={showNoDataPopover}
lensInspector={lensInspector}
indexPatternService={indexPatternService}
/>
)}
</div>
Expand Down Expand Up @@ -449,13 +470,19 @@ const MemoizedEditorFrameWrapper = React.memo(function EditorFrameWrapper({
editorFrame,
showNoDataPopover,
lensInspector,
indexPatternService,
}: {
editorFrame: EditorFrameInstance;
lensInspector: LensInspector;
showNoDataPopover: () => void;
indexPatternService: IndexPatternServiceAPI;
}) {
const { EditorFrameContainer } = editorFrame;
return (
<EditorFrameContainer showNoDataPopover={showNoDataPopover} lensInspector={lensInspector} />
<EditorFrameContainer
showNoDataPopover={showNoDataPopover}
lensInspector={lensInspector}
indexPatternService={indexPatternService}
/>
);
});
128 changes: 62 additions & 66 deletions x-pack/plugins/lens/public/app_plugin/lens_top_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { tableHasFormulas } from '@kbn/data-plugin/common';
import { exporters, getEsQueryConfig } from '@kbn/data-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import type { StateSetter } from '../types';
import {
LensAppServices,
LensTopNavActions,
Expand All @@ -29,16 +28,15 @@ import {
useLensDispatch,
LensAppState,
DispatchSetState,
updateDatasourceState,
} from '../state_management';
import {
getIndexPatternsObjects,
getIndexPatternsIds,
getResolvedDateRange,
handleIndexPatternChange,
refreshIndexPatternsList,
} from '../utils';
import { combineQueryAndFilters, getLayerMetaInfo } from './show_underlying_data';
import { changeIndexPattern } from '../state_management/lens_slice';

function getLensTopNavConfig(options: {
showSaveAndReturn: boolean;
Expand Down Expand Up @@ -219,6 +217,7 @@ export const LensTopNavMenu = ({
topNavMenuEntryGenerators,
initialContext,
theme$,
indexPatternService,
}: LensTopNavMenuProps) => {
const {
data,
Expand All @@ -231,14 +230,59 @@ export const LensTopNavMenu = ({
dashboardFeatureFlag,
dataViewFieldEditor,
dataViewEditor,
dataViews,
dataViews: dataViewsService,
} = useKibana<LensAppServices>().services;

const {
isSaveable,
isLinkedToOriginatingApp,
query,
activeData,
savedQuery,
activeDatasourceId,
datasourceStates,
visualization,
filters,
dataViews,
} = useLensSelector((state) => state.lens);

const dispatch = useLensDispatch();
const dispatchSetState: DispatchSetState = React.useCallback(
(state: Partial<LensAppState>) => dispatch(setState(state)),
[dispatch]
);
const dispatchChangeIndexPattern = React.useCallback(
async (indexPatternId) => {
const [newIndexPatternRefs, newIndexPatterns] = await Promise.all([
// Reload refs in case it's a new indexPattern created on the spot
dataViews.indexPatternRefs[indexPatternId]
? dataViews.indexPatternRefs
: indexPatternService.loadIndexPatternRefs({
isFullEditor: true,
}),
indexPatternService.ensureIndexPattern({
id: indexPatternId,
cache: dataViews.indexPatterns,
}),
]);
dispatch(
changeIndexPattern({
dataViews: { indexPatterns: newIndexPatterns, indexPatternRefs: newIndexPatternRefs },
datasourceIds: Object.keys(datasourceStates),
visualizationIds: visualization.activeId ? [visualization.activeId] : [],
indexPatternId,
})
);
},
[
dataViews.indexPatternRefs,
dataViews.indexPatterns,
datasourceStates,
dispatch,
indexPatternService,
visualization.activeId,
]
);

const [indexPatterns, setIndexPatterns] = useState<DataView[]>([]);
const [currentIndexPattern, setCurrentIndexPattern] = useState<DataView>();
Expand All @@ -247,18 +291,6 @@ export const LensTopNavMenu = ({
const closeFieldEditor = useRef<() => void | undefined>();
const closeDataViewEditor = useRef<() => void | undefined>();

const {
isSaveable,
isLinkedToOriginatingApp,
query,
activeData,
savedQuery,
activeDatasourceId,
datasourceStates,
visualization,
filters,
} = useLensSelector((state) => state.lens);

const allLoaded = Object.values(datasourceStates).every(({ isLoading }) => isLoading === false);

useEffect(() => {
Expand Down Expand Up @@ -290,7 +322,7 @@ export const LensTopNavMenu = ({

// Update the cached index patterns if the user made a change to any of them
if (hasIndexPatternsChanged) {
getIndexPatternsObjects(indexPatternIds, dataViews).then(
getIndexPatternsObjects(indexPatternIds, dataViewsService).then(
({ indexPatterns: indexPatternObjects, rejectedIds }) => {
setIndexPatterns(indexPatternObjects);
setRejectedIndexPatterns(rejectedIds);
Expand All @@ -303,7 +335,7 @@ export const LensTopNavMenu = ({
rejectedIndexPatterns,
datasourceMap,
indexPatterns,
dataViews,
dataViewsService,
]);

useEffect(() => {
Expand Down Expand Up @@ -366,6 +398,7 @@ export const LensTopNavMenu = ({
datasourceMap[activeDatasourceId],
datasourceStates[activeDatasourceId].state,
activeData,
dataViews.indexPatterns,
data.query.timefilter.timefilter.getTime(),
application.capabilities
);
Expand All @@ -375,6 +408,7 @@ export const LensTopNavMenu = ({
datasourceMap,
datasourceStates,
activeData,
dataViews.indexPatterns,
data.query.timefilter.timefilter,
application.capabilities,
]);
Expand Down Expand Up @@ -604,20 +638,8 @@ export const LensTopNavMenu = ({
});
}, [data.query.filterManager, data.query.queryString, dispatchSetState]);

const setDatasourceState: StateSetter<unknown> = useMemo(() => {
return (updater) => {
dispatch(
updateDatasourceState({
updater,
datasourceId: activeDatasourceId!,
clearStagedPreview: true,
})
);
};
}, [activeDatasourceId, dispatch]);

const refreshFieldList = useCallback(async () => {
if (currentIndexPattern && currentIndexPattern.id) {
if (currentIndexPattern?.id) {
refreshIndexPatternsList({
activeDatasources: Object.keys(datasourceStates).reduce(
(acc, datasourceId) => ({
Expand All @@ -627,7 +649,8 @@ export const LensTopNavMenu = ({
{}
),
indexPatternId: currentIndexPattern.id,
setDatasourceState,
indexPatternService,
indexPatternsCache: dataViews.indexPatterns,
});
}
// start a new session so all charts are refreshed
Expand All @@ -637,7 +660,8 @@ export const LensTopNavMenu = ({
data.search.session,
datasourceMap,
datasourceStates,
setDatasourceState,
indexPatternService,
dataViews.indexPatterns,
]);

const editField = useMemo(
Expand Down Expand Up @@ -679,32 +703,14 @@ export const LensTopNavMenu = ({
closeDataViewEditor.current = dataViewEditor.openEditor({
onSave: async (dataView) => {
if (dataView.id) {
handleIndexPatternChange({
activeDatasources: Object.keys(datasourceStates).reduce(
(acc, datasourceId) => ({
...acc,
[datasourceId]: datasourceMap[datasourceId],
}),
{}
),
datasourceStates,
indexPatternId: dataView.id,
setDatasourceState,
});
dispatchChangeIndexPattern(dataView.id);
Copy link
Contributor

@flash1293 flash1293 Aug 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a new data view is created from within Lens, the data panel crashes (seems like the new data view isn't added to the cache correctly?)

reportUnhandledError.js:10 Uncaught TypeError: Cannot read properties of undefined (reading 'fields')
    at InnerIndexPatternDataPanel (datapanel.tsx:211:1)
    at nh (react-dom.profiling.min.js:153:1)
    at ii (react-dom.profiling.min.js:175:1)
    at hi (react-dom.profiling.min.js:175:1)
    at fi (react-dom.profiling.min.js:174:1)
    at ek (react-dom.profiling.min.js:272:1)
    at dk (react-dom.profiling.min.js:248:1)
    at Xj (react-dom.profiling.min.js:247:1)
    at Ij (react-dom.profiling.min.js:240:1)
    at react-dom.profiling.min.js:122:1
InnerIndexPatternDataPanel	@	datapanel

refreshFieldList();
}
},
});
}
: undefined,
[
dataViewEditor,
canEditDataView,
datasourceMap,
datasourceStates,
refreshFieldList,
setDatasourceState,
]
[canEditDataView, dataViewEditor, dispatchChangeIndexPattern, refreshFieldList]
);

const dataViewPickerProps = {
Expand All @@ -721,18 +727,7 @@ export const LensTopNavMenu = ({
(indexPattern) => indexPattern.id === newIndexPatternId
);
setCurrentIndexPattern(currentDataView);
handleIndexPatternChange({
activeDatasources: Object.keys(datasourceStates).reduce(
(acc, datasourceId) => ({
...acc,
[datasourceId]: datasourceMap[datasourceId],
}),
{}
),
datasourceStates,
indexPatternId: newIndexPatternId,
setDatasourceState,
});
dispatchChangeIndexPattern(newIndexPatternId);
},
};

Expand All @@ -759,7 +754,8 @@ export const LensTopNavMenu = ({
allLoaded &&
activeDatasourceId &&
datasourceMap[activeDatasourceId].isTimeBased(
datasourceStates[activeDatasourceId].state
datasourceStates[activeDatasourceId].state,
dataViews.indexPatterns
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/app_plugin/mounter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export async function mountApp(
};
const lensStore: LensRootStore = makeConfigureStore(storeDeps, {
lens: getPreloadedState(storeDeps) as LensAppState,
} as PreloadedState<LensState>);
} as unknown as PreloadedState<LensState>);

const EditorRenderer = React.memo(
(props: { id?: string; history: History<unknown>; editByValue?: boolean }) => {
Expand Down
Loading