From 1c3333eb3ad883a8c8bbd54750f7e468b4b9b20c Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Fri, 12 Jun 2020 13:39:35 -0500 Subject: [PATCH 1/3] index pattern(s) take dependencies as object --- .../index_patterns/index_pattern.test.ts | 54 +++++++++------- .../index_patterns/index_pattern.ts | 64 +++++++++++-------- .../index_patterns/index_patterns.test.ts | 12 ++-- .../index_patterns/index_patterns.ts | 47 ++++++++------ src/plugins/data/public/plugin.ts | 16 +++-- 5 files changed, 111 insertions(+), 82 deletions(-) diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.test.ts b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.test.ts index 8ec3072bf916bc..e253d20ab59627 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.test.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.test.ts @@ -97,16 +97,18 @@ const apiClient = { // helper function to create index patterns function create(id: string, payload?: any): Promise { - const indexPattern = new IndexPattern( - id, - (cfg: any) => config.get(cfg), - savedObjectsClient as any, + const indexPattern = new IndexPattern(id, { + getConfig: (cfg: any) => config.get(cfg), + savedObjectsClient: savedObjectsClient as any, apiClient, patternCache, - ({ getDefaultInstance: () => {}, getType: () => {} } as unknown) as FieldFormatsStartCommon, - () => {}, - () => {} - ); + fieldFormats: ({ + getDefaultInstance: () => {}, + getType: () => {}, + } as unknown) as FieldFormatsStartCommon, + onNotification: () => {}, + onError: () => {}, + }); setDocsourcePayload(id, payload); @@ -363,31 +365,35 @@ describe('IndexPattern', () => { }, }); // Create a normal index pattern - const pattern = new IndexPattern( - 'foo', - (cfg: any) => config.get(cfg), - savedObjectsClient as any, + const pattern = new IndexPattern('foo', { + getConfig: (cfg: any) => config.get(cfg), + savedObjectsClient: savedObjectsClient as any, apiClient, patternCache, - ({ getDefaultInstance: () => {}, getType: () => {} } as unknown) as FieldFormatsStartCommon, - () => {}, - () => {} - ); + fieldFormats: ({ + getDefaultInstance: () => {}, + getType: () => {}, + } as unknown) as FieldFormatsStartCommon, + onNotification: () => {}, + onError: () => {}, + }); await pattern.init(); expect(get(pattern, 'version')).toBe('fooa'); // Create the same one - we're going to handle concurrency - const samePattern = new IndexPattern( - 'foo', - (cfg: any) => config.get(cfg), - savedObjectsClient as any, + const samePattern = new IndexPattern('foo', { + getConfig: (cfg: any) => config.get(cfg), + savedObjectsClient: savedObjectsClient as any, apiClient, patternCache, - ({ getDefaultInstance: () => {}, getType: () => {} } as unknown) as FieldFormatsStartCommon, - () => {}, - () => {} - ); + fieldFormats: ({ + getDefaultInstance: () => {}, + getType: () => {}, + } as unknown) as FieldFormatsStartCommon, + onNotification: () => {}, + onError: () => {}, + }); await samePattern.init(); expect(get(samePattern, 'version')).toBe('fooaa'); diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts index aefa8734b01c73..666d99362ce803 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts @@ -45,6 +45,16 @@ import { expandShorthand, FieldMappingSpec, MappingObject } from '../../field_ma const MAX_ATTEMPTS_TO_RESOLVE_CONFLICTS = 3; const type = 'index-pattern'; +interface IndexPatternDeps { + getConfig: any; + savedObjectsClient: SavedObjectsClientContract; + apiClient: IIndexPatternsApiClient; + patternCache: PatternCache; + fieldFormats: FieldFormatsStartCommon; + onNotification: OnNotification; + onError: OnError; +} + export class IndexPattern implements IIndexPattern { [key: string]: any; @@ -97,13 +107,15 @@ export class IndexPattern implements IIndexPattern { constructor( id: string | undefined, - getConfig: any, - savedObjectsClient: SavedObjectsClientContract, - apiClient: IIndexPatternsApiClient, - patternCache: PatternCache, - fieldFormats: FieldFormatsStartCommon, - onNotification: OnNotification, - onError: OnError + { + getConfig, + savedObjectsClient, + apiClient, + patternCache, + fieldFormats, + onNotification, + onError, + }: IndexPatternDeps ) { this.id = id; this.savedObjectsClient = savedObjectsClient; @@ -400,16 +412,15 @@ export class IndexPattern implements IIndexPattern { async create(allowOverride: boolean = false) { const _create = async (duplicateId?: string) => { if (duplicateId) { - const duplicatePattern = new IndexPattern( - duplicateId, - this.getConfig, - this.savedObjectsClient, - this.apiClient, - this.patternCache, - this.fieldFormats, - this.onNotification, - this.onError - ); + const duplicatePattern = new IndexPattern(duplicateId, { + getConfig: this.getConfig, + savedObjectsClient: this.savedObjectsClient, + apiClient: this.apiClient, + patternCache: this.patternCache, + fieldFormats: this.fieldFormats, + onNotification: this.onNotification, + onError: this.onError, + }); await duplicatePattern.destroy(); } @@ -453,16 +464,15 @@ export class IndexPattern implements IIndexPattern { _.get(err, 'res.status') === 409 && saveAttempts++ < MAX_ATTEMPTS_TO_RESOLVE_CONFLICTS ) { - const samePattern = new IndexPattern( - this.id, - this.getConfig, - this.savedObjectsClient, - this.apiClient, - this.patternCache, - this.fieldFormats, - this.onNotification, - this.onError - ); + const samePattern = new IndexPattern(this.id, { + getConfig: this.getConfig, + savedObjectsClient: this.savedObjectsClient, + apiClient: this.apiClient, + patternCache: this.patternCache, + fieldFormats: this.fieldFormats, + onNotification: this.onNotification, + onError: this.onError, + }); return samePattern.init().then(() => { // What keys changed from now and what the server returned const updatedBody = samePattern.prepBody(); diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.test.ts b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.test.ts index 5ff19a3c54cb18..b0ecfc89d376bd 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.test.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.test.ts @@ -62,15 +62,15 @@ describe('IndexPatterns', () => { }) as Promise> ); - indexPatterns = new IndexPatternsService( - core.uiSettings, + indexPatterns = new IndexPatternsService({ + uiSettings: core.uiSettings, savedObjectsClient, http, fieldFormats, - () => {}, - () => {}, - () => {} - ); + onNotification: () => {}, + onError: () => {}, + onRedirectNoIndexPattern: () => {}, + }); }); test('does cache gets for the same id', async () => { diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts index db2a68956f1063..22d1765d793488 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts @@ -49,6 +49,16 @@ export interface IndexPatternSavedObjectAttrs { title: string; } +interface IndexPatternsServiceDeps { + uiSettings: CoreStart['uiSettings']; + savedObjectsClient: SavedObjectsClientContract; + http: HttpStart; + fieldFormats: FieldFormatsStartCommon; + onNotification: OnNotification; + onError: OnError; + onRedirectNoIndexPattern: () => void; +} + export class IndexPatternsService { private config: IUiSettingsClient; private savedObjectsClient: SavedObjectsClientContract; @@ -65,15 +75,15 @@ export class IndexPatternsService { shortDotsEnable: boolean ) => Field; - constructor( - uiSettings: CoreStart['uiSettings'], - savedObjectsClient: SavedObjectsClientContract, - http: HttpStart, - fieldFormats: FieldFormatsStartCommon, - onNotification: OnNotification, - onError: OnError, - onRedirectNoIndexPattern: () => void - ) { + constructor({ + uiSettings, + savedObjectsClient, + http, + fieldFormats, + onNotification, + onError, + onRedirectNoIndexPattern, + }: IndexPatternsServiceDeps) { this.apiClient = new IndexPatternsApiClient(http); this.config = uiSettings; this.savedObjectsClient = savedObjectsClient; @@ -186,16 +196,15 @@ export class IndexPatternsService { }; make = (id?: string): Promise => { - const indexPattern = new IndexPattern( - id, - (cfg: any) => this.config.get(cfg), - this.savedObjectsClient, - this.apiClient, - indexPatternCache, - this.fieldFormats, - this.onNotification, - this.onError - ); + const indexPattern = new IndexPattern(id, { + getConfig: (cfg: any) => this.config.get(cfg), + savedObjectsClient: this.savedObjectsClient, + apiClient: this.apiClient, + patternCache: indexPatternCache, + fieldFormats: this.fieldFormats, + onNotification: this.onNotification, + onError: this.onError, + }); return indexPattern.init(); }; diff --git a/src/plugins/data/public/plugin.ts b/src/plugins/data/public/plugin.ts index baac2cf0d7a77c..493733daf261b2 100644 --- a/src/plugins/data/public/plugin.ts +++ b/src/plugins/data/public/plugin.ts @@ -164,17 +164,21 @@ export class DataPublicPlugin implements Plugin { + onNotification: (toastInputFields) => { notifications.toasts.add(toastInputFields); }, - notifications.toasts.addError, - onRedirectNoIndexPattern(application.capabilities, application.navigateToApp, overlays) - ); + onError: notifications.toasts.addError, + onRedirectNoIndexPattern: onRedirectNoIndexPattern( + application.capabilities, + application.navigateToApp, + overlays + ), + }); setIndexPatterns(indexPatterns); const query = this.queryService.start(savedObjects); From 453e963e7f1f10acb4e276c31b88d6461be7281e Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Fri, 12 Jun 2020 13:46:33 -0500 Subject: [PATCH 2/3] update docs --- ...n-plugins-data-public.indexpattern._constructor_.md | 10 ++-------- .../kibana-plugin-plugins-data-public.indexpattern.md | 2 +- src/plugins/data/public/public.api.md | 8 ++------ 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpattern._constructor_.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpattern._constructor_.md index c6359fc2688824..6574e7ee379266 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpattern._constructor_.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpattern._constructor_.md @@ -9,7 +9,7 @@ Constructs a new instance of the `IndexPattern` class Signature: ```typescript -constructor(id: string | undefined, getConfig: any, savedObjectsClient: SavedObjectsClientContract, apiClient: IIndexPatternsApiClient, patternCache: PatternCache, fieldFormats: FieldFormatsStartCommon, onNotification: OnNotification, onError: OnError); +constructor(id: string | undefined, { getConfig, savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, }: IndexPatternDeps); ``` ## Parameters @@ -17,11 +17,5 @@ constructor(id: string | undefined, getConfig: any, savedObjectsClient: SavedObj | Parameter | Type | Description | | --- | --- | --- | | id | string | undefined | | -| getConfig | any | | -| savedObjectsClient | SavedObjectsClientContract | | -| apiClient | IIndexPatternsApiClient | | -| patternCache | PatternCache | | -| fieldFormats | FieldFormatsStartCommon | | -| onNotification | OnNotification | | -| onError | OnError | | +| { getConfig, savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, } | IndexPatternDeps | | diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpattern.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpattern.md index 3e67b96cb80ce9..8ffa7b6b36f56b 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpattern.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpattern.md @@ -14,7 +14,7 @@ export declare class IndexPattern implements IIndexPattern | Constructor | Modifiers | Description | | --- | --- | --- | -| [(constructor)(id, getConfig, savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError)](./kibana-plugin-plugins-data-public.indexpattern._constructor_.md) | | Constructs a new instance of the IndexPattern class | +| [(constructor)(id, { getConfig, savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, })](./kibana-plugin-plugins-data-public.indexpattern._constructor_.md) | | Constructs a new instance of the IndexPattern class | ## Properties diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index f23dfe857c775f..23213d4d1165a6 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -978,12 +978,8 @@ export type IMetricAggType = MetricAggType; // // @public (undocumented) export class IndexPattern implements IIndexPattern { - // Warning: (ae-forgotten-export) The symbol "IIndexPatternsApiClient" needs to be exported by the entry point index.d.ts - // Warning: (ae-forgotten-export) The symbol "PatternCache" needs to be exported by the entry point index.d.ts - // Warning: (ae-forgotten-export) The symbol "FieldFormatsStartCommon" needs to be exported by the entry point index.d.ts - // Warning: (ae-forgotten-export) The symbol "OnNotification" needs to be exported by the entry point index.d.ts - // Warning: (ae-forgotten-export) The symbol "OnError" needs to be exported by the entry point index.d.ts - constructor(id: string | undefined, getConfig: any, savedObjectsClient: SavedObjectsClientContract, apiClient: IIndexPatternsApiClient, patternCache: PatternCache, fieldFormats: FieldFormatsStartCommon, onNotification: OnNotification, onError: OnError); + // Warning: (ae-forgotten-export) The symbol "IndexPatternDeps" needs to be exported by the entry point index.d.ts + constructor(id: string | undefined, { getConfig, savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, }: IndexPatternDeps); // (undocumented) [key: string]: any; // (undocumented) From f55a24aecc81911204b6aa089e6dec0b34d410a0 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Fri, 12 Jun 2020 16:23:42 -0500 Subject: [PATCH 3/3] better field formats mock --- .../index_patterns/index_pattern.test.ts | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.test.ts b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.test.ts index e253d20ab59627..cea476781ad3bf 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.test.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.test.ts @@ -28,7 +28,7 @@ import mockLogStashFields from '../../../../../fixtures/logstash_fields'; import { stubbedSavedObjectIndexPattern } from '../../../../../fixtures/stubbed_saved_object_index_pattern'; import { Field } from '../fields'; -import { FieldFormatsStartCommon } from '../../field_formats'; +import { fieldFormatsMock } from '../../field_formats/mocks'; jest.mock('../../field_mapping', () => { const originalModule = jest.requireActual('../../field_mapping'); @@ -102,10 +102,7 @@ function create(id: string, payload?: any): Promise { savedObjectsClient: savedObjectsClient as any, apiClient, patternCache, - fieldFormats: ({ - getDefaultInstance: () => {}, - getType: () => {}, - } as unknown) as FieldFormatsStartCommon, + fieldFormats: fieldFormatsMock, onNotification: () => {}, onError: () => {}, }); @@ -370,10 +367,7 @@ describe('IndexPattern', () => { savedObjectsClient: savedObjectsClient as any, apiClient, patternCache, - fieldFormats: ({ - getDefaultInstance: () => {}, - getType: () => {}, - } as unknown) as FieldFormatsStartCommon, + fieldFormats: fieldFormatsMock, onNotification: () => {}, onError: () => {}, }); @@ -387,10 +381,7 @@ describe('IndexPattern', () => { savedObjectsClient: savedObjectsClient as any, apiClient, patternCache, - fieldFormats: ({ - getDefaultInstance: () => {}, - getType: () => {}, - } as unknown) as FieldFormatsStartCommon, + fieldFormats: fieldFormatsMock, onNotification: () => {}, onError: () => {}, });