From 9ee3dac601dbfcf13f0411b916e85b29870e8ce9 Mon Sep 17 00:00:00 2001 From: Luke Elmers Date: Thu, 16 Apr 2020 17:44:37 -0600 Subject: [PATCH] Make sure IIndexPattern doesn't allow arbitrary properties. --- .../indexed_fields_table.test.tsx | 4 +-- .../indexed_fields_table.tsx | 6 ++-- .../confirmation_modal.test.tsx | 2 +- .../components/table/table.test.tsx | 2 +- .../components/table/table.tsx | 11 ++++--- .../scripted_field_table.test.tsx | 23 +++++++------- .../scripted_fields_table.tsx | 12 ++++---- .../scripted_fields_table/types.ts | 13 ++++---- .../components/table/table.test.tsx | 6 ++-- .../components/table/table.tsx | 4 +-- .../source_filters_table.test.tsx | 4 +-- .../source_filters_table.tsx | 4 +-- .../data/common/index_patterns/types.ts | 5 +++- .../index_patterns/_pattern_cache.ts | 4 ++- .../index_patterns/index_pattern.test.ts | 6 ++-- .../index_patterns/index_pattern.ts | 30 ++++++++++++------- .../index_patterns/index_patterns.ts | 24 +++++++-------- 17 files changed, 88 insertions(+), 72 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/indexed_fields_table/indexed_fields_table.test.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/indexed_fields_table/indexed_fields_table.test.tsx index f8b78a92e098e..53d9c18415ef4 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/indexed_fields_table/indexed_fields_table.test.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/indexed_fields_table/indexed_fields_table.test.tsx @@ -19,7 +19,7 @@ import React from 'react'; import { shallow } from 'enzyme'; -import { IndexPatternField, IIndexPattern } from '../../../../../../../../../plugins/data/public'; +import { IndexPatternField, IndexPattern } from '../../../../../../../../../plugins/data/public'; import { IndexedFieldsTable } from './indexed_fields_table'; jest.mock('@elastic/eui', () => ({ @@ -54,7 +54,7 @@ const fields = [ const indexPattern = ({ getNonScriptedFields: () => fields, -} as unknown) as IIndexPattern; +} as unknown) as IndexPattern; describe('IndexedFieldsTable', () => { test('should render normally', async () => { diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/indexed_fields_table/indexed_fields_table.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/indexed_fields_table/indexed_fields_table.tsx index 7c2bb565615d7..9f47b87110375 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/indexed_fields_table/indexed_fields_table.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/indexed_fields_table/indexed_fields_table.tsx @@ -19,19 +19,19 @@ import React, { Component } from 'react'; import { createSelector } from 'reselect'; -import { IndexPatternField, IIndexPattern } from '../../../../../../../../../plugins/data/public'; +import { IndexPatternField, IndexPattern } from '../../../../../../../../../plugins/data/public'; import { Table } from './components/table'; import { getFieldFormat } from './lib'; import { IndexedFieldItem } from './types'; interface IndexedFieldsTableProps { fields: IndexPatternField[]; - indexPattern: IIndexPattern; + indexPattern: IndexPattern; fieldFilter?: string; indexedFieldTypeFilter?: string; helpers: { redirectToRoute: (obj: any) => void; - getFieldInfo: (indexPattern: IIndexPattern, field: string) => string[]; + getFieldInfo: (indexPattern: IndexPattern, field: string) => string[]; }; fieldWildcardMatcher: (filters: any[]) => (val: any) => boolean; } diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/confirmation_modal/confirmation_modal.test.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/confirmation_modal/confirmation_modal.test.tsx index f3594e7507a6a..dfe2a1dbdd97d 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/confirmation_modal/confirmation_modal.test.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/confirmation_modal/confirmation_modal.test.tsx @@ -26,7 +26,7 @@ describe('DeleteScritpedFieldConfirmationModal', () => { test('should render normally', () => { const component = shallow( {}} hideDeleteConfirmationModal={() => {}} /> diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/table/table.test.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/table/table.test.tsx index 13b3875f58687..cbe2b81856e4a 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/table/table.test.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/table/table.test.tsx @@ -26,7 +26,7 @@ import { IIndexPattern } from '../../../../../../../../../../../plugins/data/pub const getIndexPatternMock = (mockedFields: any = {}) => ({ ...mockedFields } as IIndexPattern); -const items: ScriptedFieldItem[] = [{ name: '1', lang: 'Elastic', script: '' }]; +const items: ScriptedFieldItem[] = [{ name: '1', lang: 'Elastic', script: '', type: '' }]; describe('Table', () => { let indexPattern: IIndexPattern; diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/table/table.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/table/table.tsx index 14aed11b32203..b6773b7d78c7c 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/table/table.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/components/table/table.tsx @@ -22,14 +22,13 @@ import { get } from 'lodash'; import { i18n } from '@kbn/i18n'; import { EuiInMemoryTable, EuiBasicTableColumn } from '@elastic/eui'; -import { ScriptedFieldItem } from '../../types'; -import { IIndexPattern } from '../../../../../../../../../../../plugins/data/public'; +import { IIndexPattern, IFieldType } from '../../../../../../../../../../../plugins/data/public'; interface TableProps { indexPattern: IIndexPattern; - items: ScriptedFieldItem[]; - editField: (field: ScriptedFieldItem) => void; - deleteField: (field: ScriptedFieldItem) => void; + items: IFieldType[]; + editField: (field: IFieldType) => void; + deleteField: (field: IFieldType) => void; } export class Table extends PureComponent { @@ -43,7 +42,7 @@ export class Table extends PureComponent { render() { const { items, editField, deleteField } = this.props; - const columns: Array> = [ + const columns: Array> = [ { field: 'displayName', name: i18n.translate('kbn.management.editIndexPattern.scripted.table.nameHeader', { diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/scripted_field_table.test.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/scripted_field_table.test.tsx index 914d80f9f61d7..4beeebaad29f6 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/scripted_field_table.test.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/scripted_field_table.test.tsx @@ -21,7 +21,7 @@ import React from 'react'; import { shallow } from 'enzyme'; import { ScriptedFieldsTable } from '../scripted_fields_table'; -import { IIndexPattern } from '../../../../../../../../../plugins/data/common/index_patterns'; +import { IndexPattern } from '../../../../../../../../../plugins/data/public'; jest.mock('@elastic/eui', () => ({ EuiTitle: 'eui-title', @@ -64,10 +64,10 @@ const helpers = { getRouteHref: () => '#', }; -const getIndexPatternMock = (mockedFields: any = {}) => ({ ...mockedFields } as IIndexPattern); +const getIndexPatternMock = (mockedFields: any = {}) => ({ ...mockedFields } as IndexPattern); describe('ScriptedFieldsTable', () => { - let indexPattern: IIndexPattern; + let indexPattern: IndexPattern; beforeEach(() => { indexPattern = getIndexPatternMock({ @@ -156,7 +156,9 @@ describe('ScriptedFieldsTable', () => { ); await component.update(); // Fire `componentWillMount()` - component.instance().startDeleteField({ name: 'ScriptedField', lang: '', script: '' }); + component + .instance() + .startDeleteField({ name: 'ScriptedField', lang: '', script: '', type: '' }); await component.update(); // Ensure the modal is visible @@ -165,18 +167,15 @@ describe('ScriptedFieldsTable', () => { test('should delete a field', async () => { const removeScriptedField = jest.fn(); + const pattern = ({ ...indexPattern, removeScriptedField } as unknown) as IndexPattern; const component = shallow( - + ); await component.update(); // Fire `componentWillMount()` - component.instance().startDeleteField({ name: 'ScriptedField', lang: '', script: '' }); + component + .instance() + .startDeleteField({ name: 'ScriptedField', lang: '', script: '', type: '' }); await component.update(); await component.instance().deleteField(); diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/scripted_fields_table.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/scripted_fields_table.tsx index e8dfbd6496057..849daff5ea88c 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/scripted_fields_table.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/scripted_fields_table.tsx @@ -29,10 +29,10 @@ import { EuiSpacer } from '@elastic/eui'; import { Table, Header, CallOuts, DeleteScritpedFieldConfirmationModal } from './components'; import { ScriptedFieldItem } from './types'; -import { IIndexPattern } from '../../../../../../../../../plugins/data/public'; +import { IndexPattern } from '../../../../../../../../../plugins/data/public'; interface ScriptedFieldsTableProps { - indexPattern: IIndexPattern; + indexPattern: IndexPattern; fieldFilter?: string; scriptedFieldLanguageFilter?: string; helpers: { @@ -76,8 +76,8 @@ export class ScriptedFieldsTable extends Component< const supportedLangs = getSupportedScriptingLanguages(); for (const field of fields) { - const lang: string = field.lang; - if (deprecatedLangs.includes(lang) || !supportedLangs.includes(lang)) { + const lang = field.lang; + if (lang && (deprecatedLangs.includes(lang) || !supportedLangs.includes(lang))) { deprecatedLangsInUse.push(lang); } } @@ -125,7 +125,9 @@ export class ScriptedFieldsTable extends Component< const { indexPattern, onRemoveField } = this.props; const { fieldToDelete } = this.state; - indexPattern.removeScriptedField(fieldToDelete); + if (fieldToDelete) { + indexPattern.removeScriptedField(fieldToDelete); + } if (onRemoveField) { onRemoveField(); diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/types.ts b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/types.ts index c1227393c561f..3606bafae2705 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/types.ts +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/scripted_fields_table/types.ts @@ -17,9 +17,12 @@ * under the License. */ +import { IFieldType } from 'src/plugins/data/public'; + /** @internal **/ -export interface ScriptedFieldItem { - name: string; - lang: string; - script: string; -} +export type ScriptedFieldItem = IFieldType; +// export interface ScriptedFieldItem { +// name: string; +// lang: string; +// script: string; +// } diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/components/table/table.test.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/components/table/table.test.tsx index 4705ecd2d1685..1a95130a33e3a 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/components/table/table.test.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/components/table/table.test.tsx @@ -22,13 +22,13 @@ import { shallow, ShallowWrapper } from 'enzyme'; import { Table, TableProps, TableState } from './table'; import { EuiTableFieldDataColumnType, keyCodes } from '@elastic/eui'; -import { IIndexPattern } from '../../../../../../../../../../../plugins/data/public'; +import { IndexPattern } from '../../../../../../../../../../../plugins/data/public'; import { SourceFiltersTableFilter } from '../../types'; -const indexPattern = {} as IIndexPattern; +const indexPattern = {} as IndexPattern; const items: SourceFiltersTableFilter[] = [{ value: 'tim*', clientId: '' }]; -const getIndexPatternMock = (mockedFields: any = {}) => ({ ...mockedFields } as IIndexPattern); +const getIndexPatternMock = (mockedFields: any = {}) => ({ ...mockedFields } as IndexPattern); const getTableColumnRender = ( component: ShallowWrapper, diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/components/table/table.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/components/table/table.tsx index db2b74bbc9824..94611be1c15a9 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/components/table/table.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/components/table/table.tsx @@ -32,7 +32,7 @@ import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import { SourceFiltersTableFilter } from '../../types'; -import { IIndexPattern } from '../../../../../../../../../../../plugins/data/public'; +import { IndexPattern } from '../../../../../../../../../../../plugins/data/public'; const filterHeader = i18n.translate('kbn.management.editIndexPattern.source.table.filterHeader', { defaultMessage: 'Filter', @@ -69,7 +69,7 @@ const cancelAria = i18n.translate('kbn.management.editIndexPattern.source.table. }); export interface TableProps { - indexPattern: IIndexPattern; + indexPattern: IndexPattern; items: SourceFiltersTableFilter[]; deleteFilter: Function; fieldWildcardMatcher: Function; diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/source_filters_table.test.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/source_filters_table.test.tsx index 1b68dd13566d3..5616f5a5d1fb6 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/source_filters_table.test.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/source_filters_table.test.tsx @@ -21,7 +21,7 @@ import React from 'react'; import { shallow } from 'enzyme'; import { SourceFiltersTable } from './source_filters_table'; -import { IIndexPattern } from '../../../../../../../../../plugins/data/public'; +import { IndexPattern } from '../../../../../../../../../plugins/data/public'; jest.mock('@elastic/eui', () => ({ EuiButton: 'eui-button', @@ -52,7 +52,7 @@ const getIndexPatternMock = (mockedFields: any = {}) => ({ sourceFilters: [{ value: 'time*' }, { value: 'nam*' }, { value: 'age*' }], ...mockedFields, - } as IIndexPattern); + } as IndexPattern); describe('SourceFiltersTable', () => { test('should render normally', () => { diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/source_filters_table.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/source_filters_table.tsx index dcf8ae9e1323f..7169583e36e40 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/source_filters_table.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/edit_index_pattern/source_filters_table/source_filters_table.tsx @@ -22,11 +22,11 @@ import { createSelector } from 'reselect'; import { EuiSpacer } from '@elastic/eui'; import { AddFilter, Table, Header, DeleteFilterConfirmationModal } from './components'; -import { IIndexPattern } from '../../../../../../../../../plugins/data/public'; +import { IndexPattern } from '../../../../../../../../../plugins/data/public'; import { SourceFiltersTableFilter } from './types'; export interface SourceFiltersTableProps { - indexPattern: IIndexPattern; + indexPattern: IndexPattern; filterFilter: string; fieldWildcardMatcher: Function; onAddOrRemoveFilter?: Function; diff --git a/src/plugins/data/common/index_patterns/types.ts b/src/plugins/data/common/index_patterns/types.ts index 698edbf9cd6a8..3de4b96986684 100644 --- a/src/plugins/data/common/index_patterns/types.ts +++ b/src/plugins/data/common/index_patterns/types.ts @@ -20,7 +20,10 @@ import { IFieldType } from './fields'; export interface IIndexPattern { - [key: string]: any; + // TODO: IIndexPattern shouldn't include attributes. + // We need a dedicated type for index patterns inside a saved object + // as described in https://github.com/elastic/kibana/issues/54894 + attributes: any; fields: IFieldType[]; title: string; id?: string; diff --git a/src/plugins/data/public/index_patterns/index_patterns/_pattern_cache.ts b/src/plugins/data/public/index_patterns/index_patterns/_pattern_cache.ts index eb6c69b414316..f455f4cadc884 100644 --- a/src/plugins/data/public/index_patterns/index_patterns/_pattern_cache.ts +++ b/src/plugins/data/public/index_patterns/index_patterns/_pattern_cache.ts @@ -19,13 +19,15 @@ import { IndexPattern } from './index_pattern'; -interface PatternCache { +/** @internal */ +export interface PatternCache { get: (id: string) => IndexPattern; set: (id: string, value: IndexPattern) => IndexPattern; clear: (id: string) => void; clearAll: () => void; } +/** @internal */ export function createIndexPatternCache(): PatternCache { const vals: Record = {}; const cache: PatternCache = { diff --git a/src/plugins/data/public/index_patterns/index_patterns/index_pattern.test.ts b/src/plugins/data/public/index_patterns/index_patterns/index_pattern.test.ts index 305aa8575e4d7..a94e10a48874b 100644 --- a/src/plugins/data/public/index_patterns/index_patterns/index_pattern.test.ts +++ b/src/plugins/data/public/index_patterns/index_patterns/index_pattern.test.ts @@ -104,7 +104,7 @@ function create(id: string, payload?: any): Promise { (cfg: any) => config.get(cfg), savedObjectsClient as any, apiClient, - patternCache + patternCache as any ); setDocsourcePayload(id, payload); @@ -374,7 +374,7 @@ describe('IndexPattern', () => { (cfg: any) => config.get(cfg), savedObjectsClient as any, apiClient, - patternCache + patternCache as any ); await pattern.init(); @@ -386,7 +386,7 @@ describe('IndexPattern', () => { (cfg: any) => config.get(cfg), savedObjectsClient as any, apiClient, - patternCache + patternCache as any ); await samePattern.init(); diff --git a/src/plugins/data/public/index_patterns/index_patterns/index_pattern.ts b/src/plugins/data/public/index_patterns/index_patterns/index_pattern.ts index 768029136879d..6b6b8601afaf5 100644 --- a/src/plugins/data/public/index_patterns/index_patterns/index_pattern.ts +++ b/src/plugins/data/public/index_patterns/index_patterns/index_pattern.ts @@ -19,7 +19,7 @@ import _, { each, reject } from 'lodash'; import { i18n } from '@kbn/i18n'; -import { SavedObjectsClientContract } from 'src/core/public'; +import { SavedObjectsClientContract, IUiSettingsClient } from 'src/core/public'; import { DuplicateField, SavedObjectNotFound, @@ -34,6 +34,7 @@ import { findByTitle, getRoutes } from '../utils'; import { IndexPatternMissingIndices } from '../lib'; import { Field, FieldList, IFieldList } from '../fields'; import { createFieldsFetcher } from './_fields_fetcher'; +import { PatternCache } from './_pattern_cache'; import { formatHitProvider } from './format_hit'; import { flattenHitWrapper } from './flatten_hit'; import { IIndexPatternsApiClient } from './index_patterns_api_client'; @@ -43,6 +44,11 @@ import { TypeMeta } from './types'; const MAX_ATTEMPTS_TO_RESOLVE_CONFLICTS = 3; const type = 'index-pattern'; +interface SourceFiltersTableFilter { + value: string; + clientId?: string | number; +} + export class IndexPattern implements IIndexPattern { [key: string]: any; @@ -57,12 +63,12 @@ export class IndexPattern implements IIndexPattern { public formatField: any; public flattenHit: any; public metaFields: string[]; + public sourceFilters?: SourceFiltersTableFilter[]; // needed for index pattern ui private version: string | undefined; private savedObjectsClient: SavedObjectsClientContract; - private patternCache: any; - private getConfig: any; - private sourceFilters?: []; + private readonly patternCache: PatternCache; + private readonly getConfig: IUiSettingsClient['get']; private originalBody: { [key: string]: any } = {}; public fieldsFetcher: any; // probably want to factor out any direct usage and change to private private shortDotsEnable: boolean = false; @@ -94,7 +100,7 @@ export class IndexPattern implements IIndexPattern { getConfig: any, savedObjectsClient: SavedObjectsClientContract, apiClient: IIndexPatternsApiClient, - patternCache: any + patternCache: PatternCache ) { this.id = id; this.savedObjectsClient = savedObjectsClient; @@ -367,8 +373,8 @@ export class IndexPattern implements IIndexPattern { duplicateId, this.getConfig, this.savedObjectsClient, - this.patternCache, - this.fieldsFetcher + this.fieldsFetcher, + this.patternCache ); await duplicatePattern.destroy(); } @@ -416,8 +422,8 @@ export class IndexPattern implements IIndexPattern { this.id, this.getConfig, this.savedObjectsClient, - this.patternCache, - this.fieldsFetcher + this.fieldsFetcher, + this.patternCache ); return samePattern.init().then(() => { // What keys changed from now and what the server returned @@ -460,7 +466,9 @@ export class IndexPattern implements IIndexPattern { this.version = samePattern.version; // Clear cache - this.patternCache.clear(this.id); + if (this.id) { + this.patternCache.clear(this.id); + } // Try the save again return this.save(saveAttempts); @@ -516,8 +524,8 @@ export class IndexPattern implements IIndexPattern { } destroy() { - this.patternCache.clear(this.id); if (this.id) { + this.patternCache.clear(this.id); return this.savedObjectsClient.delete(type, this.id); } } diff --git a/src/plugins/data/public/index_patterns/index_patterns/index_patterns.ts b/src/plugins/data/public/index_patterns/index_patterns/index_patterns.ts index acce5ed57683c..0714d03ba2ffb 100644 --- a/src/plugins/data/public/index_patterns/index_patterns/index_patterns.ts +++ b/src/plugins/data/public/index_patterns/index_patterns/index_patterns.ts @@ -24,19 +24,18 @@ import { HttpStart, } from 'src/core/public'; -import { createIndexPatternCache } from './_pattern_cache'; +import { createIndexPatternCache, PatternCache } from './_pattern_cache'; import { IndexPattern } from './index_pattern'; import { IndexPatternsApiClient, GetFieldsOptions } from './index_patterns_api_client'; -const indexPatternCache = createIndexPatternCache(); - type IndexPatternCachedFieldType = 'id' | 'title'; export class IndexPatternsService { - private config: IUiSettingsClient; - private savedObjectsClient: SavedObjectsClientContract; + private readonly apiClient: IndexPatternsApiClient; + private readonly config: IUiSettingsClient; + private readonly indexPatternCache: PatternCache; + private readonly savedObjectsClient: SavedObjectsClientContract; private savedObjectsCache?: Array>> | null; - private apiClient: IndexPatternsApiClient; constructor( config: IUiSettingsClient, @@ -46,6 +45,7 @@ export class IndexPatternsService { this.apiClient = new IndexPatternsApiClient(http); this.config = config; this.savedObjectsClient = savedObjectsClient; + this.indexPatternCache = createIndexPatternCache(); } private async refreshSavedObjectsCache() { @@ -105,9 +105,9 @@ export class IndexPatternsService { clearCache = (id?: string) => { this.savedObjectsCache = null; if (id) { - indexPatternCache.clear(id); + this.indexPatternCache.clear(id); } else { - indexPatternCache.clearAll(); + this.indexPatternCache.clearAll(); } }; getCache = async () => { @@ -127,23 +127,23 @@ export class IndexPatternsService { }; get = async (id: string): Promise => { - const cache = indexPatternCache.get(id); + const cache = this.indexPatternCache.get(id); if (cache) { return cache; } const indexPattern = await this.make(id); - return indexPatternCache.set(id, indexPattern); + return this.indexPatternCache.set(id, indexPattern); }; make = (id?: string): Promise => { const indexPattern = new IndexPattern( id, - (cfg: any) => this.config.get(cfg), + (cfg: string) => this.config.get(cfg), this.savedObjectsClient, this.apiClient, - indexPatternCache + this.indexPatternCache ); return indexPattern.init();