Skip to content

Commit

Permalink
Merge branch 'master' into reenable_categorization_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Jun 2, 2021
2 parents 2ce5147 + bdd7f7e commit 0f1c0a5
Show file tree
Hide file tree
Showing 23 changed files with 917 additions and 552 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 @@ -318,7 +318,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 @@ -331,12 +331,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;
}

/**
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 @@ -1336,19 +1336,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()),
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 @@ -780,19 +780,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

0 comments on commit 0f1c0a5

Please sign in to comment.