Skip to content

Commit

Permalink
feat: use outlierDetection analytics endpoint (DHIS2-16345)
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardo committed Dec 20, 2023
1 parent d980fdb commit a41012b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
46 changes: 44 additions & 2 deletions src/api/analytics.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {
Analytics,
VIS_TYPE_PIVOT_TABLE,
layoutGetDimensionItems,
VIS_TYPE_PIVOT_TABLE,
DIMENSION_ID_PERIOD,
DAILY,
WEEKLY,
WEEKS_THIS_YEAR,
} from '@dhis2/analytics'
import { getRelativePeriodTypeUsed } from '../modules/analytics.js'
import {
getRelativePeriodTypeUsed,
getOutlierDetectionHeadersMap,
} from '../modules/analytics.js'

const periodId = DIMENSION_ID_PERIOD

Expand All @@ -24,6 +27,45 @@ export const apiFetchAnalytics = async (dataEngine, visualization, options) => {
return [new analyticsEngine.response(rawResponse)]
}

export const apiFetchAnalyticsForOutliersTable = async (
dataEngine,
visualization,
options
) => {
const headersMap = getOutlierDetectionHeadersMap(options)

const parameters = { ...options }

const columns = visualization.columns || []
const headers = []

columns.forEach(({ dimension, items }) => {
parameters[dimension] = items.map(({ id }) => id).join(',')

headers.push(headersMap[dimension])
})

// TODO
// // additional headers depending on the outlier method option
// if (options.outlierAnalysis.outlierMethod === 'MOD_Z_SCORE') {
// headers.push('modifiedzscore', 'median', 'medianabsdeviation')
// } else if (options.outlierAnalysis.outlierMethod === 'Z_SCORE') {
// headers.push('zscore', 'mean', 'stddev')
// }

headers.push('lowerbound', 'upperbound')

parameters.headers = headers.join(',')

const analyticsEngine = Analytics.getAnalytics(dataEngine)

const req = new analyticsEngine.request().withParameters(parameters)

const rawResponse = await analyticsEngine.aggregate.getOutliersData(req)

return [new analyticsEngine.response(rawResponse)]
}

export const apiFetchAnalyticsForYearOverYear = async (
dataEngine,
visualization,
Expand Down
25 changes: 24 additions & 1 deletion src/modules/analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
import { getRelativePeriodsOptionsById, WEEKLY, DAILY } from '@dhis2/analytics'
import {
getRelativePeriodsOptionsById,
DIMENSION_ID_DATA,
DIMENSION_ID_ORGUNIT,
DIMENSION_ID_PERIOD,
WEEKLY,
DAILY,
} from '@dhis2/analytics'

export const outlierDetectionHeadersMap = {
[DIMENSION_ID_DATA]: 'dxname',
[DIMENSION_ID_ORGUNIT]: 'ouname',
[DIMENSION_ID_PERIOD]: 'pename',
}

export const getOutlierDetectionHeadersMap = ({ showHierarchy }) => {
const map = Object.assign({}, outlierDetectionHeadersMap)

if (showHierarchy) {
map[DIMENSION_ID_ORGUNIT] = 'ounamehierarchy'
}

return map
}

export const computeYoYMatrix = (responses, relativePeriodTypeUsed) => {
const periodGroups = responses.reduce((list, res) => {
Expand Down
13 changes: 13 additions & 0 deletions src/modules/fetchData.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
isYearOverYear,
isOutliersTable,
DIMENSION_ID_PERIOD,
layoutGetDimensionItems,
ALL_DYNAMIC_DIMENSION_ITEMS,
} from '@dhis2/analytics'
import {
apiFetchAnalyticsForYearOverYear,
apiFetchAnalyticsForOutliersTable,
apiFetchAnalytics,
} from '../api/analytics.js'
import {
Expand Down Expand Up @@ -94,6 +96,17 @@ export const fetchData = async ({
}
}

if (isOutliersTable(adaptedVisualization.type)) {
return {
responses: await apiFetchAnalyticsForOutliersTable(
dataEngine,
adaptedVisualization,
options
),
extraOptions,
}
}

return {
responses: await apiFetchAnalytics(
dataEngine,
Expand Down

0 comments on commit a41012b

Please sign in to comment.