Skip to content

Commit

Permalink
feat: accommodate for programId in the dimension string
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardo committed Nov 17, 2023
1 parent e319dbd commit 9692cd0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/api/analytics/AnalyticsRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import AnalyticsRequestBase from './AnalyticsRequestBase.js'
import AnalyticsRequestDimensionsMixin from './AnalyticsRequestDimensionsMixin.js'
import AnalyticsRequestFiltersMixin from './AnalyticsRequestFiltersMixin.js'
import AnalyticsRequestPropertiesMixin from './AnalyticsRequestPropertiesMixin.js'
import { formatDimension } from './utils.js'

/**
* @description
Expand Down Expand Up @@ -45,6 +46,8 @@ class AnalyticsRequest extends AnalyticsRequestDimensionsMixin(
fromVisualization(visualization, passFilterAsDimension = false) {
let request = this

const outputType = visualization.outputType

// extract dimensions from visualization
const columns = visualization.columns || []
const rows = visualization.rows || []
Expand All @@ -56,23 +59,31 @@ class AnalyticsRequest extends AnalyticsRequestDimensionsMixin(
dimension += `-${d.legendSet.id}`
}

if (d.programStage?.id) {
dimension = `${d.programStage.id}.${dimension}`
}

if (d.filter) {
dimension += `:${d.filter}`
}

const programStageId = d.programStage?.id

if (d.repetition?.indexes?.length) {
d.repetition.indexes.forEach((index) => {
request = request.addDimension(
dimension.replace(/\./, `[${index}].`)
formatDimension({
programId: d.program?.id,
programStageId: `${programStageId}[${index}]`,
dimension,
outputType,
})
)
})
} else {
request = request.addDimension(
dimension,
formatDimension({
programId: d.program?.id,
programStageId,
dimension,
outputType,
}),
d.items?.map((item) => item.id)
)
}
Expand All @@ -91,23 +102,33 @@ class AnalyticsRequest extends AnalyticsRequestDimensionsMixin(
f.items?.map((item) => item.id)
)
} else {
let filterString = f.programStage?.id
? `${f.programStage.id}.${f.dimension}`
: f.dimension
let filterString = f.dimension

if (f.filter) {
filterString += `:${f.filter}`
}

const programStageId = f.programStage?.id

if (f.repetition?.indexes?.length) {
f.repetition.indexes.forEach((index) => {
request = request.addFilter(
filterString.replace(/\./, `[${index}].`)
formatDimension({
programId: f.program?.id,
programStageId: `${programStageId}[${index}]`,
dimension: filterString,
outputType,
})
)
})
} else {
request = request.addFilter(
filterString,
formatDimension({
programId: f.program?.id,
programStageId,
dimension: filterString,
outputType,
}),
f.items?.map((item) => item.id)
)
}
Expand Down
15 changes: 15 additions & 0 deletions src/api/analytics/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,18 @@ export const customEncodeURIComponent = (uri) =>

export const formatRequestPath = ({ path, program, trackedEntityType }) =>
[path, program, trackedEntityType].filter(Boolean).join('/')

export const formatDimension = ({
outputType,
programId,
programStageId,
dimension,
}) =>
[
// XXX it would be clearer to have this consistent with what is sent in the request as for EVENT/ENROLLMENT
outputType === 'TRACKED_ENTITY' ? programId : undefined,
programStageId,
dimension,
]
.filter(Boolean)
.join('.')
8 changes: 8 additions & 0 deletions src/modules/layout/dimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export const DIMENSION_PROP_LEGEND_SET = {
isValid: (prop) => isString(prop),
}

export const DIMENSION_PROP_PROGRAM = {
name: 'program',
defaultValue: {},
required: false,
isValid: (prop) => isObject(prop),
}

export const DIMENSION_PROP_PROGRAM_STAGE = {
name: 'programStage',
defaultValue: {},
Expand All @@ -56,6 +63,7 @@ export const DIMENSION_PROPS = [
DIMENSION_PROP_ITEMS,
DIMENSION_PROP_FILTER,
DIMENSION_PROP_LEGEND_SET,
DIMENSION_PROP_PROGRAM,
DIMENSION_PROP_PROGRAM_STAGE,
DIMENSION_PROP_REPETITION,
]
4 changes: 4 additions & 0 deletions src/modules/layout/dimensionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DIMENSION_PROP_ITEMS,
DIMENSION_PROP_FILTER,
DIMENSION_PROP_LEGEND_SET,
DIMENSION_PROP_PROGRAM,
DIMENSION_PROP_PROGRAM_STAGE,
DIMENSION_PROP_REPETITION,
} from './dimension.js'
Expand All @@ -17,6 +18,9 @@ export const dimensionCreate = (dimensionId, itemIds = [], args = {}) => {
...(args.legendSet && {
[DIMENSION_PROP_LEGEND_SET.name]: args.legendSet,
}),
...(args.program && {
[DIMENSION_PROP_PROGRAM.name]: args.program,
}),
...(args.programStage && {
[DIMENSION_PROP_PROGRAM_STAGE.name]: args.programStage,
}),
Expand Down

0 comments on commit 9692cd0

Please sign in to comment.