Skip to content

Commit

Permalink
fix: Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge committed Nov 26, 2024
1 parent 105b514 commit 45d734e
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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 \
Expand All @@ -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),
Expand All @@ -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.
*
Expand Down Expand Up @@ -420,7 +437,7 @@ private List<AnalyticsTableColumn> getColumns(Program program) {
return filterDimensionColumns(columns);
}

protected AnalyticsTableColumn getPartitionColumn() {
private AnalyticsTableColumn getPartitionColumn() {
return AnalyticsTableColumn.builder()
.name("year")
.dataType(INTEGER)
Expand Down Expand Up @@ -815,6 +832,7 @@ private List<Integer> getDataYears(
"fromDate",
toMediumDate(params.getFromDate())))
: EMPTY;

String sql =
replaceQualify(
"""
Expand Down

0 comments on commit 45d734e

Please sign in to comment.