Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .hooks/pre-push
100755 → 100644
Empty file.
14 changes: 1 addition & 13 deletions src/components/app-wrapper/metadata-helpers/analytics-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isMetadataInputItem } from './type-guards'
import type { AnalyticsResponseMetadataItems, MetadataInput } from './types'
import type { LineListAnalyticsDataHeader } from '@components/line-list/types'
import type { AnalyticsResponseMetadataDimensions } from '@components/plugin-wrapper/hooks/use-line-list-analytics-data'
import { headersMap } from '@modules/visualization'
import { reversedHeadersMap } from '@modules/visualization'

const extractItemsMetadata = (
items: AnalyticsResponseMetadataItems,
Expand Down Expand Up @@ -50,18 +50,6 @@ const extractItemsMetadata = (
return acc
}, {})

/* The headersMap is a lookup for app -> webApi (i.e. eventDate -> eventdate)
* but here the lookup needs to be in the reverse order (i.e. eventdate -> eventDate)
* because we need to map the keys from the header columns in the response data
* for usage in the app */
const reversedHeadersMap = Object.entries(headersMap).reduce(
(acc, [key, value]) => {
acc[value] = key
return acc
},
{}
)

const updateNamesFromHeaders = (
headers: Array<LineListAnalyticsDataHeader>,
metdataFromItems: MetadataInput
Expand Down
61 changes: 18 additions & 43 deletions src/components/app-wrapper/metadata-helpers/visualization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import i18n from '@dhis2/d2-i18n'
import deepmerge from 'deepmerge'
import type {
MetadataInput,
Expand All @@ -16,10 +15,13 @@ import {
getTimeDimensions,
getUiDimensionType,
} from '@modules/dimension'
import { transformVisualization } from '@modules/visualization'
import { getDefaultOrgUnitMetadata } from '@modules/metadata'
import {
dimensionMetadataPropMap,
transformVisualization,
} from '@modules/visualization'
import type {
DimensionId,
DimensionType,
InternalDimensionRecord,
SavedVisualization,
} from '@types'
Expand All @@ -29,34 +31,6 @@ const FIXED_DIMENSION_LOOKUP = new Set<DimensionId>([
'eventStatus',
'programStatus',
])
const DIMENSION_METADATA_PROP_MAP = {
dataElementDimensions: 'dataElement',
attributeDimensions: 'attribute',
programIndicatorDimensions: 'programIndicator',
categoryDimensions: 'category',
categoryOptionGroupSetDimensions: 'categoryOptionGroupSet',
organisationUnitGroupSetDimensions: 'organisationUnitGroupSet',
dataElementGroupSetDimensions: 'dataElementGroupSet',
}
const getDefaultOrgUnitMetadata = (
outputType: SavedVisualization['outputType']
) => ({
ou: {
id: 'ou',
dimensionType: 'ORGANISATION_UNIT' as DimensionType,
name: getDefaultOrgUnitLabel(outputType),
},
})

const getDefaultOrgUnitLabel = (
outputType: SavedVisualization['outputType']
) => {
if (outputType === 'TRACKED_ENTITY_INSTANCE') {
return i18n.t('Registration org. unit')
} else {
return i18n.t('Organisation unit')
}
}

const getDefaultDynamicTimeDimensionsMetadata = (
program?: SavedVisualization['program'],
Expand Down Expand Up @@ -165,19 +139,20 @@ export const extractProgramDimensionsMetadata = (
export const extractDimensionMetadata = (
visualization: SavedVisualization
): MetadataInputMap => {
const dimensionMetadata = Object.entries(
DIMENSION_METADATA_PROP_MAP
).reduce((metaData, [listName, dimensionName]) => {
const dimensionList = visualization[listName] || []

dimensionList.forEach((dimensionWrapper: object) => {
const dimension: InternalDimensionRecord =
dimensionWrapper[dimensionName]
metaData[dimension.id] = dimension
})
const dimensionMetadata = Object.entries(dimensionMetadataPropMap).reduce(
(metaData, [listName, dimensionName]) => {
const dimensionList = visualization[listName] || []

dimensionList.forEach((dimensionWrapper: object) => {
const dimension: InternalDimensionRecord =
dimensionWrapper[dimensionName]
metaData[dimension.id] = dimension
})

return metaData
}, {})
return metaData
},
{}
)
return dimensionMetadata
}

Expand Down
12 changes: 5 additions & 7 deletions src/components/line-list/use-transformed-line-list-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
getMainDimensions,
getProgramDimensions,
} from '@modules/dimension'
import { headersMap } from '@modules/visualization'
import {
headersMap,
getDimensionIdFromHeaderName,
} from '@modules/visualization'
import type {
CurrentVisualization,
OutputType,
Expand Down Expand Up @@ -63,12 +66,7 @@ const getHeaderDimensionId = (
id: header.name ?? '',
outputType,
})
const idMatch =
Object.keys(headersMap).find(
(key) => headersMap[key] === dimensionId
// TODO: find a better solution
// https://dhis2.atlassian.net/browse/DHIS2-20136
) ?? ''
const idMatch = getDimensionIdFromHeaderName(dimensionId) ?? ''

const formattedDimensionId = getFullDimensionId({
dimensionId: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '@modules/dimension'
import { isValueTypeNumeric } from '@modules/value-type'
import {
headersMap,
getDimensionIdFromHeaderName,
isVisualizationWithTimeDimension,
} from '@modules/visualization'
import type { CurrentUser, CurrentVisualization, OutputType } from '@types'
Expand Down Expand Up @@ -182,12 +182,7 @@ const extractHeaders = (
id: header.name,
outputType,
})
const idMatch =
Object.keys(headersMap).find(
(key) => headersMap[key] === dimensionId
// TODO: find a better solution
// https://dhis2.atlassian.net/browse/DHIS2-20136
) ?? ''
const idMatch = getDimensionIdFromHeaderName(dimensionId) ?? ''

const formattedDimensionId = getFullDimensionId({
dimensionId: [
Expand Down Expand Up @@ -242,12 +237,7 @@ const extractHeaders = (
outputType,
})

const idMatch =
Object.keys(headersMap).find(
(key) => headersMap[key] === dimensionId
// TODO: find a better solution
// https://dhis2.atlassian.net/browse/DHIS2-20136
) ?? ''
const idMatch = getDimensionIdFromHeaderName(dimensionId) ?? ''

result.column =
labels.find(
Expand Down
16 changes: 6 additions & 10 deletions src/constants/options.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import type { EventVisualizationOptions, LegendOption } from '@types'

export const OPTIONS_SECTION_KEYS_LINE_LIST = [
'data',
'style',
'legend',
] as const
export const OPTIONS_SECTION_KEYS_PIVOT_TABLE = [
'data',
'style',
'legend',
] as const
// Base options section keys shared by all visualization types
const OPTIONS_SECTION_KEYS = ['data', 'style', 'legend'] as const

// Re-export with specific names for backwards compatibility and type derivation
export const OPTIONS_SECTION_KEYS_LINE_LIST = OPTIONS_SECTION_KEYS
export const OPTIONS_SECTION_KEYS_PIVOT_TABLE = OPTIONS_SECTION_KEYS

export const DEFAULT_LEGEND_OPTION: LegendOption = {
showKey: false,
Expand Down
38 changes: 33 additions & 5 deletions src/modules/visualization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ export const headersMap: Record<DimensionId, string> = {
lastUpdated: 'lastupdated',
}

/**
* Pre-computed reverse map from API header names to dimension IDs.
* Use this when you need to look up multiple dimension IDs efficiently.
*/
export const reversedHeadersMap: Record<string, DimensionId> = Object.entries(
headersMap
).reduce((acc, [key, value]) => {
acc[value] = key as DimensionId
return acc
}, {} as Record<string, DimensionId>)

/**
* Get the dimension ID (app format) from a header name (API format).
* This is a reverse lookup from headersMap (e.g., 'eventdate' -> 'eventDate').
* Uses the pre-computed reversedHeadersMap for O(1) lookup.
* @param headerName - The header name from the API response
* @returns The dimension ID for the app, or undefined if not found
*/
export const getDimensionIdFromHeaderName = (
headerName: string
): DimensionId | undefined => reversedHeadersMap[headerName]

export const getHeadersMap = ({
showHierarchy,
}: {
Expand Down Expand Up @@ -170,14 +192,20 @@ const removeDimensionPropertiesBeforeSaving = (
})
}

const getDimensionIdFromHeaderName = (
const getDimensionIdFromHeaderNameWithContext = (
headerName: string,
visualization: CurrentVisualization
) => {
const headersMap = getHeadersMap(
getRequestOptions(visualization) as unknown as CurrentVisualization
// Type cast is needed because getRequestOptions returns ParameterRecord
// which doesn't expose showHierarchy explicitly, but may contain it.
// TODO: Consider updating getRequestOptions return type to be more specific
// See: https://dhis2.atlassian.net/browse/DHIS2-19823
const contextHeadersMap = getHeadersMap(
getRequestOptions(visualization) as { showHierarchy?: boolean }
)
return Object.keys(contextHeadersMap).find(
(key) => contextHeadersMap[key] === headerName
)
return Object.keys(headersMap).find((key) => headersMap[key] === headerName)
}

export const getSaveableVisualization = (
Expand Down Expand Up @@ -213,7 +241,7 @@ export const getSaveableVisualization = (
? [
{
dimension:
getDimensionIdFromHeaderName(
getDimensionIdFromHeaderNameWithContext(
vis.sorting[0].dimension,
vis
) || vis.sorting[0].dimension,
Expand Down