Skip to content

Commit

Permalink
[Partial Results] Move inspector adapter integration into search sour…
Browse files Browse the repository at this point in the history
…ce (#96241)

* Move inspector adapter integration into search source

* docs and ts

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
lizozom and kibanamachine committed Apr 9, 2021
1 parent 6de70f8 commit 0208c96
Show file tree
Hide file tree
Showing 29 changed files with 205 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ISearchOptions
| [isRestore](./kibana-plugin-plugins-data-public.isearchoptions.isrestore.md) | <code>boolean</code> | Whether the session is restored (i.e. search requests should re-use the stored search IDs, rather than starting from scratch) |
| [isStored](./kibana-plugin-plugins-data-public.isearchoptions.isstored.md) | <code>boolean</code> | Whether the session is already saved (i.e. sent to background) |
| [legacyHitsTotal](./kibana-plugin-plugins-data-public.isearchoptions.legacyhitstotal.md) | <code>boolean</code> | Request the legacy format for the total number of hits. If sending <code>rest_total_hits_as_int</code> to something other than <code>true</code>, this should be set to <code>false</code>. |
| [requestResponder](./kibana-plugin-plugins-data-public.isearchoptions.requestresponder.md) | <code>RequestResponder</code> | |
| [sessionId](./kibana-plugin-plugins-data-public.isearchoptions.sessionid.md) | <code>string</code> | A session ID, grouping multiple search requests into a single session. |
| [strategy](./kibana-plugin-plugins-data-public.isearchoptions.strategy.md) | <code>string</code> | Use this option to force using a specific server side search strategy. Leave empty to use the default strategy. |

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [ISearchOptions](./kibana-plugin-plugins-data-public.isearchoptions.md) &gt; [requestResponder](./kibana-plugin-plugins-data-public.isearchoptions.requestresponder.md)

## ISearchOptions.requestResponder property

<b>Signature:</b>

```typescript
requestResponder?: RequestResponder;
```
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ search: {
timeRange: import("../common").TimeRange | undefined;
} | undefined;
};
getRequestInspectorStats: typeof getRequestInspectorStats;
getResponseInspectorStats: typeof getResponseInspectorStats;
tabifyAggResponse: typeof tabifyAggResponse;
tabifyGetColumns: typeof tabifyGetColumns;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Returns body contents of the search request, often referred as query DSL.
<b>Signature:</b>

```typescript
getSearchRequestBody(): Promise<any>;
getSearchRequestBody(): any;
```
<b>Returns:</b>

`Promise<any>`
`any`

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ISearchOptions
| [isRestore](./kibana-plugin-plugins-data-server.isearchoptions.isrestore.md) | <code>boolean</code> | Whether the session is restored (i.e. search requests should re-use the stored search IDs, rather than starting from scratch) |
| [isStored](./kibana-plugin-plugins-data-server.isearchoptions.isstored.md) | <code>boolean</code> | Whether the session is already saved (i.e. sent to background) |
| [legacyHitsTotal](./kibana-plugin-plugins-data-server.isearchoptions.legacyhitstotal.md) | <code>boolean</code> | Request the legacy format for the total number of hits. If sending <code>rest_total_hits_as_int</code> to something other than <code>true</code>, this should be set to <code>false</code>. |
| [requestResponder](./kibana-plugin-plugins-data-server.isearchoptions.requestresponder.md) | <code>RequestResponder</code> | |
| [sessionId](./kibana-plugin-plugins-data-server.isearchoptions.sessionid.md) | <code>string</code> | A session ID, grouping multiple search requests into a single session. |
| [strategy](./kibana-plugin-plugins-data-server.isearchoptions.strategy.md) | <code>string</code> | Use this option to force using a specific server side search strategy. Leave empty to use the default strategy. |

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [ISearchOptions](./kibana-plugin-plugins-data-server.isearchoptions.md) &gt; [requestResponder](./kibana-plugin-plugins-data-server.isearchoptions.requestresponder.md)

## ISearchOptions.requestResponder property

<b>Signature:</b>

```typescript
requestResponder?: RequestResponder;
```
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ search: {
toAbsoluteDates: typeof toAbsoluteDates;
calcAutoIntervalLessThan: typeof calcAutoIntervalLessThan;
};
getRequestInspectorStats: typeof getRequestInspectorStats;
getResponseInspectorStats: typeof getResponseInspectorStats;
tabifyAggResponse: typeof tabifyAggResponse;
tabifyGetColumns: typeof tabifyGetColumns;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/search_examples/public/search/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ export const SearchExamplesApp = ({
});
}

setRequest(await searchSource.getSearchRequestBody());
const res = await searchSource.fetch();
setRequest(searchSource.getSearchRequestBody());
const res = await searchSource.fetch$().toPromise();
setResponse(res);

const message = <EuiText>Searched {res.hits.total} documents.</EuiText>;
Expand Down
50 changes: 20 additions & 30 deletions src/plugins/data/common/search/aggs/buckets/terms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import { noop } from 'lodash';
import { i18n } from '@kbn/i18n';
import type { RequestAdapter } from 'src/plugins/inspector/common';

import { BucketAggType, IBucketAggConfig } from './bucket_agg_type';
import { BUCKET_TYPES } from './bucket_agg_types';
Expand All @@ -21,7 +20,6 @@ import { aggTermsFnName } from './terms_fn';
import { AggConfigSerialized, BaseAggParams } from '../types';

import { KBN_FIELD_TYPES } from '../../../../common';
import { getRequestInspectorStats, getResponseInspectorStats } from '../../expressions';

import {
buildOtherBucketAgg,
Expand Down Expand Up @@ -103,36 +101,28 @@ export const getTermsBucketAgg = () =>

nestedSearchSource.setField('aggs', filterAgg);

let request: ReturnType<RequestAdapter['start']> | undefined;
if (inspectorRequestAdapter) {
request = inspectorRequestAdapter.start(
i18n.translate('data.search.aggs.buckets.terms.otherBucketTitle', {
defaultMessage: 'Other bucket',
const requestResponder = inspectorRequestAdapter?.start(
i18n.translate('data.search.aggs.buckets.terms.otherBucketTitle', {
defaultMessage: 'Other bucket',
}),
{
description: i18n.translate('data.search.aggs.buckets.terms.otherBucketDescription', {
defaultMessage:
'This request counts the number of documents that fall ' +
'outside the criterion of the data buckets.',
}),
{
description: i18n.translate('data.search.aggs.buckets.terms.otherBucketDescription', {
defaultMessage:
'This request counts the number of documents that fall ' +
'outside the criterion of the data buckets.',
}),
searchSessionId,
}
);
nestedSearchSource.getSearchRequestBody().then((body) => {
request!.json(body);
});
request.stats(getRequestInspectorStats(nestedSearchSource));
}
searchSessionId,
}
);

const response = await nestedSearchSource
.fetch$({
abortSignal,
sessionId: searchSessionId,
requestResponder,
})
.toPromise();

const response = await nestedSearchSource.fetch({
abortSignal,
sessionId: searchSessionId,
});
if (request) {
request
.stats(getResponseInspectorStats(response, nestedSearchSource))
.ok({ json: response });
}
resp = mergeOtherBucketAggResponse(aggConfigs, resp, response, aggConfig, filterAgg());
}
if (aggConfig.params.missingBucket) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('esaggs expression function - public', () => {
test('calls searchSource.fetch', async () => {
await handleRequest(mockParams);
const searchSource = await mockParams.searchSourceService.create();
expect(searchSource.fetch).toHaveBeenCalledWith({
expect(searchSource.fetch$).toHaveBeenCalledWith({
abortSignal: mockParams.abortSignal,
sessionId: mockParams.searchSessionId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
import { IAggConfigs } from '../../aggs';
import { ISearchStartSearchSource } from '../../search_source';
import { tabifyAggResponse } from '../../tabify';
import { getRequestInspectorStats, getResponseInspectorStats } from '../utils';

/** @internal */
export interface RequestHandlerParams {
Expand All @@ -41,6 +40,21 @@ export interface RequestHandlerParams {
getNow?: () => Date;
}

function getRequestMainResponder(inspectorAdapters: Adapters, searchSessionId?: string) {
return inspectorAdapters.requests?.start(
i18n.translate('data.functions.esaggs.inspector.dataRequest.title', {
defaultMessage: 'Data',
}),
{
description: i18n.translate('data.functions.esaggs.inspector.dataRequest.description', {
defaultMessage:
'This request queries Elasticsearch to fetch the data for the visualization.',
}),
searchSessionId,
}
);
}

export const handleRequest = async ({
abortSignal,
aggs,
Expand Down Expand Up @@ -113,52 +127,19 @@ export const handleRequest = async ({
requestSearchSource.setField('filter', filters);
requestSearchSource.setField('query', query);

let request;
if (inspectorAdapters.requests) {
inspectorAdapters.requests.reset();
request = inspectorAdapters.requests.start(
i18n.translate('data.functions.esaggs.inspector.dataRequest.title', {
defaultMessage: 'Data',
}),
{
description: i18n.translate('data.functions.esaggs.inspector.dataRequest.description', {
defaultMessage:
'This request queries Elasticsearch to fetch the data for the visualization.',
}),
searchSessionId,
}
);
request.stats(getRequestInspectorStats(requestSearchSource));
}

try {
const response = await requestSearchSource.fetch({
abortSignal,
sessionId: searchSessionId,
});

if (request) {
request.stats(getResponseInspectorStats(response, searchSource)).ok({ json: response });
}
inspectorAdapters.requests?.reset();
const requestResponder = getRequestMainResponder(inspectorAdapters, searchSessionId);

(searchSource as any).rawResponse = response;
} catch (e) {
// Log any error during request to the inspector
if (request) {
request.error({ json: e });
}
throw e;
} finally {
// Add the request body no matter if things went fine or not
if (request) {
request.json(await requestSearchSource.getSearchRequestBody());
}
}
const response$ = await requestSearchSource.fetch$({
abortSignal,
sessionId: searchSessionId,
requestResponder,
});

// Note that rawResponse is not deeply cloned here, so downstream applications using courier
// must take care not to mutate it, or it could have unintended side effects, e.g. displaying
// response data incorrectly in the inspector.
let response = (searchSource as any).rawResponse;
let response = await response$.toPromise();
for (const agg of aggs.aggs) {
if (agg.enabled && typeof agg.type.postFlightRequest === 'function') {
response = await agg.type.postFlightRequest(
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/common/search/expressions/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
* Side Public License, v 1.
*/

export * from './courier_inspector_stats';
export * from './function_wrapper';
1 change: 1 addition & 0 deletions src/plugins/data/common/search/search_source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export { createSearchSource } from './create_search_source';
export { injectReferences } from './inject_references';
export { extractReferences } from './extract_references';
export { parseSearchSourceJSON } from './parse_json';
export { getResponseInspectorStats } from './inspect';
export * from './fetch';
export * from './legacy';
export * from './search_source';
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/data/common/search/search_source/inspect/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * from './inspector_stats';
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

import { i18n } from '@kbn/i18n';
import type { estypes } from '@elastic/elasticsearch';
import { ISearchSource } from 'src/plugins/data/public';
import { RequestStatistics } from 'src/plugins/inspector/common';
import type { ISearchSource } from 'src/plugins/data/public';
import type { RequestStatistics } from 'src/plugins/inspector/common';

/** @public */
export function getRequestInspectorStats(searchSource: ISearchSource) {
Expand Down
Loading

0 comments on commit 0208c96

Please sign in to comment.