Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support sorting for csv report based on saved search #86

Merged
merged 3 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions dashboards-reports/server/routes/utils/dataReportHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import { DATA_REPORT_CONFIG } from './constants';

import esb from 'elastic-builder';
import esb, { Sort } from 'elastic-builder';
import moment from 'moment';
import converter from 'json-2-csv';
import _ from 'lodash';
Expand Down Expand Up @@ -61,7 +61,7 @@ export const getSelectedFields = async (columns) => {
metaData.selectedFields = selectedFields;
};

//Build the OpenSearch query from the meta data
// Build the OpenSearch query from the meta data
// is_count is set to 1 if we building the count query but 0 if we building the fetch data query
export const buildQuery = (report, is_count) => {
let requestBody = esb.boolQuery();
Expand Down Expand Up @@ -149,14 +149,10 @@ export const buildQuery = (report, is_count) => {
let reqBody = esb.requestBodySearch().query(requestBody).version(true);

if (report._source.sorting.length > 0) {
if (report._source.sorting.length === 1)
reqBody.sort(
esb.sort(report._source.sorting[0][0], report._source.sorting[0][1])
);
else
reqBody.sort(
esb.sort(report._source.sorting[0], report._source.sorting[1])
);
const sortings: Sort[] = report._source.sorting.map((element: string[]) => {
return esb.sort(element[0], element[1]);
});
reqBody.sorts(sortings);
}

//get the selected fields only
Expand All @@ -172,7 +168,7 @@ export const getOpenSearchData = (arrayHits, report, params) => {
for (let valueRes of arrayHits) {
for (let data of valueRes.hits) {
const fields = data.fields;
//get all the fields of type date and fromat them to excel format
// get all the fields of type date and format them to excel format
for (let dateType of report._source.dateFields) {
if (data._source[dateType]) {
data._source[dateType] = moment(fields[dateType][0]).format(
Expand Down Expand Up @@ -249,11 +245,11 @@ function traverse(data, keys, result = {}) {
*/
function sanitize(doc: any) {
for (const field in doc) {
if (doc[field] == null)
continue
if (doc[field] == null) continue;
if (
doc[field].toString().startsWith('+') ||
(doc[field].toString().startsWith('-') && typeof doc[field] !== "number") ||
(doc[field].toString().startsWith('-') &&
typeof doc[field] !== 'number') ||
doc[field].toString().startsWith('=') ||
doc[field].toString().startsWith('@')
) {
Expand Down
34 changes: 21 additions & 13 deletions dashboards-reports/server/routes/utils/savedSearchReportHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
} from '../../../../../src/core/server';
import { getFileName, callCluster } from './helpers';
import { CreateReportResultType } from './types';
import { RequestParams } from '@elastic/elasticsearch';
import esb from 'elastic-builder';

/**
* Specify how long scroll context should be maintained for scrolled search
Expand Down Expand Up @@ -200,16 +202,17 @@ async function generateReportData(
}

async function getOpenSearchDataByScroll() {
const searchParams: RequestParams.Search = {
index: report._source.paternName,
scroll: scrollTimeout,
body: reqBody,
size: maxResultSize,
};
// Open scroll context by fetching first batch
opensearchData = await callCluster(
client,
'search',
{
index: report._source.paternName,
scroll: scrollTimeout,
body: reqBody,
size: maxResultSize,
},
searchParams,
isScheduledTask
);
arrayHits.push(opensearchData.hits);
Expand Down Expand Up @@ -243,20 +246,23 @@ async function generateReportData(
}

async function getOpenSearchDataBySearch() {
const searchParams: RequestParams.Search = {
index: report._source.paternName,
body: reqBody,
size: total,
};

opensearchData = await callCluster(
client,
'search',
{
index: report._source.paternName,
body: reqBody,
size: total,
},
searchParams,
isScheduledTask
);

arrayHits.push(opensearchData.hits);
}

function buildRequestBody(query: any) {
function buildRequestBody(query: esb.RequestBodySearch) {
const docvalues = [];
for (const dateType of report._source.dateFields) {
docvalues.push({
Expand All @@ -265,8 +271,10 @@ async function generateReportData(
});
}

// elastic-builder doesn't provide function to build docvalue_fields with format,
// this is a workaround which appends docvalues field to the request body.
return {
query: query.toJSON().query,
...query.toJSON(),
docvalue_fields: docvalues,
};
}
Expand Down