Skip to content

Commit

Permalink
[Discover] Emit an error when Lens fails so that Discover doesn't dis…
Browse files Browse the repository at this point in the history
…play 'no hits'
  • Loading branch information
davismcphee committed Nov 30, 2022
1 parent b5742b6 commit 59be2b0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/plugins/unified_histogram/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"ui": true,
"requiredPlugins": [],
"optionalPlugins": [],
"requiredBundles": ["data", "dataViews", "embeddable", "kibanaUtils"]
"requiredBundles": ["data", "dataViews", "embeddable", "kibanaUtils", "inspector"]
}
42 changes: 42 additions & 0 deletions src/plugins/unified_histogram/public/chart/histogram.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { REQUEST_DEBOUNCE_MS } from './consts';
import { act } from 'react-dom/test-utils';
import * as buildBucketInterval from './build_bucket_interval';
import * as useTimeRange from './use_time_range';
import { RequestStatus } from '@kbn/inspector-plugin/public';

const mockBucketInterval = { description: '1 minute', scale: undefined, scaled: false };
jest.spyOn(buildBucketInterval, 'buildBucketInterval').mockReturnValue(mockBucketInterval);
Expand Down Expand Up @@ -183,6 +184,47 @@ describe('Histogram', () => {
);
});

it('should execute onLoad correctly when the request has a failure status', async () => {
const { component, props } = mountComponent();
const embeddable = unifiedHistogramServicesMock.lens.EmbeddableComponent;
const onLoad = component.find(embeddable).props().onLoad;
const adapters = createDefaultInspectorAdapters();
jest
.spyOn(adapters.requests, 'getRequests')
.mockReturnValue([{ status: RequestStatus.ERROR } as any]);
onLoad(false, adapters);
expect(props.onTotalHitsChange).toHaveBeenLastCalledWith(
UnifiedHistogramFetchStatus.error,
undefined
);
expect(props.onChartLoad).toHaveBeenLastCalledWith({ complete: false, adapters });
});

it('should execute onLoad correctly when the response has shard failures', async () => {
const { component, props } = mountComponent();
const embeddable = unifiedHistogramServicesMock.lens.EmbeddableComponent;
const onLoad = component.find(embeddable).props().onLoad;
const adapters = createDefaultInspectorAdapters();
const rawResponse = {
_shards: {
total: 1,
successful: 0,
skipped: 0,
failed: 1,
failures: [],
},
};
jest
.spyOn(adapters.requests, 'getRequests')
.mockReturnValue([{ response: { json: { rawResponse } } } as any]);
onLoad(false, adapters);
expect(props.onTotalHitsChange).toHaveBeenLastCalledWith(
UnifiedHistogramFetchStatus.error,
undefined
);
expect(props.onChartLoad).toHaveBeenLastCalledWith({ complete: false, adapters });
});

it('should not recreate onLoad in debounced lens props when hits.total changes', async () => {
const { component, props } = mountComponent();
const embeddable = unifiedHistogramServicesMock.lens.EmbeddableComponent;
Expand Down
21 changes: 17 additions & 4 deletions src/plugins/unified_histogram/public/chart/histogram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { TimeRange } from '@kbn/es-query';
import useDebounce from 'react-use/lib/useDebounce';
import { ViewMode } from '@kbn/embeddable-plugin/public';
import type { TypedLensByValueInput } from '@kbn/lens-plugin/public';
import { RequestStatus } from '@kbn/inspector-plugin/public';
import {
UnifiedHistogramBucketInterval,
UnifiedHistogramChartContext,
Expand Down Expand Up @@ -73,17 +74,29 @@ export function Histogram({

const onLoad = useCallback(
(isLoading: boolean, adapters: Partial<DefaultInspectorAdapters> | undefined) => {
const lensRequest = adapters?.requests?.getRequests()[0];
const requestFailed = lensRequest?.status === RequestStatus.ERROR;
const json = lensRequest?.response?.json as
| IKibanaSearchResponse<estypes.SearchResponse>
| undefined;
const response = json?.rawResponse;

// Lens will swallow shard failures and return `isLoading: false` because it displays
// its own errors, but this causes us to emit onTotalHitsChange(UnifiedHistogramFetchStatus.complete, 0).
// This is incorrect, so we check for request failures and shard failures here, and emit an error instead.
if (requestFailed || response?._shards.failed) {
onTotalHitsChange?.(UnifiedHistogramFetchStatus.error, undefined);
onChartLoad?.({ complete: false, adapters: adapters ?? {} });
return;
}

const totalHits = adapters?.tables?.tables?.unifiedHistogram?.meta?.statistics?.totalCount;

onTotalHitsChange?.(
isLoading ? UnifiedHistogramFetchStatus.loading : UnifiedHistogramFetchStatus.complete,
totalHits ?? previousHits.current
);

const lensRequest = adapters?.requests?.getRequests()[0];
const json = lensRequest?.response?.json as IKibanaSearchResponse<estypes.SearchResponse>;
const response = json?.rawResponse;

if (response) {
const newBucketInterval = buildBucketInterval({
data,
Expand Down

0 comments on commit 59be2b0

Please sign in to comment.