Skip to content

Commit

Permalink
Index patterns api - load field list on server (#82629)
Browse files Browse the repository at this point in the history
The server side index patterns api can now load the field list. Previously it would error if a field list refresh was required.

The regular and rollup index pattern field list loading methods were merged. Rollup index patterns require additional functionality over regular index patterns when it comes to loading the field list, but this won't be necessary once rollups v2 is released.
  • Loading branch information
mattkime authored Nov 9, 2020
1 parent bf75831 commit 7a67cff
Show file tree
Hide file tree
Showing 48 changed files with 484 additions and 394 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Get field list by providing an index patttern (or spec)
<b>Signature:</b>

```typescript
getFieldsForIndexPattern: (indexPattern: IndexPattern | IndexPatternSpec, options?: GetFieldsOptions) => Promise<any>;
getFieldsForIndexPattern: (indexPattern: IndexPattern | IndexPatternSpec, options?: GetFieldsOptions | undefined) => Promise<any>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Get field list by providing { pattern }
<b>Signature:</b>

```typescript
getFieldsForWildcard: (options?: GetFieldsOptions) => Promise<any>;
getFieldsForWildcard: (options: GetFieldsOptions) => Promise<any>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export declare class IndexPatternsService
| [get](./kibana-plugin-plugins-data-public.indexpatternsservice.get.md) | | <code>(id: string) =&gt; Promise&lt;IndexPattern&gt;</code> | Get an index pattern by id. Cache optimized |
| [getCache](./kibana-plugin-plugins-data-public.indexpatternsservice.getcache.md) | | <code>() =&gt; Promise&lt;SavedObject&lt;IndexPatternSavedObjectAttrs&gt;[] &#124; null &#124; undefined&gt;</code> | |
| [getDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefault.md) | | <code>() =&gt; Promise&lt;IndexPattern &#124; null&gt;</code> | Get default index pattern |
| [getFieldsForIndexPattern](./kibana-plugin-plugins-data-public.indexpatternsservice.getfieldsforindexpattern.md) | | <code>(indexPattern: IndexPattern &#124; IndexPatternSpec, options?: GetFieldsOptions) =&gt; Promise&lt;any&gt;</code> | Get field list by providing an index patttern (or spec) |
| [getFieldsForWildcard](./kibana-plugin-plugins-data-public.indexpatternsservice.getfieldsforwildcard.md) | | <code>(options?: GetFieldsOptions) =&gt; Promise&lt;any&gt;</code> | Get field list by providing { pattern } |
| [getFieldsForIndexPattern](./kibana-plugin-plugins-data-public.indexpatternsservice.getfieldsforindexpattern.md) | | <code>(indexPattern: IndexPattern &#124; IndexPatternSpec, options?: GetFieldsOptions &#124; undefined) =&gt; Promise&lt;any&gt;</code> | Get field list by providing an index patttern (or spec) |
| [getFieldsForWildcard](./kibana-plugin-plugins-data-public.indexpatternsservice.getfieldsforwildcard.md) | | <code>(options: GetFieldsOptions) =&gt; Promise&lt;any&gt;</code> | Get field list by providing { pattern } |
| [getIds](./kibana-plugin-plugins-data-public.indexpatternsservice.getids.md) | | <code>(refresh?: boolean) =&gt; Promise&lt;string[]&gt;</code> | Get list of index pattern ids |
| [getIdsWithTitle](./kibana-plugin-plugins-data-public.indexpatternsservice.getidswithtitle.md) | | <code>(refresh?: boolean) =&gt; Promise&lt;Array&lt;{</code><br/><code> id: string;</code><br/><code> title: string;</code><br/><code> }&gt;&gt;</code> | Get list of index pattern ids with titles |
| [getTitles](./kibana-plugin-plugins-data-public.indexpatternsservice.gettitles.md) | | <code>(refresh?: boolean) =&gt; Promise&lt;string[]&gt;</code> | Get list of index pattern titles |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [getCapabilitiesForRollupIndices](./kibana-plugin-plugins-data-server.getcapabilitiesforrollupindices.md)

## getCapabilitiesForRollupIndices() function

<b>Signature:</b>

```typescript
export declare function getCapabilitiesForRollupIndices(indices: {
[key: string]: any;
}): {
[key: string]: any;
};
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| indices | <code>{</code><br/><code> [key: string]: any;</code><br/><code>}</code> | |

<b>Returns:</b>

`{
[key: string]: any;
}`

Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ getFieldsForWildcard(options: {
fieldCapsOptions?: {
allow_no_indices: boolean;
};
type?: string;
rollupIndex?: string;
}): Promise<FieldDescriptor[]>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| options | <code>{</code><br/><code> pattern: string &#124; string[];</code><br/><code> metaFields?: string[];</code><br/><code> fieldCapsOptions?: {</code><br/><code> allow_no_indices: boolean;</code><br/><code> };</code><br/><code> }</code> | |
| options | <code>{</code><br/><code> pattern: string &#124; string[];</code><br/><code> metaFields?: string[];</code><br/><code> fieldCapsOptions?: {</code><br/><code> allow_no_indices: boolean;</code><br/><code> };</code><br/><code> type?: string;</code><br/><code> rollupIndex?: string;</code><br/><code> }</code> | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

```typescript
start(core: CoreStart, { fieldFormats, logger }: IndexPatternsServiceStartDeps): {
indexPatternsServiceFactory: (savedObjectsClient: SavedObjectsClientContract) => Promise<IndexPatternsCommonService>;
indexPatternsServiceFactory: (savedObjectsClient: SavedObjectsClientContract, elasticsearchClient: ElasticsearchClient) => Promise<IndexPatternsCommonService>;
};
```

Expand All @@ -22,6 +22,6 @@ start(core: CoreStart, { fieldFormats, logger }: IndexPatternsServiceStartDeps):
<b>Returns:</b>

`{
indexPatternsServiceFactory: (savedObjectsClient: SavedObjectsClientContract) => Promise<IndexPatternsCommonService>;
indexPatternsServiceFactory: (savedObjectsClient: SavedObjectsClientContract, elasticsearchClient: ElasticsearchClient) => Promise<IndexPatternsCommonService>;
}`

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

| Function | Description |
| --- | --- |
| [getCapabilitiesForRollupIndices(indices)](./kibana-plugin-plugins-data-server.getcapabilitiesforrollupindices.md) | |
| [getDefaultSearchParams(uiSettingsClient)](./kibana-plugin-plugins-data-server.getdefaultsearchparams.md) | |
| [getShardTimeout(config)](./kibana-plugin-plugins-data-server.getshardtimeout.md) | |
| [getTime(indexPattern, timeRange, options)](./kibana-plugin-plugins-data-server.gettime.md) | |
Expand Down Expand Up @@ -77,6 +78,7 @@
| [esQuery](./kibana-plugin-plugins-data-server.esquery.md) | |
| [fieldFormats](./kibana-plugin-plugins-data-server.fieldformats.md) | |
| [indexPatterns](./kibana-plugin-plugins-data-server.indexpatterns.md) | |
| [mergeCapabilitiesWithFields](./kibana-plugin-plugins-data-server.mergecapabilitieswithfields.md) | |
| [search](./kibana-plugin-plugins-data-server.search.md) | |
| [UI\_SETTINGS](./kibana-plugin-plugins-data-server.ui_settings.md) | |

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [mergeCapabilitiesWithFields](./kibana-plugin-plugins-data-server.mergecapabilitieswithfields.md)

## mergeCapabilitiesWithFields variable

<b>Signature:</b>

```typescript
mergeCapabilitiesWithFields: (rollupIndexCapabilities: {
[key: string]: any;
}, fieldsFromFieldCapsApi: {
[key: string]: any;
}, previousFields?: FieldDescriptor[]) => FieldDescriptor[]
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ start(core: CoreStart): {
fieldFormatServiceFactory: (uiSettings: import("src/core/server").IUiSettingsClient) => Promise<import("../common").FieldFormatsRegistry>;
};
indexPatterns: {
indexPatternsServiceFactory: (savedObjectsClient: Pick<import("src/core/server").SavedObjectsClient, "update" | "find" | "get" | "delete" | "errors" | "create" | "bulkCreate" | "checkConflicts" | "bulkGet" | "addToNamespaces" | "deleteFromNamespaces" | "bulkUpdate" | "removeReferencesTo">) => Promise<import("../common").IndexPatternsService>;
indexPatternsServiceFactory: (savedObjectsClient: Pick<import("src/core/server").SavedObjectsClient, "update" | "find" | "get" | "delete" | "errors" | "create" | "bulkCreate" | "checkConflicts" | "bulkGet" | "addToNamespaces" | "deleteFromNamespaces" | "bulkUpdate" | "removeReferencesTo">, elasticsearchClient: import("src/core/server").ElasticsearchClient) => Promise<import("../common").IndexPatternsService>;
};
search: ISearchStart<import("./search").IEsSearchRequest, import("./search").IEsSearchResponse<any>>;
};
Expand All @@ -31,7 +31,7 @@ start(core: CoreStart): {
fieldFormatServiceFactory: (uiSettings: import("src/core/server").IUiSettingsClient) => Promise<import("../common").FieldFormatsRegistry>;
};
indexPatterns: {
indexPatternsServiceFactory: (savedObjectsClient: Pick<import("src/core/server").SavedObjectsClient, "update" | "find" | "get" | "delete" | "errors" | "create" | "bulkCreate" | "checkConflicts" | "bulkGet" | "addToNamespaces" | "deleteFromNamespaces" | "bulkUpdate" | "removeReferencesTo">) => Promise<import("../common").IndexPatternsService>;
indexPatternsServiceFactory: (savedObjectsClient: Pick<import("src/core/server").SavedObjectsClient, "update" | "find" | "get" | "delete" | "errors" | "create" | "bulkCreate" | "checkConflicts" | "bulkGet" | "addToNamespaces" | "deleteFromNamespaces" | "bulkUpdate" | "removeReferencesTo">, elasticsearchClient: import("src/core/server").ElasticsearchClient) => Promise<import("../common").IndexPatternsService>;
};
search: ISearchStart<import("./search").IEsSearchRequest, import("./search").IEsSearchResponse<any>>;
}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ export class IndexPatternsService {
* Refresh cache of index pattern ids and titles
*/
private async refreshSavedObjectsCache() {
this.savedObjectsCache = await this.savedObjectsClient.find<IndexPatternSavedObjectAttrs>({
const so = await this.savedObjectsClient.find<IndexPatternSavedObjectAttrs>({
type: 'index-pattern',
fields: ['title'],
perPage: 10000,
});
this.savedObjectsCache = so;
}

/**
Expand Down Expand Up @@ -215,13 +216,13 @@ export class IndexPatternsService {
* Get field list by providing { pattern }
* @param options
*/
getFieldsForWildcard = async (options: GetFieldsOptions = {}) => {
getFieldsForWildcard = async (options: GetFieldsOptions) => {
const metaFields = await this.config.get(UI_SETTINGS.META_FIELDS);
return this.apiClient.getFieldsForWildcard({
pattern: options.pattern,
metaFields,
type: options.type,
params: options.params || {},
rollupIndex: options.rollupIndex,
});
};

Expand All @@ -231,13 +232,13 @@ export class IndexPatternsService {
*/
getFieldsForIndexPattern = async (
indexPattern: IndexPattern | IndexPatternSpec,
options: GetFieldsOptions = {}
options?: GetFieldsOptions
) =>
this.getFieldsForWildcard({
pattern: indexPattern.title as string,
...options,
type: indexPattern.type,
params: indexPattern.typeMeta && indexPattern.typeMeta.params,
rollupIndex: indexPattern?.typeMeta?.params?.rollup_index,
...options,
pattern: indexPattern.title as string,
});

/**
Expand Down Expand Up @@ -374,10 +375,10 @@ export class IndexPatternsService {
try {
spec.fields = isFieldRefreshRequired
? await this.refreshFieldSpecMap(spec.fields || {}, id, spec.title as string, {
pattern: title,
pattern: title as string,
metaFields: await this.config.get(UI_SETTINGS.META_FIELDS),
type,
params: typeMeta && typeMeta.params,
rollupIndex: typeMeta?.params?.rollupIndex,
})
: spec.fields;
} catch (err) {
Expand Down
13 changes: 10 additions & 3 deletions src/plugins/data/common/index_patterns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,22 @@ export interface SavedObjectsClientCommon {
}

export interface GetFieldsOptions {
pattern?: string;
pattern: string;
type?: string;
params?: any;
lookBack?: boolean;
metaFields?: string[];
rollupIndex?: string;
}

export interface GetFieldsOptionsTimePattern {
pattern: string;
metaFields: string[];
lookBack: number;
interval: string;
}

export interface IIndexPatternsApiClient {
getFieldsForTimePattern: (options: GetFieldsOptions) => Promise<any>;
getFieldsForTimePattern: (options: GetFieldsOptionsTimePattern) => Promise<any>;
getFieldsForWildcard: (options: GetFieldsOptions) => Promise<any>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,20 @@ describe('IndexPatternsApiClient', () => {
test('uses the right URI to fetch fields for time patterns', async function () {
const expectedPath = '/api/index_patterns/_fields_for_time_pattern';

await indexPatternsApiClient.getFieldsForTimePattern();
await indexPatternsApiClient.getFieldsForTimePattern({
pattern: 'blah',
metaFields: [],
lookBack: 5,
interval: '',
});

expect(fetchSpy).toHaveBeenCalledWith(expectedPath, expect.any(Object));
});

test('uses the right URI to fetch fields for wildcard', async function () {
const expectedPath = '/api/index_patterns/_fields_for_wildcard';

await indexPatternsApiClient.getFieldsForWildcard();

expect(fetchSpy).toHaveBeenCalledWith(expectedPath, expect.any(Object));
});

test('uses the right URI to fetch fields for wildcard given a type', async function () {
const expectedPath = '/api/index_patterns/rollup/_fields_for_wildcard';

await indexPatternsApiClient.getFieldsForWildcard({ type: 'rollup' });
await indexPatternsApiClient.getFieldsForWildcard({ pattern: 'blah' });

expect(fetchSpy).toHaveBeenCalledWith(expectedPath, expect.any(Object));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

import { HttpSetup } from 'src/core/public';
import { IndexPatternMissingIndices } from '../../../common/index_patterns/lib';
import { GetFieldsOptions, IIndexPatternsApiClient } from '../../../common/index_patterns/types';
import {
GetFieldsOptions,
IIndexPatternsApiClient,
GetFieldsOptionsTimePattern,
} from '../../../common/index_patterns/types';

const API_BASE_URL: string = `/api/index_patterns/`;

Expand Down Expand Up @@ -48,7 +52,7 @@ export class IndexPatternsApiClient implements IIndexPatternsApiClient {
return API_BASE_URL + path.filter(Boolean).map(encodeURIComponent).join('/');
}

getFieldsForTimePattern(options: GetFieldsOptions = {}) {
getFieldsForTimePattern(options: GetFieldsOptionsTimePattern) {
const { pattern, lookBack, metaFields } = options;

const url = this._getUrl(['_fields_for_time_pattern']);
Expand All @@ -60,27 +64,12 @@ export class IndexPatternsApiClient implements IIndexPatternsApiClient {
}).then((resp: any) => resp.fields);
}

getFieldsForWildcard(options: GetFieldsOptions = {}) {
const { pattern, metaFields, type, params } = options;

let url;
let query;

if (type) {
url = this._getUrl([type, '_fields_for_wildcard']);
query = {
pattern,
meta_fields: metaFields,
params: JSON.stringify(params),
};
} else {
url = this._getUrl(['_fields_for_wildcard']);
query = {
pattern,
meta_fields: metaFields,
};
}

return this._request(url, query).then((resp: any) => resp.fields);
getFieldsForWildcard({ pattern, metaFields, type, rollupIndex }: GetFieldsOptions) {
return this._request(this._getUrl(['_fields_for_wildcard']), {
pattern,
meta_fields: metaFields,
type,
rollup_index: rollupIndex,
}).then((resp: any) => resp.fields);
}
}
4 changes: 2 additions & 2 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1367,9 +1367,9 @@ export class IndexPatternsService {
// (undocumented)
getCache: () => Promise<SavedObject<IndexPatternSavedObjectAttrs>[] | null | undefined>;
getDefault: () => Promise<IndexPattern | null>;
getFieldsForIndexPattern: (indexPattern: IndexPattern | IndexPatternSpec, options?: GetFieldsOptions) => Promise<any>;
getFieldsForIndexPattern: (indexPattern: IndexPattern | IndexPatternSpec, options?: GetFieldsOptions | undefined) => Promise<any>;
// Warning: (ae-forgotten-export) The symbol "GetFieldsOptions" needs to be exported by the entry point index.d.ts
getFieldsForWildcard: (options?: GetFieldsOptions) => Promise<any>;
getFieldsForWildcard: (options: GetFieldsOptions) => Promise<any>;
getIds: (refresh?: boolean) => Promise<string[]>;
getIdsWithTitle: (refresh?: boolean) => Promise<Array<{
id: string;
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ export {
FieldDescriptor as IndexPatternFieldDescriptor,
shouldReadFieldFromDocValues, // used only in logstash_fields fixture
FieldDescriptor,
mergeCapabilitiesWithFields,
getCapabilitiesForRollupIndices,
} from './index_patterns';

export {
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/data/server/index_patterns/fetcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
*/

export * from './index_patterns_fetcher';
export { shouldReadFieldFromDocValues } from './lib';
export {
shouldReadFieldFromDocValues,
mergeCapabilitiesWithFields,
getCapabilitiesForRollupIndices,
} from './lib';
Loading

0 comments on commit 7a67cff

Please sign in to comment.