Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,26 @@ export const createEnsureDefaultIndexPattern = (core: CoreStart) => {
let timeoutId: NodeJS.Timeout | undefined;

/**
* Checks whether a default index pattern is set and exists and defines
* one otherwise.
* Checks whether a default index pattern is set and exists.
* If this is not the case returns the first index pattern of the existing index patterns
*
* If there are no index patterns, redirect to management page and show
* banner. In this case the promise returned from this function will never
* resolve to wait for the URL change to happen.
*/
return async function ensureDefaultIndexPattern(this: IndexPatternsContract, history: History) {
return async function ensureDefaultIndexPattern(this: IndexPatternsContract) {
const patterns = await this.getIds();
let defaultId = core.uiSettings.get('defaultIndex');
let defined = !!defaultId;
const defaultId = core.uiSettings.get('defaultIndex');
const defined = !!defaultId;
const exists = contains(patterns, defaultId);

if (defined && !exists) {
core.uiSettings.remove('defaultIndex');
defaultId = defined = false;
}

if (defined) {
return;
if (defined && exists) {
return defaultId;
}

// If there is any index pattern created, set the first as default
if (patterns.length >= 1) {
defaultId = patterns[0];
core.uiSettings.set('defaultIndex', defaultId);
return patterns[0];
} else {
const canManageIndexPatterns = core.application.capabilities.management.kibana.index_patterns;
const redirectTarget = canManageIndexPatterns ? '/management/kibana/indexPatterns' : '/home';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,29 @@ export class IndexPatternsService {
}
return this.savedObjectsCache;
};

/**
* Returns the default index pattern, configured in index pattern management.
* In case no index pattern is configured or the configured default index pattern doesn't exist
* the first index pattern of the cached list is returned
*/
getDefault = async () => {
const getFallbackIndexPattern = async () => {
const list = await this.getCache();
if (list && list.length) {
return await this.get(list[0].id);
}
return null;
};
const defaultIndexPatternId = this.config.get('defaultIndex');
if (defaultIndexPatternId) {
return await this.get(defaultIndexPatternId);
if (!defaultIndexPatternId) {
return await getFallbackIndexPattern();
}

return null;
try {
return await this.get(defaultIndexPatternId);
} catch (e) {
return await getFallbackIndexPattern();
}
};

get = async (id: string): Promise<IndexPattern> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { fetchIndexPatternFields } from './lib/fetch_fields';
import { getSavedObjectsClient, getUISettings, getI18n } from '../services';
import { getUISettings, getI18n, getDataStart } from '../services';
import { VisEditor } from './components/vis_editor_lazy';

export class EditorController {
Expand All @@ -37,17 +37,8 @@ export class EditorController {
};
}

fetchDefaultIndexPattern = async () => {
const indexPattern = await getSavedObjectsClient().client.get(
'index-pattern',
getUISettings().get('defaultIndex')
);

return indexPattern.attributes;
};

fetchDefaultParams = async () => {
const { title, timeFieldName } = await this.fetchDefaultIndexPattern();
const { title, timeFieldName } = await getDataStart().indexPatterns.getDefault();

this.state.vis.params.default_index_pattern = title;
this.state.vis.params.default_timefield = timeFieldName;
Expand Down