Skip to content

Commit

Permalink
fix: sort analytics request params and items for cache hit optimizati…
Browse files Browse the repository at this point in the history
…on (DHIS2-17861) (#1695)

Relevant apps: DV
  • Loading branch information
janhenrikoverland authored Aug 8, 2024
1 parent c3acdff commit 80c9306
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
27 changes: 15 additions & 12 deletions src/api/analytics/AnalyticsBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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(';')}`
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/api/analytics/__tests__/AnalyticsBase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
},
]
Expand Down

0 comments on commit 80c9306

Please sign in to comment.