Skip to content

Commit

Permalink
[Lens] give data plugin control of filter extraction, injection, and …
Browse files Browse the repository at this point in the history
…migrations (#120305)
  • Loading branch information
drewdaemon authored Jan 13, 2022
1 parent 1aadcd3 commit efc07ee
Show file tree
Hide file tree
Showing 42 changed files with 1,092 additions and 378 deletions.
5 changes: 4 additions & 1 deletion src/plugins/data/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import { UsageCollectionSetup } from '../../usage_collection/server';
import { AutocompleteService } from './autocomplete';
import { FieldFormatsSetup, FieldFormatsStart } from '../../field_formats/server';
import { getUiSettings } from './ui_settings';
import { QuerySetup } from './query';

interface DataEnhancements {
search: SearchEnhancements;
}

export interface DataPluginSetup {
search: ISearchSetup;
query: QuerySetup;
/**
* @deprecated - use "fieldFormats" plugin directly instead
*/
Expand Down Expand Up @@ -88,7 +90,7 @@ export class DataServerPlugin
{ bfetch, expressions, usageCollection, fieldFormats }: DataPluginSetupDependencies
) {
this.scriptsService.setup(core);
this.queryService.setup(core);
const querySetup = this.queryService.setup(core);
this.autocompleteService.setup(core);
this.kqlTelemetryService.setup(core, { usageCollection });

Expand All @@ -105,6 +107,7 @@ export class DataServerPlugin
searchSetup.__enhance(enhancements.search);
},
search: searchSetup,
query: querySetup,
fieldFormats,
};
}
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/server/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
*/

export { QueryService } from './query_service';
export type { QuerySetup } from './query_service';
3 changes: 3 additions & 0 deletions src/plugins/data/server/query/query_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ export class QueryService implements Plugin<void> {

public start() {}
}

/** @public */
export type QuerySetup = ReturnType<QueryService['setup']>;
4 changes: 2 additions & 2 deletions src/plugins/kibana_utils/common/persistable_state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export interface PersistableState<P extends SerializableRecord = SerializableRec
*/
export type MigrateFunctionsObject = { [semver: string]: MigrateFunction<any, any> };
export type MigrateFunction<
FromVersion extends SerializableRecord = SerializableRecord,
ToVersion extends SerializableRecord = SerializableRecord
FromVersion extends Serializable = SerializableRecord,
ToVersion extends Serializable = SerializableRecord
> = (state: FromVersion) => ToVersion;

/**
Expand Down
20 changes: 12 additions & 8 deletions x-pack/plugins/cases/server/common/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { SavedObject, SavedObjectsFindResponse } from 'kibana/server';
import { lensEmbeddableFactory } from '../../../lens/server/embeddable/lens_embeddable_factory';
import { makeLensEmbeddableFactory } from '../../../lens/server/embeddable/make_lens_embeddable_factory';
import { SECURITY_SOLUTION_OWNER } from '../../common/constants';
import {
AssociationType,
Expand Down Expand Up @@ -879,7 +879,7 @@ describe('common utils', () => {
].join('\n\n');

const extractedReferences = extractLensReferencesFromCommentString(
lensEmbeddableFactory,
makeLensEmbeddableFactory({}),
commentString
);

Expand Down Expand Up @@ -977,12 +977,16 @@ describe('common utils', () => {
)}},"editMode":false}}`,
].join('\n\n');

const updatedReferences = getOrUpdateLensReferences(lensEmbeddableFactory, newCommentString, {
references: currentCommentReferences,
attributes: {
comment: currentCommentString,
},
} as SavedObject<CommentRequestUserType>);
const updatedReferences = getOrUpdateLensReferences(
makeLensEmbeddableFactory({}),
newCommentString,
{
references: currentCommentReferences,
attributes: {
comment: currentCommentString,
},
} as SavedObject<CommentRequestUserType>
);

const expectedReferences = [
...nonLensCurrentCommentReferences,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '../../../common/utils/markdown_plugins/utils';

import { savedObjectsServiceMock } from '../../../../../../src/core/server/mocks';
import { lensEmbeddableFactory } from '../../../../lens/server/embeddable/lens_embeddable_factory';
import { makeLensEmbeddableFactory } from '../../../../lens/server/embeddable/make_lens_embeddable_factory';
import { LensDocShape715 } from '../../../../lens/server';
import {
SavedObjectReference,
Expand All @@ -32,7 +32,7 @@ import { SerializableRecord } from '@kbn/utility-types';

describe('comments migrations', () => {
const migrations = createCommentsMigrations({
lensEmbeddableFactory,
lensEmbeddableFactory: makeLensEmbeddableFactory({}),
});

const contextMock = savedObjectsServiceMock.createMigrationContext();
Expand Down
42 changes: 24 additions & 18 deletions x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,13 @@ describe('Lens App', () => {
defaultSavedObjectId = '1234';
defaultDoc = {
savedObjectId: defaultSavedObjectId,
visualizationType: 'testVis',
type: 'lens',
title: 'An extremely cool default document!',
expression: 'definitely a valid expression',
state: {
query: 'lucene',
filters: [{ query: { match_phrase: { src: 'test' } } }],
filters: [{ query: { match_phrase: { src: 'test' } }, meta: { index: 'index-pattern-0' } }],
},
references: [{ type: 'index-pattern', id: '1', name: 'index-pattern-0' }],
} as unknown as Document;
Expand Down Expand Up @@ -685,7 +687,7 @@ describe('Lens App', () => {
savedObjectId: defaultSavedObjectId,
title: 'hello there2',
state: expect.objectContaining({
filters: [unpinned],
filters: services.data.query.filterManager.inject([unpinned], []),
}),
}),
true,
Expand Down Expand Up @@ -1252,24 +1254,28 @@ describe('Lens App', () => {
});

it('should not confirm when changes are saved', async () => {
const { props } = await mountWith({
preloadedState: {
persistedDoc: {
...defaultDoc,
state: {
...defaultDoc.state,
datasourceStates: { testDatasource: {} },
visualization: {},
},
},
isSaveable: true,
...(defaultDoc.state as Partial<LensAppState>),
visualization: {
activeId: 'testVis',
state: {},
const preloadedState = {
persistedDoc: {
...defaultDoc,
state: {
...defaultDoc.state,
datasourceStates: { testDatasource: {} },
visualization: {},
},
},
});
isSaveable: true,
...(defaultDoc.state as Partial<LensAppState>),
visualization: {
activeId: 'testVis',
state: {},
},
};

const customProps = makeDefaultProps();
customProps.datasourceMap.testDatasource.isEqual = () => true; // if this returns false, the documents won't be accounted equal

const { props } = await mountWith({ preloadedState, props: customProps });

const lastCall = props.onAppLeave.mock.calls[props.onAppLeave.mock.calls.length - 1][0];
lastCall({ default: defaultLeave, confirm: confirmLeave });
expect(defaultLeave).toHaveBeenCalled();
Expand Down
36 changes: 23 additions & 13 deletions x-pack/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import './app.scss';

import { isEqual } from 'lodash';
import React, { useState, useEffect, useCallback } from 'react';
import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiBreadcrumb } from '@elastic/eui';
import {
Expand All @@ -32,13 +31,10 @@ import {
DispatchSetState,
selectSavedObjectFormat,
} from '../state_management';
import {
SaveModalContainer,
getLastKnownDocWithoutPinnedFilters,
runSaveLensVisualization,
} from './save_modal_container';
import { SaveModalContainer, runSaveLensVisualization } from './save_modal_container';
import { LensInspector } from '../lens_inspector_service';
import { getEditPath } from '../../common';
import { isLensEqual } from './lens_document_equality';

export type SaveProps = Omit<OnSaveProps, 'onTitleDuplicate' | 'newDescription'> & {
returnToOrigin: boolean;
Expand Down Expand Up @@ -92,8 +88,17 @@ export function App({
isSaveable,
} = useLensSelector((state) => state.lens);

const selectorDependencies = useMemo(
() => ({
datasourceMap,
visualizationMap,
extractFilterReferences: data.query.filterManager.extract,
}),
[datasourceMap, visualizationMap, data.query.filterManager.extract]
);

const currentDoc = useLensSelector((state) =>
selectSavedObjectFormat(state, datasourceMap, visualizationMap)
selectSavedObjectFormat(state, selectorDependencies)
);

// Used to show a popover that guides the user towards changing the date range when no data is available.
Expand Down Expand Up @@ -146,12 +151,9 @@ export function App({

useEffect(() => {
onAppLeave((actions) => {
// Confirm when the user has made any changes to an existing doc
// or when the user has configured something without saving

if (
application.capabilities.visualize.save &&
!isEqual(persistedDoc?.state, getLastKnownDocWithoutPinnedFilters(lastKnownDoc)?.state) &&
!isLensEqual(persistedDoc, lastKnownDoc, data.query.filterManager.inject, datasourceMap) &&
(isSaveable || persistedDoc)
) {
return actions.confirm(
Expand All @@ -166,7 +168,15 @@ export function App({
return actions.default();
}
});
}, [onAppLeave, lastKnownDoc, isSaveable, persistedDoc, application.capabilities.visualize.save]);
}, [
onAppLeave,
lastKnownDoc,
isSaveable,
persistedDoc,
application.capabilities.visualize.save,
data.query.filterManager.inject,
datasourceMap,
]);

const getLegacyUrlConflictCallout = useCallback(() => {
// This function returns a callout component *if* we have encountered a "legacy URL conflict" scenario
Expand Down
Loading

0 comments on commit efc07ee

Please sign in to comment.