From 8fe2bf6efd636387cb6b69f655d88a2626d8a256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Henrik=20=C3=98verland?= Date: Wed, 7 Aug 2024 16:05:00 +0200 Subject: [PATCH 1/4] fix: sort analytics params and items --- package.json | 1 - src/api/analytics/AnalyticsBase.js | 26 +++++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index cd4f59bc5..0d29a0331 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ }, "scripts": { "build": "d2-app-scripts build", - "postbuild": "yarn build-storybook", "build-storybook": "build-storybook", "start-storybook": "start-storybook --port 5000", "start": "yarn start-storybook", diff --git a/src/api/analytics/AnalyticsBase.js b/src/api/analytics/AnalyticsBase.js index 9d5d82cff..98e41c1a8 100644 --- a/src/api/analytics/AnalyticsBase.js +++ b/src/api/analytics/AnalyticsBase.js @@ -27,8 +27,12 @@ const analyticsDataQuery = { }), params: ({ dimensions, filters, parameters }) => { return { - dimension: dimensions.length ? dimensions : undefined, - filter: filters.length ? filters : undefined, + dimension: dimensions.length + ? generateDimensionStrings(dimensions, { sorted: true }) + : undefined, + filter: filters.length + ? generateDimensionStrings(filters, { sorted: true }) + : undefined, ...parameters, skipMeta: true, skipData: false, @@ -45,8 +49,10 @@ const analyticsMetaDataQuery = { trackedEntityType, }), params: ({ dimensions, filters, parameters }) => ({ - dimension: dimensions.length ? dimensions : undefined, - filter: filters.length ? filters : undefined, + dimension: dimensions.length + ? generateDimensionStrings(dimensions) + : undefined, + filter: filters.length ? generateDimensionStrings(filters) : undefined, ...parameters, skipMeta: false, skipData: true, @@ -55,11 +61,9 @@ const analyticsMetaDataQuery = { } export const generateDimensionStrings = (dimensions = [], options) => { - if (options && options.sorted) { - dimensions = sortBy(dimensions, 'dimension') - } - - return dimensions.map(({ dimension, items }) => { + const sortedDimensions = sortBy(dimensions, 'dimension') + console.log('arrays the same?', sortedDimensions === dimensions) + return sortedDimensions.map(({ dimension, items }) => { if (Array.isArray(items) && items.length) { if (options && options.sorted) { items.sort() @@ -131,8 +135,8 @@ class AnalyticsBase { path: req.path, program: req.program, trackedEntityType: req.trackedEntityType, - dimensions: generateDimensionStrings(req.dimensions), - filters: generateDimensionStrings(req.filters), + dimensions: req.dimensions, + filters: req.filters, parameters: req.parameters, dataParams: dataReq.parameters, metaDataParams: metaDataReq.parameters, From d6b2faa6630f0f7ce699c4b73d3f90a35bed58f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Henrik=20=C3=98verland?= Date: Thu, 8 Aug 2024 10:34:07 +0200 Subject: [PATCH 2/4] fix: make the function pure --- src/api/analytics/AnalyticsBase.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/api/analytics/AnalyticsBase.js b/src/api/analytics/AnalyticsBase.js index 98e41c1a8..3bd153818 100644 --- a/src/api/analytics/AnalyticsBase.js +++ b/src/api/analytics/AnalyticsBase.js @@ -62,11 +62,10 @@ const analyticsMetaDataQuery = { export const generateDimensionStrings = (dimensions = [], options) => { const sortedDimensions = sortBy(dimensions, 'dimension') - console.log('arrays the same?', sortedDimensions === dimensions) return sortedDimensions.map(({ dimension, items }) => { if (Array.isArray(items) && items.length) { if (options && options.sorted) { - items.sort() + items = items.slice().sort() } return `${dimension}:${items.join(';')}` From 081e4828bda86fa43ad1e27cb29783ab5c7e1c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Henrik=20=C3=98verland?= Date: Thu, 8 Aug 2024 10:57:51 +0200 Subject: [PATCH 3/4] chore: add back postbuild --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 0d29a0331..cd4f59bc5 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ }, "scripts": { "build": "d2-app-scripts build", + "postbuild": "yarn build-storybook", "build-storybook": "build-storybook", "start-storybook": "start-storybook --port 5000", "start": "yarn start-storybook", From 240c4293492c98790fd9fc696f9bd76c8906ad24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Henrik=20=C3=98verland?= Date: Thu, 8 Aug 2024 11:18:27 +0200 Subject: [PATCH 4/4] test: always sort params --- src/api/analytics/__tests__/AnalyticsBase.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/analytics/__tests__/AnalyticsBase.spec.js b/src/api/analytics/__tests__/AnalyticsBase.spec.js index c5d3fb1d8..9bc51eae6 100644 --- a/src/api/analytics/__tests__/AnalyticsBase.spec.js +++ b/src/api/analytics/__tests__/AnalyticsBase.spec.js @@ -33,7 +33,7 @@ describe('generateDimensionString', () => { items: ['item1'], }, ], - output: ['dim2:item2;item1', 'dim1:item1'], + output: ['dim1:item1', 'dim2:item2;item1'], outputSorted: ['dim1:item1', 'dim2:item1;item2'], }, ]