Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataViews] Remove No matching indices found toasts 🍞 #151788

Merged
merged 6 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/plugins/data_views/common/data_views/data_views.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import {
IDataViewsApiClient,
} from '../types';
import { stubbedSavedObjectIndexPattern } from '../data_view.stub';
import { DataViewMissingIndices } from '../lib';

const createFieldsFetcher = () =>
({
getFieldsForWildcard: jest.fn(async () => ({ fields: [], indices: [] })),
getFieldsForWildcard: jest.fn(async () => ({ fields: [], indices: ['test'] })),
} as any as IDataViewsApiClient);

const fieldFormats = fieldFormatsMock;
Expand Down Expand Up @@ -71,6 +72,7 @@ describe('IndexPatterns', () => {
const indexPatternObj = { id: 'id', version: 'a', attributes: { title: 'title' } };

beforeEach(() => {
jest.clearAllMocks();
savedObjectsClient = {} as SavedObjectsClientCommon;
savedObjectsClient.find = jest.fn(
() => Promise.resolve([indexPatternObj]) as Promise<Array<SavedObject<any>>>
Expand Down Expand Up @@ -220,6 +222,35 @@ describe('IndexPatterns', () => {
expect((await indexPatterns.get(id)).fields.length).toBe(1);
});

test('existing indices, so dataView.matchedIndices.length equals 1 ', async () => {
const id = '1';
setDocsourcePayload(id, {
id: 'foo',
version: 'foo',
attributes: {
title: 'something',
},
});
const dataView = await indexPatterns.get(id);
expect(dataView.matchedIndices.length).toBe(1);
});

test('missing indices, so dataView.matchedIndices.length equals 0 ', async () => {
const id = '1';
setDocsourcePayload(id, {
id: 'foo',
version: 'foo',
attributes: {
title: 'something',
},
});
apiClient.getFieldsForWildcard = jest.fn().mockImplementation(async () => {
throw new DataViewMissingIndices('Catch me if you can!');
});
const dataView = await indexPatterns.get(id);
expect(dataView.matchedIndices.length).toBe(0);
});

test('savedObjectCache pre-fetches title, type, typeMeta', async () => {
expect(await indexPatterns.getIds()).toEqual(['id']);
expect(savedObjectsClient.find).toHaveBeenCalledWith({
Expand Down
49 changes: 19 additions & 30 deletions src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,8 @@ export class DataViewsService {
};

/**
* Refresh field list for a given index pattern.
* @param indexPattern
* Refresh field list for a given data view.
* @param dataView
* @param displayErrors - If set false, API consumer is responsible for displaying and handling errors.
*/
refreshFields = async (dataView: DataView, displayErrors: boolean = true) => {
Expand All @@ -582,22 +582,19 @@ export class DataViewsService {
await this.refreshFieldsFn(dataView);
} catch (err) {
if (err instanceof DataViewMissingIndices) {
this.onNotification(
{ title: err.message, color: 'danger', iconType: 'error' },
`refreshFields:${dataView.getIndexPattern()}`
// not considered an error, check dataView.matchedIndices.length to be 0
} else {
this.onError(
err,
{
title: i18n.translate('dataViews.fetchFieldErrorTitle', {
defaultMessage: 'Error fetching fields for data view {title} (ID: {id})',
values: { id: dataView.id, title: dataView.getIndexPattern() },
}),
},
dataView.getIndexPattern()
);
}

this.onError(
err,
{
title: i18n.translate('dataViews.fetchFieldErrorTitle', {
defaultMessage: 'Error fetching fields for data view {title} (ID: {id})',
values: { id: dataView.id, title: dataView.getIndexPattern() },
}),
},
dataView.getIndexPattern()
);
}
};

Expand Down Expand Up @@ -635,14 +632,12 @@ export class DataViewsService {
return { fields: this.fieldArrayToMap(updatedFieldList, fieldAttrs), indices };
} catch (err) {
if (err instanceof DataViewMissingIndices) {
if (displayErrors) {
this.onNotification(
{ title: err.message, color: 'danger', iconType: 'error' },
`refreshFieldSpecMap:${title}`
);
}
// not considered an error, check dataView.matchedIndices.length to be 0
return {};
}
if (!displayErrors) {
throw err;
}

this.onError(
err,
Expand Down Expand Up @@ -798,19 +793,13 @@ export class DataViewsService {
const fieldsAndIndices = await this.initFromSavedObjectLoadFields({
savedObjectId: savedObject.id,
spec,
displayErrors,
});
fields = fieldsAndIndices.fields;
indices = fieldsAndIndices.indices;
} catch (err) {
if (err instanceof DataViewMissingIndices) {
this.onNotification(
{
title: err.message,
color: 'danger',
iconType: 'error',
},
`initFromSavedObject:${spec.title}`
);
// not considered an error, check dataView.matchedIndices.length to be 0
} else {
this.onError(
err,
Expand Down