Skip to content

Commit

Permalink
[Discover] Step 2 - remove SavedObjectLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Sep 30, 2021
1 parent 6eebf24 commit 0aa4101
Show file tree
Hide file tree
Showing 62 changed files with 622 additions and 261 deletions.
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,7 +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';
const SidebarMemoized = React.memo(DiscoverSidebarResponsive);
const TopNavMemoized = React.memo(DiscoverTopNav);
const DiscoverChartMemoized = React.memo(DiscoverChart);
Expand Down Expand Up @@ -74,6 +77,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 @@ -162,6 +167,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,10 @@ export async function onSaveSearch({
}) => {
const currentTitle = savedSearch.title;
savedSearch.title = newTitle;
savedSearch.copyOnSave = newCopyOnSave;
const saveOptions = {
const saveOptions: SaveSavedSearchOptions = {
confirmOverwrite: false,
isTitleDuplicateConfirmed,
onTitleDuplicate,
copyOnSave: newCopyOnSave,
};
const response = await saveDataSource({
indexPattern,
Expand All @@ -133,7 +134,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,16 +147,22 @@ export function useDiscoverState({
*/
const resetSavedSearch = useCallback(
async (id?: string) => {
const newSavedSearch = await services.getSavedSearchById(id);
const newIndexPattern = newSavedSearch.searchSource.getField('index') || indexPattern;
newSavedSearch.searchSource.setField('index', newIndexPattern);
const newAppState = getStateDefaults({
config,
data,
savedSearch: newSavedSearch,
});
await stateContainer.replaceUrlAppState(newAppState);
setState(newAppState);
if (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({
config,
data,
savedSearch: newSavedSearch,
});
await stateContainer.replaceUrlAppState(newAppState);
setState(newAppState);
}
},
[indexPattern, services, config, data, stateContainer]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { BehaviorSubject, merge, Subject } from 'rxjs';
import { debounceTime, filter, tap } from 'rxjs/operators';
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
Loading

0 comments on commit 0aa4101

Please sign in to comment.