Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Join attribute value table for attributes [DHIS2-18417] #19299

Merged
merged 82 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 63 commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
5fdf098
fix: Update code
larshelge Nov 25, 2024
12f1f27
fix: Update code
larshelge Nov 25, 2024
6647eac
fix: Update code
larshelge Nov 25, 2024
50825c4
fix: Update code
larshelge Nov 25, 2024
719655c
fix: Update code
larshelge Nov 25, 2024
c5863b1
fix: Update code
larshelge Nov 25, 2024
d2095b9
fix: Update code
larshelge Nov 25, 2024
18b9782
fix: Update code
larshelge Nov 25, 2024
d1520e3
fix: Update code
larshelge Nov 25, 2024
6c0b5a6
fix: Update code
larshelge Nov 25, 2024
7d42bb0
fix: Update code
larshelge Nov 25, 2024
1ebed16
Merge branch 'master' into DHIS2-18417
larshelge Nov 25, 2024
cb3d523
fix: Update code
larshelge Nov 26, 2024
9f369e4
fix: Update code
larshelge Nov 26, 2024
acccd5d
fix: Update code
larshelge Nov 26, 2024
97b6697
fix: Update code
larshelge Nov 26, 2024
7c725c7
fix: Update code
larshelge Nov 26, 2024
e58f104
Merge branch 'master' into DHIS2-18417
larshelge Nov 26, 2024
e8e862a
Merge branch 'master' into DHIS2-18417
larshelge Nov 26, 2024
490ea09
fix: Update code
larshelge Nov 26, 2024
781d2e4
fix: Update code
larshelge Nov 26, 2024
c1b20d7
fix: Update code
larshelge Nov 26, 2024
c51fb14
fix: Update code
larshelge Nov 26, 2024
a84ff58
fix: Update code
larshelge Nov 26, 2024
e375211
fix: Update code
larshelge Nov 26, 2024
ccbc1e4
Merge branch 'master' into DHIS2-18417
larshelge Nov 26, 2024
43ff102
fix: Update code
larshelge Nov 26, 2024
2cab8fc
fix: Update code
larshelge Nov 26, 2024
adc1599
fix: Update code
larshelge Nov 26, 2024
d013060
Merge branch 'master' into DHIS2-18417
larshelge Nov 26, 2024
793b9fa
fix: Update code
larshelge Nov 26, 2024
105b514
fix: Update code
larshelge Nov 26, 2024
45d734e
fix: Update code
larshelge Nov 26, 2024
793a55b
fix: Update code
larshelge Nov 26, 2024
0640b81
fix: Update code
larshelge Nov 26, 2024
f33bd88
fix: Update code
larshelge Nov 26, 2024
3a6b574
fix: Update code
larshelge Nov 26, 2024
7ac31de
fix: Update code
larshelge Nov 26, 2024
cd9c2ed
fix: Update code
larshelge Nov 26, 2024
e4c070d
fix: Update code
larshelge Nov 26, 2024
9cf4321
fix: Update code
larshelge Nov 26, 2024
df69357
fix: Update code
larshelge Nov 26, 2024
007fabb
fix: Update code
larshelge Nov 29, 2024
8af5d48
Revert "fix: Update code"
larshelge Nov 29, 2024
62627f0
Merge branch 'master' into DHIS2-18417
larshelge Nov 29, 2024
b3b0391
Merge branch 'master' into DHIS2-18417
larshelge Nov 29, 2024
77f814b
Merge branch 'master' into DHIS2-18417
larshelge Nov 30, 2024
4038b0d
fix: Update code
larshelge Nov 30, 2024
b48b83c
fix: Update code
larshelge Dec 1, 2024
f9ed2a3
fix: Update code
larshelge Dec 1, 2024
9a3afb8
fix: Update code
larshelge Dec 1, 2024
92be248
fix: Update code
larshelge Dec 1, 2024
8186edc
fix: Update code
larshelge Dec 1, 2024
31524ca
fix: Update code
larshelge Dec 1, 2024
04965f3
Merge branch 'master' into DHIS2-18417
larshelge Dec 2, 2024
4742e3b
fix: Update code
larshelge Dec 2, 2024
4259834
fix: Update code
larshelge Dec 2, 2024
25f4feb
fix: Update code
larshelge Dec 2, 2024
df215ec
fix: Update code
larshelge Dec 2, 2024
83b4758
fix: Update code
larshelge Dec 2, 2024
7a481d6
fix: Update code
larshelge Dec 2, 2024
bca8df5
fix: Update code
larshelge Dec 2, 2024
fdc5578
fix: Update code
larshelge Dec 2, 2024
13fbc75
fix: Update code
larshelge Dec 3, 2024
50da70c
Merge branch 'master' into DHIS2-18417
larshelge Dec 3, 2024
6633861
Revert "feat: skipAnalytics and ConfidentialFlag only for QUERY Analy…
larshelge Dec 4, 2024
693ac89
fix: Update code
larshelge Dec 4, 2024
b1e7bb3
Merge branch 'master' into DHIS2-18417
larshelge Dec 4, 2024
7b310eb
fix: Update code
larshelge Dec 4, 2024
be271e6
fix: Update code
larshelge Dec 4, 2024
e178384
fix: Update code
larshelge Dec 4, 2024
d3c1957
fix: Update code
larshelge Dec 4, 2024
55d8a84
fix: Update code
larshelge Dec 4, 2024
86ef201
fix: Update code
larshelge Dec 4, 2024
f4e7684
fix: Update code
larshelge Dec 4, 2024
a032072
fix: Update code
larshelge Dec 4, 2024
87a0fd3
fix: Update code
larshelge Dec 4, 2024
c93f4ff
fix: Update code
larshelge Dec 4, 2024
8d9b40f
fix: Update code
larshelge Dec 4, 2024
00536ff
fix: Update code
larshelge Dec 4, 2024
15cb92a
fix: Update code
larshelge Dec 4, 2024
85c2c92
fix: Update code
larshelge Dec 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@
} else if (valueType.isOrganisationUnit()) {
String ouClause =
isTea
? "ou.uid from ${organisationunit} ou where ou.uid = (select ${columnName}"
: "ou.uid from ${organisationunit} ou where ou.uid = ${columnName}";
return replaceQualify(ouClause, Map.of("columnName", columnExpression));
? "ou.uid from ${organisationunit} ou where ou.uid = (select ${columnExpression}"
: "ou.uid from ${organisationunit} ou where ou.uid = ${columnExpression}";
return replaceQualify(ouClause, Map.of("columnExpression", columnExpression));
} else {
return columnExpression;
}
Expand Down Expand Up @@ -261,7 +261,7 @@
Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet());

if (attribute.getValueType().isOrganisationUnit()) {
columns.addAll(getColumnForOrgUnitTrackedEntityAttribute(attribute, dataFilterClause));
columns.addAll(getColumnForOrgUnitAttribute(attribute, dataFilterClause));
}

columns.add(
Expand All @@ -283,7 +283,7 @@
* @param dataFilterClause the data filter clause.
* @return a list of {@link AnalyticsTableColumn}.
*/
private List<AnalyticsTableColumn> getColumnForOrgUnitTrackedEntityAttribute(
private List<AnalyticsTableColumn> getColumnForOrgUnitAttribute(
TrackedEntityAttribute attribute, String dataFilterClause) {
List<AnalyticsTableColumn> columns = new ArrayList<>();

Expand Down Expand Up @@ -318,6 +318,13 @@
return columns;
}

private String getSelectForInsert(
TrackedEntityAttribute attribute, String selectExpression, String dataFilterClause) {
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
return String.format(
"""
%s.%s as %s, """);
Fixed Show fixed Hide fixed
}

/**
* The select subquery statement.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ protected AnalyticsTable getLatestAnalyticsTable(
protected void invokeTimeAndLog(String sql, String logPattern, Object... args) {
Timer timer = new SystemTimer().start();

log.debug("Populate table SQL: '{}'", sql);
log.info("Populate table SQL: '{}'", sql);

jdbcTemplate.execute(sql);

Expand Down
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 All @@ -65,6 +66,7 @@
import org.hisp.dhis.calendar.Calendar;
import org.hisp.dhis.category.Category;
import org.hisp.dhis.category.CategoryService;
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.commons.collection.ListUtils;
Expand Down Expand Up @@ -330,6 +332,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 = getAttributeValueJoinClause(program);

String fromClause =
replaceQualify(
Expand All @@ -346,6 +349,7 @@ left join analytics_rs_dateperiodstructure dps on cast(${eventDateExpression} as
left join analytics_rs_organisationunitgroupsetstructure ougs on ev.organisationunitid=ougs.organisationunitid \
left join ${organisationunit} enrollmentou on en.organisationunitid=enrollmentou.organisationunitid \
inner join analytics_rs_categorystructure acs on ev.attributeoptioncomboid=acs.categoryoptioncomboid \
${attributeJoinClause}\
where ev.lastupdated < '${startTime}' ${partitionClause} \
and pr.programid=${programId} \
and ev.organisationunitid is not null \
Expand All @@ -358,6 +362,7 @@ and ev.status in (${exportableEventStatues}) \
Map.of(
"eventDateExpression", eventDateExpression,
"partitionClause", partitionClause,
"attributeJoinClause", attributeJoinClause,
"startTime", toLongDate(params.getStartTime()),
"programId", String.valueOf(program.getId()),
"firstDataYear", String.valueOf(firstDataYear),
Expand All @@ -367,6 +372,36 @@ and ev.status in (${exportableEventStatues}) \
populateTableInternal(partition, fromClause);
}

/**
* Returns a join clause for attribute value for every attribute of the given program.
*
* @param program the {@link Program}.
* @return a join clause.
*/
private String getAttributeValueJoinClause(Program program) {
String template =
"""
left join ${trackedentityattributevalue} as ${uid} \
on en.trackedentityid=${uid}.trackedentityid \
and ${uid}.trackedentityattributeid = ${id}\s""";

return program.getNonConfidentialTrackedEntityAttributes().stream()
.map(attribute -> replaceQualify(template, toVariableMap(attribute)))
.collect(Collectors.joining());
}

/**
* Returns a variable map.
*
* @param object the object.
* @return a {@link Map}.
*/
private Map<String, String> toVariableMap(IdentifiableObject object) {
return Map.of(
"id", String.valueOf(object.getId()),
"uid", quote(object.getUid()));
}

/**
* Returns a partition SQL clause.
*
Expand Down Expand Up @@ -624,7 +659,7 @@ private List<AnalyticsTableColumn> getColumnForAttributeWithLegendSet(
}

/**
* Retyrns a select statement for the given select expression.
* Returns a select statement for the given select expression.
*
* @param dataElement the data element to create the select statement for.
* @param selectExpression the select expression.
Expand All @@ -633,6 +668,7 @@ private List<AnalyticsTableColumn> getColumnForAttributeWithLegendSet(
*/
private String getSelectForInsert(
DataElement dataElement, String selectExpression, String dataFilterClause) {
String parentheses = getClosingParentheses(selectExpression);
String sqlTemplate =
dataElement.getValueType().isOrganisationUnit()
? "(select ${selectExpression} ${dataClause})${closingParentheses} as ${uid}"
Expand All @@ -641,14 +677,10 @@ private String getSelectForInsert(
return replaceQualify(
sqlTemplate,
Map.of(
"selectExpression",
selectExpression,
"dataClause",
dataFilterClause,
"closingParentheses",
getClosingParentheses(selectExpression),
"uid",
quote(dataElement.getUid())));
"selectExpression", selectExpression,
"dataClause", dataFilterClause,
"closingParentheses", parentheses,
"uid", quote(dataElement.getUid())));
}

/**
Expand Down Expand Up @@ -726,15 +758,14 @@ private List<Integer> getDataYears(
Program program,
Integer firstDataYear,
Integer lastDataYear) {
String fromDate = toMediumDate(params.getFromDate());
String fromDateClause =
params.getFromDate() != null
? replace(
"and (${eventDateExpression}) >= '${fromDate}'",
Map.of(
"eventDateExpression",
eventDateExpression,
"fromDate",
toMediumDate(params.getFromDate())))
"eventDateExpression", eventDateExpression,
"fromDate", fromDate))
: EMPTY;

String sql =
Expand Down
Loading