Skip to content

Commit c511ebd

Browse files
committed
cleaning up discover so it doesnt put spec on its state
1 parent c586664 commit c511ebd

7 files changed

Lines changed: 23 additions & 42 deletions

File tree

src/plugins/data_views/common/data_views/data_views.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,9 @@ export class DataViewsService {
399399
* Clear index pattern list cache.
400400
* @param id optionally clear a single id
401401
*/
402-
clearCache = (id?: string) => {
402+
clearCache = (id?: boolean) => {
403403
this.savedObjectsCache = null;
404404
if (id) {
405-
this.dataViewCache.clear(id);
406-
} else {
407405
this.dataViewCache.clearAll();
408406
}
409407
};

src/plugins/discover/public/application/main/discover_main_route.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export function DiscoverMainRoute(props: Props) {
7777
const loadDefaultOrCurrentIndexPattern = useCallback(
7878
async (searchSource: ISearchSource) => {
7979
try {
80+
debugger;
81+
8082
const hasUserDataViewValue = await data.dataViews.hasData
8183
.hasUserDataView()
8284
.catch(() => false);

src/plugins/discover/public/application/main/hooks/use_discover_state.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,8 @@ export function useDiscoverState({
135135
* That's because appState is updated before savedSearchData$
136136
* The following line of code catches this, but should be improved
137137
*/
138-
if (typeof nextState.index === 'string') {
139-
const nextIndexPattern = await loadIndexPattern(nextState.index, indexPatterns, config);
140-
savedSearch.searchSource.setField('index', nextIndexPattern.loaded);
141-
} else {
142-
const { runtimeFieldMap, ...nextStateIndex } = nextState.index;
143-
const nextIndexPattern = await services.data.dataViews.create(nextStateIndex);
144-
savedSearch.searchSource.setField('index', nextIndexPattern);
145-
}
138+
const nextIndexPattern = await loadIndexPattern(nextState.index, indexPatterns, config);
139+
savedSearch.searchSource.setField('index', nextIndexPattern.loaded);
146140

147141
reset();
148142
}
@@ -163,7 +157,6 @@ export function useDiscoverState({
163157
data$,
164158
reset,
165159
savedSearch.searchSource,
166-
services.data.dataViews,
167160
]);
168161

169162
/**

src/plugins/discover/public/application/main/services/discover_state.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
SearchSessionInfoProvider,
3434
syncQueryStateWithUrl,
3535
} from '@kbn/data-plugin/public';
36-
import { DataView, DataViewSpec } from '@kbn/data-views-plugin/public';
36+
import { DataView } from '@kbn/data-views-plugin/public';
3737
import { DiscoverGridSettings } from '../../../components/discover_grid/types';
3838
import { SavedSearch } from '../../../services/saved_searches';
3939
import { handleSourceColumnState } from '../../../utils/state_helpers';
@@ -61,7 +61,7 @@ export interface AppState {
6161
/**
6262
* id of the used index pattern
6363
*/
64-
index?: string | DataViewSpec;
64+
index?: string;
6565
/**
6666
* Used interval of the histogram
6767
*/
@@ -260,10 +260,7 @@ export function getState({
260260
filterManager: FilterManager,
261261
data: DataPublicPluginStart
262262
) => {
263-
const dataViewId = !isDataViewSpec(appStateContainer.getState().index)
264-
? appStateContainer.getState().index
265-
: '';
266-
if (dataViewId && dataViewId !== indexPattern.id) {
263+
if (appStateContainer.getState().index !== indexPattern.id) {
267264
// used index pattern is different than the given by url/state which is invalid
268265
setState(appStateContainerModified, { index: indexPattern.id });
269266
}
@@ -423,7 +420,3 @@ function createUrlGeneratorState({
423420
hideAggregatedPreview: appState.hideAggregatedPreview,
424421
};
425422
}
426-
427-
function isDataViewSpec(dataView: string | DataViewSpec | undefined): dataView is DataViewSpec {
428-
return Boolean(dataView) && typeof dataView !== 'string';
429-
}

src/plugins/discover/public/application/main/utils/get_switch_index_pattern_app_state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function getSwitchIndexPatternAppState(
5252
}
5353

5454
return {
55-
index: nextIndexPattern.isPersisted() ? nextIndexPattern.id : nextIndexPattern.toSpec(),
55+
index: nextIndexPattern.id,
5656
columns,
5757
sort: nextSort,
5858
};

src/plugins/discover/public/application/main/utils/resolve_index_pattern.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { i18n } from '@kbn/i18n';
10-
import type { DataView, DataViewsContract, DataViewSpec } from '@kbn/data-views-plugin/public';
10+
import type { DataView, DataViewsContract } from '@kbn/data-views-plugin/public';
1111
import type { ISearchSource } from '@kbn/data-plugin/public';
1212
import type { IUiSettingsClient, SavedObject, ToastsStart } from '@kbn/core/public';
1313
export type IndexPatternSavedObject = SavedObject & { title: string };
@@ -75,27 +75,23 @@ export function getIndexPatternId(
7575
* Function to load the given index pattern by id, providing a fallback if it doesn't exist
7676
*/
7777
export async function loadIndexPattern(
78-
id: string | DataViewSpec,
79-
dataViews: DataViewsContract,
78+
id: string,
79+
indexPatterns: DataViewsContract,
8080
config: IUiSettingsClient
8181
): Promise<IndexPatternData> {
82-
const indexPatternList = (await dataViews.getCache()) as unknown as IndexPatternSavedObject[];
83-
84-
if (typeof id === 'string') {
85-
const actualId = getIndexPatternId(id, indexPatternList, config.get('defaultIndex'));
86-
return {
87-
list: indexPatternList || [],
88-
loaded: await dataViews.get(actualId),
89-
stateVal: id,
90-
stateValFound: !!id && actualId === id,
91-
};
82+
let dataView;
83+
try {
84+
dataView = await indexPatterns.get(id);
85+
} catch (e) {
86+
dataView = await config.get('defaultIndex');
9287
}
93-
const dataView = await dataViews.create(id);
88+
const indexPatternList = (await indexPatterns.getCache()) as unknown as IndexPatternSavedObject[];
89+
9490
return {
9591
list: indexPatternList || [],
9692
loaded: dataView,
97-
stateVal: dataView.id!,
98-
stateValFound: true,
93+
stateVal: id,
94+
stateValFound: !!id && dataView.id === id,
9995
};
10096
}
10197

src/plugins/discover/public/locator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import type { SerializableRecord } from '@kbn/utility-types';
1010
import type { Filter, TimeRange, Query } from '@kbn/es-query';
11-
import { DataViewSpec } from '@kbn/data-views-plugin/common';
1211
import type { GlobalQueryStateFromUrl, RefreshInterval } from '@kbn/data-plugin/public';
1312
import type { LocatorDefinition, LocatorPublic } from '@kbn/share-plugin/public';
1413
import { setStateToKbnUrl } from '@kbn/kibana-utils-plugin/public';
@@ -25,7 +24,7 @@ export interface DiscoverAppLocatorParams extends SerializableRecord {
2524
/**
2625
* Optionally set index pattern ID.
2726
*/
28-
indexPatternId?: string | DataViewSpec;
27+
indexPatternId?: string;
2928

3029
/**
3130
* Optionally set the time range in the time picker.
@@ -119,7 +118,7 @@ export class DiscoverAppLocatorDefinition implements LocatorDefinition<DiscoverA
119118
const appState: {
120119
query?: Query;
121120
filters?: Filter[];
122-
index?: string | DataViewSpec;
121+
index?: string;
123122
columns?: string[];
124123
interval?: string;
125124
sort?: string[][];

0 commit comments

Comments
 (0)