Skip to content

Commit

Permalink
refactor: Centralize data year logic [DHIS2-16705] (#19281)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Nov 25, 2024
1 parent c0b0f23 commit 7b15092
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import static org.hisp.dhis.db.model.DataType.GEOMETRY;
import static org.hisp.dhis.db.model.DataType.INTEGER;
import static org.hisp.dhis.db.model.DataType.TEXT;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.DATABASE;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.SYSTEM_DEFINED;
import static org.hisp.dhis.util.DateUtils.toLongDate;
import static org.hisp.dhis.util.DateUtils.toMediumDate;

Expand Down Expand Up @@ -150,8 +148,7 @@ public List<AnalyticsTable> getAnalyticsTables(AnalyticsTableUpdateParams params
isSpatialSupport());

List<Integer> availableDataYears =
periodDataProvider.getAvailableYears(
analyticsTableSettings.getMaxPeriodYearsOffset() == null ? SYSTEM_DEFINED : DATABASE);
periodDataProvider.getAvailableYears(analyticsTableSettings.getPeriodSource());

return params.isLatestUpdate()
? getLatestAnalyticsTables(params)
Expand Down Expand Up @@ -330,8 +327,7 @@ protected List<String> getPartitionChecks(Integer year, Date endDate) {
@Override
public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTablePartition partition) {
List<Integer> availableDataYears =
periodDataProvider.getAvailableYears(
analyticsTableSettings.getMaxPeriodYearsOffset() == null ? SYSTEM_DEFINED : DATABASE);
periodDataProvider.getAvailableYears(analyticsTableSettings.getPeriodSource());
Integer firstDataYear = availableDataYears.get(0);
Integer latestDataYear = availableDataYears.get(availableDataYears.size() - 1);
Program program = partition.getMasterTable().getProgram();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
import static org.hisp.dhis.db.model.DataType.VARCHAR_50;
import static org.hisp.dhis.db.model.constraint.Nullable.NOT_NULL;
import static org.hisp.dhis.db.model.constraint.Nullable.NULL;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.DATABASE;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.SYSTEM_DEFINED;
import static org.hisp.dhis.period.PeriodDataProvider.PeriodSource.DATABASE;
import static org.hisp.dhis.period.PeriodDataProvider.PeriodSource.SYSTEM_DEFINED;
import static org.hisp.dhis.util.DateUtils.toLongDate;
import static org.hisp.dhis.util.DateUtils.toMediumDate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_TABLE_SKIP_COLUMN;
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_TABLE_SKIP_INDEX;
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_TABLE_UNLOGGED;
import static org.hisp.dhis.period.PeriodDataProvider.PeriodSource.DATABASE;
import static org.hisp.dhis.period.PeriodDataProvider.PeriodSource.SYSTEM_DEFINED;
import static org.hisp.dhis.util.ObjectUtils.isNull;

import com.google.common.collect.Lists;
Expand All @@ -50,6 +52,7 @@
import org.hisp.dhis.db.model.Database;
import org.hisp.dhis.db.model.Logged;
import org.hisp.dhis.external.conf.DhisConfigurationProvider;
import org.hisp.dhis.period.PeriodDataProvider.PeriodSource;
import org.hisp.dhis.setting.SystemSettings;
import org.hisp.dhis.setting.SystemSettingsProvider;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -92,6 +95,15 @@ public Integer getMaxPeriodYearsOffset() {
return yearsOffset < 0 ? null : yearsOffset;
}

/**
* Returns the {@link PeriodSource} based on the max years offset.
*
* @return the {@link PeriodSource}.
*/
public PeriodSource getPeriodSource() {
return getMaxPeriodYearsOffset() == null ? SYSTEM_DEFINED : DATABASE;
}

/**
* Indicates whether an analytics database instance is configured.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public final class AnalyticsPeriodCriteriaUtils {
*
* @param criteria {@link EventsAnalyticsQueryCriteria} query criteria.
* @param periodDataProvider {@link EventsAnalyticsQueryCriteria} period data provider.
* @param dataSource {@link PeriodDataProvider.DataSource} source of data.
* @param dataSource {@link PeriodDataProvider.PeriodSource} source of data.
*/
public static void defineDefaultPeriodForCriteria(
EnrollmentAnalyticsQueryCriteria criteria,
PeriodDataProvider periodDataProvider,
PeriodDataProvider.DataSource dataSource) {
PeriodDataProvider.PeriodSource dataSource) {
List<Integer> availableYears = periodDataProvider.getAvailableYears(dataSource);

if (PeriodCriteriaUtils.hasPeriod(criteria)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

import static java.time.temporal.ChronoUnit.YEARS;
import static java.util.Comparator.reverseOrder;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.DATABASE;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.SYSTEM_DEFINED;
import static org.hisp.dhis.scheduling.JobProgress.FailurePolicy.SKIP_ITEM;

import com.google.common.collect.Lists;
Expand Down Expand Up @@ -185,8 +183,7 @@ private final List<ResourceTable> getApprovalResourceTables() {
*/
List<Integer> getAndValidateAvailableDataYears() {
List<Integer> availableYears =
periodDataProvider.getAvailableYears(
analyticsTableSettings.getMaxPeriodYearsOffset() == null ? SYSTEM_DEFINED : DATABASE);
periodDataProvider.getAvailableYears(analyticsTableSettings.getPeriodSource());
validateYearsOffset(availableYears);
return availableYears;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import static org.hisp.dhis.db.model.DataType.TEXT;
import static org.hisp.dhis.db.model.DataType.TIMESTAMP;
import static org.hisp.dhis.db.model.Table.STAGING_TABLE_SUFFIX;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.DATABASE;
import static org.hisp.dhis.period.PeriodDataProvider.PeriodSource.DATABASE;
import static org.hisp.dhis.test.TestBase.createDataElement;
import static org.hisp.dhis.test.TestBase.createProgram;
import static org.hisp.dhis.test.TestBase.createProgramStage;
Expand Down Expand Up @@ -64,6 +64,7 @@
import org.hisp.dhis.db.sql.SqlBuilder;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
import org.hisp.dhis.period.PeriodDataProvider;
import org.hisp.dhis.period.PeriodDataProvider.PeriodSource;
import org.hisp.dhis.period.PeriodType;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;
Expand Down Expand Up @@ -143,6 +144,7 @@ void setUp() {
when(databaseInfoProvider.getDatabaseInfo()).thenReturn(DatabaseInfo.builder().build());
when(settingsProvider.getCurrentSettings()).thenReturn(settings);
when(settings.getLastSuccessfulResourceTablesUpdate()).thenReturn(new Date(0L));
when(analyticsTableSettings.getPeriodSource()).thenReturn(PeriodSource.DATABASE);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import static org.hisp.dhis.db.model.DataType.TIMESTAMP;
import static org.hisp.dhis.db.model.Table.STAGING_TABLE_SUFFIX;
import static org.hisp.dhis.db.model.constraint.Nullable.NULL;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.DATABASE;
import static org.hisp.dhis.period.PeriodDataProvider.PeriodSource.DATABASE;
import static org.hisp.dhis.system.util.SqlUtils.quote;
import static org.hisp.dhis.test.TestBase.createCategory;
import static org.hisp.dhis.test.TestBase.createCategoryCombo;
Expand Down Expand Up @@ -91,6 +91,7 @@
import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
import org.hisp.dhis.period.PeriodDataProvider;
import org.hisp.dhis.period.PeriodDataProvider.PeriodSource;
import org.hisp.dhis.period.PeriodType;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;
Expand Down Expand Up @@ -246,8 +247,7 @@ void verifyGetTableWithCategoryCombo() {
addCategoryCombo(program, categoryCombo);

when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(program));
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);

Expand Down Expand Up @@ -291,8 +291,7 @@ void verifyGetTableWithCategoryCombo() {
void verifyClientSideTimestampsColumns() {
Program program = createProgram('A');
when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(program));
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);

Expand Down Expand Up @@ -327,8 +326,7 @@ void verifyClientSideTimestampsColumns() {
void verifyAnalyticsEventTableHasDefaultPartition() {
Program program = createProgram('A');
when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(program));
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2021, 2022, 2023, 2024, 2025));
mockPeriodYears(List.of(2021, 2022, 2023, 2024, 2025));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);

Expand Down Expand Up @@ -426,8 +424,7 @@ void verifyGetTableWithDataElements() {
.today(today)
.build();

when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);

Expand Down Expand Up @@ -524,8 +521,7 @@ void verifyGetTableWithTrackedEntityAttribute() {
.today(today)
.build();

when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);

Expand Down Expand Up @@ -588,8 +584,7 @@ void verifyDataElementTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable(
.today(today)
.build();

when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);

Expand Down Expand Up @@ -632,8 +627,7 @@ void verifyTeiTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() {
programA.setProgramAttributes(List.of(programTrackedEntityAttribute));

when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(programA));
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);

Expand Down Expand Up @@ -682,8 +676,7 @@ void verifyOrgUnitOwnershipJoinsWhenPopulatingEventAnalyticsTable() {
programA.setProgramAttributes(List.of(programTrackedEntityAttribute));

when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(programA));
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);

Expand Down Expand Up @@ -721,8 +714,7 @@ void verifyGetAnalyticsTableWithOuLevels() {
Program programA = rnd.nextObject(Program.class);
programA.setId(0);

when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);
int startYear = availableDataYears.get(0);
Expand Down Expand Up @@ -784,8 +776,7 @@ void verifyGetAnalyticsTableWithOuGroupSet() {
when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(programA));
when(idObjectManager.getDataDimensionsNoAcl(OrganisationUnitGroupSet.class))
.thenReturn(ouGroupSet);
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);

Expand Down Expand Up @@ -825,8 +816,7 @@ void verifyGetAnalyticsTableWithOptionGroupSets() {

when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(programA));
when(categoryService.getAttributeCategoryOptionGroupSetsNoAcl()).thenReturn(cogs);
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);

Expand Down Expand Up @@ -903,15 +893,13 @@ void verifyTeaTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() {

programA.setProgramAttributes(List.of(programTrackedEntityAttribute));

when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));
when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(programA));
mockPeriodYears(List.of(2018, 2019, now().getYear()));

List<Integer> availableDataYears = periodDataProvider.getAvailableYears(DATABASE);
int startYear = availableDataYears.get(0);
int latestYear = availableDataYears.get(availableDataYears.size() - 1);

when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(programA));

when(jdbcTemplate.queryForList(
"select temp.supportedyear from (select distinct extract(year from "
+ DATE_CLAUSE
Expand Down Expand Up @@ -1000,4 +988,9 @@ private String getYearQueryForCurrentYear(

return sql;
}

private void mockPeriodYears(List<Integer> years) {
when(analyticsTableSettings.getPeriodSource()).thenReturn(PeriodSource.DATABASE);
when(periodDataProvider.getAvailableYears(DATABASE)).thenReturn(years);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import org.hisp.dhis.external.conf.ConfigurationKey;
import org.hisp.dhis.external.conf.DhisConfigurationProvider;
import org.hisp.dhis.setting.SystemSettingsService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

Expand All @@ -49,12 +49,7 @@ class AnalyticsTableSettingsTest {

@Mock private SystemSettingsService systemSettings;

private AnalyticsTableSettings settings;

@BeforeEach
public void before() {
settings = new AnalyticsTableSettings(config, systemSettings);
}
@InjectMocks private AnalyticsTableSettings settings;

@Test
void testGetAndValidateDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
package org.hisp.dhis.analytics.trackedentity;

import static java.util.Collections.emptySet;
import static org.hisp.dhis.analytics.common.AnalyticsDimensionsTestSupport.allValueTypeDataElements;
import static org.hisp.dhis.analytics.common.AnalyticsDimensionsTestSupport.allValueTypeTEAs;
import static org.hisp.dhis.analytics.common.AnalyticsDimensionsTestSupport.trackedEntityType;
import static org.hisp.dhis.analytics.common.DimensionServiceCommonTest.queryDisallowedValueTypesPredicate;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -42,28 +40,26 @@
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.PrefixedDimension;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramService;
import org.hisp.dhis.security.acl.AclService;
import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
import org.hisp.dhis.trackedentity.TrackedEntityTypeService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

/** Unit tests for {@link TrackedEntityAnalyticsDimensionsService}. */
@ExtendWith(MockitoExtension.class)
class TrackedEntityAnalyticsDimensionsServiceTest {
@Mock private ProgramService programService;

@Mock private TrackedEntityTypeService trackedEntityTypeService;

private TrackedEntityAnalyticsDimensionsService trackedEntityAnalyticsDimensionsService;

@BeforeEach
void setup() {
ProgramService programService = mock(ProgramService.class);
Program program = mock(Program.class);
TrackedEntityTypeService trackedEntityTypeService = mock(TrackedEntityTypeService.class);

when(programService.getProgram(any())).thenReturn(program);
when(program.getDataElements()).thenReturn(allValueTypeDataElements());
when(program.getProgramIndicators()).thenReturn(emptySet());
when(program.getTrackedEntityAttributes()).thenReturn(allValueTypeTEAs());
when(trackedEntityTypeService.getTrackedEntityType(any())).thenReturn(trackedEntityType());

trackedEntityAnalyticsDimensionsService =
Expand Down
Loading

0 comments on commit 7b15092

Please sign in to comment.