Skip to content

Commit

Permalink
Link datasource to indexpattern
Browse files Browse the repository at this point in the history
Signed-off-by: Kristen Tian <tyarong@amazon.com>
  • Loading branch information
kristenTian committed Aug 10, 2022
1 parent 4727e54 commit e204ce4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 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,
SavedObjectReference,
} from '../types';
import { SerializedFieldFormat } from '../../../../expressions/common';

interface IndexPatternDeps {
Expand All @@ -67,6 +73,7 @@ interface SavedObjectBody {

type FormatFieldFn = (hit: Record<string, any>, fieldName: string) => any;

const DATA_SOURCE_REFERNECE_NAME = 'dataSource';
export class IndexPattern implements IIndexPattern {
public id?: string;
public title: string = '';
Expand All @@ -86,6 +93,7 @@ export class IndexPattern implements IIndexPattern {
// savedObject version
public version: string | undefined;
public sourceFilters?: SourceFilter[];
public dataSourceRef?: SavedObjectReference;
private originalSavedObjectBody: SavedObjectBody = {};
private shortDotsEnable: boolean = false;
private fieldFormats: FieldFormatsStartCommon;
Expand Down Expand Up @@ -128,6 +136,7 @@ export class IndexPattern implements IIndexPattern {
this.fieldFormatMap = _.mapValues(fieldFormatMap, (mapping) => {
return this.deserializeFieldFormatMap(mapping);
});
this.dataSourceRef = spec.dataSourceRef;
}

/**
Expand Down Expand Up @@ -357,6 +366,17 @@ export class IndexPattern implements IIndexPattern {
};
}

getSaveObjectReference() {
return this.dataSourceRef
? [
{
...this.dataSourceRef,
name: DATA_SOURCE_REFERNECE_NAME,
},
]
: [];
}

/**
* Provide a field, get its formatter
* @param field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,11 @@ export class IndexPatternsService {
}

const body = indexPattern.getAsSavedObjectBody();
const references = indexPattern.getSaveObjectReference();

const response = await this.savedObjectsClient.create(savedObjectType, body, {
id: indexPattern.id,
references,
});
indexPattern.id = response.id;
indexPatternCache.set(indexPattern.id, indexPattern);
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/data/common/index_patterns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ export interface FieldSpec {

export type IndexPatternFieldMap = Record<string, FieldSpec>;

export interface SavedObjectReference {
name?: string;
id: string;
type: string;
}
export interface IndexPatternSpec {
id?: string;
version?: string;
Expand All @@ -194,6 +199,7 @@ export interface IndexPatternSpec {
fields?: IndexPatternFieldMap;
typeMeta?: TypeMeta;
type?: string;
dataSourceRef?: SavedObjectReference;
}

export interface SourceFilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ export class CreateIndexPatternWizard extends Component<
createIndexPattern = async (timeFieldName: string | undefined, indexPatternId: string) => {
let emptyPattern: IndexPattern;
const { history } = this.props;
const { indexPattern } = this.state;
const { indexPattern, dataSourceRef } = this.state;

try {
emptyPattern = await this.context.services.data.indexPatterns.createAndSave({
id: indexPatternId,
title: indexPattern,
timeFieldName,
dataSourceRef,
...this.state.indexPatternCreationType.getIndexPatternMappings(),
});
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function mountManagementSection(
] = await getStartServices();
const canSave = Boolean(application.capabilities.indexPatterns.save);
const dataSourceEnabled = false;
// todo: Boolean(application.capabilities.indexPatterns.dataSourceEnabled) or get from plugin constructor;
// todo: Boolean(application.capabilities.indexPatterns.dataSourceEnabled) or get from plugin constructor; #2111

if (!canSave) {
chrome.setBadge(readOnlyBadge);
Expand Down

0 comments on commit e204ce4

Please sign in to comment.