Skip to content

Commit

Permalink
flatten hits recursive function and modified nested test
Browse files Browse the repository at this point in the history
Signed-off-by: sumukhswamy <[email protected]>
  • Loading branch information
sumukhswamy committed Jan 27, 2025
1 parent 10f563b commit 92af2c8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
48 changes: 41 additions & 7 deletions server/routes/utils/__tests__/savedSearchReportHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,12 +912,22 @@ describe('test create saved search report', () => {
'geoip.location': { lon: -0.1, lat: 51.5 },
customer_birth_date: '2023-04-26T04:34:32Z',
order_date: '2023-04-26T04:34:32Z',
products: [{ created_on: '2023-04-26T04:34:32Z' }],
products: [
{ created_on: '2023-04-26T04:34:32Z', price: 100, category: 'Electronics' },

Check failure on line 916 in server/routes/utils/__tests__/savedSearchReportHelper.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `·created_on:·'2023-04-26T04:34:32Z',·price:·100,·category:·'Electronics'` with `⏎··············created_on:·'2023-04-26T04:34:32Z',⏎··············price:·100,⏎··············category:·'Electronics',⏎···········`
{ created_on: '2023-05-01T08:22:00Z', price: 50, category: 'Books' },

Check failure on line 917 in server/routes/utils/__tests__/savedSearchReportHelper.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `·created_on:·'2023-05-01T08:22:00Z',·price:·50,·category:·'Books'` with `⏎··············created_on:·'2023-05-01T08:22:00Z',⏎··············price:·50,⏎··············category:·'Books',⏎···········`
],
customer: {
name: 'John Doe',
address: { city: 'London', postcode: 'SW1A 1AA' },
},
},
{
customer_birth_date: '2023-04-26T04:34:32Z',
order_date: '2023-04-26T04:34:32Z',
'products.created_on': '2023-04-26T04:34:32Z',
'customer.name': 'John Doe',
'customer.address.city': 'London',
'customer.address.postcode': 'SW1A 1AA',
}
),
hit(
Expand All @@ -926,20 +936,40 @@ describe('test create saved search report', () => {
'geoip.city_name': 'New York',
'geoip.location': { lon: -74, lat: 40.8 },
customer_birth_date: '2023-04-26T04:34:32Z',
order_date: '2023-04-26T04:34:32Z',
products: [{ created_on: '2023-04-26T04:34:32Z' }],
products: [{ created_on: '2023-06-10T14:30:00Z', price: 150, category: 'Furniture' }],

Check failure on line 939 in server/routes/utils/__tests__/savedSearchReportHelper.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `{·created_on:·'2023-06-10T14:30:00Z',·price:·150,·category:·'Furniture'·}` with `⏎············{⏎··············created_on:·'2023-06-10T14:30:00Z',⏎··············price:·150,⏎··············category:·'Furniture',⏎············},⏎··········`
customer: {
name: 'Jane Smith',
address: { city: 'New York', postcode: '10001' },
},
},
{
customer_birth_date: '2023-04-26T04:34:32Z',
order_date: '2023-04-26T04:34:32Z',
'products.created_on': '2023-04-26T04:34:32Z',
'customer.name': 'Jane Smith',
'customer.address.city': 'New York',
'customer.address.postcode': '10001',
}
),
hit(
{
'geoip.country_iso_code': 'CA',
'geoip.city_name': 'Toronto',
'geoip.location': { lon: -79.38, lat: 43.65 },
customer: {
name: 'Alice Johnson',
address: { city: 'Toronto', postcode: 'M5H 2N2' },
},
},
{}
),
];

Check failure on line 967 in server/routes/utils/__tests__/savedSearchReportHelper.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
const client = mockOpenSearchClient(
hits,
'"geoip.country_iso_code", "geoip.city_name", "geoip.location"'
'"geoip.country_iso_code", "geoip.city_name", "geoip.location", "customer.name", "customer.address.city", "customer.address.postcode"'
);

Check failure on line 972 in server/routes/utils/__tests__/savedSearchReportHelper.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
const { dataUrl } = await createSavedSearchReport(
input,
client,
Expand All @@ -950,10 +980,14 @@ describe('test create saved search report', () => {
mockLogger,
mockTimezone
);

Check failure on line 983 in server/routes/utils/__tests__/savedSearchReportHelper.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
console.log(dataUrl);

Check failure on line 985 in server/routes/utils/__tests__/savedSearchReportHelper.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
expect(dataUrl).toEqual(
'geoip\\.country_iso_code,products\\.created_on,geoip\\.location\\.lon,geoip\\.location\\.lat,geoip\\.city_name\n' +
'GB,"[""2023-04-26T04:34:32Z""]",-0.1,51.5, \n' +
'US,"[""2023-04-26T04:34:32Z""]",-74,40.8,New York'
'geoip\\.country_iso_code,products\\.created_on,products\\.price,products\\.category,geoip\\.location\\.lon,geoip\\.location\\.lat,customer\\.name,customer\\.address\\.city,customer\\.address\\.postcode,geoip\\.city_name\n' +
'GB,"[""2023-04-26T04:34:32Z"",""2023-05-01T08:22:00Z""]","[100,50]","[""Electronics"",""Books""]",-0.1,51.5,John Doe,London,SW1A 1AA, \n' +
'US,"[""2023-06-10T14:30:00Z""]",[150],"[""Furniture""]",-74,40.8,Jane Smith,New York,10001,New York\n' +
'CA, , , ,-79.38,43.65,Alice Johnson,Toronto,M5H 2N2,Toronto'
);
}, 20000);

Expand Down
13 changes: 6 additions & 7 deletions server/routes/utils/dataReportHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,19 @@ export const convertToCSV = async (dataset, csvSeparator) => {
return convertedData;
};

function flattenHits(hits, result = {}, prefix = '') {
for (const [key, value] of Object.entries(hits)) {
if (!hits.hasOwnProperty(key)) continue;
function flattenHits(hits: any, result: { [key: string]: any } = {}, prefix = '') {
Object.entries(hits).forEach(([key, value]) => {
if (
value != null &&
value !== null &&
typeof value === 'object' &&
!Array.isArray(value) &&
Object.keys(value).length > 0
) {
flattenHits(value, result, prefix + key + '.');
flattenHits(value, result, `${prefix}${key}.`);
} else {
result[prefix.replace(/^_source\./, '') + key] = value;
result[`${prefix.replace(/^_source\./, '')}${key}`] = value;
}
}
});
return result;
}

Expand Down
Empty file removed server/routes/utils/test
Empty file.

0 comments on commit 92af2c8

Please sign in to comment.