From 92af2c8864063122ac43dcb071a978b0042cb746 Mon Sep 17 00:00:00 2001 From: sumukhswamy Date: Mon, 27 Jan 2025 15:22:18 -0800 Subject: [PATCH] flatten hits recursive function and modified nested test Signed-off-by: sumukhswamy --- .../__tests__/savedSearchReportHelper.test.ts | 48 ++++++++++++++++--- server/routes/utils/dataReportHelpers.ts | 13 +++-- server/routes/utils/test | 0 3 files changed, 47 insertions(+), 14 deletions(-) delete mode 100644 server/routes/utils/test diff --git a/server/routes/utils/__tests__/savedSearchReportHelper.test.ts b/server/routes/utils/__tests__/savedSearchReportHelper.test.ts index 07ad2f22..7a976df9 100644 --- a/server/routes/utils/__tests__/savedSearchReportHelper.test.ts +++ b/server/routes/utils/__tests__/savedSearchReportHelper.test.ts @@ -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' }, + { 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( @@ -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' }], + 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' }, + }, + }, + {} + ), ]; + 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"' ); + const { dataUrl } = await createSavedSearchReport( input, client, @@ -950,10 +980,14 @@ describe('test create saved search report', () => { mockLogger, mockTimezone ); + + console.log(dataUrl); + 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); diff --git a/server/routes/utils/dataReportHelpers.ts b/server/routes/utils/dataReportHelpers.ts index 61ecb058..43938c01 100644 --- a/server/routes/utils/dataReportHelpers.ts +++ b/server/routes/utils/dataReportHelpers.ts @@ -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; } diff --git a/server/routes/utils/test b/server/routes/utils/test deleted file mode 100644 index e69de29b..00000000