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

[visualizations] get index pattern via service instead of saved object #84458

Merged
Merged
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
32 changes: 7 additions & 25 deletions src/plugins/visualizations/public/embeddable/get_index_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,19 @@
*/

import { VisSavedObject } from '../types';
import {
indexPatterns,
IIndexPattern,
IndexPatternAttributes,
} from '../../../../plugins/data/public';
import { getUISettings, getSavedObjects } from '../services';
import type { IndexPattern } from '../../../../plugins/data/public';
import { getIndexPatterns } from '../services';

export async function getIndexPattern(
savedVis: VisSavedObject
): Promise<IIndexPattern | undefined> {
): Promise<IndexPattern | undefined | null> {
if (savedVis.visState.type !== 'metrics') {
return savedVis.searchSource!.getField('index');
}

const savedObjectsClient = getSavedObjects().client;
const defaultIndex = getUISettings().get('defaultIndex');
const indexPatternsClient = getIndexPatterns();

if (savedVis.visState.params.index_pattern) {
const indexPatternObjects = await savedObjectsClient.find<IndexPatternAttributes>({
type: 'index-pattern',
fields: ['title', 'fields'],
search: `"${savedVis.visState.params.index_pattern}"`,
searchFields: ['title'],
});
const [indexPattern] = indexPatternObjects.savedObjects.map(indexPatterns.getFromSavedObject);
return indexPattern;
}

const savedObject = await savedObjectsClient.get<IndexPatternAttributes>(
'index-pattern',
defaultIndex
);
return indexPatterns.getFromSavedObject(savedObject);
return savedVis.visState.params.index_pattern
? (await indexPatternsClient.find(`"${savedVis.visState.params.index_pattern}"`))[0]
: await indexPatternsClient.getDefault();
}