Skip to content

Commit

Permalink
migrate courier.ts to request_handler.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
fbaligand committed Mar 14, 2022
1 parent 93426c9 commit 1cf893c
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 212 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ rules:
"@kbn/eslint/require-license-header": off
"prettier/prettier": off
"import/no-extraneous-dependencies": off
"template-curly-spacing" : 0
"template-curly-spacing": 0
"no-setter-return": off
indent:
- off
- 2
Expand Down
16 changes: 7 additions & 9 deletions public/agg_table/agg_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CSV_SEPARATOR_SETTING, CSV_QUOTE_VALUES_SETTING } from '../../../../src
import aggTableTemplate from './agg_table.html';
import { getFormatService } from '../services';
import { fieldFormatter } from '../field_formatter';import { computeColumnTotal } from '../column_total_computer';
import { handleCourierRequest } from '../data_load/kibana_cloned_code/courier';
import { handleRequest } from '../data_load/kibana_cloned_code/request_handler';
import { createTable } from '../data_load/document-table-response-handler';
import { streamSaver } from './stream_saver';

Expand Down Expand Up @@ -54,11 +54,11 @@ function KbnEnhancedAggTableController($scope, tableConfig){
const table = $scope.table;

if ($scope.csvFullExport && self.csv.totalHits === undefined) {
self.csv.totalHits = _.get(table.request.searchSource, 'finalResponse.hits.total', -1);
self.csv.totalHits = table.totalHits;
}

if ($scope.csvFullExport && self.csv.totalHits > table.rows.length) {
self.exportFullAsCsv(formatted, table.request);
self.exportFullAsCsv(formatted, table.request, table.hits);
}
else {
const csvContent = self.toCsv(table, formatted, true);
Expand All @@ -67,11 +67,10 @@ function KbnEnhancedAggTableController($scope, tableConfig){
}
};

self.exportFullAsCsv = async function (formatted, request) {
self.exportFullAsCsv = async function (formatted, request, initialHits) {

// store initial table last sort value
if (self.csv.lastSortValue === undefined) {
const initialHits = _.get(request.searchSource, 'finalResponse.hits.hits', []);
self.csv.lastSortValue = initialHits[initialHits.length - 1].sort;
}

Expand All @@ -91,11 +90,10 @@ function KbnEnhancedAggTableController($scope, tableConfig){
let searchAfter = self.csv.lastSortValue;
do {
const hitsSize = Math.min(remainingSize, self.csv.maxHitsSize);
request.searchSource.setField('size', hitsSize);
request.searchSource.setField('search_after', searchAfter);
const response = await handleCourierRequest(request);
request.searchSourceFields.size = hitsSize;
request.searchSourceFields.search_after = searchAfter;
const response = await handleRequest(request).toPromise();
response.aggs = request.aggs;
response.hits = _.get(request.searchSource, 'finalResponse.hits.hits', []);
response.fieldColumns = $scope.fieldColumns;
const table = createTable(response);
csvBuffer = self.toCsv(table, formatted, false);
Expand Down
2 changes: 1 addition & 1 deletion public/data_load/document-table-response-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function createRow(hit, columns) {
}

export function createTable(response) {
const table = { columns: [], rows: [], request: response.request };
const table = { columns: [], rows: [], request: response.request, hits: response.hits, totalHits: response.totalHits };

const aggConfigs = response.aggs;
aggConfigs.aggs = [];
Expand Down
85 changes: 40 additions & 45 deletions public/data_load/enhanced-table-request-handler.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
import _ from 'lodash';
import { getSearchService } from '../services';
import { handleCourierRequest } from './kibana_cloned_code/courier';
import { handleRequest } from './kibana_cloned_code/request_handler';

export async function enhancedTableRequestHandler ({
partialRows,
metricsAtAllLevels,
visParams,
timeRange,
query,
abortSignal,
aggs,
filters,
indexPattern,
inspectorAdapters,
forceFetch,
aggs,
queryFilter,
searchSessionId
partialRows,
query,
searchSessionId,
timeFields,
timeRange,
executionContext,
visParams,
}) {

const filterManager = queryFilter;
const MAX_HITS_SIZE = 10000;

// create search source with query parameters
const searchService = getSearchService();
const searchSource = await searchService.searchSource.create();
searchSource.setField('index', aggs.indexPattern);
// create search source fields
const searchSourceFields = {};
let hitsSize = (visParams.hitsSize !== undefined ? Math.min(visParams.hitsSize, MAX_HITS_SIZE) : 0);
searchSource.setField('size', hitsSize);
searchSourceFields.size = hitsSize;

// specific request params for "field columns"
if (visParams.fieldColumns !== undefined) {
if (!visParams.fieldColumns.some(fieldColumn => fieldColumn.field.name === '_source')) {
searchSource.setField('_source', visParams.fieldColumns.map(fieldColumn => fieldColumn.field.name));
searchSourceFields._source = visParams.fieldColumns.map(fieldColumn => fieldColumn.field.name);
}
searchSource.setField('docvalue_fields', visParams.fieldColumns.filter(fieldColumn => fieldColumn.field.readFromDocValues).map(fieldColumn => fieldColumn.field.name));
searchSourceFields.docvalue_fields = visParams.fieldColumns.filter(fieldColumn => fieldColumn.field.readFromDocValues).map(fieldColumn => fieldColumn.field.name);
const scriptFields = {};
visParams.fieldColumns.filter(fieldColumn => fieldColumn.field.scripted).forEach(fieldColumn => {
scriptFields[fieldColumn.field.name] = {
Expand All @@ -40,18 +37,18 @@ export async function enhancedTableRequestHandler ({
}
};
});
searchSource.setField('script_fields', scriptFields);
searchSourceFields.script_fields = scriptFields;
}

// set search sort
if (visParams.sortField !== undefined) {
searchSource.setField('sort', [{
searchSourceFields.sort = [{
[visParams.sortField.name]: {
order: visParams.sortOrder
}
}]);
}];
if ((visParams.hitsSize !== undefined && visParams.hitsSize > MAX_HITS_SIZE) || visParams.csvFullExport) {
searchSource.getField('sort').push({'_id': {'order': 'asc','unmapped_type': 'keyword'}});
searchSourceFields.sort.push({'_id': {'order': 'asc','unmapped_type': 'keyword'}});
}
}

Expand All @@ -68,29 +65,29 @@ export async function enhancedTableRequestHandler ({

// execute elasticsearch query
const request = {
searchSource: searchSource,
aggs: aggs,
indexPattern: aggs.indexPattern,
timeRange: timeRange,
query: query,
filters: filters,
forceFetch: forceFetch,
metricsAtAllLevels: metricsAtAllLevels,
partialRows: partialRows,
inspectorAdapters: inspectorAdapters,
filterManager: filterManager,
searchSessionId: searchSessionId
abortSignal,
aggs,
filters,
indexPattern,
inspectorAdapters,
partialRows,
query,
searchSessionId,
searchSourceService: getSearchService().searchSource,
timeFields,
timeRange,
executionContext,
searchSourceFields,
};
const response = await handleCourierRequest(request);
const response = await handleRequest(request).toPromise();

// set 'split tables' direction
const splitAggs = aggs.bySchemaName('split');
if (splitAggs.length > 0) {
splitAggs[0].params.row = visParams.row;
}

// enrich response: total & aggs
response.totalHits = _.get(searchSource, 'finalResponse.hits.total', -1);
// enrich response: aggs
response.aggs = aggs;

// enrich columns: aggConfig
Expand All @@ -101,7 +98,6 @@ export async function enhancedTableRequestHandler ({
// enrich response: hits
if (visParams.fieldColumns !== undefined) {
response.fieldColumns = visParams.fieldColumns;
response.hits = _.get(searchSource, 'finalResponse.hits.hits', []);

// continue requests until expected hits size is reached
if (visParams.hitsSize !== undefined && visParams.hitsSize > MAX_HITS_SIZE && response.totalHits > MAX_HITS_SIZE) {
Expand All @@ -110,12 +106,11 @@ export async function enhancedTableRequestHandler ({
remainingSize -= hitsSize;
const searchAfter = response.hits[response.hits.length - 1].sort;
hitsSize = Math.min(remainingSize, MAX_HITS_SIZE);
searchSource.setField('size', hitsSize);
searchSource.setField('search_after', searchAfter);
await handleCourierRequest(request);
const nextResponseHits = _.get(searchSource, 'finalResponse.hits.hits', []);
for (let i = 0; i < nextResponseHits.length; i++) {
response.hits.push(nextResponseHits[i]);
searchSourceFields.size = hitsSize;
searchSourceFields.search_after = searchAfter;
const nextResponse = await handleRequest(request).toPromise();
for (let i = 0; i < nextResponse.hits.length; i++) {
response.hits.push(nextResponse.hits[i]);
}
} while (remainingSize > hitsSize);
}
Expand Down
136 changes: 0 additions & 136 deletions public/data_load/kibana_cloned_code/courier.ts

This file was deleted.

Loading

0 comments on commit 1cf893c

Please sign in to comment.