diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts index b269eb749764..e6ce9d812baa 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts @@ -43,7 +43,13 @@ import { IndexPatternField, IIndexPatternFieldList, fieldList } from '../fields' import { formatHitProvider } from './format_hit'; import { flattenHitWrapper } from './flatten_hit'; import { FieldFormatsStartCommon, FieldFormat } from '../../field_formats'; -import { IndexPatternSpec, TypeMeta, SourceFilter, IndexPatternFieldMap } from '../types'; +import { + IndexPatternSpec, + TypeMeta, + SourceFilter, + IndexPatternFieldMap, + DataSourceRef, +} from '../types'; import { SerializedFieldFormat } from '../../../../expressions/common'; interface IndexPatternDeps { @@ -86,7 +92,8 @@ export class IndexPattern implements IIndexPattern { // savedObject version public version: string | undefined; public sourceFilters?: SourceFilter[]; - public dataSourcesJSON: string | undefined; + public dataSourcesJSON?: string; // todo: cleanup, only keep one + public dataSourceRef?: DataSourceRef; private originalSavedObjectBody: SavedObjectBody = {}; private shortDotsEnable: boolean = false; @@ -132,8 +139,18 @@ export class IndexPattern implements IIndexPattern { }); this.dataSourcesJSON = spec.dataSourcesJSON; + this.dataSourceRef = this.getDataSourceRef(spec.dataSourcesJSON); } + getDataSourceRef = (dataSourcesJSON?: string) => { + if (dataSourcesJSON) { + const refs = JSON.parse(dataSourcesJSON); + if (!_.isEmpty(refs)) { + return refs[0]; // only support 1-1 mapping now + } + } + }; + /** * Get last saved saved object fields */ diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts index 6213dc13dd26..43a132c71a08 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts @@ -127,7 +127,7 @@ export class IndexPatternsService { fields: ['title'], perPage: 10000, }); - } + } // todo: does this find method imcompatible with using loader? /** * Get list of index pattern ids @@ -384,6 +384,46 @@ export class IndexPatternsService { }; }; + /** + * Converts index pattern saved object to index pattern spec + * @param savedObject + */ + + indexPatternSavedObjectToSpec = (savedObject: IndexPatternSavedObject): IndexPatternSpec => { + const { + id, + version, + title, + timeFieldName, + intervalName, + fields, + sourceFilters, + fieldFormatMap, + typeMeta, + type, + dataSourcesJSON, + } = savedObject; + + const parsedSourceFilters = sourceFilters ? JSON.parse(sourceFilters) : undefined; + const parsedTypeMeta = typeMeta ? JSON.parse(typeMeta) : undefined; + const parsedFieldFormatMap = fieldFormatMap ? JSON.parse(fieldFormatMap) : {}; + const parsedFields: FieldSpec[] = fields ? JSON.parse(fields) : []; + + this.addFormatsToFields(parsedFields, parsedFieldFormatMap); + return { + id, + version, + title, + intervalName, + timeFieldName, + sourceFilters: parsedSourceFilters, + fields: this.fieldArrayToMap(parsedFields), + typeMeta: parsedTypeMeta, + type, + dataSourcesJSON, + }; + }; + /** * Get an index pattern by id. Cache optimized * @param id @@ -395,12 +435,15 @@ export class IndexPatternsService { return cache; } - const savedObject = await this.savedObjectsClient.get( - savedObjectType, - id - ); + // const savedObject = await this.savedObjectsClient.get( + // savedObjectType, + // id + // ); + + const savedIndexPattern: IndexPatternSavedObject = await this.savedIndexPattern.get(id); + const { version, fieldFormatMap } = savedIndexPattern; - if (!savedObject.version) { + if (!version) { throw new SavedObjectNotFound( savedObjectType, id, @@ -408,11 +451,10 @@ export class IndexPatternsService { ); } - const spec = this.savedObjectToSpec(savedObject); + // const spec = this.savedObjectToSpec(savedObject); + const spec = this.indexPatternSavedObjectToSpec(savedIndexPattern); const { title, type, typeMeta } = spec; - const parsedFieldFormats: FieldFormatMap = savedObject.attributes.fieldFormatMap - ? JSON.parse(savedObject.attributes.fieldFormatMap) - : {}; + const parsedFieldFormats: FieldFormatMap = fieldFormatMap ? JSON.parse(fieldFormatMap) : {}; const isFieldRefreshRequired = this.isFieldRefreshRequired(spec.fields); let isSaveRequired = isFieldRefreshRequired; diff --git a/src/plugins/data/common/index_patterns/types.ts b/src/plugins/data/common/index_patterns/types.ts index 29ac84f009e1..46fd736f60d7 100644 --- a/src/plugins/data/common/index_patterns/types.ts +++ b/src/plugins/data/common/index_patterns/types.ts @@ -49,6 +49,7 @@ export interface IIndexPattern { getFormatterForField?: ( field: IndexPatternField | IndexPatternField['spec'] | IFieldType ) => FieldFormat; + dataSourceRef?: DataSourceRef; } export interface IndexPatternAttributes { @@ -194,9 +195,14 @@ export interface IndexPatternSpec { fields?: IndexPatternFieldMap; typeMeta?: TypeMeta; type?: string; - dataSourcesJSON?: string; + dataSourcesJSON?: string; // todo: refactor to dataSourceRefs : DataSourceRef[] } export interface SourceFilter { value: string; } + +export interface DataSourceRef { + id: string; + type: string; +} diff --git a/src/plugins/data/public/index_patterns/saved_index_patterns/_saved_index_pattern.ts b/src/plugins/data/public/index_patterns/saved_index_patterns/_saved_index_pattern.ts index e5d711a2ec8b..cf20832d1e8a 100644 --- a/src/plugins/data/public/index_patterns/saved_index_patterns/_saved_index_pattern.ts +++ b/src/plugins/data/public/index_patterns/saved_index_patterns/_saved_index_pattern.ts @@ -23,7 +23,7 @@ export function createSavedIndexPatternClass(services: SavedObjectOpenSearchDash sourceFilters: 'text', fields: 'text', fieldFormatMap: 'text', - type: 'text', + type: 'keyword', typeMeta: 'text', dataSourcesJSON: 'text', // todo diff --git a/src/plugins/data/public/index_patterns/types.ts b/src/plugins/data/public/index_patterns/types.ts index 5eddbba0e4f8..0f4ebb66a0dd 100644 --- a/src/plugins/data/public/index_patterns/types.ts +++ b/src/plugins/data/public/index_patterns/types.ts @@ -12,9 +12,9 @@ export interface IndexPatternObject { title: string; intervalName?: string; timeFieldName?: string; - sourceFilters?: SourceFilter[]; - fields?: IndexPatternFieldMap; - typeMeta?: TypeMeta; + sourceFilters?: string; + fields?: string; + typeMeta?: string; type?: string; fieldFormatMap?: string; dataSourcesJSON?: string;