Skip to content

Commit

Permalink
fix: Conflicts [DHIS2-16202]
Browse files Browse the repository at this point in the history
  • Loading branch information
maikelarabori committed Nov 27, 2023
2 parents 6860ac0 + abcd608 commit 76dfc47
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 14,941 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -62,11 +63,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 @@ -159,23 +160,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 @@ -189,7 +175,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 @@ -202,8 +190,7 @@ private void buildLeftJoins(
"("
+ enrollmentProgramIndicatorSelect(
param.getDimensionIdentifier().getProgram(),
expression,
filter,
programIndicatorQueryParts,
false)
+ ") as "
+ enrollmentAlias,
Expand All @@ -214,21 +201,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();
int offset = program.getOffsetWithDefault();

return "select innermost_enr.*"
Expand All @@ -251,29 +265,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
Loading

0 comments on commit 76dfc47

Please sign in to comment.