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] give data plugin control of filter extraction, injection, and migrations #120305

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
59144f5
inject filterManager.extract into selector
drewdaemon Dec 2, 2021
49b2932
tentative migration
drewdaemon Dec 2, 2021
f54cf88
Replace local injectFilterReferences with FilterManager.inject
drewdaemon Dec 2, 2021
133f488
apply migration in embedded context
drewdaemon Dec 2, 2021
fbb3835
some fixes
drewdaemon Dec 3, 2021
f17bb31
expose query service in server data plugin setup
drewdaemon Dec 3, 2021
7218e4f
apply filter migrations to embedded lens visualizations
drewdaemon Dec 3, 2021
842304c
fix imports
drewdaemon Dec 3, 2021
9ad9b37
fix type export
drewdaemon Dec 3, 2021
4ba7359
statically import filter inject/extract
drewdaemon Dec 3, 2021
2e791e5
Merge pull request #1 from andrewctate/inject-extract-static-imports
drewdaemon Dec 6, 2021
d1594a8
replacing dynamic injection with static import
drewdaemon Dec 6, 2021
1e8d09e
a little more cleanup
drewdaemon Dec 6, 2021
9ff5fa9
apply filter migrations to lens migration map
drewdaemon Dec 6, 2021
ec82855
remove unused query service dep
drewdaemon Dec 6, 2021
6ce57e3
move a test
drewdaemon Dec 6, 2021
99e9b7c
changing common imports to relative
drewdaemon Dec 6, 2021
ab7d33d
Merge branch 'main' into 114482/correctly-apply-filter-migrations-in-…
kibanamachine Dec 6, 2021
c2031a4
remove QuerySetup export
drewdaemon Dec 6, 2021
ff137b7
Merge branch '114482/correctly-apply-filter-migrations-in-saved-objec…
drewdaemon Dec 6, 2021
0b05d81
remove problem import
drewdaemon Dec 6, 2021
954150a
mergeSavedObjectMigrationMaps
drewdaemon Dec 6, 2021
38d7de5
revert selector changes
drewdaemon Dec 7, 2021
0ef428e
some more cleanup
drewdaemon Dec 7, 2021
c958f18
reverting data server plugin changes
drewdaemon Dec 7, 2021
ab48c85
fix embeddable test
drewdaemon Dec 7, 2021
3a063ce
Fix load_initial test
drewdaemon Dec 7, 2021
cc1e3cd
attempt to fix confirmation test
drewdaemon Dec 7, 2021
195fea3
inject filter manager inject function for save_modal_container
drewdaemon Dec 8, 2021
6432ec1
inject injectFilterReferences into embeddable
drewdaemon Dec 8, 2021
26387a9
use inject from data plugin for load_initial
drewdaemon Dec 8, 2021
6b52608
inject extractFilterReferences into selector
drewdaemon Dec 8, 2021
49be9a0
update embeddable test
drewdaemon Dec 8, 2021
0763942
fix saves app filters test
drewdaemon Dec 8, 2021
b9785fe
skip failing unit test
drewdaemon Dec 8, 2021
a22a1d8
merge main
drewdaemon Dec 8, 2021
eb84e16
inject filter migrations for server-side lens saved-object migrations
drewdaemon Dec 8, 2021
a4540a5
inject filter migrations in dashboard embedded migrations context
drewdaemon Dec 8, 2021
ffa8ca7
revert data plugin persistable state static export
drewdaemon Dec 8, 2021
584f5af
Merge branch 'main' of github.com:elastic/kibana into 114482/correctl…
drewdaemon Dec 8, 2021
218fe00
fix imports
drewdaemon Dec 8, 2021
6ada5be
fix type export
drewdaemon Dec 8, 2021
abcd2ec
fix a couple of lens factory imports
drewdaemon Dec 8, 2021
46cedec
bespoke lens doc comparison func
drewdaemon Dec 15, 2021
4cf9500
fix indexpattern datasource test
drewdaemon Dec 16, 2021
6b53671
Merge branch 'main' of github.com:elastic/kibana into 114482/correctl…
drewdaemon Dec 16, 2021
aafbe17
fix some typings
drewdaemon Dec 16, 2021
b13b452
fix filter migrations
drewdaemon Dec 17, 2021
8590865
Merge branch 'main' of github.com:elastic/kibana into 114482/correctl…
drewdaemon Dec 27, 2021
5002a59
Merge branch 'main' into 114482/correctly-apply-filter-migrations-in-…
kibanamachine Dec 28, 2021
dc27360
Merge branch 'main' into 114482/correctly-apply-filter-migrations-in-…
kibanamachine Dec 30, 2021
78910c2
reset functional test fixture
drewdaemon Dec 31, 2021
c918458
Merge branch '114482/correctly-apply-filter-migrations-in-saved-objec…
drewdaemon Dec 31, 2021
d9410b7
style fix
drewdaemon Dec 31, 2021
c9395d3
Merge branch 'main' into 114482/correctly-apply-filter-migrations-in-…
kibanamachine Dec 31, 2021
da6a106
ignore ordering in the datasource map
drewdaemon Dec 31, 2021
17e0d39
Merge branch '114482/correctly-apply-filter-migrations-in-saved-objec…
drewdaemon Dec 31, 2021
3358b5b
Merge branch 'main' into 114482/correctly-apply-filter-migrations-in-…
kibanamachine Jan 4, 2022
6049389
Update persistable_state.ts
drewdaemon Jan 4, 2022
33e96f4
lensEmbeddableFactoryFactory => makeLensEmbeddableFactory
drewdaemon Jan 4, 2022
9bdd538
Merge branch 'main' into 114482/correctly-apply-filter-migrations-in-…
kibanamachine Jan 5, 2022
c24b7ac
fix applying filter migration in lens embedded context
drewdaemon Jan 5, 2022
16e11db
Merge branch '114482/correctly-apply-filter-migrations-in-saved-objec…
drewdaemon Jan 5, 2022
ba6172b
Merge branch 'main' of github.com:elastic/kibana into 114482/correctl…
drewdaemon Jan 5, 2022
97494a7
re-enabling skipped app test
drewdaemon Jan 5, 2022
e63a5d7
test that filter migrations are applied to lens migration map
drewdaemon Jan 5, 2022
b60fae8
Merge branch 'main' into 114482/correctly-apply-filter-migrations-in-…
kibanamachine Jan 10, 2022
7077464
expect Filter array instead of individual Filter object
drewdaemon Jan 11, 2022
7df6319
Merge branch 'main' into 114482/correctly-apply-filter-migrations-in-…
kibanamachine Jan 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
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