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] Navigate from discover to lens #77873

Merged
merged 25 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f2c3e56
Create lens action and unregister the visualize one
stratoula Sep 18, 2020
278e51b
remove console
stratoula Sep 18, 2020
a8a7591
Implement Discover to Lens, wip, missing tests
stratoula Sep 22, 2020
c018989
Add unit tests
stratoula Sep 23, 2020
39992f5
Merge branch 'master' into navogate-discover-to-lens
elasticmachine Sep 23, 2020
80e56ae
fix embed lens to empty dashboard functional tests
stratoula Sep 23, 2020
03b5cb9
fix suggestions on save
stratoula Sep 23, 2020
601361b
Fix bug on save button, query and filters should be transferred from …
stratoula Sep 23, 2020
bf30921
resolve conflicts
stratoula Sep 24, 2020
b50e628
Add functional test for the navigation from Discover to Lens
stratoula Sep 24, 2020
0f48077
Merge branch 'master' into navogate-discover-to-lens
stratoula Sep 28, 2020
1519f93
PR update after code review
stratoula Sep 28, 2020
ff28aee
Merge branch 'master' into navogate-discover-to-lens
elasticmachine Sep 29, 2020
89c8662
unregister visualize action only if the action exists
stratoula Sep 29, 2020
caaed27
Change the test to not be flaky
stratoula Sep 29, 2020
2f949fe
Merge branch 'master' into navogate-discover-to-lens
elasticmachine Sep 30, 2020
83da4fa
Move suggestions to editor frame and hide the emptyWorkspace for visu…
stratoula Sep 30, 2020
eedc7bb
resolve conflicts and merge with master
stratoula Oct 1, 2020
bd09d21
Merge branch 'master' into navogate-discover-to-lens
elasticmachine Oct 1, 2020
5e62b42
Update ui actions docs
stratoula Oct 1, 2020
92d512b
Merge branch 'master' into navogate-discover-to-lens
elasticmachine Oct 2, 2020
32dd2bd
Add a retry to remove test flakiness
stratoula Oct 2, 2020
35b8297
Merge branch 'navogate-discover-to-lens' of https://github.com/strato…
stratoula Oct 2, 2020
a27df91
Fix bug of infinite loader when removing the y axis dimension
stratoula Oct 2, 2020
d5e6b68
Merge branch 'master' into navogate-discover-to-lens
kibanamachine Oct 5, 2020
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
1 change: 1 addition & 0 deletions src/plugins/ui_actions/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export {
VisualizeFieldContext,
ACTION_VISUALIZE_FIELD,
ACTION_VISUALIZE_GEO_FIELD,
ACTION_VISUALIZE_LENS_FIELD,
} from './types';
export {
ActionByType,
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/ui_actions/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ export interface TriggerContextMapping {
const DEFAULT_ACTION = '';
export const ACTION_VISUALIZE_FIELD = 'ACTION_VISUALIZE_FIELD';
export const ACTION_VISUALIZE_GEO_FIELD = 'ACTION_VISUALIZE_GEO_FIELD';
export const ACTION_VISUALIZE_LENS_FIELD = 'ACTION_VISUALIZE_LENS_FIELD';
export type ActionType = keyof ActionContextMapping;

export interface ActionContextMapping {
[DEFAULT_ACTION]: BaseContext;
[ACTION_VISUALIZE_FIELD]: VisualizeFieldContext;
[ACTION_VISUALIZE_GEO_FIELD]: VisualizeFieldContext;
[ACTION_VISUALIZE_LENS_FIELD]: VisualizeFieldContext;
Copy link
Contributor

Choose a reason for hiding this comment

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

So if I understand this correctly, this is using the same trigger, but different action for Lens- this sounds like what we agreed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, exactly same trigger VISUALIZE_FIELD_TRIGGER but different action!

stratoula marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { AGGS_TERMS_SIZE_SETTING } from '../../common/constants';

export const visualizeFieldAction = createAction<typeof ACTION_VISUALIZE_FIELD>({
type: ACTION_VISUALIZE_FIELD,
id: ACTION_VISUALIZE_FIELD,
getDisplayName: () =>
i18n.translate('visualize.discover.visualizeFieldLabel', {
defaultMessage: 'Visualize field',
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/lens/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
"urlForwarding",
"visualizations",
"dashboard",
"charts"
"charts",
"uiActions"
stratoula marked this conversation as resolved.
Show resolved Hide resolved
],
"optionalPlugins": ["embeddable", "usageCollection", "taskManager", "uiActions", "globalSearch"],
"optionalPlugins": ["embeddable", "usageCollection", "taskManager", "globalSearch"],
"configPath": ["xpack", "lens"],
"extraPublicDirs": ["common/constants"],
"requiredBundles": ["savedObjects", "kibanaUtils", "kibanaReact", "embeddable"]
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ describe('Lens App', () => {
},
"doc": undefined,
"filters": Array [],
"initialContext": undefined,
"onChange": [Function],
"onError": [Function],
"query": Object {
Expand Down
12 changes: 9 additions & 3 deletions x-pack/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function App({
incomingState,
redirectToOrigin,
setHeaderActionMenu,
initialContext,
}: LensAppProps) {
const {
data,
Expand All @@ -65,7 +66,7 @@ export function App({
const [state, setState] = useState<LensAppState>(() => {
const currentRange = data.query.timefilter.timefilter.getTime();
return {
query: data.query.queryString.getDefaultQuery(),
query: data.query.queryString.getQuery(),
filters: data.query.filterManager.getFilters(),
isLoading: Boolean(initialInput),
indexPatternsForTopNav: [],
Expand Down Expand Up @@ -140,8 +141,11 @@ export function App({

useEffect(() => {
// Clear app-specific filters when navigating to Lens. Necessary because Lens
// can be loaded without a full page refresh
data.query.filterManager.setAppFilters([]);
// can be loaded without a full page refresh. If the user navigates to Lens from Discover
// we keep the filters
if (!initialContext) {
data.query.filterManager.setAppFilters([]);
}

const filterSubscription = data.query.filterManager.getUpdates$().subscribe({
next: () => {
Expand Down Expand Up @@ -185,6 +189,7 @@ export function App({
uiSettings,
data.query,
history,
initialContext,
]);

useEffect(() => {
Expand Down Expand Up @@ -574,6 +579,7 @@ export function App({
doc: state.persistedDoc,
onError,
showNoDataPopover,
initialContext,
onChange: ({ filterableIndexPatterns, doc, isSaveable }) => {
if (isSaveable !== state.isSaveable) {
setState((s) => ({ ...s, isSaveable }));
Expand Down
9 changes: 8 additions & 1 deletion x-pack/plugins/lens/public/app_plugin/mounter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import {
LensByReferenceInput,
LensByValueInput,
} from '../editor_frame_service/embeddable/embeddable';
import { ACTION_VISUALIZE_LENS_FIELD } from '../../../../../src/plugins/ui_actions/public';
import { LensAttributeService } from '../lens_attribute_service';
import { LensAppServices, RedirectToOriginProps } from './types';
import { LensAppServices, RedirectToOriginProps, HistoryLocationState } from './types';
import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public';

export async function mountApp(
Expand All @@ -46,6 +47,7 @@ export async function mountApp(
const instance = await createEditorFrame();
const storage = new Storage(localStorage);
const stateTransfer = embeddable?.getStateTransfer(params.history);
const historyLocationState = params.history.location.state as HistoryLocationState;
const embeddableEditorIncomingState = stateTransfer?.getIncomingEditorState();

const lensServices: LensAppServices = {
Expand Down Expand Up @@ -132,6 +134,11 @@ export async function mountApp(
onAppLeave={params.onAppLeave}
setHeaderActionMenu={params.setHeaderActionMenu}
history={routeProps.history}
initialContext={
historyLocationState && historyLocationState.type === ACTION_VISUALIZE_LENS_FIELD
? historyLocationState.payload
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay, but I was almost expecting the entire historyLocationState to be passed in here so that the app is making fewer decisions. No need to change it unless you're planning on making other changes.

: undefined
}
stratoula marked this conversation as resolved.
Show resolved Hide resolved
/>
);
};
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/lens/public/app_plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import { NavigationPublicPluginStart } from '../../../../../src/plugins/navigati
import { LensAttributeService } from '../lens_attribute_service';
import { IStorageWrapper } from '../../../../../src/plugins/kibana_utils/public';
import { DashboardFeatureFlagConfig } from '../../../../../src/plugins/dashboard/public';
import {
VisualizeFieldContext,
ACTION_VISUALIZE_LENS_FIELD,
} from '../../../../../src/plugins/ui_actions/public';
import { EmbeddableEditorState } from '../../../../../src/plugins/embeddable/public';
import { EditorFrameInstance } from '..';

Expand Down Expand Up @@ -75,6 +79,12 @@ export interface LensAppProps {

// State passed in by the container which is used to determine the id of the Originating App.
incomingState?: EmbeddableEditorState;
initialContext?: VisualizeFieldContext;
}

export interface HistoryLocationState {
type: typeof ACTION_VISUALIZE_LENS_FIELD;
payload: VisualizeFieldContext;
}

export interface LensAppServices {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ describe('editor_frame', () => {
/>
);
});
expect(mockDatasource.initialize).toHaveBeenCalledWith(datasource1State, []);
expect(mockDatasource2.initialize).toHaveBeenCalledWith(datasource2State, []);
expect(mockDatasource.initialize).toHaveBeenCalledWith(datasource1State, [], undefined);
expect(mockDatasource2.initialize).toHaveBeenCalledWith(datasource2State, [], undefined);
expect(mockDatasource3.initialize).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -972,6 +972,32 @@ describe('editor_frame', () => {
});

describe('suggestions', () => {
it('should fetch suggestions of currently active datasource when initializes from visualization trigger', async () => {
await act(async () => {
mount(
<EditorFrame
{...getDefaultProps()}
initialContext={{
indexPatternId: '1',
fieldName: 'test',
}}
visualizationMap={{
testVis: mockVisualization,
}}
datasourceMap={{
testDatasource: mockDatasource,
testDatasource2: mockDatasource2,
}}
initialDatasourceId="testDatasource"
initialVisualizationId="testVis"
ExpressionRenderer={expressionRendererMock}
/>
);
});

expect(mockDatasource.getDatasourceSuggestionsForVisualizeField).toHaveBeenCalled();
});

it('should fetch suggestions of currently active datasource', async () => {
await act(async () => {
mount(
Expand Down Expand Up @@ -1208,6 +1234,7 @@ describe('editor_frame', () => {
...mockDatasource,
getDatasourceSuggestionsForField: () => [generateSuggestion()],
getDatasourceSuggestionsFromCurrentState: () => [generateSuggestion()],
getDatasourceSuggestionsForVisualizeField: () => [generateSuggestion()],
},
}}
initialDatasourceId="testDatasource"
Expand Down Expand Up @@ -1274,6 +1301,7 @@ describe('editor_frame', () => {
...mockDatasource,
getDatasourceSuggestionsForField: () => [generateSuggestion()],
getDatasourceSuggestionsFromCurrentState: () => [generateSuggestion()],
getDatasourceSuggestionsForVisualizeField: () => [generateSuggestion()],
renderDataPanel: (_element, { dragDropContext: { setDragging, dragging } }) => {
if (dragging !== 'draggedField') {
setDragging('draggedField');
Expand Down Expand Up @@ -1370,6 +1398,7 @@ describe('editor_frame', () => {
...mockDatasource,
getDatasourceSuggestionsForField: () => [generateSuggestion()],
getDatasourceSuggestionsFromCurrentState: () => [generateSuggestion()],
getDatasourceSuggestionsForVisualizeField: () => [generateSuggestion()],
renderDataPanel: (_element, { dragDropContext: { setDragging, dragging } }) => {
if (dragging !== 'draggedField') {
setDragging('draggedField');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { RootDragDropProvider } from '../../drag_drop';
import { getSavedObjectFormat } from './save';
import { generateId } from '../../id_generator';
import { Filter, Query, SavedQuery } from '../../../../../../src/plugins/data/public';
import { VisualizeFieldContext } from '../../../../../../src/plugins/ui_actions/public';
import { EditorFrameStartPlugins } from '../service';
import { initializeDatasources, createDatasourceLayers } from './state_helpers';

Expand All @@ -45,6 +46,7 @@ export interface EditorFrameProps {
isSaveable: boolean;
}) => void;
showNoDataPopover: () => void;
initialContext?: VisualizeFieldContext;
}

export function EditorFrame(props: EditorFrameProps) {
Expand All @@ -63,7 +65,12 @@ export function EditorFrame(props: EditorFrameProps) {
// prevents executing dispatch on unmounted component
let isUnmounted = false;
if (!allLoaded) {
initializeDatasources(props.datasourceMap, state.datasourceStates, props.doc?.references)
initializeDatasources(
props.datasourceMap,
state.datasourceStates,
props.doc?.references,
props.initialContext
)
.then((result) => {
if (!isUnmounted) {
Object.entries(result).forEach(([datasourceId, { state: datasourceState }]) => {
Expand All @@ -84,7 +91,6 @@ export function EditorFrame(props: EditorFrameProps) {
// eslint-disable-next-line react-hooks/exhaustive-deps
[allLoaded, onError]
);

const datasourceLayers = createDatasourceLayers(props.datasourceMap, state.datasourceStates);

const framePublicAPI: FramePublicAPI = {
Expand Down Expand Up @@ -275,6 +281,7 @@ export function EditorFrame(props: EditorFrameProps) {
ExpressionRenderer={props.ExpressionRenderer}
core={props.core}
plugins={props.plugins}
visualizeTriggerFieldContext={props.initialContext}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import { Ast } from '@kbn/interpreter/common';
import { Datasource, DatasourcePublicAPI, Visualization } from '../../types';
import { buildExpression } from './expression_helpers';
import { Document } from '../../persistence/saved_object_store';
import { VisualizeFieldContext } from '../../../../../../src/plugins/ui_actions/public';

export async function initializeDatasources(
datasourceMap: Record<string, Datasource>,
datasourceStates: Record<string, { state: unknown; isLoading: boolean }>,
references?: SavedObjectReference[]
references?: SavedObjectReference[],
initialContext?: VisualizeFieldContext
) {
const states: Record<string, { isLoading: boolean; state: unknown }> = {};
await Promise.all(
Object.entries(datasourceMap).map(([datasourceId, datasource]) => {
if (datasourceStates[datasourceId]) {
return datasource
.initialize(datasourceStates[datasourceId].state || undefined, references)
.initialize(datasourceStates[datasourceId].state || undefined, references, initialContext)
.then((datasourceState) => {
states[datasourceId] = { isLoading: false, state: datasourceState };
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,75 @@ describe('suggestion helpers', () => {
expect(multiDatasourceMap.mock3.getDatasourceSuggestionsForField).not.toHaveBeenCalled();
});

it('should call getDatasourceSuggestionsForVisualizeField when a visualizeTriggerField is passed', () => {
datasourceMap.mock.getDatasourceSuggestionsForVisualizeField.mockReturnValue([
generateSuggestion(),
]);
getSuggestions({
visualizationMap: {
vis1: createMockVisualization(),
},
activeVisualizationId: 'vis1',
visualizationState: {},
datasourceMap,
datasourceStates,
visualizeTriggerFieldContext: {
indexPatternId: '1',
fieldName: 'test',
},
});
expect(datasourceMap.mock.getDatasourceSuggestionsForVisualizeField).toHaveBeenCalledWith(
datasourceStates.mock.state,
'1',
'test'
);
});

it('should call getDatasourceSuggestionsForVisualizeField from all datasources with a state', () => {
const multiDatasourceStates = {
mock: {
isLoading: false,
state: {},
},
mock2: {
isLoading: false,
state: {},
},
};
const multiDatasourceMap = {
mock: createMockDatasource('a'),
mock2: createMockDatasource('a'),
mock3: createMockDatasource('a'),
};
const visualizeTriggerField = {
indexPatternId: '1',
fieldName: 'test',
};
getSuggestions({
visualizationMap: {
vis1: createMockVisualization(),
},
activeVisualizationId: 'vis1',
visualizationState: {},
datasourceMap: multiDatasourceMap,
datasourceStates: multiDatasourceStates,
visualizeTriggerFieldContext: visualizeTriggerField,
});
expect(multiDatasourceMap.mock.getDatasourceSuggestionsForVisualizeField).toHaveBeenCalledWith(
multiDatasourceStates.mock.state,
'1',
'test'
);
expect(multiDatasourceMap.mock2.getDatasourceSuggestionsForVisualizeField).toHaveBeenCalledWith(
multiDatasourceStates.mock2.state,
'1',
'test'
);
expect(
multiDatasourceMap.mock3.getDatasourceSuggestionsForVisualizeField
).not.toHaveBeenCalled();
});

it('should rank the visualizations by score', () => {
const mockVisualization1 = createMockVisualization();
const mockVisualization2 = createMockVisualization();
Expand Down
Loading