Skip to content

Commit

Permalink
hasData service - hit search api in case of an error with resolve api (
Browse files Browse the repository at this point in the history
  • Loading branch information
shivindera authored May 20, 2022
1 parent 7c37eda commit 1d8bc7e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/plugins/data_views/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type {
HasDataViewsResponse,
IndicesResponse,
IndicesResponseModified,
IndicesViaSearchResponse,
} from './types';

// Export plugin after all other imports
Expand Down
61 changes: 52 additions & 9 deletions src/plugins/data_views/public/services/has_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

import { CoreStart, HttpStart } from '@kbn/core/public';
import { DEFAULT_ASSETS_TO_IGNORE } from '../../common';
import { HasDataViewsResponse, IndicesResponse, IndicesResponseModified } from '..';
import {
HasDataViewsResponse,
IndicesResponse,
IndicesResponseModified,
IndicesViaSearchResponse,
} from '..';

export class HasData {
private removeAliases = (source: IndicesResponseModified): boolean => !source.item.indices;
Expand Down Expand Up @@ -77,6 +82,41 @@ export class HasData {
return source;
};

private getIndicesViaSearch = async ({
http,
pattern,
showAllIndices,
}: {
http: HttpStart;
pattern: string;
showAllIndices: boolean;
}): Promise<boolean> =>
http
.post<IndicesViaSearchResponse>(`/internal/search/ese`, {
body: JSON.stringify({
params: {
ignore_unavailable: true,
expand_wildcards: showAllIndices ? 'all' : 'open',
index: pattern,
body: {
size: 0, // no hits
aggs: {
indices: {
terms: {
field: '_index',
size: 200,
},
},
},
},
},
}),
})
.then((resp) => {
return !!(resp && resp.total >= 0);
})
.catch(() => false);

private getIndices = async ({
http,
pattern,
Expand All @@ -96,26 +136,29 @@ export class HasData {
} else {
return this.responseToItemArray(response);
}
})
.catch(() => []);
});

private checkLocalESData = (http: HttpStart): Promise<boolean> =>
this.getIndices({
http,
pattern: '*',
showAllIndices: false,
}).then((dataSources: IndicesResponseModified[]) => {
return dataSources.some(this.isUserDataIndex);
});
})
.then((dataSources: IndicesResponseModified[]) => {
return dataSources.some(this.isUserDataIndex);
})
.catch(() => this.getIndicesViaSearch({ http, pattern: '*', showAllIndices: false }));

private checkRemoteESData = (http: HttpStart): Promise<boolean> =>
this.getIndices({
http,
pattern: '*:*',
showAllIndices: false,
}).then((dataSources: IndicesResponseModified[]) => {
return !!dataSources.filter(this.removeAliases).length;
});
})
.then((dataSources: IndicesResponseModified[]) => {
return !!dataSources.filter(this.removeAliases).length;
})
.catch(() => this.getIndicesViaSearch({ http, pattern: '*:*', showAllIndices: false }));

// Data Views

Expand Down
4 changes: 4 additions & 0 deletions src/plugins/data_views/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export interface IndicesResponse {
data_streams?: IndicesResponseItemDataStream[];
}

export interface IndicesViaSearchResponse {
total: number;
}

export interface HasDataViewsResponse {
hasDataView: boolean;
hasUserDataView: boolean;
Expand Down

0 comments on commit 1d8bc7e

Please sign in to comment.