[SearchProfiler] Handle scenario when user has no indices (#128066)

This commit is contained in:
Alison Goryachev 2022-03-23 11:46:42 -04:00 committed by GitHub
parent c55bb917fa
commit d253355234
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 14 deletions

View file

@ -21,6 +21,16 @@ interface ReturnValue {
error?: string;
}
interface ProfileResponse {
profile?: { shards: ShardSerialized[] };
_shards: {
failed: number;
skipped: number;
total: number;
successful: number;
};
}
const extractProfilerErrorMessage = (e: any): string | undefined => {
if (e.body?.attributes?.error?.reason) {
const { reason, line, col } = e.body.attributes.error;
@ -67,8 +77,7 @@ export const useRequestProfile = () => {
try {
const resp = await http.post<
| { ok: true; resp: { profile: { shards: ShardSerialized[] } } }
| { ok: false; err: { msg: string } }
{ ok: true; resp: ProfileResponse } | { ok: false; err: { msg: string } }
>('../api/searchprofiler/profile', {
body: JSON.stringify(payload),
headers: { 'Content-Type': 'application/json' },
@ -78,7 +87,23 @@ export const useRequestProfile = () => {
return { data: null, error: resp.err.msg };
}
return { data: resp.resp.profile.shards };
// If a user attempts to run Search Profiler without any indices,
// _shards=0 and a "profile" output will not be returned
if (resp.resp._shards.total === 0) {
notifications.addDanger({
'data-test-subj': 'noShardsNotification',
title: i18n.translate('xpack.searchProfiler.errorNoShardsTitle', {
defaultMessage: 'Unable to profile',
}),
text: i18n.translate('xpack.searchProfiler.errorNoShardsDescription', {
defaultMessage: 'Verify your index input matches a valid index',
}),
});
return { data: null };
}
return { data: resp.resp.profile!.shards };
} catch (e) {
const profilerErrorMessage = extractProfilerErrorMessage(e);
if (profilerErrorMessage) {