Skip to content

Commit

Permalink
Merge branch 'master' into batchhandler-autoclose
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-p-pickering authored Nov 14, 2023
2 parents 850109e + bc2befc commit cd2d4bc
Show file tree
Hide file tree
Showing 64 changed files with 954 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

/** Enum that maps database column names to their respective "business" names. */
public enum TimeField {
EVENT_DATE("executiondate"),
EVENT_DATE("occurreddate"),
ENROLLMENT_DATE("enrollmentdate"),
INCIDENT_DATE("incidentdate"),
// Not a typo, different naming convention between FE and database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class AnalyticsPeriodBoundary extends BaseIdentifiableObject implements E
public static final Pattern COHORT_HAVING_ATTRIBUTE_PATTERN =
Pattern.compile(COHORT_HAVING_ATTRIBUTE_REGEX);

public static final String DB_EVENT_DATE = "executiondate";
public static final String DB_EVENT_DATE = "occurreddate";

public static final String DB_ENROLLMENT_DATE = "enrollmentdate";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import static com.google.common.base.Preconditions.checkNotNull;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hisp.dhis.cache.Cache;
import org.hisp.dhis.cache.CacheProvider;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.session.SessionInformation;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -51,11 +54,15 @@ public class CurrentUserService {

private final Cache<CurrentUserGroupInfo> currentUserGroupInfoCache;

public CurrentUserService(@Lazy UserStore userStore, CacheProvider cacheProvider) {
private final SessionRegistry sessionRegistry;

public CurrentUserService(
@Lazy UserStore userStore, CacheProvider cacheProvider, SessionRegistry sessionRegistry) {
checkNotNull(userStore);

this.userStore = userStore;
this.currentUserGroupInfoCache = cacheProvider.createCurrentUserGroupInfoCache();
this.sessionRegistry = sessionRegistry;
}

/**
Expand Down Expand Up @@ -113,4 +120,20 @@ public void invalidateUserGroupCache(String userUID) {
// Ignore if key doesn't exist
}
}

public CurrentUserDetailsImpl getCurrentUserPrincipal(String uid) {
return sessionRegistry.getAllPrincipals().stream()
.map(CurrentUserDetailsImpl.class::cast)
.filter(principal -> principal.getUid().equals(uid))
.findFirst()
.orElse(null);
}

public void invalidateUserSessions(String uid) {
CurrentUserDetailsImpl principal = getCurrentUserPrincipal(uid);
if (principal != null) {
List<SessionInformation> allSessions = sessionRegistry.getAllSessions(principal, false);
allSessions.forEach(SessionInformation::expireNow);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* An example of a generated sub-query is:
*
* <pre>
* avg((date_part('year', age(cast(executiondate as date), cast("iESIqZ0R0R0" as date)))))
* avg((date_part('year', age(cast(occurreddate as date), cast("iESIqZ0R0R0" as date)))))
* FROM analytics_event_uy2gu8kt1jf as subax"
* </pre>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public class DefaultEventDataQueryService implements EventDataQueryService {

private static final String COL_NAME_EVENT_STATUS = "psistatus";

private static final String COL_NAME_EVENTDATE = "executiondate";
private static final String COL_NAME_EVENTDATE = "occurreddate";

private static final String COL_NAME_ENROLLMENTDATE = "enrollmentdate";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ private String getProgramIndicatorEventInProgramStageSql(
+ eventTableName
+ ".pi = "
+ ANALYTICS_TBL_ALIAS
+ ".pi and executiondate is not null ";
+ ".pi and occurreddate is not null ";

for (AnalyticsPeriodBoundary boundary : boundaries) {
sql +=
" and executiondate "
" and occurreddate "
+ (boundary.getAnalyticsPeriodBoundaryType().isStartBoundary() ? ">=" : "<")
+ " cast( '"
+ format.format(boundary.getBoundaryDate(reportingStartDate, reportingEndDate))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ 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 ORDER_BY_EXECUTION_DATE = "order by occurreddate ";

private static final String LIMIT_1 = "limit 1";

Expand Down Expand Up @@ -553,7 +553,7 @@ protected String getColumn(QueryItem item, String suffix) {
&& !item.getRepeatableStageParams().simpleStageValueExpected()) {
return "(select json_agg(t1) from (select "
+ colName
+ ", incidentdate, scheduleddate, executiondate "
+ ", incidentdate, scheduleddate, occurreddate "
+ " from "
+ eventTableName
+ " where "
Expand Down Expand Up @@ -640,14 +640,14 @@ private static String getExecutionDateFilter(Date startDate, Date endDate) {
StringBuilder sb = new StringBuilder();

if (startDate != null) {
sb.append(" and executiondate >= ");
sb.append(" and occurreddate >= ");

sb.append(
String.format("%s ", SqlUtils.singleQuote(DateUtils.getMediumDateString(startDate))));
}

if (endDate != null) {
sb.append(" and executiondate <= ");
sb.append(" and occurreddate <= ");

sb.append(String.format("%s ", SqlUtils.singleQuote(DateUtils.getMediumDateString(endDate))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ protected String getSelectClause(EventQueryParams params) {
.add(
"psi",
"ps",
"executiondate",
"occurreddate",
"storedby",
"createdbydisplayname",
"lastupdatedbydisplayname",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public JdbcEventAnalyticsTableManager(
new AnalyticsTableColumn(quote("ao"), CHARACTER_11, NOT_NULL, "ao.uid"),
new AnalyticsTableColumn(quote("enrollmentdate"), TIMESTAMP, "pi.enrollmentdate"),
new AnalyticsTableColumn(quote("incidentdate"), TIMESTAMP, "pi.incidentdate"),
new AnalyticsTableColumn(quote("executiondate"), TIMESTAMP, "psi.executiondate"),
new AnalyticsTableColumn(quote("occurreddate"), TIMESTAMP, "psi.occurreddate"),
new AnalyticsTableColumn(quote("scheduleddate"), TIMESTAMP, "psi.scheduleddate"),
new AnalyticsTableColumn(quote("completeddate"), TIMESTAMP, "psi.completeddate"),

Expand Down Expand Up @@ -262,7 +262,7 @@ public List<AnalyticsTable> getAnalyticsTables(AnalyticsTableUpdateParams params
* status
*/
static String getDateLinkedToStatus() {
return "CASE WHEN 'SCHEDULE' = psi.status THEN psi.scheduleddate ELSE psi.executiondate END";
return "CASE WHEN 'SCHEDULE' = psi.status THEN psi.scheduleddate ELSE psi.occurreddate END";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public JdbcTeiEventsAnalyticsTableManager(
new AnalyticsTableColumn(quote("programinstanceuid"), CHARACTER_11, NULL, "pi.uid"),
new AnalyticsTableColumn(quote("programstageuid"), CHARACTER_11, NULL, "ps.uid"),
new AnalyticsTableColumn(quote("programstageinstanceuid"), CHARACTER_11, NULL, "psi.uid"),
new AnalyticsTableColumn(quote("executiondate"), TIMESTAMP, "psi.executiondate"),
new AnalyticsTableColumn(quote("occurreddate"), TIMESTAMP, "psi.occurreddate"),
new AnalyticsTableColumn(quote("lastupdated"), TIMESTAMP, "psi.lastupdated"),
new AnalyticsTableColumn(quote("created"), TIMESTAMP, "psi.created"),
new AnalyticsTableColumn(quote("scheduleddate"), TIMESTAMP, "psi.scheduleddate"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static String eventSelect(
SqlParameterManager sqlParameterManager) {
return "select innermost_evt.*"
+ " from (select *,"
+ " row_number() over (partition by programinstanceuid order by executiondate desc) as rn"
+ " row_number() over (partition by programinstanceuid order by occurreddate desc) as rn"
+ " from "
+ ANALYTICS_TEI_EVT
+ trackedEntityType.getUid().toLowerCase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ static String eventProgramIndicatorSelect(
+ ", "
+ expression
+ " as value, "
+ " row_number() over (partition by pi order by executiondate desc) as rn "
+ " row_number() over (partition by pi order by occurreddate desc) as rn "
+ " from analytics_event_"
+ program.getElement().getUid()
+ " as "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private void verifyWithProgramStageAndDataElement(ValueType valueType) {
+ programA.getUid()
+ ".pi = ax.pi and \"fWIAEtYVEGk\" is not null and ps = '"
+ programStage.getUid()
+ "' order by executiondate desc limit 1 )";
+ "' order by occurreddate desc limit 1 )";

if (valueType == ValueType.NUMBER) {
subSelect = subSelect + " as \"fWIAEtYVEGk\"";
Expand Down Expand Up @@ -257,7 +257,7 @@ private void verifyWithRepeatableProgramStageAndDataElement(ValueType valueType)
+ programUid
+ ".pi = ax.pi and ps = '"
+ repeatableProgramStage.getUid()
+ "' order by executiondate desc offset 1 limit 1 ) "
+ "' order by occurreddate desc offset 1 limit 1 ) "
+ "as \""
+ programStageUid
+ "[-1]."
Expand All @@ -272,7 +272,7 @@ private void verifyWithRepeatableProgramStageAndDataElement(ValueType valueType)
+ programUid
+ ".pi = ax.pi and ps = '"
+ programStageUid
+ "' order by executiondate desc offset 1 limit 1 )) "
+ "' order by occurreddate desc offset 1 limit 1 )) "
+ "as \""
+ programStageUid
+ "[-1]."
Expand Down Expand Up @@ -303,7 +303,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 occurreddate desc limit 1 )";

String expected =
"ax.\"quarterly\",ax.\"ou\","
Expand Down Expand Up @@ -377,7 +377,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 occurreddate desc limit 1 )";

String expected =
"ax.\"quarterly\",ax.\"ou\","
Expand All @@ -404,7 +404,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 occurreddate desc limit 1 )";

String expected = subSelect + " is null";

Expand All @@ -423,7 +423,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 occurreddate desc limit 1 )";

String expected = subSelect + " is not null";
testIt(
Expand All @@ -441,7 +441,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 occurreddate desc limit 1 )";

String numericValues = String.join(OPTION_SEP, "10", "11", "12");
String expected =
Expand All @@ -467,7 +467,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 occurreddate desc limit 1 )";

String numericValues = String.join(OPTION_SEP, "10", "11", "12");
String expected = subSelect + " in (" + String.join(",", numericValues.split(OPTION_SEP)) + ")";
Expand All @@ -486,7 +486,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 occurreddate desc limit 1 )";

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

@Test
Expand All @@ -744,13 +744,13 @@ void verifyGetColumnOfTypeCoordinateAndWithProgramStagesAndParamsWithReferenceTy
is(
"(select json_agg(t1) from (select \""
+ dataElementA.getUid()
+ "\", incidentdate, scheduleddate, executiondate from analytics_event_"
+ "\", incidentdate, scheduleddate, occurreddate from analytics_event_"
+ programB.getUid()
+ " where analytics_event_"
+ 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 occurreddate >= '2022-01-01' and occurreddate <= '2022-01-31' order by occurreddate desc LIMIT 100 ) as t1)"));
}

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class EventAnalyticsManagerTest extends EventAnalyticsTest {
private static final String TABLE_NAME = "analytics_event";

private static final String DEFAULT_COLUMNS_WITH_REGISTRATION =
"psi,ps,executiondate,storedby,"
"psi,ps,occurreddate,storedby,"
+ "createdbydisplayname"
+ ","
+ "lastupdatedbydisplayname"
Expand Down Expand Up @@ -153,7 +153,7 @@ void verifyGetEventSqlWithProgramWithNoRegistration() {
verify(jdbcTemplate).queryForRowSet(sql.capture());

String expected =
"select psi,ps,executiondate,storedby,"
"select psi,ps,occurreddate,storedby,"
+ "createdbydisplayname"
+ ","
+ "lastupdatedbydisplayname"
Expand Down Expand Up @@ -214,7 +214,7 @@ void verifyGetEventSqlWithOrgUnitTypeDataElement() {
verify(jdbcTemplate).queryForRowSet(sql.capture());

String expected =
"select psi,ps,executiondate,storedby,"
"select psi,ps,occurreddate,storedby,"
+ "createdbydisplayname"
+ ","
+ "lastupdatedbydisplayname"
Expand Down Expand Up @@ -563,9 +563,9 @@ void verifyLastLastOrgUnitAggregationTypeSubquery() {
"from (select \"psi\",ax.\"deabcdefghX\",\"ou\","
+ "\"monthly\",\"jkYhtGth12t\","
+ "row_number() over (partition by ax.\"ou\",ax.\"jkYhtGth12t\" "
+ "order by ax.\"executiondate\" desc, ax.\"created\" desc) as pe_rank "
+ "order by ax.\"occurreddate\" desc, ax.\"created\" desc) as pe_rank "
+ "from analytics_event_prabcdefghA as ax "
+ "where ax.\"executiondate\" >= '2012-01-31' and ax.\"executiondate\" <= '2022-01-31' "
+ "where ax.\"occurreddate\" >= '2012-01-31' and ax.\"occurreddate\" <= '2022-01-31' "
+ "and ax.\"deabcdefghX\" is not null)";

assertThat(params.isAggregationType(AggregationType.LAST), is(true));
Expand Down Expand Up @@ -648,15 +648,15 @@ private void verifyFirstOrLastAggregationTypeSubquery(
"from (select \"psi\",ax.\""
+ deU.getUid()
+ "\",\"quarterly\",\"ou\","
+ "row_number() over (partition by ax.\"ou\",ax.\"ao\" order by ax.\"executiondate\" "
+ "row_number() over (partition by ax.\"ou\",ax.\"ao\" order by ax.\"occurreddate\" "
+ order
+ ", ax.\"created\" "
+ order
+ ") as pe_rank "
+ "from "
+ getTable(programA.getUid())
+ " as ax where ax.\"executiondate\" >= '1990-03-31' "
+ "and ax.\"executiondate\" <= '2000-03-31' and ax.\""
+ " as ax where ax.\"occurreddate\" >= '1990-03-31' "
+ "and ax.\"occurreddate\" <= '2000-03-31' and ax.\""
+ deU.getUid()
+ "\" is not null)";

Expand Down
Loading

0 comments on commit cd2d4bc

Please sign in to comment.