Skip to content

Commit

Permalink
fix: Investigate the reason of differences in events x enrollments in…
Browse files Browse the repository at this point in the history
… analytics query[2.40-DHIS2-16227]
  • Loading branch information
d-bernat committed Jun 10, 2024
1 parent e84df46 commit eecb477
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ public class JdbcEnrollmentAnalyticsManager extends AbstractJdbcEventAnalyticsMa

private static final String ANALYTICS_EVENT = "analytics_event_";

private static final String ORDER_BY_EXECUTION_DATE = "order by executiondate ";
private static final String DIRECTION_PLACEHOLDER = "#DIRECTION_PLACEHOLDER";
private static final String ORDER_BY_EXECUTION_DATE =
"order by executiondate " + DIRECTION_PLACEHOLDER + ", created " + DIRECTION_PLACEHOLDER;

private static final String LIMIT_1 = "limit 1";

Expand Down Expand Up @@ -517,8 +519,9 @@ protected ColumnAndAlias getCoordinateColumn(final QueryItem item, final String
+ colName
+ " is not null "
+ psCondition
+ ORDER_BY_EXECUTION_DATE
+ createOrderTypeAndOffset(item.getProgramStageOffset())
+ createOrderType(item.getProgramStageOffset())
+ " "
+ createOffset(item.getProgramStageOffset())
+ " "
+ LIMIT_1
+ " )",
Expand Down Expand Up @@ -569,8 +572,9 @@ protected String getColumn(final QueryItem item, final String suffix) {
+ getExecutionDateFilter(
item.getRepeatableStageParams().getStartDate(),
item.getRepeatableStageParams().getEndDate())
+ ORDER_BY_EXECUTION_DATE
+ createOrderTypeAndOffset(item.getProgramStageOffset())
+ createOrderType(item.getProgramStageOffset())
+ " "
+ createOffset(item.getProgramStageOffset())
+ getLimit(item.getRepeatableStageParams().getCount())
+ " ) as t1)";
}
Expand All @@ -591,8 +595,9 @@ protected String getColumn(final QueryItem item, final String suffix) {
+ getExecutionDateFilter(
item.getRepeatableStageParams().getStartDate(),
item.getRepeatableStageParams().getEndDate())
+ ORDER_BY_EXECUTION_DATE
+ createOrderTypeAndOffset(item.getProgramStageOffset())
+ createOrderType(item.getProgramStageOffset())
+ " "
+ createOffset(item.getProgramStageOffset())
+ " "
+ LIMIT_1
+ " )";
Expand All @@ -618,8 +623,9 @@ protected String getColumn(final QueryItem item, final String suffix) {
+ "and ps = '"
+ item.getProgramStage().getUid()
+ "' "
+ ORDER_BY_EXECUTION_DATE
+ createOrderTypeAndOffset(item.getProgramStageOffset())
+ createOrderType(item.getProgramStageOffset())
+ " "
+ createOffset(item.getProgramStageOffset())
+ " "
+ LIMIT_1
+ " )";
Expand Down Expand Up @@ -690,14 +696,26 @@ protected AnalyticsType getAnalyticsType() {
return AnalyticsType.ENROLLMENT;
}

private String createOrderTypeAndOffset(int offset) {
private String createOffset(int offset) {
if (offset == 0) {
return EMPTY;
}

if (offset < 0) {
return "offset " + (-1 * offset);
} else {
return "offset " + (offset - 1);
}
}

private String createOrderType(int offset) {
if (offset == 0) {
return "desc";
return ORDER_BY_EXECUTION_DATE.replace(DIRECTION_PLACEHOLDER, "desc");
}
if (offset < 0) {
return "desc offset " + (-1 * offset);
return ORDER_BY_EXECUTION_DATE.replace(DIRECTION_PLACEHOLDER, "desc");
} else {
return "asc offset " + (offset - 1);
return ORDER_BY_EXECUTION_DATE.replace(DIRECTION_PLACEHOLDER, "asc");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void verifyWithProgramStageAndNumericDataElement(ValueType valueType) {
+ programA.getUid()
+ ".pi = ax.pi and \"fWIAEtYVEGk\" is not null and ps = '"
+ programStage.getUid()
+ "' order by executiondate desc limit 1 )";
+ "' order by executiondate desc, created desc limit 1 )";

if (valueType == ValueType.NUMBER) {
subSelect = subSelect + " as \"fWIAEtYVEGk\"";
Expand Down Expand Up @@ -259,7 +259,7 @@ private void verifyWithRepeatableProgramStageAndDataElement(ValueType valueType)
+ programUid
+ ".pi = ax.pi and ps = '"
+ repeatableProgramStage.getUid()
+ "' order by executiondate desc offset 1 limit 1 ) "
+ "' order by executiondate desc, created desc offset 1 limit 1 ) "
+ "as \""
+ programStageUid
+ "[-1]."
Expand All @@ -274,7 +274,7 @@ private void verifyWithRepeatableProgramStageAndDataElement(ValueType valueType)
+ programUid
+ ".pi = ax.pi and ps = '"
+ programStageUid
+ "' order by executiondate desc offset 1 limit 1 )) "
+ "' order by executiondate desc, created desc offset 1 limit 1 )) "
+ "as \""
+ programStageUid
+ "[-1]."
Expand Down Expand Up @@ -306,7 +306,7 @@ void verifyWithProgramStageAndTextualDataElementAndFilter() {
+ programA.getUid()
+ ".pi = ax.pi and \"fWIAEtYVEGk\" is not null and ps = '"
+ programStage.getUid()
+ "' order by executiondate desc limit 1 )";
+ "' order by executiondate desc, created desc limit 1 )";

String expected =
"ax.\"quarterly\",ax.\"ou\","
Expand Down Expand Up @@ -357,7 +357,7 @@ void verifyWithProgramStageAndNumericDataElementAndFilter2() {
+ programA.getUid()
+ ".pi = ax.pi and \"fWIAEtYVEGk\" is not null and ps = '"
+ programStage.getUid()
+ "' order by executiondate desc limit 1 )";
+ "' order by executiondate desc, created desc limit 1 )";

String expected =
"ax.\"quarterly\",ax.\"ou\","
Expand Down Expand Up @@ -407,7 +407,7 @@ void verifyGetEnrollmentsWithMissingValueEqFilter() {
+ programA.getUid()
+ ".pi = ax.pi and \"fWIAEtYVEGk\" is not null and ps = '"
+ programStage.getUid()
+ "' order by executiondate desc limit 1 )";
+ "' order by executiondate desc, created desc limit 1 )";

String expected = subSelect + " is null";

Expand All @@ -426,7 +426,7 @@ void verifyGetEnrollmentsWithMissingValueNeqFilter() {
+ programA.getUid()
+ ".pi = ax.pi and \"fWIAEtYVEGk\" is not null and ps = '"
+ programStage.getUid()
+ "' order by executiondate desc limit 1 )";
+ "' order by executiondate desc, created desc limit 1 )";

String expected = subSelect + " is not null";
testIt(
Expand All @@ -444,7 +444,7 @@ void verifyGetEnrollmentsWithMissingValueAndNumericValuesInFilter() {
+ programA.getUid()
+ ".pi = ax.pi and \"fWIAEtYVEGk\" is not null and ps = '"
+ programStage.getUid()
+ "' order by executiondate desc limit 1 )";
+ "' order by executiondate desc, created desc limit 1 )";

String numericValues = String.join(OPTION_SEP, "10", "11", "12");
String expected =
Expand Down Expand Up @@ -472,7 +472,7 @@ void verifyGetEnrollmentsWithoutMissingValueAndNumericValuesInFilter() {
+ programA.getUid()
+ ".pi = ax.pi and \"fWIAEtYVEGk\" is not null and ps = '"
+ programStage.getUid()
+ "' order by executiondate desc limit 1 )";
+ "' order by executiondate desc, created desc limit 1 )";

String numericValues = String.join(OPTION_SEP, "10", "11", "12");
String expected = subSelect + " in (" + String.join(",", numericValues.split(OPTION_SEP)) + ")";
Expand All @@ -491,7 +491,7 @@ void verifyGetEnrollmentsWithOnlyMissingValueInFilter() {
+ programA.getUid()
+ ".pi = ax.pi and \"fWIAEtYVEGk\" is not null and ps = '"
+ programStage.getUid()
+ "' order by executiondate desc limit 1 )";
+ "' order by executiondate desc, created desc limit 1 )";

String expected = subSelect + " is null";
String unexpected = "(" + subSelect + " in (";
Expand Down Expand Up @@ -729,7 +729,7 @@ void verifyGetColumnOfTypeCoordinateAndWithProgramStages() {
+ dataElementA.getUid()
+ "\" is not null and ps = '"
+ programStage.getUid()
+ "' order by executiondate desc limit 1 )"));
+ "' order by executiondate desc, created desc limit 1 )"));
}

@Test
Expand Down Expand Up @@ -763,7 +763,7 @@ void verifyGetColumnOfTypeCoordinateAndWithProgramStagesAndParamsWithReferenceTy
+ programB.getUid()
+ ".pi = ax.pi and ps = '"
+ repeatableProgramStage.getUid()
+ "' and executiondate >= '2022-01-01' and executiondate <= '2022-01-31' order by executiondate desc LIMIT 100 ) as t1)"));
+ "' and executiondate >= '2022-01-01' and executiondate <= '2022-01-31' order by executiondate desc, created desc LIMIT 100 ) as t1)"));
}

@Test
Expand Down Expand Up @@ -795,7 +795,7 @@ void verifyGetColumnOfTypeCoordinateAndWithProgramStagesAndParamsWithNumberTypeV
+ programB.getUid()
+ ".pi = ax.pi and ps = '"
+ repeatableProgramStage.getUid()
+ "' order by executiondate desc limit 1 )"));
+ "' order by executiondate desc, created desc limit 1 )"));
}

@Test
Expand Down Expand Up @@ -834,8 +834,8 @@ void verifyGetCoordinateColumnAndNoProgramStage() {
+ "and "
+ colName
+ " is not null "
+ "order by executiondate "
+ "desc limit 1 )"));
+ "order by executiondate desc, created desc "
+ " limit 1 )"));
}

@Test
Expand Down Expand Up @@ -877,8 +877,8 @@ void verifyGetCoordinateColumnWithProgramStage() {
+ " is not null "
+ "and ps = '"
+ item.getProgramStage().getUid()
+ "' order by executiondate "
+ "desc limit 1 )"));
+ "' order by executiondate desc, created desc "
+ " limit 1 )"));
}

@Test
Expand Down

0 comments on commit eecb477

Please sign in to comment.