Skip to content

Commit

Permalink
Merge branch 'master' into DHIS2-16215
Browse files Browse the repository at this point in the history
  • Loading branch information
david-mackessy committed Nov 27, 2023
2 parents 137ce47 + abcd608 commit 056bbe3
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.analytics.AggregationType;
import org.hisp.dhis.analytics.DataType;
import org.hisp.dhis.analytics.common.params.AnalyticsSortingParams;
import org.hisp.dhis.analytics.common.params.dimension.DimensionIdentifier;
Expand All @@ -61,11 +62,11 @@
import org.hisp.dhis.analytics.tei.query.context.sql.RenderableSqlQuery;
import org.hisp.dhis.analytics.tei.query.context.sql.SqlQueryBuilder;
import org.hisp.dhis.analytics.tei.query.context.sql.SqlQueryBuilders;
import org.hisp.dhis.commons.util.TextUtils;
import org.hisp.dhis.program.AnalyticsType;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramIndicator;
import org.hisp.dhis.program.ProgramIndicatorService;
import org.hisp.dhis.program.ProgramStage;
import org.springframework.stereotype.Service;

/**
Expand Down Expand Up @@ -158,23 +159,8 @@ private void buildLeftJoins(
ProgramIndicator programIndicator =
(ProgramIndicator) param.getDimensionIdentifier().getDimension().getQueryItem().getItem();

String expression =
programIndicatorService.getAnalyticsSql(
programIndicator.getExpression(),
DataType.NUMERIC,
programIndicator,
null,
null,
SUBQUERY_TABLE_ALIAS);

String filter =
programIndicatorService.getAnalyticsSql(
programIndicator.getFilter(),
DataType.BOOLEAN,
programIndicator,
null,
null,
SUBQUERY_TABLE_ALIAS);
ProgramIndicatorQueryParts programIndicatorQueryParts =
getProgramIndicatorQueryParts(programIndicator);

builder.selectField(
Field.ofUnquoted(
Expand All @@ -188,7 +174,9 @@ private void buildLeftJoins(
() ->
"("
+ enrollmentProgramIndicatorSelect(
param.getDimensionIdentifier().getProgram(), expression, filter, true)
param.getDimensionIdentifier().getProgram(),
programIndicatorQueryParts,
true)
+ ") as "
+ assignedAlias,
fieldsEqual(TEI_ALIAS, TEI_UID, assignedAlias, TEI_UID)));
Expand All @@ -201,8 +189,7 @@ private void buildLeftJoins(
"("
+ enrollmentProgramIndicatorSelect(
param.getDimensionIdentifier().getProgram(),
expression,
filter,
programIndicatorQueryParts,
false)
+ ") as "
+ enrollmentAlias,
Expand All @@ -213,21 +200,48 @@ private void buildLeftJoins(
"("
+ eventProgramIndicatorSelect(
param.getDimensionIdentifier().getProgram(),
param.getDimensionIdentifier().getProgramStage(),
expression,
filter)
programIndicatorQueryParts)
+ ") as "
+ assignedAlias,
fieldsEqual(enrollmentAlias, PI_UID, assignedAlias, PI_UID)));
}
}
}

private record ProgramIndicatorQueryParts(String function, String expression, String filter) {}

private ProgramIndicatorQueryParts getProgramIndicatorQueryParts(
ProgramIndicator programIndicator) {

return new ProgramIndicatorQueryParts(
// function
TextUtils.emptyIfEqual(
programIndicator.getAggregationTypeFallback().getValue(),
AggregationType.CUSTOM.getValue()),
// expression
programIndicatorService.getAnalyticsSql(
programIndicator.getExpression(),
DataType.NUMERIC,
programIndicator,
null,
null,
SUBQUERY_TABLE_ALIAS),
// filter
programIndicatorService.getAnalyticsSql(
programIndicator.getFilter(),
DataType.BOOLEAN,
programIndicator,
null,
null,
SUBQUERY_TABLE_ALIAS));
}

private static String enrollmentProgramIndicatorSelect(
ElementWithOffset<Program> program,
String expression,
String filter,
ProgramIndicatorQueryParts programIndicatorQueryParts,
boolean needsExpressions) {
String expression = programIndicatorQueryParts.expression();
String filter = programIndicatorQueryParts.filter();
return "select innermost_enr.*"
+ " from (select tei as "
+ TEI_UID
Expand All @@ -246,29 +260,31 @@ private static String enrollmentProgramIndicatorSelect(
}

static String eventProgramIndicatorSelect(
ElementWithOffset<Program> program,
ElementWithOffset<ProgramStage> programStage,
String expression,
String filter) {
String condition = SUBQUERY_TABLE_ALIAS + ".ps = '" + programStage.getElement().getUid() + "'";
ElementWithOffset<Program> program, ProgramIndicatorQueryParts programIndicatorQueryParts) {

String filter = programIndicatorQueryParts.filter();
String function = programIndicatorQueryParts.function();
String expression = programIndicatorQueryParts.expression();

String whereCondition = "";

if (StringUtils.isNotBlank(filter)) {
condition = condition + " and " + filter;
whereCondition = " where " + filter;
}
return "select innermost_evt.*"
return "select innermost_evt.programinstanceuid, "
+ function
+ "(innermost_evt.value) as value"
+ " from (select pi as "
+ PI_UID
+ ", "
+ expression
+ " as value, "
+ " row_number() over (partition by pi order by occurreddate desc) as rn "
+ " as value "
+ " from analytics_event_"
+ program.getElement().getUid()
+ " as "
+ SUBQUERY_TABLE_ALIAS
+ " where "
+ condition
+ ") innermost_evt"
+ " where innermost_evt.rn = 1";
+ whereCondition
+ ") innermost_evt group by innermost_evt.programinstanceuid";
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2695,4 +2695,69 @@ public void queryWithEventStatus() {
"",
"COMPLETED"));
}

@Test
public void queryProgramIndicator() {
// Given
QueryParamsBuilder params =
new QueryParamsBuilder()
.add("program=IpHINAT79UW")
.add(
"dimension=IpHINAT79UW.GxdhnY5wmHq,w75KJ2mc4zz:eq:Justin,zDhUuAYrxNC:eq:Hayes,ou:eqPIdr5yD1Q")
.add("desc=IpHINAT79UW.GxdhnY5wmHq")
.add("lastUpdated=LAST_YEAR")
.add("relativePeriodDate=2016-01-01");

// When
ApiResponse response = analyticsTeiActions.query().get("nEenWmSyUEp", JSON, JSON, params);

// Then
response
.validate()
.statusCode(200)
.body("rows", hasSize(equalTo(1)))
.body("height", equalTo(1))
.body("width", equalTo(17))
.body("headerWidth", equalTo(17))
.body("headers", hasSize(equalTo(17)))
.body("metaData.pager.page", equalTo(1))
.body("metaData.pager.pageSize", equalTo(50))
.body("metaData.pager.isLastPage", is(true))
.body("metaData.pager", not(hasKey("total")))
.body("metaData.pager", not(hasKey("pageCount")))
.body("metaData.items.GxdhnY5wmHq.name", equalTo("Average weight (g)"))
.body("metaData.dimensions", hasKey("pe"));

validateHeader(
response,
16,
"IpHINAT79UW.GxdhnY5wmHq",
"Average weight (g)",
"NUMBER",
"java.lang.Double",
false,
true);

validateRow(
response,
0,
List.of(
"a04hYxjC8lM",
"2015-08-06 21:20:52.547",
"",
"2015-08-06 21:20:52.547",
"",
"",
"",
"",
"",
"Rokolon MCHP",
"OU_707826",
"Sierra Leone / Moyamba / Ribbi / Rokolon MCHP",
"Justin",
"Hayes",
"Male",
"",
"2994.5"));
}
}

0 comments on commit 056bbe3

Please sign in to comment.