Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Index Patterns] Fix return saved index pattern object #101051

Merged
merged 7 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -308,7 +308,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 @@ -321,12 +321,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 @@ -230,7 +230,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 @@ -403,6 +403,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 @@ -412,7 +418,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 @@ -423,6 +429,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 @@ -450,7 +457,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 @@ -516,9 +523,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 @@ -538,15 +545,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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change. I want returned index pattern to go through the all transformations that are happening when saved objects is saved

}

/**
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 @@ -1335,19 +1335,7 @@ export class IndexPattern implements IIndexPattern {
delay?: string | undefined;
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()),
Dosant marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -779,19 +779,7 @@ export class IndexPattern implements IIndexPattern {
delay?: string | undefined;
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