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

[Discover] Removing SavedObject usage for savedSearch #112983

Merged
merged 34 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bb3da54
[Discover] Step 2 - remove SavedObjectLoader
alexwizp Sep 23, 2021
38258c5
Fix PR comments
alexwizp Oct 4, 2021
0a8623e
Merge branch 'master' into discover-saved-object-loader
kibanamachine Oct 5, 2021
a8df731
fix test names
alexwizp Oct 5, 2021
7f3f676
fix ts error
alexwizp Oct 5, 2021
dc3ee7b
add handling of missed 'so'
alexwizp Oct 5, 2021
232109d
add Embeddable error
alexwizp Oct 5, 2021
b3d256c
Merge remote-tracking branch 'upstream/main' into discover-saved-obje…
alexwizp Oct 5, 2021
03214e3
fix jest
alexwizp Oct 5, 2021
64c29b6
add DiscoverError component
alexwizp Oct 5, 2021
9d31455
Merge remote-tracking branch 'upstream/master' into discover-saved-ob…
alexwizp Oct 5, 2021
21af3ec
fix Joe comments
alexwizp Oct 5, 2021
eaa583b
add search params
alexwizp Oct 5, 2021
7178e31
Merge remote-tracking branch 'upstream/main' into discover-saved-obje…
alexwizp Oct 5, 2021
88e517e
add throwErrorOnUrlConflict util method
alexwizp Oct 5, 2021
465625f
add error handling into transform plugin
alexwizp Oct 5, 2021
e93c44e
do some updates
alexwizp Oct 6, 2021
85cfdb1
add spaces into visualize, visualizations
alexwizp Oct 6, 2021
c52ba68
Merge remote-tracking branch 'upstream/main' into discover-saved-obje…
alexwizp Oct 6, 2021
17d5516
fix Tim's comment
alexwizp Oct 6, 2021
e876ddb
Merge remote-tracking branch 'upstream/main' into discover-saved-obje…
alexwizp Oct 6, 2021
dc4a0ad
Merge branch 'master' into discover-saved-object-loader
kibanamachine Oct 7, 2021
7aebc9c
Merge remote-tracking branch 'upstream/master' into discover-saved-ob…
alexwizp Oct 7, 2021
06c15de
pass false into createGetterSetter for getSpaces
alexwizp Oct 7, 2021
dfeef9c
Merge branch 'master' into discover-saved-object-loader
kibanamachine Oct 7, 2021
db1e59b
Merge branch 'master' into discover-saved-object-loader
kibanamachine Oct 7, 2021
fe00324
Merge branch 'master' into discover-saved-object-loader
kibanamachine Oct 7, 2021
150a2ca
Merge branch 'master' into discover-saved-object-loader
kibanamachine Oct 8, 2021
54a9508
Merge branch 'master' into discover-saved-object-loader
kibanamachine Oct 11, 2021
a86d821
Merge remote-tracking branch 'upstream/master' into discover-saved-ob…
VladLasitsa Oct 11, 2021
ff973c5
Fix comments
VladLasitsa Oct 11, 2021
5bf1a0e
Fix lint
VladLasitsa Oct 11, 2021
4814a0a
Merge branch 'master' into discover-saved-object-loader
kibanamachine Oct 12, 2021
76ee791
Merge remote-tracking branch 'upstream/main' into discover-saved-obje…
alexwizp Oct 12, 2021
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
2 changes: 1 addition & 1 deletion src/plugins/discover/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"savedObjects",
"indexPatternFieldEditor"
],
"optionalPlugins": ["home", "share", "usageCollection"],
"optionalPlugins": ["home", "share", "usageCollection", "spaces"],
"requiredBundles": ["kibanaUtils", "home", "kibanaReact", "fieldFormats"],
"extraPublicDirs": ["common"],
"owner": {
Expand Down
34 changes: 0 additions & 34 deletions src/plugins/discover/public/__mocks__/saved_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,10 @@ import { indexPatternWithTimefieldMock } from './index_pattern_with_timefield';

export const savedSearchMock = {
id: 'the-saved-search-id',
type: 'search',
attributes: {
title: 'the-saved-search-title',
kibanaSavedObjectMeta: {
searchSourceJSON:
'{"highlightAll":true,"version":true,"query":{"query":"foo : \\"bar\\" ","language":"kuery"},"filter":[],"indexRefName":"kibanaSavedObjectMeta.searchSourceJSON.index"}',
},
},
references: [
{
name: 'kibanaSavedObjectMeta.searchSourceJSON.index',
type: 'index-pattern',
id: 'the-index-pattern-id',
},
],
migrationVersion: { search: '7.5.0' },
error: undefined,
searchSource: createSearchSourceMock({ index: indexPatternMock }),
} as unknown as SavedSearch;

export const savedSearchMockWithTimeField = {
id: 'the-saved-search-id-with-timefield',
type: 'search',
attributes: {
title: 'the-saved-search-title',
kibanaSavedObjectMeta: {
searchSourceJSON:
'{"highlightAll":true,"version":true,"query":{"query":"foo : \\"bar\\" ","language":"kuery"},"filter":[],"indexRefName":"kibanaSavedObjectMeta.searchSourceJSON.index"}',
},
},
references: [
{
name: 'kibanaSavedObjectMeta.searchSourceJSON.index',
type: 'index-pattern',
id: 'the-index-pattern-id',
},
],
migrationVersion: { search: '7.5.0' },
error: undefined,
searchSource: createSearchSourceMock({ index: indexPatternWithTimefieldMock }),
} as unknown as SavedSearch;
2 changes: 0 additions & 2 deletions src/plugins/discover/public/__mocks__/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
SAMPLE_SIZE_SETTING,
SORT_DEFAULT_ORDER_SETTING,
} from '../../common';
import { savedSearchMock } from './saved_search';
import { UI_SETTINGS } from '../../../data/common';
import { TopNavMenu } from '../../../navigation/public';
import { FORMATS_UI_SETTINGS } from 'src/plugins/field_formats/common';
Expand Down Expand Up @@ -78,7 +77,6 @@ export const discoverServiceMock = {
editIndexPattern: jest.fn(),
},
},
getSavedSearchById: (id?: string) => Promise.resolve(savedSearchMock),
navigation: {
ui: { TopNavMenu },
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { Filter, IndexPattern, IndexPatternsContract, SearchSource } from 'src/plugins/data/public';
import {
Filter,
IndexPattern,
IndexPatternsContract,
ISearchSource,
} from 'src/plugins/data/public';
import { reverseSortDir, SortDirection } from './utils/sorting';
import { convertIsoToMillis, extractNanos } from './utils/date_conversion';
import { fetchHitsInInterval } from './utils/fetch_hits_in_interval';
Expand Down Expand Up @@ -58,7 +63,7 @@ function fetchContextProvider(indexPatterns: IndexPatternsContract, useNewFields
}
const indexPattern = await indexPatterns.get(indexPatternId);
const { data } = getServices();
const searchSource = data.search.searchSource.createEmpty() as SearchSource;
const searchSource = data.search.searchSource.createEmpty();
updateSearchSource(searchSource, indexPattern, filters, Boolean(useNewFieldsApi));
const sortDirToApply = type === SurrDocType.SUCCESSORS ? sortDir : reverseSortDir(sortDir);

Expand Down Expand Up @@ -110,7 +115,7 @@ function fetchContextProvider(indexPatterns: IndexPatternsContract, useNewFields
}

export function updateSearchSource(
searchSource: SearchSource,
searchSource: ISearchSource,
indexPattern: IndexPattern,
filters: Filter[],
useNewFieldsApi: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function DiscoverDocumentsComponent({
sort={state.sort || []}
isLoading={isLoading}
searchDescription={savedSearch.description}
sharedItemTitle={savedSearch.lastSavedTitle}
sharedItemTitle={savedSearch.title}
onAddColumn={onAddColumn}
onFilter={onAddFilter as DocViewFilterFn}
onMoveColumn={onMoveColumn}
Expand All @@ -156,7 +156,7 @@ function DiscoverDocumentsComponent({
sort={(state.sort as SortPairArr[]) || []}
sampleSize={sampleSize}
searchDescription={savedSearch.description}
searchTitle={savedSearch.lastSavedTitle}
searchTitle={savedSearch.title}
setExpandedDoc={setExpandedDoc}
showTimeCol={showTimeCol}
services={services}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ import { useDataGridColumns } from '../../../../helpers/use_data_grid_columns';
import { DiscoverDocuments } from './discover_documents';
import { FetchStatus } from '../../../../types';
import { useDataState } from '../../utils/use_data_state';
import {
SavedSearchURLConflictCallout,
useSavedSearchAliasMatchRedirect,
} from '../../../../../saved_searches';

/**
* Local storage key for sidebar persistence state
Expand Down Expand Up @@ -80,6 +84,8 @@ export function DiscoverLayout({
}
}, [dataState.fetchStatus]);

useSavedSearchAliasMatchRedirect({ savedSearch, spaces: services.spaces });

const timeField = useMemo(() => {
return indexPattern.type !== 'rollup' ? indexPattern.timeFieldName : undefined;
}, [indexPattern]);
Expand Down Expand Up @@ -174,6 +180,7 @@ export function DiscoverLayout({
resetSavedSearch={resetSavedSearch}
/>
<EuiPageBody className="dscPageBody" aria-describedby="savedSearchTitle">
<SavedSearchURLConflictCallout savedSearch={savedSearch} spaces={services.spaces} />
<h1 id="savedSearchTitle" className="euiScreenReaderOnly">
{savedSearch.title}
</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import React from 'react';
import { i18n } from '@kbn/i18n';
import { SavedObjectSaveModal, showSaveModal } from '../../../../../../../saved_objects/public';
import { SavedSearch } from '../../../../../saved_searches';
import { SavedSearch, SaveSavedSearchOptions } from '../../../../../saved_searches';
import { IndexPattern } from '../../../../../../../data/common';
import { DiscoverServices } from '../../../../../build_services';
import { GetStateReturn } from '../../services/discover_state';
Expand All @@ -27,11 +27,7 @@ async function saveDataSource({
indexPattern: IndexPattern;
navigateTo: (url: string) => void;
savedSearch: SavedSearch;
saveOptions: {
confirmOverwrite: boolean;
isTitleDuplicateConfirmed: boolean;
onTitleDuplicate: () => void;
};
saveOptions: SaveSavedSearchOptions;
services: DiscoverServices;
state: GetStateReturn;
}) {
Expand All @@ -47,14 +43,20 @@ async function saveDataSource({
}),
'data-test-subj': 'saveSearchSuccess',
});

if (savedSearch.id !== prevSavedSearchId) {
navigateTo(`/view/${encodeURIComponent(savedSearch.id)}`);
if (id !== prevSavedSearchId) {
navigateTo(`/view/${encodeURIComponent(id)}`);
} else {
// Update defaults so that "reload saved query" functions correctly
state.resetAppState();
services.chrome.docTitle.change(savedSearch.lastSavedTitle!);
setBreadcrumbsTitle(savedSearch, services.chrome);
services.chrome.docTitle.change(savedSearch.title!);

setBreadcrumbsTitle(
{
...savedSearch,
id: prevSavedSearchId ?? id,
},
services.chrome
);
}
}
}
Expand Down Expand Up @@ -106,11 +108,9 @@ export async function onSaveSearch({
}) => {
const currentTitle = savedSearch.title;
savedSearch.title = newTitle;
savedSearch.copyOnSave = newCopyOnSave;
const saveOptions = {
confirmOverwrite: false,
isTitleDuplicateConfirmed,
const saveOptions: SaveSavedSearchOptions = {
onTitleDuplicate,
copyOnSave: newCopyOnSave,
};
const response = await saveDataSource({
indexPattern,
Expand All @@ -133,7 +133,7 @@ export async function onSaveSearch({
<SavedObjectSaveModal
onSave={onSave}
onClose={() => {}}
title={savedSearch.title}
title={savedSearch.title ?? ''}
showCopyOnSave={!!savedSearch.id}
objectType={i18n.translate('discover.localMenu.saveSaveSearchObjectType', {
defaultMessage: 'search',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import React, { useEffect, useState, memo } from 'react';
import { History } from 'history';
import { useParams } from 'react-router-dom';
import type { SavedObject as SavedObjectDeprecated } from 'src/plugins/saved_objects/public';
import { IndexPatternAttributes, SavedObject } from 'src/plugins/data/common';
import { IndexPatternAttributes, ISearchSource, SavedObject } from 'src/plugins/data/common';
import { DiscoverServices } from '../../../build_services';
import { SavedSearch } from '../../../saved_searches';
import { SavedSearch, getSavedSearch, getSavedSearchFullPathUrl } from '../../../saved_searches';
import { getState } from './services/discover_state';
import { loadIndexPattern, resolveIndexPattern } from './utils/resolve_index_pattern';
import { DiscoverMainApp } from './discover_main_app';
Expand Down Expand Up @@ -58,36 +57,44 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) {
useEffect(() => {
const savedSearchId = id;

async function loadDefaultOrCurrentIndexPattern(usedSavedSearch: SavedSearch) {
async function loadDefaultOrCurrentIndexPattern(searchSource: ISearchSource) {
await data.indexPatterns.ensureDefaultDataView();
const { appStateContainer } = getState({ history, uiSettings: config });
const { index } = appStateContainer.getState();
const ip = await loadIndexPattern(index || '', data.indexPatterns, config);
const ipList = ip.list as Array<SavedObject<IndexPatternAttributes>>;
const indexPatternData = await resolveIndexPattern(
ip,
usedSavedSearch.searchSource,
toastNotifications
);
const indexPatternData = await resolveIndexPattern(ip, searchSource, toastNotifications);

setIndexPatternList(ipList);

return indexPatternData;
}

async function loadSavedSearch() {
try {
// force a refresh if a given saved search without id was saved
setSavedSearch(undefined);
const loadedSavedSearch = await services.getSavedSearchById(savedSearchId);
const loadedIndexPattern = await loadDefaultOrCurrentIndexPattern(loadedSavedSearch);
if (loadedSavedSearch && !loadedSavedSearch?.searchSource.getField('index')) {
loadedSavedSearch.searchSource.setField('index', loadedIndexPattern);

const currentSavedSearch = await getSavedSearch(savedSearchId, {
search: services.data.search,
savedObjectsClient: core.savedObjects.client,
});

const loadedIndexPattern = await loadDefaultOrCurrentIndexPattern(
currentSavedSearch.searchSource
);

if (!currentSavedSearch.searchSource.getField('index')) {
currentSavedSearch.searchSource.setField('index', loadedIndexPattern);
}
setSavedSearch(loadedSavedSearch);
if (savedSearchId) {

setSavedSearch(currentSavedSearch);

if (currentSavedSearch.id) {
chrome.recentlyAccessed.add(
(loadedSavedSearch as unknown as SavedObjectDeprecated).getFullPath(),
loadedSavedSearch.title,
loadedSavedSearch.id
getSavedSearchFullPathUrl(currentSavedSearch.id),
currentSavedSearch.title ?? '',
currentSavedSearch.id
);
}
} catch (e) {
Expand All @@ -112,6 +119,7 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) {

loadSavedSearch();
}, [
core.savedObjects.client,
basePath,
chrome.recentlyAccessed,
config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from './discover_state';
import { createBrowserHistory, History } from 'history';
import { dataPluginMock } from '../../../../../../data/public/mocks';
import { SavedSearch } from '../../../../saved_searches';
import type { SavedSearch } from '../../../../saved_searches';
import { SEARCH_FIELDS_FROM_SOURCE } from '../../../../../common';

let history: History;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { History } from 'history';
import { getState } from './discover_state';
import { getStateDefaults } from '../utils/get_state_defaults';
import { DiscoverServices } from '../../../../build_services';
import { SavedSearch } from '../../../../saved_searches';
import { SavedSearch, getSavedSearch } from '../../../../saved_searches';
import { loadIndexPattern } from '../utils/resolve_index_pattern';
import { useSavedSearch as useSavedSearchData } from './use_saved_search';
import {
Expand Down Expand Up @@ -147,7 +147,11 @@ export function useDiscoverState({
*/
const resetSavedSearch = useCallback(
async (id?: string) => {
const newSavedSearch = await services.getSavedSearchById(id);
const newSavedSearch = await getSavedSearch(id, {
search: services.data.search,
savedObjectsClient: services.core.savedObjects.client,
});

const newIndexPattern = newSavedSearch.searchSource.getField('index') || indexPattern;
newSavedSearch.searchSource.setField('index', newIndexPattern);
const newAppState = getStateDefaults({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useCallback, useEffect, useMemo, useRef } from 'react';
import { BehaviorSubject, Subject } from 'rxjs';
import { DiscoverServices } from '../../../../build_services';
import { DiscoverSearchSessionManager } from './discover_search_session';
import { SearchSource } from '../../../../../../data/common';
import { ISearchSource } from '../../../../../../data/common';
import { GetStateReturn } from './discover_state';
import { ElasticSearchHit } from '../../../doc_views/doc_views_types';
import { RequestAdapter } from '../../../../../../inspector/public';
Expand Down Expand Up @@ -91,7 +91,7 @@ export const useSavedSearch = ({
}: {
initialFetchStatus: FetchStatus;
searchSessionManager: DiscoverSearchSessionManager;
searchSource: SearchSource;
searchSource: ISearchSource;
services: DiscoverServices;
stateContainer: GetStateReturn;
useNewFieldsApi: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
sendResetMsg,
} from '../services/use_saved_search_messages';
import { updateSearchSource } from './update_search_source';
import { SortOrder } from '../../../../saved_searches/types';
import type { SortOrder } from '../../../../saved_searches';
import { fetchDocuments } from './fetch_documents';
import { fetchTotalHits } from './fetch_total_hits';
import { fetchChart } from './fetch_chart';
import { SearchSource } from '../../../../../../data/common';
import { ISearchSource } from '../../../../../../data/common';
import { Adapters } from '../../../../../../inspector';
import { AppState } from '../services/discover_state';
import { FetchStatus } from '../../../types';
Expand All @@ -29,7 +29,7 @@ import { ReduxLikeStateContainer } from '../../../../../../kibana_utils/common';

export function fetchAll(
dataSubjects: SavedSearchData,
searchSource: SearchSource,
searchSource: ISearchSource,
reset = false,
fetchDeps: {
abortController: AbortController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
DataPublicPluginStart,
isCompleteResponse,
search,
SearchSource,
ISearchSource,
} from '../../../../../../data/public';
import { Adapters } from '../../../../../../inspector';
import { getChartAggConfigs, getDimensions } from './index';
Expand All @@ -25,7 +25,7 @@ import { sendErrorMsg, sendLoadingMsg } from '../services/use_saved_search_messa

export function fetchChart(
data$: SavedSearchData,
searchSource: SearchSource,
searchSource: ISearchSource,
{
abortController,
appStateContainer,
Expand Down Expand Up @@ -114,7 +114,7 @@ export function fetchChart(
}

export function updateSearchSource(
searchSource: SearchSource,
searchSource: ISearchSource,
interval: string,
data: DataPublicPluginStart
) {
Expand Down
Loading