From eb9f8a504063aa8cd5c2389df0689ee5a71b2d05 Mon Sep 17 00:00:00 2001 From: Chris Roberson Date: Mon, 24 Jul 2017 15:52:12 -0400 Subject: [PATCH] [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 --- .../index_patterns/{_get_ids.js => _get.js} | 32 ++++++++++++------- .../public/index_patterns/_index_pattern.js | 4 +-- .../public/index_patterns/index_patterns.js | 6 ++-- .../route_setup/load_default.js | 4 +-- 4 files changed, 28 insertions(+), 18 deletions(-) rename src/ui/public/index_patterns/{_get_ids.js => _get.js} (50%) diff --git a/src/ui/public/index_patterns/_get_ids.js b/src/ui/public/index_patterns/_get.js similarity index 50% rename from src/ui/public/index_patterns/_get_ids.js rename to src/ui/public/index_patterns/_get.js index 1d149baf93020..6be923a35e6d3 100644 --- a/src/ui/public/index_patterns/_get_ids.js +++ b/src/ui/public/index_patterns/_get.js @@ -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; } diff --git a/src/ui/public/index_patterns/_index_pattern.js b/src/ui/public/index_patterns/_index_pattern.js index 3d59d195e463d..0b75052b22ea4 100644 --- a/src/ui/public/index_patterns/_index_pattern.js +++ b/src/ui/public/index_patterns/_index_pattern.js @@ -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); diff --git a/src/ui/public/index_patterns/index_patterns.js b/src/ui/public/index_patterns/index_patterns.js index f63afa668b62c..dd9c5946307ba 100644 --- a/src/ui/public/index_patterns/index_patterns.js +++ b/src/ui/public/index_patterns/index_patterns.js @@ -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); diff --git a/src/ui/public/index_patterns/route_setup/load_default.js b/src/ui/public/index_patterns/route_setup/load_default.js index e3cee5ce44cd5..757cff657e127 100644 --- a/src/ui/public/index_patterns/route_setup/load_default.js +++ b/src/ui/public/index_patterns/route_setup/load_default.js @@ -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()