Skip to content

Commit

Permalink
Refactor esaggs & courier request handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Nov 11, 2020
1 parent c21656d commit 0476f73
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,32 @@
*/

import { set } from '@elastic/safer-lodash-set';
import { FormattedData } from '../../../../../plugins/inspector/public';
import { TabbedTable } from '../../../common';
import { FormatFactory } from '../../../common/field_formats/utils';
import { createFilter } from './create_filter';
import {
FormattedData,
TabularData,
TabularDataValue,
} from '../../../../../../plugins/inspector/common';
import { TabbedTable } from '../../../../common';
import { FormatFactory } from '../../../../common/field_formats/utils';
import { FilterManager } from '../../../query';
import { createFilter } from '../create_filter';

/**
* @deprecated
*
* Do not use this function.
*
* @todo This function is used only by Courier. Courier will
* soon be removed, and this function will be deleted, too. If Courier is not removed,
* move this function inside Courier.
*
* ---
*
* This function builds tabular data from the response and attaches it to the
* inspector. It will only be called when the data view in the inspector is opened.
*
* @internal
*/
export async function buildTabularInspectorData(
table: TabbedTable,
{
queryFilter,
deserializeFieldFormat,
queryFilter,
}: {
queryFilter: { addFilters: (filter: any) => void };
deserializeFieldFormat: FormatFactory;
queryFilter?: Pick<FilterManager, 'addFilters'>;
}
) {
): Promise<TabularData> {
const aggConfigs = table.columns.map((column) => column.aggConfig);
const rows = table.rows.map((row) => {
return table.columns.reduce<Record<string, FormattedData>>((prev, cur, colIndex) => {
Expand Down Expand Up @@ -74,8 +71,9 @@ export async function buildTabularInspectorData(
name: col.name,
field: `col-${colIndex}-${col.aggConfig.id}`,
filter:
queryFilter &&
isCellContentFilterable &&
((value: { raw: unknown }) => {
((value: TabularDataValue) => {
const rowIndex = rows.findIndex(
(row) => row[`col-${colIndex}-${col.aggConfig.id}`].raw === value.raw
);
Expand All @@ -86,8 +84,9 @@ export async function buildTabularInspectorData(
}
}),
filterOut:
queryFilter &&
isCellContentFilterable &&
((value: { raw: unknown }) => {
((value: TabularDataValue) => {
const rowIndex = rows.findIndex(
(row) => row[`col-${colIndex}-${col.aggConfig.id}`].raw === value.raw
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import { Datatable, DatatableColumn } from 'src/plugins/expressions/common';
import { Adapters } from 'src/plugins/inspector/common';

import { calculateBounds, EsaggsExpressionFunctionDefinition } from '../../../../common';
import { getIndexPatterns, getQueryService, getSearchService } from '../../../services';
import {
getFieldFormats,
getIndexPatterns,
getQueryService,
getSearchService,
} from '../../../services';
import { handleRequest } from './request_handler';

const name = 'esaggs';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
* under the License.
*/

import { hasIn } from 'lodash';
import { i18n } from '@kbn/i18n';

import { Adapters } from 'src/plugins/inspector/common';

import {
Expand All @@ -38,42 +36,43 @@ import {
ISearchSource,
tabifyAggResponse,
} from '../../../../common/search';
import { FormatFactory } from '../../../../common/field_formats/utils';

import { FilterManager } from '../../../query';
import { getFieldFormats } from '../../../services';
import { buildTabularInspectorData } from '../build_tabular_inspector_data';
import { buildTabularInspectorData } from './build_tabular_inspector_data';

interface RequestHandlerParams {
searchSource: ISearchSource;
abortSignal?: AbortSignal;
aggs: IAggConfigs;
timeRange?: TimeRange;
timeFields?: string[];
indexPattern?: IIndexPattern;
query?: Query;
deserializeFieldFormat: FormatFactory;
filters?: Filter[];
filterManager: FilterManager;
partialRows?: boolean;
indexPattern?: IIndexPattern;
inspectorAdapters: Adapters;
metricsAtAllLevels?: boolean;
visParams?: any;
abortSignal?: AbortSignal;
partialRows?: boolean;
query?: Query;
queryFilter?: Pick<FilterManager, 'addFilters'>;
searchSessionId?: string;
searchSource: ISearchSource;
timeFields?: string[];
timeRange?: TimeRange;
}

export const handleRequest = async ({
searchSource,
abortSignal,
aggs,
timeRange,
timeFields,
indexPattern,
query,
deserializeFieldFormat,
filters,
partialRows,
metricsAtAllLevels,
indexPattern,
inspectorAdapters,
filterManager,
abortSignal,
metricsAtAllLevels,
partialRows,
query,
queryFilter,
searchSessionId,
searchSource,
timeFields,
timeRange,
}: RequestHandlerParams) => {
// Create a new search source that inherits the original search source
// but has the appropriate timeRange applied via a filter.
Expand Down Expand Up @@ -155,19 +154,17 @@ export const handleRequest = async ({
throw e;
} finally {
// Add the request body no matter if things went fine or not
requestSearchSource.getSearchRequestBody().then((req: unknown) => {
request.json(req);
});
request.json(await requestSearchSource.getSearchRequestBody());
}

// 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 resp = (searchSource as any).rawResponse;
let response = (searchSource as any).rawResponse;
for (const agg of aggs.aggs) {
if (hasIn(agg, 'type.postFlightRequest')) {
resp = await agg.type.postFlightRequest(
resp,
if (typeof agg.type.postFlightRequest === 'function') {
response = await agg.type.postFlightRequest(
response,
aggs,
agg,
requestSearchSource,
Expand All @@ -177,8 +174,6 @@ export const handleRequest = async ({
}
}

(searchSource as any).finalResponse = resp;

const parsedTimeRange = timeRange ? calculateBounds(timeRange) : null;
const tabifyParams = {
metricsAtAllLevels,
Expand All @@ -188,18 +183,16 @@ export const handleRequest = async ({
: undefined,
};

const response = tabifyAggResponse(aggs, (searchSource as any).finalResponse, tabifyParams);

(searchSource as any).tabifiedResponse = response;
const tabifiedResponse = tabifyAggResponse(aggs, response, tabifyParams);

inspectorAdapters.data.setTabularLoader(
() =>
buildTabularInspectorData((searchSource as any).tabifiedResponse, {
queryFilter: filterManager,
deserializeFieldFormat: getFieldFormats().deserialize,
buildTabularInspectorData(tabifiedResponse, {
queryFilter,
deserializeFieldFormat,
}),
{ returnsFormattedValues: true }
);

return response;
return tabifiedResponse;
};

0 comments on commit 0476f73

Please sign in to comment.