Skip to content

Commit

Permalink
[data views] Add index pattern setter and getter and deprecate title …
Browse files Browse the repository at this point in the history
…field (#142945)

* add index pattern setter and getter and deprecate title field
  • Loading branch information
mattkime authored Oct 10, 2022
1 parent 3d14629 commit c7b742c
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const IndexPatternEditorFlyoutContentComponent = ({
isAdHoc: false,
...(editData
? {
title: editData.title,
title: editData.getIndexPattern(),
id: editData.id,
name: editData.name,
...(editData.timeFieldName
Expand Down Expand Up @@ -142,7 +142,7 @@ const IndexPatternEditorFlyoutContentComponent = ({
};
}

if (editData && editData.title !== formData.title) {
if (editData && editData.getIndexPattern() !== formData.title) {
editDataViewModal({
dataViewName: formData.name || formData.title,
overlays,
Expand Down Expand Up @@ -324,7 +324,7 @@ const IndexPatternEditorFlyoutContentComponent = ({
useEffect(() => {
if (editData) {
loadSources();
reloadMatchedIndices(removeSpaces(editData.title));
reloadMatchedIndices(removeSpaces(editData.getIndexPattern()));
}
// We use the below eslint-disable as adding 'loadSources' and 'reloadMatchedIndices' as a dependency creates an infinite loop
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const IndexPatternFlyoutContentContainer = ({
let saveResponse;
if (editData) {
const { name = '', timeFieldName, title = '' } = dataViewSpec;
editData.title = title;
editData.setIndexPattern(title);
editData.name = name;
editData.timeFieldName = timeFieldName;
saveResponse = editData.isPersisted()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const WithFieldEditorDependencies =
const dependencies: Context = {
dataView: {
title: indexPatternNameForTest,
getIndexPattern: () => indexPatternNameForTest,
name: indexPatternNameForTest,
getName: () => indexPatternNameForTest,
fields: { getAll: spyIndexPatternGetAllFields },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ export const FieldEditorFlyoutContentContainer = ({

if (!editedField) {
throw new Error(
`Unable to find field named '${updatedField.name}' on index pattern '${dataView.title}'`
`Unable to find field named '${
updatedField.name
}' on index pattern '${dataView.getIndexPattern()}'`
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export const FieldPreviewProvider: FunctionComponent = ({ children }) => {
const [response, searchError] = await search
.search({
params: {
index: dataView.title,
index: dataView.getIndexPattern(),
body: {
size: limit,
},
Expand Down Expand Up @@ -269,7 +269,7 @@ export const FieldPreviewProvider: FunctionComponent = ({ children }) => {
const [response, searchError] = await search
.search({
params: {
index: dataView.title,
index: dataView.getIndexPattern(),
body: {
size: 1,
query: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const FieldPreviewHeader = () => {
{i18n.translate('indexPatternFieldEditor.fieldPreview.subTitle', {
defaultMessage: 'From: {from}',
values: {
from: from.value === 'cluster' ? dataView.title : i18nTexts.customData,
from: from.value === 'cluster' ? dataView.getIndexPattern() : i18nTexts.customData,
},
})}
</EuiTextColor>
Expand Down
15 changes: 14 additions & 1 deletion src/plugins/data_views/common/data_views/data_view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,19 @@ describe('IndexPattern', () => {
});
});

describe('getIndexPattern', () => {
test('should return the index pattern, labeled title on the data view spec', () => {
expect(indexPattern.getIndexPattern()).toBe(
stubbedSavedObjectIndexPattern().attributes.title
);
});

test('setIndexPattern', () => {
indexPattern.setIndexPattern('test');
expect(indexPattern.getIndexPattern()).toBe('test');
});
});

describe('getFormatterForField', () => {
test('should return the default one for empty objects', () => {
indexPattern.setFieldFormat('scriptedFieldWithEmptyFormatter', {});
Expand Down Expand Up @@ -452,7 +465,7 @@ describe('IndexPattern', () => {
metaFields: [],
});
expect(restoredPattern.id).toEqual(indexPattern.id);
expect(restoredPattern.title).toEqual(indexPattern.title);
expect(restoredPattern.getIndexPattern()).toEqual(indexPattern.getIndexPattern());
expect(restoredPattern.timeFieldName).toEqual(indexPattern.timeFieldName);
expect(restoredPattern.fields.length).toEqual(indexPattern.fields.length);
});
Expand Down
21 changes: 19 additions & 2 deletions src/plugins/data_views/common/data_views/data_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class DataView implements DataViewBase {
public id?: string;
/**
* Title of data view
* @deprecated use getIndexPattern instead
*/
public title: string = '';
/**
Expand Down Expand Up @@ -193,6 +194,22 @@ export class DataView implements DataViewBase {
*/
getName = () => (this.name ? this.name : this.title);

/**
* Get index pattern
* @returns index pattern string
*/

getIndexPattern = () => this.title;

/**
* Set index pattern
* @param string index pattern string
*/

setIndexPattern = (indexPattern: string) => {
this.title = indexPattern;
};

/**
* Get last saved saved object fields
*/
Expand Down Expand Up @@ -298,7 +315,7 @@ export class DataView implements DataViewBase {
const spec: DataViewSpec = {
id: this.id,
version: this.version,
title: this.title,
title: this.getIndexPattern(),
timeFieldName: this.timeFieldName,
sourceFilters: [...(this.sourceFilters || [])],
fields,
Expand Down Expand Up @@ -412,7 +429,7 @@ export class DataView implements DataViewBase {

return {
fieldAttrs: fieldAttrs ? JSON.stringify(fieldAttrs) : undefined,
title: this.title,
title: this.getIndexPattern(),
timeFieldName: this.timeFieldName,
sourceFilters: this.sourceFilters ? JSON.stringify(this.sourceFilters) : undefined,
fields: JSON.stringify(this.fields?.filter((field) => field.scripted) ?? []),
Expand Down
22 changes: 11 additions & 11 deletions src/plugins/data_views/common/data_views/data_views.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ describe('IndexPatterns', () => {

// This will conflict because samePattern did a save (from refreshFields)
// but the resave should work fine
pattern.title = 'foo2';
pattern.setIndexPattern('foo2');
await indexPatterns.updateSavedObject(pattern);

// This should not be able to recover
samePattern.title = 'foo3';
samePattern.setIndexPattern('foo3');

let result;
try {
Expand All @@ -241,18 +241,18 @@ describe('IndexPatterns', () => {
});

test('create', async () => {
const title = 'kibana-*';
const indexPattern = 'kibana-*';
indexPatterns.refreshFields = jest.fn();

const indexPattern = await indexPatterns.create({ title }, true);
expect(indexPattern).toBeInstanceOf(DataView);
expect(indexPattern.title).toBe(title);
const dataView = await indexPatterns.create({ title: indexPattern }, true);
expect(dataView).toBeInstanceOf(DataView);
expect(dataView.getIndexPattern()).toBe(indexPattern);
expect(indexPatterns.refreshFields).not.toBeCalled();

await indexPatterns.create({ title });
await indexPatterns.create({ title: indexPattern });
expect(indexPatterns.refreshFields).toBeCalled();
expect(indexPattern.id).toBeDefined();
expect(indexPattern.isPersisted()).toBe(false);
expect(dataView.id).toBeDefined();
expect(dataView.isPersisted()).toBe(false);
});

test('createSavedObject', async () => {
Expand All @@ -267,14 +267,14 @@ describe('IndexPatterns', () => {
version,
attributes: {
...savedObject.attributes,
title: dataView.title,
title: dataView.getIndexPattern(),
},
});

const indexPattern = await indexPatterns.createSavedObject(dataView);
expect(indexPattern).toBeInstanceOf(DataView);
expect(indexPattern.id).toBe(dataView.id);
expect(indexPattern.title).toBe(title);
expect(indexPattern.getIndexPattern()).toBe(title);
expect(indexPattern.isPersisted()).toBe(true);
});

Expand Down
10 changes: 5 additions & 5 deletions src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ export class DataViewsService {
type: dataView.type,
rollupIndex: dataView?.typeMeta?.params?.rollup_index,
allowNoIndex: dataView.allowNoIndex,
pattern: dataView.title as string,
pattern: dataView.getIndexPattern(),
metaFields,
});
};
Expand Down Expand Up @@ -556,7 +556,7 @@ export class DataViewsService {
if (err instanceof DataViewMissingIndices) {
this.onNotification(
{ title: err.message, color: 'danger', iconType: 'alert' },
`refreshFields:${indexPattern.title}`
`refreshFields:${indexPattern.getIndexPattern()}`
);
}

Expand All @@ -565,10 +565,10 @@ export class DataViewsService {
{
title: i18n.translate('dataViews.fetchFieldErrorTitle', {
defaultMessage: 'Error fetching fields for data view {title} (ID: {id})',
values: { id: indexPattern.id, title: indexPattern.title },
values: { id: indexPattern.id, title: indexPattern.getIndexPattern() },
}),
},
indexPattern.title
indexPattern.getIndexPattern()
);
}
};
Expand Down Expand Up @@ -1000,7 +1000,7 @@ export class DataViewsService {

this.onNotification(
{ title, color: 'danger' },
`updateSavedObject:${indexPattern.title}`
`updateSavedObject:${indexPattern.getIndexPattern()}`
);
throw err;
}
Expand Down

0 comments on commit c7b742c

Please sign in to comment.