Skip to content

Commit

Permalink
refactor: rename analytics fields to match tracker ones [DHIS2-17908] (
Browse files Browse the repository at this point in the history
…#18431)

* refactor: adds symbols for easier refactoring [DHIS2-17908]

Signed-off-by: Giuseppe Nespolino <[email protected]>

* refactor: date fields renamed [DHIS2-17908]

Signed-off-by: Giuseppe Nespolino <[email protected]>

* style: private no-args constructor [DHIS2-17908]

Signed-off-by: Giuseppe Nespolino <[email protected]>

* fix: adds new fields to requests [DHIS2-17908]

Signed-off-by: Giuseppe Nespolino <[email protected]>

* fix: fixes deprecated field usage [DHIS2-17908]

Signed-off-by: Giuseppe Nespolino <[email protected]>

* fix: peer review [DHIS2-17908]

Signed-off-by: Giuseppe Nespolino <[email protected]>

---------

Signed-off-by: Giuseppe Nespolino <[email protected]>
  • Loading branch information
gnespolino authored Sep 2, 2024
1 parent 6623332 commit c18b704
Show file tree
Hide file tree
Showing 28 changed files with 948 additions and 488 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,32 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.analytics.table.EnrollmentAnalyticsColumnName;
import org.hisp.dhis.analytics.table.EventAnalyticsColumnName;

/** Enum that maps database column names to their respective "business" names. */
@RequiredArgsConstructor
public enum TimeField {
EVENT_DATE("occurreddate"),
ENROLLMENT_DATE("enrollmentdate"),
INCIDENT_DATE("incidentdate", "occurreddate"),
EVENT_DATE(EventAnalyticsColumnName.OCCURRED_DATE_COLUMN_NAME),
ENROLLMENT_DATE(
EnrollmentAnalyticsColumnName.ENROLLMENT_DATE_COLUMN_NAME,
EventAnalyticsColumnName.ENROLLMENT_DATE_COLUMN_NAME,
"enrollmentdate"),
INCIDENT_DATE(
EnrollmentAnalyticsColumnName.OCCURRED_DATE_COLUMN_NAME,
EventAnalyticsColumnName.ENROLLMENT_OCCURRED_DATE_COLUMN_NAME,
"occurreddate"),
OCCURRED_DATE("occurreddate"),
SCHEDULED_DATE("scheduleddate"),
COMPLETED_DATE("completeddate"),
CREATED("created"),
LAST_UPDATED("lastupdated");
SCHEDULED_DATE(EventAnalyticsColumnName.SCHEDULED_DATE_COLUMN_NAME),
COMPLETED_DATE(EventAnalyticsColumnName.COMPLETED_DATE_COLUMN_NAME),
CREATED(EventAnalyticsColumnName.CREATED_COLUMN_NAME),
LAST_UPDATED(
EnrollmentAnalyticsColumnName.LAST_UPDATED_COLUMN_NAME,
EventAnalyticsColumnName.LAST_UPDATED_COLUMN_NAME,
"lastupdated");

@Getter private final String eventAndEnrollmentColumnName;
@Getter private final String enrollmentColumnName;
@Getter private final String eventColumnName;
@Getter private final String trackedEntityColumnName;

public static final Collection<String> DEFAULT_TIME_FIELDS =
Expand All @@ -62,15 +74,16 @@ public enum TimeField {
* These constants represent the columns that can be compared using the raw period column (in the
* analytics tables), instead of dates. This is preferable for performance reasons.
*/
private static final Collection<TimeField> TIME_FIELDS_SUPPORT_RAW_PERIODS =
private static final Collection<TimeField> SUPPORTING_RAW_FIELD_TIME_FIELDS =
List.of(EVENT_DATE, SCHEDULED_DATE, ENROLLMENT_DATE);

private static final Set<String> FIELD_NAMES =
newHashSet(TimeField.values()).stream().map(TimeField::name).collect(toSet());

TimeField(final String field) {
this.trackedEntityColumnName = field;
this.eventAndEnrollmentColumnName = field;
this.eventColumnName = field;
this.enrollmentColumnName = field;
}

public static Optional<TimeField> of(final String timeField) {
Expand All @@ -79,7 +92,7 @@ public static Optional<TimeField> of(final String timeField) {

private static Optional<TimeField> from(final String field) {
return Arrays.stream(values())
.filter(tf -> tf.getEventAndEnrollmentColumnName().equals(field))
.filter(timeField -> timeField.getEventColumnName().equals(field))
.findFirst();
}

Expand All @@ -88,14 +101,10 @@ public static boolean fieldIsValid(final String field) {
}

public boolean supportsRawPeriod() {
return Optional.of(eventAndEnrollmentColumnName)
return Optional.of(eventColumnName)
.filter(StringUtils::isNotBlank)
.flatMap(TimeField::from)
.map(this::supportsRawPeriods)
.map(SUPPORTING_RAW_FIELD_TIME_FIELDS::contains)
.orElse(false);
}

private boolean supportsRawPeriods(TimeField timeField) {
return TIME_FIELDS_SUPPORT_RAW_PERIODS.contains(timeField);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,14 @@ public class CommonRequestParams {
/** Indicates which property to display. */
private DisplayProperty displayProperty;

/** Custom date filters. */
private Set<String> occurredDate = new LinkedHashSet<>();

// Time fields

/**
* @deprecated use {@link #occurredDate} instead. Kept for backward compatibility.
*/
@Deprecated(since = "2.42")
private Set<String> eventDate = new LinkedHashSet<>();

private Set<String> enrollmentDate = new LinkedHashSet<>();
Expand All @@ -221,8 +228,6 @@ public class CommonRequestParams {
@Deprecated(since = "2.42")
private Set<String> incidentDate = new LinkedHashSet<>();

private Set<String> occurredDate = new LinkedHashSet<>();

private Set<String> lastUpdated = new LinkedHashSet<>();

private Set<String> created = new LinkedHashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.analytics.table;

import lombok.NoArgsConstructor;

@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
public final class EnrollmentAnalyticsColumnName {

public static final String ENROLLMENT_COLUMN_NAME = "enrollment";
public static final String TRACKED_ENTITY_COLUMN_NAME = "trackedentity";
public static final String ENROLLMENT_DATE_COLUMN_NAME = "enrollmentdate";
public static final String OCCURRED_DATE_COLUMN_NAME = "occurreddate";
public static final String COMPLETED_DATE_COLUMN_NAME = "completeddate";
public static final String LAST_UPDATED_COLUMN_NAME = "lastupdated";
public static final String STORED_BY_COLUMN_NAME = "storedby";
public static final String CREATED_BY_USERNAME_COLUMN_NAME = "createdbyusername";
public static final String CREATED_BY_NAME_COLUMN_NAME = "createdbyname";
public static final String CREATED_BY_LASTNAME_COLUMN_NAME = "createdbylastname";
public static final String CREATED_BY_DISPLAY_NAME_COLUMN_NAME = "createdbydisplayname";
public static final String LAST_UPDATED_BY_USERNAME_COLUMN_NAME = "lastupdatedbyusername";
public static final String LAST_UPDATED_BY_NAME_COLUMN_NAME = "lastupdatedbyname";
public static final String LAST_UPDATED_BY_LASTNAME_COLUMN_NAME = "lastupdatedbylastname";
public static final String LAST_UPDATED_BY_DISPLAY_NAME_COLUMN_NAME = "lastupdatedbydisplayname";
public static final String ENROLLMENT_STATUS_COLUMN_NAME = "enrollmentstatus";
public static final String LONGITUDE_COLUMN_NAME = "longitude";
public static final String LATITUDE_COLUMN_NAME = "latitude";
public static final String OU_COLUMN_NAME = "ou";
public static final String OU_NAME_COLUMN_NAME = "ouname";
public static final String OU_CODE_COLUMN_NAME = "oucode";
public static final String OU_LEVEL_COLUMN_NAME = "oulevel";
public static final String ENROLLMENT_GEOMETRY_COLUMN_NAME = "enrollmentgeometry";
public static final String REGISTRATION_OU_COLUMN_NAME = "registrationou";
public static final String TRACKED_ENTITY_GEOMETRY_COLUMN_NAME = "tegeometry";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.analytics.table;

import lombok.NoArgsConstructor;

@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
public final class EventAnalyticsColumnName {

public static final String EVENT_COLUMN_NAME = "event";
public static final String ENROLLMENT_COLUMN_NAME = "enrollment";
public static final String TRACKED_ENTITY_COLUMN_NAME = "trackedentity";
public static final String PS_COLUMN_NAME = "ps";
public static final String AO_COLUMN_NAME = "ao";
public static final String ENROLLMENT_DATE_COLUMN_NAME = "enrollmentdate";
public static final String ENROLLMENT_OCCURRED_DATE_COLUMN_NAME = "enrollmentoccurreddate";
public static final String OCCURRED_DATE_COLUMN_NAME = "occurreddate";
public static final String SCHEDULED_DATE_COLUMN_NAME = "scheduleddate";
public static final String COMPLETED_DATE_COLUMN_NAME = "completeddate";
public static final String CREATED_COLUMN_NAME = "created";
public static final String LAST_UPDATED_COLUMN_NAME = "lastupdated";
public static final String STORED_BY_COLUMN_NAME = "storedby";
public static final String CREATED_BY_USERNAME_COLUMN_NAME = "createdbyusername";
public static final String CREATED_BY_NAME_COLUMN_NAME = "createdbyname";
public static final String CREATED_BY_LASTNAME_COLUMN_NAME = "createdbylastname";
public static final String CREATED_BY_DISPLAYNAME_COLUMN_NAME = "createdbydisplayname";
public static final String LAST_UPDATED_BY_USERNAME_COLUMN_NAME = "lastupdatedbyusername";
public static final String LAST_UPDATED_BY_NAME_COLUMN_NAME = "lastupdatedbyname";
public static final String LAST_UPDATED_BY_LASTNAME_COLUMN_NAME = "lastupdatedbylastname";
public static final String LAST_UPDATED_BY_DISPLAYNAME_COLUMN_NAME = "lastupdatedbydisplayname";
public static final String EVENT_STATUS_COLUMN_NAME = "eventstatus";
public static final String ENROLLMENT_STATUS_COLUMN_NAME = "enrollmentstatus";
public static final String EVENT_GEOMETRY_COLUMN_NAME = "eventgeometry";
public static final String LONGITUDE_COLUMN_NAME = "longitude";
public static final String LATITUDE_COLUMN_NAME = "latitude";
public static final String OU_COLUMN_NAME = "ou";
public static final String OU_NAME_COLUMN_NAME = "ouname";
public static final String OU_CODE_COLUMN_NAME = "oucode";
public static final String OU_LEVEL_COLUMN_NAME = "oulevel";
public static final String OU_GEOMETRY_COLUMN_NAME = "ougeometry";
public static final String ENROLLMENT_GEOMETRY_COLUMN_NAME = "enrollmentgeometry";
public static final String REGISTRATION_OU_COLUMN_NAME = "registrationou";
public static final String ENROLLMENT_OU_COLUMN_NAME = "enrollmentou";
public static final String TRACKED_ENTITY_GEOMETRY_COLUMN_NAME = "tegeometry";
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
@Getter
@RequiredArgsConstructor
public enum AnalyticsDateFilter {
/**
* @deprecated use {@link #OCCURRED_DATE} instead. Kept for backward compatibility.
*/
@Deprecated(since = "2.42")
EVENT_DATE(
TimeField.EVENT_DATE,
EventsAnalyticsQueryCriteria::getEventDate,
Expand All @@ -64,8 +68,11 @@ public enum AnalyticsDateFilter {
@Deprecated(since = "2.42")
INCIDENT_DATE(
TimeField.INCIDENT_DATE,
// Events
EventsAnalyticsQueryCriteria::getIncidentDate,
// Enrollments
EnrollmentAnalyticsQueryCriteria::getIncidentDate,
// TEs
CommonRequestParams::getIncidentDate),
OCCURRED_DATE(
TimeField.OCCURRED_DATE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@ public class EventsAnalyticsQueryCriteria extends AnalyticsPagingCriteria {
/** End date for events. This is a mandatory field. */
private Date endDate;

/** Time interval for event date; */
/**
* Time interval for event date;
*
* @deprecated use @see {@link #occurredDate} instead
*/
@Deprecated(since = "2.42")
private String eventDate;

/** Time interval for event date; */
private String occurredDate;

/** Time interval for enrollment date; */
private String enrollmentDate;

Expand All @@ -72,12 +80,13 @@ public class EventsAnalyticsQueryCriteria extends AnalyticsPagingCriteria {
/**
* Time interval for incident date;
*
* @deprecated use @see {@link #occurredDate} instead
* @deprecated use @see {@link #enrollmentOccurredDate} instead
*/
@Deprecated(since = "2.42")
private String incidentDate;

private String occurredDate;
/** Time interval for incident date; */
private String enrollmentOccurredDate;

/** Time interval for last updated date; */
private String lastUpdated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public static <T extends IdentifiableObject> boolean containsUid(
* @return the merged map
*/
@SafeVarargs
public static <T> Map<T, T> merge(Map<T, T>... maps) {
public static <T> Map<T, T> mergeMaps(Map<T, T>... maps) {
Map<T, T> result = new HashMap<>();
Stream.of(maps).forEach(result::putAll);
return ImmutableMap.copyOf(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.Date;
import java.util.Objects;
import java.util.regex.Pattern;
import org.hisp.dhis.analytics.table.EnrollmentAnalyticsColumnName;
import org.hisp.dhis.analytics.table.EventAnalyticsColumnName;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.DxfNamespaces;
import org.hisp.dhis.common.EmbeddedObject;
Expand Down Expand Up @@ -92,13 +94,16 @@ 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 = "occurreddate";
public static final String DB_EVENT_DATE = EventAnalyticsColumnName.OCCURRED_DATE_COLUMN_NAME;

public static final String DB_ENROLLMENT_DATE = "enrollmentdate";
public static final String DB_ENROLLMENT_DATE =
EnrollmentAnalyticsColumnName.ENROLLMENT_DATE_COLUMN_NAME;

public static final String DB_INCIDENT_DATE = "incidentdate";
public static final String DB_INCIDENT_DATE =
EnrollmentAnalyticsColumnName.OCCURRED_DATE_COLUMN_NAME;

public static final String DB_SCHEDULED_DATE = "scheduleddate";
public static final String DB_SCHEDULED_DATE =
EventAnalyticsColumnName.SCHEDULED_DATE_COLUMN_NAME;

public static final String DB_QUOTE = "\"";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ public static boolean hasPeriod(EventsAnalyticsQueryCriteria criteria) {
return (criteria.getDimension().stream().anyMatch(d -> d.startsWith(PERIOD_DIM_ID)))
|| (criteria.getFilter().stream().anyMatch(d -> d.startsWith(PERIOD_DIM_ID)))
|| !isBlank(criteria.getEventDate())
|| !isBlank(criteria.getOccurredDate())
|| !isBlank(criteria.getEnrollmentDate())
|| (criteria.getStartDate() != null && criteria.getEndDate() != null)
|| !isBlank(criteria.getIncidentDate())
|| !isBlank(criteria.getOccurredDate())
|| !isBlank(criteria.getEnrollmentOccurredDate())
|| !isBlank(criteria.getLastUpdated())
|| !isBlank(criteria.getScheduledDate())
|| criteria.getRelativePeriodDate() != null;
Expand Down
Loading

0 comments on commit c18b704

Please sign in to comment.