From 45d734e4f9b38b548420d3d67e35779b3caf84ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 20:32:08 +0100 Subject: [PATCH] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 72e93ba076ee..3d7844f9091e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -50,6 +50,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; @@ -332,6 +333,7 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti Integer latestDataYear = availableDataYears.get(availableDataYears.size() - 1); Program program = partition.getMasterTable().getProgram(); String partitionClause = getPartitionClause(partition); + String attributeJoinClause = getAttributeJoinClause(program); String fromClause = replaceQualify( @@ -350,6 +352,7 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti left join ${organisationunit} enrollmentou on en.organisationunitid=enrollmentou.organisationunitid \ inner join analytics_rs_categorystructure acs on ev.attributeoptioncomboid=acs.categoryoptioncomboid \ left join analytics_rs_dateperiodstructure dps on cast(${eventDateExpression} as date)=dps.dateperiod \ + ${attributeJoinClause}\ where ev.lastupdated < '${startTime}' ${partitionClause} \ and pr.programid=${programId} \ and ev.organisationunitid is not null \ @@ -362,6 +365,7 @@ and ev.status in (${exportableEventStatues}) \ "eventDateMonth", sqlBuilder.dateTrunc("month", eventDateExpression), "eventDateExpression", eventDateExpression, "partitionClause", partitionClause, + "attributeJoinClause", attributeJoinClause, "startTime", toLongDate(params.getStartTime()), "programId", String.valueOf(program.getId()), "firstDataYear", String.valueOf(firstDataYear), @@ -371,6 +375,19 @@ and ev.status in (${exportableEventStatues}) \ populateTableInternal(partition, fromClause); } + private String getAttributeJoinClause(Program program) { + String template = + """ + left join ${trackedentityattributevalue} ${uid} \ + on en.trackedentityid=${uid}.trackedentityid \ + and ${uid}.trackedentityattributeid = ${uid}"""; + + return getAttributeColumns(program).stream() + .map(col -> replaceQualify(template, Map.of("uid", quote(col.getName())))) + .collect(Collectors.joining(" ")) + + " "; + } + /** * Returns a partition SQL clause. * @@ -420,7 +437,7 @@ private List getColumns(Program program) { return filterDimensionColumns(columns); } - protected AnalyticsTableColumn getPartitionColumn() { + private AnalyticsTableColumn getPartitionColumn() { return AnalyticsTableColumn.builder() .name("year") .dataType(INTEGER) @@ -815,6 +832,7 @@ private List getDataYears( "fromDate", toMediumDate(params.getFromDate()))) : EMPTY; + String sql = replaceQualify( """