Skip to content

Commit

Permalink
cleaning up esaggs (#77646) (#77967)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Sep 21, 2020
1 parent ea360ec commit 64ddd61
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 58 deletions.
87 changes: 32 additions & 55 deletions src/plugins/data/public/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import { get, hasIn } from 'lodash';
import { i18n } from '@kbn/i18n';
import { KibanaDatatable, KibanaDatatableColumn } from 'src/plugins/expressions/public';
import { calculateObjectHash } from '../../../../../plugins/kibana_utils/public';
import { PersistedState } from '../../../../../plugins/visualizations/public';
import { Adapters } from '../../../../../plugins/inspector/public';

Expand Down Expand Up @@ -60,7 +59,6 @@ export interface RequestHandlerParams {
indexPattern?: IIndexPattern;
query?: Query;
filters?: Filter[];
forceFetch: boolean;
filterManager: FilterManager;
uiState?: PersistedState;
partialRows?: boolean;
Expand All @@ -80,7 +78,6 @@ const handleCourierRequest = async ({
indexPattern,
query,
filters,
forceFetch,
partialRows,
metricsAtAllLevels,
inspectorAdapters,
Expand Down Expand Up @@ -137,46 +134,35 @@ const handleCourierRequest = async ({
requestSearchSource.setField('filter', filters);
requestSearchSource.setField('query', query);

const reqBody = await requestSearchSource.getSearchRequestBody();

const queryHash = calculateObjectHash(reqBody);
// We only need to reexecute the query, if forceFetch was true or the hash of the request body has changed
// since the last request
const shouldQuery = forceFetch || (searchSource as any).lastQuery !== queryHash;

if (shouldQuery) {
inspectorAdapters.requests.reset();
const request = inspectorAdapters.requests.start(
i18n.translate('data.functions.esaggs.inspector.dataRequest.title', {
defaultMessage: 'Data',
inspectorAdapters.requests.reset();
const 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.',
}),
{
description: i18n.translate('data.functions.esaggs.inspector.dataRequest.description', {
defaultMessage:
'This request queries Elasticsearch to fetch the data for the visualization.',
}),
}
);
request.stats(getRequestInspectorStats(requestSearchSource));

try {
const response = await requestSearchSource.fetch({ abortSignal });

(searchSource as any).lastQuery = queryHash;

request.stats(getResponseInspectorStats(response, searchSource)).ok({ json: response });

(searchSource as any).rawResponse = response;
} catch (e) {
// Log any error during request to the inspector
request.error({ json: e });
throw e;
} finally {
// Add the request body no matter if things went fine or not
requestSearchSource.getSearchRequestBody().then((req: unknown) => {
request.json(req);
});
}
);
request.stats(getRequestInspectorStats(requestSearchSource));

try {
const response = await requestSearchSource.fetch({ abortSignal });

request.stats(getResponseInspectorStats(response, searchSource)).ok({ json: response });

(searchSource as any).rawResponse = response;
} catch (e) {
// Log any error during request to the inspector
request.error({ json: e });
throw e;
} finally {
// Add the request body no matter if things went fine or not
requestSearchSource.getSearchRequestBody().then((req: unknown) => {
request.json(req);
});
}

// Note that rawResponse is not deeply cloned here, so downstream applications using courier
Expand Down Expand Up @@ -207,19 +193,11 @@ const handleCourierRequest = async ({
: undefined,
};

const tabifyCacheHash = calculateObjectHash({ tabifyAggs: aggs, ...tabifyParams });
// We only need to reexecute tabify, if either we did a new request or some input params to tabify changed
const shouldCalculateNewTabify =
shouldQuery || (searchSource as any).lastTabifyHash !== tabifyCacheHash;

if (shouldCalculateNewTabify) {
(searchSource as any).lastTabifyHash = tabifyCacheHash;
(searchSource as any).tabifiedResponse = tabifyAggResponse(
aggs,
(searchSource as any).finalResponse,
tabifyParams
);
}
(searchSource as any).tabifiedResponse = tabifyAggResponse(
aggs,
(searchSource as any).finalResponse,
tabifyParams
);

inspectorAdapters.data.setTabularLoader(
() =>
Expand Down Expand Up @@ -294,7 +272,6 @@ export const esaggs = (): EsaggsExpressionFunctionDefinition => ({
query: get(input, 'query', undefined) as any,
filters: get(input, 'filters', undefined),
timeFields: args.timeFields,
forceFetch: true,
metricsAtAllLevels: args.metricsAtAllLevels,
partialRows: args.partialRows,
inspectorAdapters: inspectorAdapters as Adapters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export function getTimelionRequestHandler({
filters: Filter[];
query: Query;
visParams: VisParams;
forceFetch?: boolean;
}): Promise<TimelionSuccessResponse> {
const expression = visParams.expression;

Expand Down
1 change: 0 additions & 1 deletion src/plugins/vis_type_timelion/public/timelion_vis_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export const getTimelionVisualizationConfig = (
query: get(input, 'query') as Query,
filters: get(input, 'filters') as Filter[],
visParams,
forceFetch: true,
});

response.visType = TIMELION_VIS_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ export const visualization = (): ExpressionFunctionVisualization => ({
uiState,
inspectorAdapters,
queryFilter: getFilterManager(),
forceFetch: true,
aggs,
});
}
Expand Down

0 comments on commit 64ddd61

Please sign in to comment.