diff --git a/src/api/analytics.js b/src/api/analytics.js index f6d3b338de..fc4751ea4a 100644 --- a/src/api/analytics.js +++ b/src/api/analytics.js @@ -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 @@ -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, diff --git a/src/modules/analytics.js b/src/modules/analytics.js index 7bdc725763..92fc15f0b3 100644 --- a/src/modules/analytics.js +++ b/src/modules/analytics.js @@ -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) => { diff --git a/src/modules/fetchData.js b/src/modules/fetchData.js index d935136fc0..e74d28bab2 100644 --- a/src/modules/fetchData.js +++ b/src/modules/fetchData.js @@ -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 { @@ -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,