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

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
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;
const cachedPromises = {};

const getIds = function () {
if (cachedPromise) {
const get = function (field) {
if (cachedPromises[field]) {
// return a clone of the cached response
return cachedPromise.then(function (cachedResp) {
return cachedPromises[field].then(function (cachedResp) {
return _.clone(cachedResp);
});
}

cachedPromise = savedObjectsClient.find({
cachedPromises[field] = 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));
});

// ensure that the response stays pristine by cloning it here too
return cachedPromise.then(function (resp) {
return cachedPromises[field].then(function (resp) {
return _.clone(resp);
});
};

getIds.clearCache = function () {
cachedPromise = null;
return (field) => {
const getter = get.bind(get, field);
getter.clearCache = function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the IndexPatternsProvider would clear the complete cache whenever it was invalidated. With this change, only the part of the cache used by the respective user is cleared. This seems like a dangerous way to end up with everything sitting in memory forever.

I wonder if we should only cache IDs, and simply fetch other things on demand?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup great point. I'm honestly not sure if caching will provide much benefit as it's not clear how consumers will use this for non-ids, but it made sense to me (at the time) to keep the behavior consistent. The previous callers to clearCache were only clearing cache around ids so it felt odd to clear the entire cache, but that might be the right call, or as you eluded to, maybe just not bother caching non-ids requests entirely.

I'm open to changing it.

delete cachedPromises[field];
};
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