Skip to content

Commit

Permalink
Merge pull request #20 from elastic-analytics/yt_ds_POC
Browse files Browse the repository at this point in the history
Modify to use loader to get index pattern & Add dataSourceRef
  • Loading branch information
kristenTian authored Jul 21, 2022
2 parents e41efcc + 64a66f4 commit 89c51aa
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -395,24 +435,26 @@ export class IndexPatternsService {
return cache;
}

const savedObject = await this.savedObjectsClient.get<IndexPatternAttributes>(
savedObjectType,
id
);
// const savedObject = await this.savedObjectsClient.get<IndexPatternAttributes>(
// savedObjectType,
// id
// );

const savedIndexPattern: IndexPatternSavedObject = await this.savedIndexPattern.get(id);
const { version, fieldFormatMap } = savedIndexPattern;

if (!savedObject.version) {
if (!version) {
throw new SavedObjectNotFound(
savedObjectType,
id,
'management/opensearch-dashboards/indexPatterns'
);
}

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;
Expand Down
8 changes: 7 additions & 1 deletion src/plugins/data/common/index_patterns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface IIndexPattern {
getFormatterForField?: (
field: IndexPatternField | IndexPatternField['spec'] | IFieldType
) => FieldFormat;
dataSourceRef?: DataSourceRef;
}

export interface IndexPatternAttributes {
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function createSavedIndexPatternClass(services: SavedObjectOpenSearchDash
sourceFilters: 'text',
fields: 'text',
fieldFormatMap: 'text',
type: 'text',
type: 'keyword',
typeMeta: 'text',
dataSourcesJSON: 'text',
// todo
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/data/public/index_patterns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 89c51aa

Please sign in to comment.