From 80c93068991aee21556f12960567cca7b594a324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Henrik=20=C3=98verland?= Date: Thu, 8 Aug 2024 12:44:08 +0200 Subject: [PATCH] fix: sort analytics request params and items for cache hit optimization (DHIS2-17861) (#1695) Relevant apps: DV --- src/api/analytics/AnalyticsBase.js | 27 ++++++++++--------- .../analytics/__tests__/AnalyticsBase.spec.js | 2 +- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/api/analytics/AnalyticsBase.js b/src/api/analytics/AnalyticsBase.js index 9d5d82cff..3bd153818 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,14 +61,11 @@ 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') + 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(';')}` @@ -131,8 +134,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, 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'], }, ]