Skip to content

Commit

Permalink
[Management] Provide a way to fetch index pattern titles (elastic#13030)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
chrisronline committed Jul 24, 2017
1 parent 811e692 commit eb9f8a5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 2 additions & 2 deletions src/ui/public/index_patterns/_index_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/ui/public/index_patterns/index_patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/ui/public/index_patterns/route_setup/load_default.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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()
Expand Down

0 comments on commit eb9f8a5

Please sign in to comment.