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 adhoc dataviews #138482

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 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
af27366
Merge with main and resolve conflicts
stratoula Aug 10, 2022
f340ff5
Persistable state change
stratoula Aug 10, 2022
70000f0
Fix types
stratoula Aug 10, 2022
7c77bf1
Adds support for adhoc dataviews
stratoula Aug 11, 2022
b43a72a
Fix types and unit test
stratoula Aug 12, 2022
5ca1470
Fixes field statistics
stratoula Aug 12, 2022
b5ee47e
Fix
stratoula Aug 12, 2022
e8657ee
Populate adhoc dataview to dashboard
stratoula Aug 12, 2022
fb223fd
Enhace the dataview picker lists with the adhoc dataviews
stratoula Aug 12, 2022
0916a3c
Fix test
stratoula Aug 12, 2022
2a38fdf
Merge with main and resolve conflicts
stratoula Aug 12, 2022
eaa2093
Cleanup from merge coflicts
stratoula 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('filter manager persistable state tests', () => {
const updatedFilters = inject(filters, [
{ type: DATA_VIEW_SAVED_OBJECT_TYPE, name: 'test123', id: '123' },
]);
expect(updatedFilters[0]).toHaveProperty('meta.index', undefined);
expect(updatedFilters[0]).toHaveProperty('meta.index', 'test');
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/common/query/filters/persistable_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export const inject = (filters: Filter[], references: SavedObjectReference[]) =>
...filter,
meta: {
...filter.meta,
index: reference && reference.id,
// if no reference has been found, keep the current "index" property (used for adhoc data views)
index: reference ? reference.id : filter.meta.index,
},
};
});
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/query/persistable_state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('query service persistable state tests', () => {
const updatedQueryState = inject(queryState, [
{ type: DATA_VIEW_SAVED_OBJECT_TYPE, name: 'test123', id: '123' },
]);
expect(updatedQueryState.filters[0]).toHaveProperty('meta.index', undefined);
expect(updatedQueryState.filters[0]).toHaveProperty('meta.index', 'test');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function ChangeDataView({
onSaveTextLanguageQuery,
onTextLangQuerySubmit,
textBasedLanguage,
adHocDataViews,
}: DataViewPickerPropsExtended) {
const { euiTheme } = useEuiTheme();
const [isPopoverOpen, setPopoverIsOpen] = useState(false);
Expand All @@ -93,10 +94,21 @@ export function ChangeDataView({
useEffect(() => {
const fetchDataViews = async () => {
const dataViewsRefs = await data.dataViews.getIdsWithTitle();
if (adHocDataViews?.length) {
adHocDataViews.forEach((adHocDataView) => {
if (adHocDataView.id) {
dataViewsRefs.push({
title: adHocDataView.title,
name: adHocDataView.name,
id: adHocDataView.id,
});
}
});
}
setDataViewsList(dataViewsRefs);
};
fetchDataViews();
}, [data, currentDataViewId]);
}, [data, currentDataViewId, adHocDataViews]);

useEffect(() => {
if (trigger.label) {
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/unified_search/public/dataview_picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import React from 'react';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { EuiButtonProps, EuiSelectableProps } from '@elastic/eui';
import type { AggregateQuery, Query } from '@kbn/es-query';
import { ChangeDataView } from './change_dataview';
Expand Down Expand Up @@ -44,6 +45,10 @@ export interface DataViewPickerProps {
* The id of the selected dataview.
*/
currentDataViewId?: string;
/**
* The adHocDataview selected.
*/
adHocDataViews?: DataView[];
/**
* EuiSelectable properties.
*/
Expand Down Expand Up @@ -84,6 +89,7 @@ export interface DataViewPickerPropsExtended extends DataViewPickerProps {
export const DataViewPicker = ({
isMissingCurrent,
currentDataViewId,
adHocDataViews,
onChangeDataView,
onAddField,
onDataViewCreated,
Expand All @@ -98,6 +104,7 @@ export const DataViewPicker = ({
<ChangeDataView
isMissingCurrent={isMissingCurrent}
currentDataViewId={currentDataViewId}
adHocDataViews={adHocDataViews}
onChangeDataView={onChangeDataView}
onAddField={onAddField}
onDataViewCreated={onDataViewCreated}
Expand Down
57 changes: 50 additions & 7 deletions x-pack/plugins/lens/public/app_plugin/lens_top_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isEqual } from 'lodash';
import { i18n } from '@kbn/i18n';
import React, { useCallback, useEffect, useMemo, useState, useRef } from 'react';
import { useStore } from 'react-redux';
import { asyncForEach } from '@kbn/std';
import { TopNavMenuData } from '@kbn/navigation-plugin/public';
import { downloadMultipleAs } from '@kbn/share-plugin/public';
import { tableHasFormulas } from '@kbn/data-plugin/common';
Expand Down Expand Up @@ -251,8 +252,14 @@ export const LensTopNavMenu = ({
(state: Partial<LensAppState>) => dispatch(setState(state)),
[dispatch]
);
const [indexPatterns, setIndexPatterns] = useState<DataView[]>([]);
const [adHocDataViews, setAdHocDataViews] = useState<DataView[]>();
const [currentIndexPattern, setCurrentIndexPattern] = useState<DataView>();
const [rejectedIndexPatterns, setRejectedIndexPatterns] = useState<string[]>([]);

const dispatchChangeIndexPattern = React.useCallback(
async (indexPatternId) => {
async (dataViewOrId) => {
const indexPatternId = typeof dataViewOrId === 'string' ? dataViewOrId : dataViewOrId.id;
const [newIndexPatternRefs, newIndexPatterns] = await Promise.all([
// Reload refs in case it's a new indexPattern created on the spot
dataViews.indexPatternRefs[indexPatternId]
Expand All @@ -265,9 +272,25 @@ export const LensTopNavMenu = ({
cache: dataViews.indexPatterns,
}),
]);

// enhance the references with the adHoc dataviews
if (adHocDataViews?.length) {
adHocDataViews.forEach((adHoc) => {
if (adHoc.id) {
newIndexPatternRefs.push({
title: adHoc.title,
name: adHoc.name,
id: adHoc.id,
});
}
});
}
dispatch(
changeIndexPattern({
dataViews: { indexPatterns: newIndexPatterns, indexPatternRefs: newIndexPatternRefs },
dataViews: {
indexPatterns: newIndexPatterns,
indexPatternRefs: newIndexPatternRefs,
},
datasourceIds: Object.keys(datasourceStates),
visualizationIds: visualization.activeId ? [visualization.activeId] : [],
indexPatternId,
Expand All @@ -281,12 +304,10 @@ export const LensTopNavMenu = ({
dispatch,
indexPatternService,
visualization.activeId,
adHocDataViews,
]
);

const [indexPatterns, setIndexPatterns] = useState<DataView[]>([]);
const [currentIndexPattern, setCurrentIndexPattern] = useState<DataView>();
const [rejectedIndexPatterns, setRejectedIndexPatterns] = useState<string[]>([]);
const canEditDataView = Boolean(dataViewEditor?.userPermissions.editDataView());
const closeFieldEditor = useRef<() => void | undefined>();
const closeDataViewEditor = useRef<() => void | undefined>();
Expand Down Expand Up @@ -344,6 +365,21 @@ export const LensTopNavMenu = ({
}
}, [indexPatterns]);

// add to the dataview picker list the adHoc dataviews
useEffect(() => {
const setAdHoc = async () => {
await asyncForEach(indexPatterns, async (indexPattern) => {
if (indexPattern.id && !adHocDataViews?.some((d) => d.id === indexPattern.id)) {
const dataViewInstance = await data.dataViews.get(indexPattern.id);
if (!dataViewInstance.isPersisted()) {
setAdHocDataViews([...(adHocDataViews ?? []), indexPattern]);
}
}
});
};
setAdHoc();
}, [adHocDataViews, data.dataViews, indexPatterns]);

useEffect(() => {
return () => {
// Make sure to close the editors when unmounting
Expand Down Expand Up @@ -703,14 +739,20 @@ export const LensTopNavMenu = ({
closeDataViewEditor.current = dataViewEditor.openEditor({
onSave: async (dataView) => {
if (dataView.id) {
dispatchChangeIndexPattern(dataView.id);
dispatchChangeIndexPattern(dataView);
setCurrentIndexPattern(dataView);
if (!dataView.isPersisted()) {
// add the ad-hoc dataview on the indexPatterns list
setIndexPatterns([...indexPatterns, dataView]);
}
refreshFieldList();
}
},
allowAdHocDataView: true,
});
}
: undefined,
[canEditDataView, dataViewEditor, dispatchChangeIndexPattern, refreshFieldList]
[canEditDataView, dataViewEditor, dispatchChangeIndexPattern, indexPatterns, refreshFieldList]
);

const dataViewPickerProps = {
Expand All @@ -720,6 +762,7 @@ export const LensTopNavMenu = ({
title: currentIndexPattern?.title || '',
},
currentDataViewId: currentIndexPattern?.id,
adHocDataViews,
onAddField: addField,
onDataViewCreated: createNewDataView,
onChangeDataView: (newIndexPatternId: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import { loadIndexPatternRefs, loadIndexPatterns } from '../../indexpattern_serv
function getIndexPatterns(
references?: SavedObjectReference[],
initialContext?: VisualizeFieldContext | VisualizeEditorContext,
initialId?: string
initialId?: string,
adHocDataviews?: string[]
) {
const indexPatternIds = [];
if (initialContext) {
Expand All @@ -67,6 +68,9 @@ function getIndexPatterns(
}
}
}
if (adHocDataviews) {
indexPatternIds.push(...adHocDataviews);
}
return [...new Set(indexPatternIds)];
}

Expand Down Expand Up @@ -113,7 +117,27 @@ export async function initializeDataViews(
? fallbackId
: undefined;

const usedIndexPatterns = getIndexPatterns(references, initialContext, initialId);
const adHocDataviewsIds: string[] = [];
let adHocDataviews;
Object.keys(datasourceMap).forEach((datasourceId) => {
const datasource = datasourceMap[datasourceId];
const datasourceState = datasourceStates[datasourceId]?.state;
const adHocSpecs = datasource?.getAdHocIndexSpecs?.(datasourceState);
if (adHocSpecs) {
const dataViewsIds: string[] = Object.keys(adHocSpecs);
if (dataViewsIds.length) {
adHocDataviewsIds.push(...dataViewsIds);
adHocDataviews = Object.values(adHocSpecs);
}
}
});

const usedIndexPatterns = getIndexPatterns(
references,
initialContext,
initialId,
adHocDataviewsIds
);

// load them
const availableIndexPatterns = new Set(indexPatternRefs.map(({ id }: IndexPatternRef) => id));
Expand All @@ -125,6 +149,7 @@ export async function initializeDataViews(
patterns: usedIndexPatterns,
notUsedPatterns,
cache: {},
adHocDataviews,
});

return { indexPatternRefs, indexPatterns };
Expand Down
37 changes: 35 additions & 2 deletions x-pack/plugins/lens/public/embeddable/embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ import { LensAttributeService } from '../lens_attribute_service';
import type { ErrorMessage, TableInspectorAdapter } from '../editor_frame_service/types';
import { getLensInspectorService, LensInspector } from '../lens_inspector_service';
import { SharingSavedObjectProps, VisualizationDisplayOptions } from '../types';
import { getActiveDatasourceIdFromDoc, getIndexPatternsObjects, inferTimeField } from '../utils';
import {
getActiveDatasourceIdFromDoc,
getIndexPatternsObjects,
inferTimeField,
getAdHocIndexSpecs,
} from '../utils';
import { getLayerMetaInfo, combineQueryAndFilters } from '../app_plugin/show_underlying_data';
import { convertDataViewIntoLensIndexPattern } from '../indexpattern_service/loader';

Expand Down Expand Up @@ -770,8 +775,23 @@ export class Embeddable

this.activeDataInfo.activeDatasource = this.deps.datasourceMap[activeDatasourceId];
const docDatasourceState = this.savedVis?.state.datasourceStates[activeDatasourceId];
const adHocIndexPatterns =
this.activeDataInfo.activeDatasource?.getAdHocIndexSpecs?.(docDatasourceState);

const adHocDataviews: DataView[] = [];

if (adHocIndexPatterns) {
const adHocSpecs = Object.values(adHocIndexPatterns);
if (adHocSpecs?.length) {
for (const addHocDataView of adHocSpecs) {
const d = await this.deps.dataViews.create(addHocDataView);
adHocDataviews.push(d);
}
}
}
const allIndexPatterns = [...this.indexPatterns, ...adHocDataviews];

const indexPatternsCache = this.indexPatterns.reduce(
const indexPatternsCache = allIndexPatterns.reduce(
(acc, indexPattern) => ({
[indexPattern.id!]: convertDataViewIntoLensIndexPattern(indexPattern),
...acc,
Expand Down Expand Up @@ -831,6 +851,19 @@ export class Embeddable
this.savedVis?.references.map(({ id }) => id) || [],
this.deps.dataViews
);
const activeDatasourceId = getActiveDatasourceIdFromDoc(this.savedVis);
if (activeDatasourceId) {
const activeDatasource = this.deps.datasourceMap[activeDatasourceId];
const dataSourceState = this.savedVis?.state.datasourceStates[activeDatasourceId];
const { adHocDataviews } = await getAdHocIndexSpecs(
activeDatasource,
dataSourceState,
this.deps.dataViews
);
if (adHocDataviews.length) {
indexPatterns.push(...adHocDataviews);
}
}

this.indexPatterns = uniqBy(indexPatterns, 'id');

Expand Down
Loading