Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Jun 1, 2021
1 parent 9dfe69f commit f150fad
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
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,11 +321,9 @@ 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,
type: this.type!,
typeMeta: this.typeMeta ? JSON.stringify(this.typeMeta) : undefined,
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 @@ -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 Down Expand Up @@ -451,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 @@ -539,11 +545,15 @@ export class IndexPatternsService {
}

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

const createdIndexPattern = await this.getSavedObjectAndInit(response.id);
const createdIndexPattern = await this.initFromSavedObject(response);
this.indexPatternCache.set(createdIndexPattern.id!, Promise.resolve(createdIndexPattern));
if (this.savedObjectsCache) {
this.savedObjectsCache.push(response as SavedObject<IndexPatternSavedObjectAttrs>);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/index_patterns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface IndexPatternAttributes {
type: string;
fields: string;
title: string;
typeMeta: string;
typeMeta?: string;
timeFieldName?: string;
intervalName?: string;
sourceFilters?: string;
Expand Down

0 comments on commit f150fad

Please sign in to comment.