Skip to content

Commit

Permalink
[Index Patterns] Fix return saved index pattern object (elastic#101051)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Jun 2, 2021
1 parent 4f07817 commit 979f37f
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,9 @@ Returns index pattern as saved object body for saving
<b>Signature:</b>

```typescript
getAsSavedObjectBody(): {
fieldAttrs: string | undefined;
title: string;
timeFieldName: string | undefined;
intervalName: string | undefined;
sourceFilters: string | undefined;
fields: string | undefined;
fieldFormatMap: string | undefined;
type: string | undefined;
typeMeta: string | undefined;
allowNoIndex: true | undefined;
runtimeFieldMap: string | undefined;
};
getAsSavedObjectBody(): IndexPatternAttributes;
```
<b>Returns:</b>

`{
fieldAttrs: string | undefined;
title: string;
timeFieldName: string | undefined;
intervalName: string | undefined;
sourceFilters: string | undefined;
fields: string | undefined;
fieldFormatMap: string | undefined;
type: string | undefined;
typeMeta: string | undefined;
allowNoIndex: true | undefined;
runtimeFieldMap: string | undefined;
}`
`IndexPatternAttributes`

Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,9 @@ Returns index pattern as saved object body for saving
<b>Signature:</b>

```typescript
getAsSavedObjectBody(): {
fieldAttrs: string | undefined;
title: string;
timeFieldName: string | undefined;
intervalName: string | undefined;
sourceFilters: string | undefined;
fields: string | undefined;
fieldFormatMap: string | undefined;
type: string | undefined;
typeMeta: string | undefined;
allowNoIndex: true | undefined;
runtimeFieldMap: string | undefined;
};
getAsSavedObjectBody(): IndexPatternAttributes;
```
<b>Returns:</b>

`{
fieldAttrs: string | undefined;
title: string;
timeFieldName: string | undefined;
intervalName: string | undefined;
sourceFilters: string | undefined;
fields: string | undefined;
fieldFormatMap: string | undefined;
type: string | undefined;
typeMeta: string | undefined;
allowNoIndex: true | undefined;
runtimeFieldMap: string | undefined;
}`
`IndexPatternAttributes`

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import _, { each, reject } from 'lodash';
import { FieldAttrs, FieldAttrSet } from '../..';
import { FieldAttrs, FieldAttrSet, IndexPatternAttributes } from '../..';
import type { RuntimeField } from '../types';
import { DuplicateField } from '../../../../kibana_utils/common';

Expand Down Expand Up @@ -331,7 +331,7 @@ export class IndexPattern implements IIndexPattern {
/**
* Returns index pattern as saved object body for saving
*/
getAsSavedObjectBody() {
getAsSavedObjectBody(): IndexPatternAttributes {
const fieldFormatMap = _.isEmpty(this.fieldFormatMap)
? undefined
: JSON.stringify(this.fieldFormatMap);
Expand All @@ -344,12 +344,10 @@ export class IndexPattern implements IIndexPattern {
timeFieldName: this.timeFieldName,
intervalName: this.intervalName,
sourceFilters: this.sourceFilters ? JSON.stringify(this.sourceFilters) : undefined,
fields: this.fields
? JSON.stringify(this.fields.filter((field) => field.scripted))
: undefined,
fields: JSON.stringify(this.fields?.filter((field) => field.scripted) ?? []),
fieldFormatMap,
type: this.type,
typeMeta: this.typeMeta ? JSON.stringify(this.typeMeta) : undefined,
type: this.type!,
typeMeta: JSON.stringify(this.typeMeta ?? {}),
allowNoIndex: this.allowNoIndex ? this.allowNoIndex : undefined,
runtimeFieldMap: runtimeFieldMap ? JSON.stringify(runtimeFieldMap) : undefined,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,12 @@ describe('IndexPatterns', () => {

test('createAndSave', async () => {
const title = 'kibana-*';
indexPatterns.createSavedObject = jest.fn();

indexPatterns.createSavedObject = jest.fn(() =>
Promise.resolve(({
id: 'id',
} as unknown) as IndexPattern)
);
indexPatterns.setDefault = jest.fn();
await indexPatterns.createAndSave({ title });
expect(indexPatterns.createSavedObject).toBeCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ export class IndexPatternsService {
throw new SavedObjectNotFound(savedObjectType, id, 'management/kibana/indexPatterns');
}

return this.initFromSavedObject(savedObject);
};

private initFromSavedObject = async (
savedObject: SavedObject<IndexPatternAttributes>
): Promise<IndexPattern> => {
const spec = this.savedObjectToSpec(savedObject);
const { title, type, typeMeta, runtimeFieldMap } = spec;
spec.fieldAttrs = savedObject.attributes.fieldAttrs
Expand All @@ -416,7 +422,7 @@ export class IndexPatternsService {
try {
spec.fields = await this.refreshFieldSpecMap(
spec.fields || {},
id,
savedObject.id,
spec.title as string,
{
pattern: title as string,
Expand All @@ -427,6 +433,7 @@ export class IndexPatternsService {
},
spec.fieldAttrs
);

// CREATE RUNTIME FIELDS
for (const [key, value] of Object.entries(runtimeFieldMap || {})) {
// do not create runtime field if mapped field exists
Expand Down Expand Up @@ -454,7 +461,7 @@ export class IndexPatternsService {
this.onError(err, {
title: i18n.translate('data.indexPatterns.fetchFieldErrorTitle', {
defaultMessage: 'Error fetching fields for index pattern {title} (ID: {id})',
values: { id, title },
values: { id: savedObject.id, title },
}),
});
}
Expand Down Expand Up @@ -548,9 +555,9 @@ export class IndexPatternsService {

async createAndSave(spec: IndexPatternSpec, override = false, skipFetchFields = false) {
const indexPattern = await this.create(spec, skipFetchFields);
await this.createSavedObject(indexPattern, override);
await this.setDefault(indexPattern.id!);
return indexPattern;
const createdIndexPattern = await this.createSavedObject(indexPattern, override);
await this.setDefault(createdIndexPattern.id!);
return createdIndexPattern;
}

/**
Expand All @@ -570,15 +577,20 @@ export class IndexPatternsService {
}

const body = indexPattern.getAsSavedObjectBody();
const response = await this.savedObjectsClient.create(savedObjectType, body, {
id: indexPattern.id,
});
indexPattern.id = response.id;
this.indexPatternCache.set(indexPattern.id, Promise.resolve(indexPattern));
const response: SavedObject<IndexPatternAttributes> = (await this.savedObjectsClient.create(
savedObjectType,
body,
{
id: indexPattern.id,
}
)) as SavedObject<IndexPatternAttributes>;

const createdIndexPattern = await this.initFromSavedObject(response);
this.indexPatternCache.set(createdIndexPattern.id!, Promise.resolve(createdIndexPattern));
if (this.savedObjectsCache) {
this.savedObjectsCache.push(response as SavedObject<IndexPatternSavedObjectAttrs>);
}
return indexPattern;
return createdIndexPattern;
}

/**
Expand Down
14 changes: 1 addition & 13 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1339,19 +1339,7 @@ export class IndexPattern implements IIndexPattern {
*/
time_zone?: string | undefined;
}>> | undefined;
getAsSavedObjectBody(): {
fieldAttrs: string | undefined;
title: string;
timeFieldName: string | undefined;
intervalName: string | undefined;
sourceFilters: string | undefined;
fields: string | undefined;
fieldFormatMap: string | undefined;
type: string | undefined;
typeMeta: string | undefined;
allowNoIndex: true | undefined;
runtimeFieldMap: string | undefined;
};
getAsSavedObjectBody(): IndexPatternAttributes;
// (undocumented)
getComputedFields(): {
storedFields: string[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import type { DataPluginStart, DataPluginStartDependencies } from '../../plugin'

const indexPatternSpecSchema = schema.object({
title: schema.string(),

id: schema.maybe(schema.string()),
version: schema.maybe(schema.string()),
id: schema.maybe(schema.string()),
type: schema.maybe(schema.string()),
timeFieldName: schema.maybe(schema.string()),
sourceFilters: schema.maybe(
Expand Down
14 changes: 1 addition & 13 deletions src/plugins/data/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -781,19 +781,7 @@ export class IndexPattern implements IIndexPattern {
*/
time_zone?: string | undefined;
}>> | undefined;
getAsSavedObjectBody(): {
fieldAttrs: string | undefined;
title: string;
timeFieldName: string | undefined;
intervalName: string | undefined;
sourceFilters: string | undefined;
fields: string | undefined;
fieldFormatMap: string | undefined;
type: string | undefined;
typeMeta: string | undefined;
allowNoIndex: true | undefined;
runtimeFieldMap: string | undefined;
};
getAsSavedObjectBody(): IndexPatternAttributes;
// (undocumented)
getComputedFields(): {
storedFields: string[];
Expand Down
Loading

0 comments on commit 979f37f

Please sign in to comment.