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

Fix empty or multiple date values in csv #293

Merged
merged 1 commit into from
Mar 2, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,59 @@ test('create report for data set with metadata fields', async () => {
);
}, 20000);

test('create report with empty/one/multiple(list) date values', async () => {
const hits = [
hit(
{ category: 'c1', customer_gender: 'Ma', order_date: [] },
{ order_date: [] }
),
hit(
{
category: 'c2',
customer_gender: 'le',
order_date: ['2021-12-16T14:04:55'],
},
{ order_date: ['2021-12-16T14:04:55'] }
),
hit(
{
category: 'c3',
customer_gender: 'he',
order_date: ['2021-12-17T14:04:55', '2021-12-18T14:04:55'],
},
{ order_date: ['2021-12-17T14:04:55', '2021-12-18T14:04:55'] }
),
hit(
{
category: 'c4',
customer_gender: 'te',
order_date: '2021-12-19T14:04:55',
},
{ order_date: ['2021-12-19T14:04:55'] }
),
];
const client = mockOpenSearchClient(
hits,
'"category", "customer_gender", "order_date"'
);
const { dataUrl } = await createSavedSearchReport(
input,
client,
mockDateFormat,
',',
undefined,
mockLogger
);

expect(dataUrl).toEqual(
'category,customer_gender,order_date\n' +
'c1,Ma,[]\n' +
'c2,le,"[""12/16/2021 2:04:55.000 pm""]"\n' +
'c3,he,"[""12/17/2021 2:04:55.000 pm"",""12/18/2021 2:04:55.000 pm""]"\n' +
'c4,te,12/19/2021 2:04:55.000 pm'
);
}, 20000);

/**
* Mock Elasticsearch client and return different mock objects based on endpoint and parameters.
*/
Expand Down Expand Up @@ -588,8 +641,9 @@ function mockIndexSettings() {
`);
}

function hit(kv: any) {
function hit(source_kv: any, fields_kv = {}) {
return {
_source: kv,
_source: source_kv,
fields: fields_kv,
};
}
22 changes: 17 additions & 5 deletions dashboards-reports/server/routes/utils/dataReportHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,23 @@ export const getOpenSearchData = (
for (let data of valueRes.hits) {
const fields = data.fields;
// 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(
dateFormat
);
for (let dateField of report._source.dateFields) {
const dateValue = data._source[dateField];
if (dateValue && dateValue.length !== 0) {
if (dateValue instanceof Array) {
// loop through array
dateValue.forEach((element, index) => {
data._source[dateField][index] = moment(
fields[dateField][index]
).format(dateFormat);
});
} else {
// The fields response always returns an array of values for each field
// https://www.elastic.co/guide/en/elasticsearch/reference/master/search-fields.html#search-fields-response
data._source[dateField] = moment(fields[dateField][0]).format(
dateFormat
);
}
}
}
delete data['fields'];
Expand Down