mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
/*
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
*/
|
|
|
|
import { IndexPatternAttributes } from 'src/plugins/data/public';
|
|
|
|
import { API_ROUTE } from '../../common/lib/constants';
|
|
// @ts-ignore untyped local
|
|
import { fetch } from '../../common/lib/fetch';
|
|
import { ErrorStrings } from '../../i18n';
|
|
import { notifyService } from '../services';
|
|
import { getCoreStart } from '../legacy';
|
|
|
|
const { esService: strings } = ErrorStrings;
|
|
|
|
const getApiPath = function() {
|
|
const basePath = getCoreStart().http.basePath.get();
|
|
return basePath + API_ROUTE;
|
|
};
|
|
|
|
const getSavedObjectsClient = function() {
|
|
return getCoreStart().savedObjects.client;
|
|
};
|
|
|
|
const getAdvancedSettings = function() {
|
|
return getCoreStart().uiSettings;
|
|
};
|
|
|
|
export const getFields = (index = '_all') => {
|
|
return fetch
|
|
.get(`${getApiPath()}/es_fields?index=${index}`)
|
|
.then(({ data: mapping }: { data: object }) =>
|
|
Object.keys(mapping)
|
|
.filter(field => !field.startsWith('_')) // filters out meta fields
|
|
.sort()
|
|
)
|
|
.catch((err: Error) =>
|
|
notifyService.getService().error(err, {
|
|
title: strings.getFieldsFetchErrorMessage(index),
|
|
})
|
|
);
|
|
};
|
|
|
|
export const getIndices = () =>
|
|
getSavedObjectsClient()
|
|
.find<IndexPatternAttributes>({
|
|
type: 'index-pattern',
|
|
fields: ['title'],
|
|
searchFields: ['title'],
|
|
perPage: 1000,
|
|
})
|
|
.then(resp => {
|
|
return resp.savedObjects.map(savedObject => {
|
|
return savedObject.attributes.title;
|
|
});
|
|
})
|
|
.catch((err: Error) =>
|
|
notifyService.getService().error(err, { title: strings.getIndicesFetchErrorMessage() })
|
|
);
|
|
|
|
export const getDefaultIndex = () => {
|
|
const defaultIndexId = getAdvancedSettings().get('defaultIndex');
|
|
|
|
return defaultIndexId
|
|
? getSavedObjectsClient()
|
|
.get<IndexPatternAttributes>('index-pattern', defaultIndexId)
|
|
.then(defaultIndex => defaultIndex.attributes.title)
|
|
.catch(err =>
|
|
notifyService
|
|
.getService()
|
|
.error(err, { title: strings.getDefaultIndexFetchErrorMessage() })
|
|
)
|
|
: Promise.resolve('');
|
|
};
|