From 78e26b96cde1790b9c5613ad2c3b00a909c4d534 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Sat, 28 Nov 2020 11:26:57 -0600 Subject: [PATCH 1/4] embeddable - get index pattern via service --- .../public/embeddable/get_index_pattern.ts | 34 ++++--------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/src/plugins/visualizations/public/embeddable/get_index_pattern.ts b/src/plugins/visualizations/public/embeddable/get_index_pattern.ts index c12c95145fe44..8765c3973b1e1 100644 --- a/src/plugins/visualizations/public/embeddable/get_index_pattern.ts +++ b/src/plugins/visualizations/public/embeddable/get_index_pattern.ts @@ -18,37 +18,17 @@ */ import { VisSavedObject } from '../types'; -import { - indexPatterns, - IIndexPattern, - IndexPatternAttributes, -} from '../../../../plugins/data/public'; -import { getUISettings, getSavedObjects } from '../services'; +import type { IndexPattern } from '../../../../plugins/data/public'; +import { getIndexPatterns } from '../services'; -export async function getIndexPattern( - savedVis: VisSavedObject -): Promise { +export async function getIndexPattern(savedVis: VisSavedObject): Promise { if (savedVis.visState.type !== 'metrics') { return savedVis.searchSource!.getField('index'); } - const savedObjectsClient = getSavedObjects().client; - const defaultIndex = getUISettings().get('defaultIndex'); + const indexPatternsClient = getIndexPatterns(); - if (savedVis.visState.params.index_pattern) { - const indexPatternObjects = await savedObjectsClient.find({ - type: 'index-pattern', - fields: ['title', 'fields'], - search: `"${savedVis.visState.params.index_pattern}"`, - searchFields: ['title'], - }); - const [indexPattern] = indexPatternObjects.savedObjects.map(indexPatterns.getFromSavedObject); - return indexPattern; - } - - const savedObject = await savedObjectsClient.get( - 'index-pattern', - defaultIndex - ); - return indexPatterns.getFromSavedObject(savedObject); + return savedVis.visState.params.index_pattern + ? indexPatternsClient.find(`"${savedVis.visState.params.index_pattern}"`) + : indexPatternsClient.getDefault(); } From 452f5d446bbe721fb0dfe64006009c05f54cb850 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Sat, 28 Nov 2020 11:46:03 -0600 Subject: [PATCH 2/4] embeddable - get index pattern via service --- .../index_patterns/index_patterns.ts | 14 ++++++++++++++ .../public/embeddable/get_index_pattern.ts | 8 +++++--- 2 files changed, 19 insertions(+), 3 deletions(-) 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 e09246ae8cd3e..5f6fd3d72c3ae 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 @@ -174,6 +174,20 @@ export class IndexPatternsService { return this.savedObjectsCache; }; + find = async (search: string, size: number = 10): Promise => { + const savedObjects = await this.savedObjectsClient.find({ + type: 'index-pattern', + fields: ['title'], + search, + searchFields: ['title'], + perPage: size, + }); + const getIndexPatternPromises = savedObjects.map(async (savedObject) => { + return await this.get(savedObject.id); + }); + return await Promise.all(getIndexPatternPromises); + }; + /** * Get default index pattern */ diff --git a/src/plugins/visualizations/public/embeddable/get_index_pattern.ts b/src/plugins/visualizations/public/embeddable/get_index_pattern.ts index 8765c3973b1e1..22993eb6a2f6a 100644 --- a/src/plugins/visualizations/public/embeddable/get_index_pattern.ts +++ b/src/plugins/visualizations/public/embeddable/get_index_pattern.ts @@ -21,7 +21,9 @@ import { VisSavedObject } from '../types'; import type { IndexPattern } from '../../../../plugins/data/public'; import { getIndexPatterns } from '../services'; -export async function getIndexPattern(savedVis: VisSavedObject): Promise { +export async function getIndexPattern( + savedVis: VisSavedObject +): Promise { if (savedVis.visState.type !== 'metrics') { return savedVis.searchSource!.getField('index'); } @@ -29,6 +31,6 @@ export async function getIndexPattern(savedVis: VisSavedObject): Promise Date: Sat, 28 Nov 2020 19:13:47 -0600 Subject: [PATCH 3/4] update docs --- ...n-plugins-data-public.indexpatternsservice.find.md | 11 +++++++++++ ...plugin-plugins-data-public.indexpatternsservice.md | 1 + src/plugins/data/public/public.api.md | 2 ++ 3 files changed, 14 insertions(+) create mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.find.md diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.find.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.find.md new file mode 100644 index 0000000000000..f642965c5da80 --- /dev/null +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.find.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [IndexPatternsService](./kibana-plugin-plugins-data-public.indexpatternsservice.md) > [find](./kibana-plugin-plugins-data-public.indexpatternsservice.find.md) + +## IndexPatternsService.find property + +Signature: + +```typescript +find: (search: string, size?: number) => Promise; +``` diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md index 48019fe410b97..30ce1fa1de386 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md @@ -23,6 +23,7 @@ export declare class IndexPatternsService | [clearCache](./kibana-plugin-plugins-data-public.indexpatternsservice.clearcache.md) | | (id?: string | undefined) => void | Clear index pattern list cache | | [ensureDefaultIndexPattern](./kibana-plugin-plugins-data-public.indexpatternsservice.ensuredefaultindexpattern.md) | | EnsureDefaultIndexPattern | | | [fieldArrayToMap](./kibana-plugin-plugins-data-public.indexpatternsservice.fieldarraytomap.md) | | (fields: FieldSpec[], fieldAttrs?: FieldAttrs | undefined) => Record<string, FieldSpec> | Converts field array to map | +| [find](./kibana-plugin-plugins-data-public.indexpatternsservice.find.md) | | (search: string, size?: number) => Promise<IndexPattern[]> | | | [get](./kibana-plugin-plugins-data-public.indexpatternsservice.get.md) | | (id: string) => Promise<IndexPattern> | Get an index pattern by id. Cache optimized | | [getCache](./kibana-plugin-plugins-data-public.indexpatternsservice.getcache.md) | | () => Promise<SavedObject<IndexPatternSavedObjectAttrs>[] | null | undefined> | | | [getDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefault.md) | | () => Promise<IndexPattern | null> | Get default index pattern | diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index a6daaf834a424..8ceb91269adbe 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -1396,6 +1396,8 @@ export class IndexPatternsService { // (undocumented) ensureDefaultIndexPattern: EnsureDefaultIndexPattern; fieldArrayToMap: (fields: FieldSpec[], fieldAttrs?: FieldAttrs | undefined) => Record; + // (undocumented) + find: (search: string, size?: number) => Promise; get: (id: string) => Promise; // Warning: (ae-forgotten-export) The symbol "IndexPatternSavedObjectAttrs" needs to be exported by the entry point index.d.ts // From 3d36ed7ac75af367e3949e8575537589aac04306 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Mon, 30 Nov 2020 12:53:01 -0600 Subject: [PATCH 4/4] remove dupe code --- .../index_patterns/index_patterns.ts | 14 -------------- 1 file changed, 14 deletions(-) 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 3fc1d712f3793..4a266b3cad649 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 @@ -188,20 +188,6 @@ export class IndexPatternsService { return this.savedObjectsCache; }; - find = async (search: string, size: number = 10): Promise => { - const savedObjects = await this.savedObjectsClient.find({ - type: 'index-pattern', - fields: ['title'], - search, - searchFields: ['title'], - perPage: size, - }); - const getIndexPatternPromises = savedObjects.map(async (savedObject) => { - return await this.get(savedObject.id); - }); - return await Promise.all(getIndexPatternPromises); - }; - /** * Get default index pattern */