Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/DHIS2-18451' into DHIS2-18451
Browse files Browse the repository at this point in the history
  • Loading branch information
gnespolino committed Dec 12, 2024
2 parents ee03476 + 302b25a commit e6da392
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 785 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,9 @@ protected void populateTableInternal(AnalyticsTablePartition partition, String f
List<AnalyticsTableColumn> columns = partition.getMasterTable().getAnalyticsTableColumns();

String sql = "insert into " + tableName + " (";

sql += toCommaSeparated(columns, col -> quote(col.getName()));

sql += ") select ";

sql += toCommaSeparated(columns, AnalyticsTableColumn::getSelectExpression);

sql += " " + fromClause;

invokeTimeAndLog(sql, "Populating table: '{}'", tableName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,7 @@ private List<AnalyticsTableColumn> getColumnForDataElement(
sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value");
String columnExpression = getColumnExpression(dataElement.getValueType(), jsonExpression);
String dataFilterClause = getDataFilterClause(dataElement);
String selectExpression =
String.format("%s as %s", columnExpression, quote(dataElement.getUid()));
String selectExpression = getSelectExpression(dataElement, columnExpression);
Skip skipIndex = skipIndex(dataElement.getValueType(), dataElement.hasOptionSet());

if (withLegendSet) {
Expand All @@ -516,6 +515,17 @@ private List<AnalyticsTableColumn> getColumnForDataElement(
return columns;
}

/**
* Returns a select expression.
*
* @param dataElement the {@link DataElement}.
* @param columnExpression the column expression.
* @return a select expression.
*/
private String getSelectExpression(DataElement dataElement, String columnExpression) {
return String.format("%s as %s", columnExpression, quote(dataElement.getUid()));
}

/**
* Returns a list of columns.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static java.lang.String.join;
import static java.util.stream.Collectors.groupingBy;
import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static org.hisp.dhis.analytics.AnalyticsTableType.TRACKED_ENTITY_INSTANCE;
import static org.hisp.dhis.analytics.table.JdbcEventAnalyticsTableManager.EXPORTABLE_EVENT_STATUSES;
import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType;
Expand Down Expand Up @@ -176,25 +177,27 @@ private List<AnalyticsTableColumn> getColumns(
(Map<String, List<Program>>) params.getExtraParam("", PROGRAMS_BY_TET_KEY);

List<AnalyticsTableColumn> columns = new ArrayList<>(getFixedColumns());

String enrolledInProgramExpression =
"""
\s exists(select 1 from ${enrollment} en_0 \
where en_0.trackedentityid = te.trackedentityid \
and en_0.programid = ${programId})""";

emptyIfNull(programsByTetUid.get(trackedEntityType.getUid()))
.forEach(
program ->
columns.add(
AnalyticsTableColumn.builder()
.name(program.getUid())
.dataType(BOOLEAN)
.selectExpression(
replaceQualify(
enrolledInProgramExpression,
Map.of("programId", String.valueOf(program.getId()))))
.build()));
List<Program> programs = programsByTetUid.get(trackedEntityType.getUid());

if (isNotEmpty(programs)) {
String enrolledInProgramExpression =
"""
\s exists(select 1 from ${enrollment} en_0 \
where en_0.trackedentityid = te.trackedentityid \
and en_0.programid = ${programId})""";

programs.forEach(
program ->
columns.add(
AnalyticsTableColumn.builder()
.name(program.getUid())
.dataType(BOOLEAN)
.selectExpression(
replaceQualify(
enrolledInProgramExpression,
Map.of("programId", String.valueOf(program.getId()))))
.build()));
}

List<TrackedEntityAttribute> trackedEntityAttributes =
getAllTrackedEntityAttributes(trackedEntityType, programsByTetUid)
Expand Down Expand Up @@ -271,7 +274,6 @@ private List<AnalyticsTableColumn> getFixedColumns() {
columns.addAll(getOrganisationUnitLevelColumns());
columns.add(getOrganisationUnitNameHierarchyColumn());
columns.addAll(getFixedNonGroupByColumns());

return columns;
}

Expand Down Expand Up @@ -308,18 +310,23 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti
left join analytics_rs_organisationunitgroupsetstructure ougs on te.organisationunitid=ougs.organisationunitid""",
Map.of()));

((List<TrackedEntityAttribute>)
params.getExtraParam(trackedEntityType.getUid(), ALL_NON_CONFIDENTIAL_TET_ATTRIBUTES))
.forEach(
tea ->
sql.append(
replaceQualify(
"""
\s left join ${trackedentityattributevalue} ${teaUid} on ${teaUid}.trackedentityid=te.trackedentityid \
and ${teaUid}.trackedentityattributeid = ${teaId}""",
Map.of(
"teaUid", quote(tea.getUid()),
"teaId", String.valueOf(tea.getId())))));
List<TrackedEntityAttribute> attributes =
((List<TrackedEntityAttribute>)
params.getExtraParam(trackedEntityType.getUid(), ALL_NON_CONFIDENTIAL_TET_ATTRIBUTES));

if (isNotEmpty(attributes)) {
attributes.forEach(
tea ->
sql.append(
replaceQualify(
"""
\s left join ${trackedentityattributevalue} ${teaUid} on ${teaUid}.trackedentityid=te.trackedentityid \
and ${teaUid}.trackedentityattributeid = ${teaId}""",
Map.of(
"teaUid", quote(tea.getUid()),
"teaId", String.valueOf(tea.getId())))));
}

sql.append(
replaceQualify(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ public void queryAggregatedenrollmentsmacase2() throws JSONException {
.validate()
.statusCode(200)
.body("headers", hasSize(equalTo(3)))
.body("rows", hasSize(equalTo(0)))
.body("height", equalTo(0))
.body("width", equalTo(0))
.body("rows", hasSize(equalTo(1)))
.body("height", equalTo(1))
.body("width", equalTo(3))
.body("headerWidth", equalTo(3));

// Assert metaData.
Expand All @@ -172,6 +172,7 @@ public void queryAggregatedenrollmentsmacase2() throws JSONException {
validateHeader(response, 2, "pe", "Period", "TEXT", "java.lang.String", false, true);

// Assert rows.
validateRow(response, List.of("1", "ImspTQPwCqd", "202304"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"name": "sortByTeDateAsc",
"query": "/api/analytics/trackedEntities/query/nEenWmSyUEp.json?dimension=ou:USER_ORGUNIT,gHGyrwKPzej,ciq2USN94oJ,cejWyOfXge6,IpHINAT79UW.A03MvHHogjR.bx6fsa0t90x,IpHINAT79UW.A03MvHHogjR.a3kGcGDCuk6&headers=ouname,gHGyrwKPzej,ciq2USN94oJ,cejWyOfXge6,IpHINAT79UW.A03MvHHogjR.bx6fsa0t90x,IpHINAT79UW.A03MvHHogjR.a3kGcGDCuk6,created&totalPages=false&rowContext=true&created=YESTERDAY&displayProperty=NAME&pageSize=100&page=1&includeMetadataDetails=true&asc=created&relativePeriodDate=2017-01-27",
"query": "/api/analytics/trackedEntities/query/nEenWmSyUEp.json?dimension=ou:USER_ORGUNIT,gHGyrwKPzej,ciq2USN94oJ,cejWyOfXge6,IpHINAT79UW.A03MvHHogjR.bx6fsa0t90x,IpHINAT79UW.A03MvHHogjR.a3kGcGDCuk6&headers=ouname,gHGyrwKPzej,ciq2USN94oJ,cejWyOfXge6,IpHINAT79UW.A03MvHHogjR.bx6fsa0t90x,IpHINAT79UW.A03MvHHogjR.a3kGcGDCuk6,created&totalPages=false&rowContext=true&created=YESTERDAY&displayProperty=NAME&pageSize=10&page=1&includeMetadataDetails=true&asc=created&relativePeriodDate=2017-01-27",
"version": {
"min": 41
}
Expand Down Expand Up @@ -51,7 +51,7 @@
},
{
"name": "sortByEnrollmentDateAscMultiProgram",
"query": "/api/analytics/trackedEntities/query/nEenWmSyUEp.json?dimension=ou:USER_ORGUNIT,cejWyOfXge6,w75KJ2mc4zz,IpHINAT79UW.A03MvHHogjR.a3kGcGDCuk6,ur1Edk5Oe2n.EPEcjy3FWmI.fTZFU8cWvb3&headers=ouname,cejWyOfXge6,w75KJ2mc4zz,IpHINAT79UW.A03MvHHogjR.a3kGcGDCuk6,IpHINAT79UW.ouname,ur1Edk5Oe2n.EPEcjy3FWmI.fTZFU8cWvb3,ur1Edk5Oe2n.enrollmentdate&totalPages=false&rowContext=true&enrollmentDate=ur1Edk5Oe2n.LAST_12_MONTHS,ur1Edk5Oe2n.LAST_5_YEARS&displayProperty=NAME&pageSize=30&page=1&includeMetadataDetails=true&asc=ur1Edk5Oe2n.enrollmentdate&relativePeriodDate=2022-01-27",
"query": "/api/analytics/trackedEntities/query/nEenWmSyUEp.json?dimension=ou:USER_ORGUNIT,cejWyOfXge6,w75KJ2mc4zz,IpHINAT79UW.A03MvHHogjR.a3kGcGDCuk6,ur1Edk5Oe2n.EPEcjy3FWmI.fTZFU8cWvb3&headers=ouname,cejWyOfXge6,w75KJ2mc4zz,IpHINAT79UW.A03MvHHogjR.a3kGcGDCuk6,IpHINAT79UW.ouname,ur1Edk5Oe2n.EPEcjy3FWmI.fTZFU8cWvb3,ur1Edk5Oe2n.enrollmentdate&totalPages=false&rowContext=true&enrollmentDate=ur1Edk5Oe2n.LAST_12_MONTHS,ur1Edk5Oe2n.LAST_5_YEARS&displayProperty=NAME&pageSize=30&page=1&includeMetadataDetails=true&asc=ur1Edk5Oe2n.enrollmentdate&relativePeriodDate=2028-01-27",
"version": {
"min": 41
}
Expand Down Expand Up @@ -86,7 +86,7 @@
},
{
"name": "singleOrgUnitLongitudeAsc",
"query": "/api/analytics/trackedEntities/query/nEenWmSyUEp.json?dimension=ou:jNb63DIHuwU,RG7uGl4w5Jq,lw1SqmMlnfh:GT:180&headers=ouname,RG7uGl4w5Jq,lw1SqmMlnfh&totalPages=false&rowContext=true&displayProperty=NAME&pageSize=100&page=1&includeMetadataDetails=true&asc=RG7uGl4w5Jq&relativePeriodDate=2024-06-13",
"query": "/api/analytics/trackedEntities/query/nEenWmSyUEp.json?includeMetadataDetails=true&dimension=ou:ImspTQPwCqd,RG7uGl4w5Jq,lw1SqmMlnfh:GE:190&headers=ouname,RG7uGl4w5Jq,lw1SqmMlnfh&totalPages=false&rowContext=true&displayProperty=NAME&pageSize=100&page=1&includeMetadataDetails=true&asc=RG7uGl4w5Jq&relativePeriodDate=2024-06-13",
"version": {
"min": 41
}
Expand Down Expand Up @@ -128,7 +128,28 @@
},
{
"name": "financialYear2018Sep",
"query": "/api/42/analytics/trackedEntities/query/Zy2SEgA61ys?dimension=ou:USER_ORGUNIT,B6TnnFMgmCk&headers=ouname,B6TnnFMgmCk,created&totalPages=false&rowContext=true&created=2018Sep&displayProperty=NAME&pageSize=100&page=1&includeMetadataDetails=true",
"query": "/api/42/analytics/trackedEntities/query/Zy2SEgA61ys.json?dimension=ou:USER_ORGUNIT,B6TnnFMgmCk&headers=ouname,B6TnnFMgmCk,created&totalPages=false&rowContext=true&created=2018Sep&displayProperty=NAME&pageSize=100&page=1&includeMetadataDetails=true",
"version": {
"min": 42
}
},
{
"name": "queryWithProgramAndEnrollmentDateAndNegativeEnrollmentOffset",
"query": "/api/analytics/trackedEntities/query/nEenWmSyUEp.json?program=IpHINAT79UW&enrollmentDate=IpHINAT79UW[1].LAST_YEAR&desc=lastupdated&relativePeriodDate=2023-04-03&headers=ouname,w75KJ2mc4zz,zDhUuAYrxNC",
"version": {
"min": 42
}
},
{
"name": "queryWithProgramAndFilterByEnrollmentDate",
"query": "/api/analytics/trackedEntities/query/nEenWmSyUEp.json?program=IpHINAT79UW&enrollmentDate=IpHINAT79UW.LAST_5_YEARS&desc=lastupdated&relativePeriodDate=2023-09-27&headers=trackedentity,lastupdated,createdbydisplayname,lastupdatedbydisplayname,geometry,longitude,latitude,ouname,oucode,ounamehierarchy,w75KJ2mc4zz,zDhUuAYrxNC,cejWyOfXge6,lZGmxYbs97q",
"version": {
"min": 42
}
},
{
"name": "queryWithProgramAndPagination",
"query": "/api/analytics/trackedEntities/query/nEenWmSyUEp.json?program=IpHINAT79UW&lastUpdated=LAST_10_YEARS&pageSize=10&totalPages=true&asc=lastupdated&relativePeriodDate=2022-09-27&headers=trackedentity,lastupdated,createdbydisplayname,lastupdatedbydisplayname,geometry,longitude,latitude,ouname,oucode,ounamehierarchy,w75KJ2mc4zz,zDhUuAYrxNC,cejWyOfXge6,lZGmxYbs97q",
"version": {
"min": 42
}
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit e6da392

Please sign in to comment.