Skip to content

Commit

Permalink
[DataViews] Remove No matching indices found toasts (#151788)
Browse files Browse the repository at this point in the history
## Summary
This PR removes the `No matching indices found ` toasts, part of some
toast storms . The toasts were triggered when there were no matching
indices of a given data view's index pattern.

If indication is needed, that there are no available matching indices
(it could also be an issue of lack of permissions to the requested
indices), it's easy to check following property of an existing data
view, if the length of matchedIndices is 0, this equals a 'indices are
missing condition`.
  • Loading branch information
kertal authored Mar 6, 2023
1 parent 0d462db commit ec690bc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
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

0 comments on commit ec690bc

Please sign in to comment.