[Management] Provide a way to fetch index pattern titles (#13030)

* Refactor how `get_ids` works to enable fetching other properties of index patterns, specifically the `title` or name

* Remove unnecessary lines

* Only cache for `id` calls
This commit is contained in:
Chris Roberson 2017-07-24 15:52:12 -04:00 committed by GitHub
parent e676668967
commit 89c745660c
4 changed files with 28 additions and 18 deletions

View file

@ -1,38 +1,46 @@
import _ from 'lodash';
import { SavedObjectsClientProvider } from 'ui/saved_objects';
export function IndexPatternsGetIdsProvider(Private) {
export function IndexPatternsGetProvider(Private) {
const savedObjectsClient = Private(SavedObjectsClientProvider);
// many places may require the id list, so we will cache it separately
// didn't incorporate with the indexPattern cache to prevent id collisions.
let cachedPromise;
let cachedIdPromise;
const getIds = function () {
if (cachedPromise) {
const get = function (field) {
if (field === 'id' && cachedIdPromise) {
// return a clone of the cached response
return cachedPromise.then(function (cachedResp) {
return cachedIdPromise.then(function (cachedResp) {
return _.clone(cachedResp);
});
}
cachedPromise = savedObjectsClient.find({
const promise = savedObjectsClient.find({
type: 'index-pattern',
fields: [],
perPage: 10000
}).then(resp => {
return resp.savedObjects.map(obj => obj.id);
return resp.savedObjects.map(obj => _.get(obj, field));
});
if (field === 'id') {
cachedIdPromise = promise;
}
// ensure that the response stays pristine by cloning it here too
return cachedPromise.then(function (resp) {
return promise.then(function (resp) {
return _.clone(resp);
});
};
getIds.clearCache = function () {
cachedPromise = null;
return (field) => {
const getter = get.bind(get, field);
if (field === 'id') {
getter.clearCache = function () {
cachedIdPromise = null;
};
}
return getter;
};
return getIds;
}

View file

@ -7,7 +7,7 @@ import { Notifier } from 'ui/notify';
import { getComputedFields } from './_get_computed_fields';
import { formatHit } from './_format_hit';
import { IndexPatternsGetIdsProvider } from './_get_ids';
import { IndexPatternsGetProvider } from './_get';
import { IndexPatternsIntervalsProvider } from './_intervals';
import { IndexPatternsFieldListProvider } from './_field_list';
import { IndexPatternsFlattenHitProvider } from './_flatten_hit';
@ -29,7 +29,7 @@ export function getRoutes() {
export function IndexPatternProvider(Private, $http, config, kbnIndex, Promise, confirmModalPromise, kbnUrl) {
const fieldformats = Private(RegistryFieldFormatsProvider);
const getConfig = (...args) => config.get(...args);
const getIds = Private(IndexPatternsGetIdsProvider);
const getIds = Private(IndexPatternsGetProvider)('id');
const fieldsFetcher = Private(FieldsFetcherProvider);
const intervals = Private(IndexPatternsIntervalsProvider);
const mappingSetup = Private(UtilsMappingSetupProvider);

View file

@ -2,7 +2,7 @@ import 'ui/filters/short_dots';
import { IndexPatternMissingIndices } from 'ui/errors';
import { IndexPatternProvider } from 'ui/index_patterns/_index_pattern';
import { IndexPatternsPatternCacheProvider } from 'ui/index_patterns/_pattern_cache';
import { IndexPatternsGetIdsProvider } from 'ui/index_patterns/_get_ids';
import { IndexPatternsGetProvider } from 'ui/index_patterns/_get';
import { IndexPatternsIntervalsProvider } from 'ui/index_patterns/_intervals';
import { FieldsFetcherProvider } from './fields_fetcher_provider';
import { RegistryFieldFormatsProvider } from 'ui/registry/field_formats';
@ -16,6 +16,7 @@ export function IndexPatternsProvider(Notifier, Private) {
const IndexPattern = Private(IndexPatternProvider);
const patternCache = Private(IndexPatternsPatternCacheProvider);
const getProvider = Private(IndexPatternsGetProvider);
self.get = function (id) {
if (!id) return self.make();
@ -38,7 +39,8 @@ export function IndexPatternsProvider(Notifier, Private) {
};
self.cache = patternCache;
self.getIds = Private(IndexPatternsGetIdsProvider);
self.getIds = getProvider('id');
self.getTitles = getProvider('attributes.title');
self.intervals = Private(IndexPatternsIntervalsProvider);
self.fieldsFetcher = Private(FieldsFetcherProvider);
self.fieldFormats = Private(RegistryFieldFormatsProvider);

View file

@ -1,7 +1,7 @@
import _ from 'lodash';
import { Notifier } from 'ui/notify/notifier';
import { NoDefaultIndexPattern } from 'ui/errors';
import { IndexPatternsGetIdsProvider } from '../_get_ids';
import { IndexPatternsGetProvider } from '../_get';
import uiRoutes from 'ui/routes';
const notify = new Notifier({
location: 'Index Patterns'
@ -15,7 +15,7 @@ export default function (opts) {
uiRoutes
.addSetupWork(function loadDefaultIndexPattern(Private, Promise, $route, config) {
const getIds = Private(IndexPatternsGetIdsProvider);
const getIds = Private(IndexPatternsGetProvider)('id');
const route = _.get($route, 'current.$$route');
return getIds()