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

fix: Make period gen. backward compat. [DHIS2-15573] #15634

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -30,6 +30,8 @@
import static java.time.temporal.ChronoUnit.YEARS;
import static java.util.Comparator.reverseOrder;
import static java.util.stream.Collectors.toList;
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 @@ -183,7 +185,9 @@ public void generateDataElementTable() {

@Override
public void generateDatePeriodTable() {
List<Integer> availableYears = periodDataProvider.getAvailableYears();
List<Integer> availableYears =
periodDataProvider.getAvailableYears(
analyticsExportSettings.getMaxPeriodYearsOffset() == null ? SYSTEM_DEFINED : DATABASE);
checkYearsOffset(availableYears);

resourceTableStore.generateResourceTable(
Expand All @@ -202,30 +206,33 @@ public void generateDatePeriodTable() {
* @param yearsToCheck the list of years to be checked.
*/
private void checkYearsOffset(List<Integer> yearsToCheck) {
int maxYearsOffset = analyticsExportSettings.getMaxPeriodYearsOffset();
int minRangeAllowed = Year.now().minus(maxYearsOffset, YEARS).getValue();
int maxRangeAllowed = Year.now().plus(maxYearsOffset, YEARS).getValue();

boolean yearsOutOfRange =
yearsToCheck.stream().anyMatch(year -> year < minRangeAllowed || year > maxRangeAllowed);

if (yearsOutOfRange) {
String errorMessage = "Your database contains years out of the allowed offset.";
errorMessage +=
"\n Range of years allowed (based on your system settings and existing data): "
+ yearsToCheck.stream()
.filter(year -> year >= minRangeAllowed && year <= maxRangeAllowed)
.collect(toList())
+ ".";
errorMessage +=
"\n Years out of range found: "
+ yearsToCheck.stream()
.filter(year -> year < minRangeAllowed || year > maxRangeAllowed)
.collect(toList())
+ ".";

log.warn(errorMessage);
throw new RuntimeException(errorMessage);
Integer maxYearsOffset = analyticsExportSettings.getMaxPeriodYearsOffset();

if (maxYearsOffset != null) {
int minRangeAllowed = Year.now().minus(maxYearsOffset, YEARS).getValue();
int maxRangeAllowed = Year.now().plus(maxYearsOffset, YEARS).getValue();

boolean yearsOutOfRange =
yearsToCheck.stream().anyMatch(year -> year < minRangeAllowed || year > maxRangeAllowed);

if (yearsOutOfRange) {
String errorMessage = "Your database contains years out of the allowed offset.";
errorMessage +=
"\n Range of years allowed (based on your system settings and existing data): "
+ yearsToCheck.stream()
.filter(year -> year >= minRangeAllowed && year <= maxRangeAllowed)
.collect(toList())
+ ".";
errorMessage +=
"\n Years out of range found: "
+ yearsToCheck.stream()
.filter(year -> year < minRangeAllowed || year > maxRangeAllowed)
.collect(toList())
+ ".";

log.warn(errorMessage);
throw new RuntimeException(errorMessage);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package org.hisp.dhis.resourcetable;

import static java.time.temporal.ChronoUnit.YEARS;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.DATABASE;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -63,7 +64,7 @@ void generateDatePeriodTableWhenYearIsOutOfRange() {
int defaultOffset = 22;

// When
when(periodDataProvider.getAvailableYears()).thenReturn(yearsToCheck);
when(periodDataProvider.getAvailableYears(DATABASE)).thenReturn(yearsToCheck);
when(analyticsExportSettings.getMaxPeriodYearsOffset()).thenReturn(defaultOffset);

// Then
Expand All @@ -82,7 +83,7 @@ void generateDatePeriodTableWhenOffsetIsZeroWithPreviousYears() {
int zeroOffset = 0;

// When
when(periodDataProvider.getAvailableYears()).thenReturn(yearsToCheck);
when(periodDataProvider.getAvailableYears(DATABASE)).thenReturn(yearsToCheck);
when(analyticsExportSettings.getMaxPeriodYearsOffset()).thenReturn(zeroOffset);

// Then
Expand All @@ -101,7 +102,7 @@ void generateDatePeriodTableWhenOffsetIsZeroWithCurrentYear() {
int zeroOffset = 0;

// When
when(periodDataProvider.getAvailableYears()).thenReturn(yearsToCheck);
when(periodDataProvider.getAvailableYears(DATABASE)).thenReturn(yearsToCheck);
when(analyticsExportSettings.getMaxPeriodYearsOffset()).thenReturn(zeroOffset);
doNothing().when(resourceTableStore).generateResourceTable(any());

Expand All @@ -120,7 +121,7 @@ void generateDatePeriodTableWhenYearsAreInExpectedRange() {
int defaultOffset = 2;

// When
when(periodDataProvider.getAvailableYears()).thenReturn(yearsToCheck);
when(periodDataProvider.getAvailableYears(DATABASE)).thenReturn(yearsToCheck);
when(analyticsExportSettings.getMaxPeriodYearsOffset()).thenReturn(defaultOffset);
doNothing().when(resourceTableStore).generateResourceTable(any());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import static org.hisp.dhis.analytics.util.AnalyticsSqlUtils.quote;
import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType;
import static org.hisp.dhis.analytics.util.DisplayNameUtils.getDisplayName;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.DATABASE;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.SYSTEM_DEFINED;
import static org.hisp.dhis.system.util.MathUtils.NUMERIC_LENIENT_REGEXP;
import static org.hisp.dhis.util.DateUtils.getLongDateString;

Expand Down Expand Up @@ -220,7 +222,9 @@ public List<AnalyticsTable> getAnalyticsTables(AnalyticsTableUpdateParams params
"Get tables using earliest: %s, spatial support: %b",
params.getFromDate(), databaseInfo.isSpatialSupport()));

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

return params.isLatestUpdate()
? getLatestAnalyticsTables(params)
Expand Down Expand Up @@ -423,7 +427,9 @@ protected List<String> getPartitionChecks(AnalyticsTablePartition partition) {
protected void populateTable(
AnalyticsTableUpdateParams params, AnalyticsTablePartition partition) {

List<Integer> availableDataYears = periodDataProvider.getAvailableYears();
List<Integer> availableDataYears =
periodDataProvider.getAvailableYears(
analyticsExportSettings.getMaxPeriodYearsOffset() == null ? SYSTEM_DEFINED : DATABASE);
Integer firstDataYear = availableDataYears.get(0);
Integer latestDataYear = availableDataYears.get(availableDataYears.size() - 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import static org.hisp.dhis.analytics.ColumnDataType.INTEGER;
import static org.hisp.dhis.analytics.ColumnDataType.TEXT;
import static org.hisp.dhis.analytics.ColumnDataType.TIMESTAMP;
import static org.hisp.dhis.period.PeriodDataProvider.DataSource.DATABASE;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -221,7 +222,8 @@ void verifyGetLatestAnalyticsTables() {
.thenReturn(lastLatestPartitionUpdate);
when(jdbcTemplate.queryForList(Mockito.anyString())).thenReturn(queryResp);
when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(programs);
when(periodDataProvider.getAvailableYears()).thenReturn(List.of(2018, 2019, now().getYear()));
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));

List<AnalyticsTable> tables = subject.getAnalyticsTables(params);
assertThat(tables, hasSize(2));
Expand Down Expand Up @@ -259,9 +261,10 @@ void verifyGetTableWithCategoryCombo() {
addCategoryCombo(program, categoryCombo);

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

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

when(jdbcTemplate.queryForList(
getYearQueryForCurrentYear(program, true, availableDataYears), Integer.class))
Expand Down Expand Up @@ -308,7 +311,8 @@ void verifyGetTableWithDataElements() {
program.setProgramStages(Sets.newHashSet(ps1));

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

String aliasD1 = "(select eventdatavalues #>> '{%s, value}' " + FROM_CLAUSE + " ) as \"%s\"";
String aliasD2 =
Expand Down Expand Up @@ -370,7 +374,7 @@ void verifyGetTableWithDataElements() {
.withToday(today)
.build();

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

when(jdbcTemplate.queryForList(
getYearQueryForCurrentYear(program, true, availableDataYears), Integer.class))
Expand Down Expand Up @@ -422,9 +426,10 @@ void verifyGetTableWithTrackedEntityAttribute() {
program.setProgramStages(Sets.newHashSet(ps1));

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

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

String aliasD1 = "(select eventdatavalues #>> '{%s, value}' " + FROM_CLAUSE + " ) as \"%s\"";
String aliasTea1 =
Expand Down Expand Up @@ -483,9 +488,10 @@ void verifyDataElementTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable(
programA.setProgramStages(Sets.newHashSet(ps1));

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

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

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder()
Expand Down Expand Up @@ -531,9 +537,10 @@ void verifyTeiTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() {
programA.setProgramAttributes(Lists.newArrayList(programTrackedEntityAttribute));

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

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

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder()
Expand Down Expand Up @@ -578,9 +585,10 @@ void verifyOrgUnitOwnershipJoinsWhenPopulatingEventAnalyticsTable() {
programA.setProgramAttributes(Lists.newArrayList(programTrackedEntityAttribute));

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

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

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder()
Expand Down Expand Up @@ -615,9 +623,10 @@ void verifyGetAnalyticsTableWithOuLevels() {
rnd.objects(OrganisationUnitLevel.class, 2).collect(Collectors.toList());
Program programA = rnd.nextObject(Program.class);
programA.setId(0);
when(periodDataProvider.getAvailableYears()).thenReturn(List.of(2018, 2019, now().getYear()));
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));

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

Expand Down Expand Up @@ -678,9 +687,10 @@ void verifyGetAnalyticsTableWithOuGroupSet() {
.thenReturn(Collections.singletonList(programA));
when(idObjectManager.getDataDimensionsNoAcl(OrganisationUnitGroupSet.class))
.thenReturn(ouGroupSet);
when(periodDataProvider.getAvailableYears()).thenReturn(List.of(2018, 2019, now().getYear()));
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));

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

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder().withStartTime(START_TIME).build();
Expand Down Expand Up @@ -718,9 +728,10 @@ void verifyGetAnalyticsTableWithOptionGroupSets() {
when(idObjectManager.getAllNoAcl(Program.class))
.thenReturn(Collections.singletonList(programA));
when(categoryService.getAttributeCategoryOptionGroupSetsNoAcl()).thenReturn(cogs);
when(periodDataProvider.getAvailableYears()).thenReturn(List.of(2018, 2019, now().getYear()));
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2018, 2019, now().getYear()));

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

when(jdbcTemplate.queryForList(
getYearQueryForCurrentYear(programA, false, availableDataYears), Integer.class))
Expand Down Expand Up @@ -753,9 +764,10 @@ void verifyGetAnalyticsTableWithOptionGroupSets() {
void verifyAnalyticsEventTableHasDefaultPartition() {
Program program = createProgram('A');
when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(program));
when(periodDataProvider.getAvailableYears()).thenReturn(List.of(2021, 2022, 2023, 2024, 2025));
when(periodDataProvider.getAvailableYears(DATABASE))
.thenReturn(List.of(2021, 2022, 2023, 2024, 2025));

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

when(jdbcTemplate.queryForList(
getYearQueryForCurrentYear(program, true, availableDataYears), Integer.class))
Expand Down Expand Up @@ -823,9 +835,10 @@ void verifyTeaTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() {
programA.setProgramAttributes(Lists.newArrayList(programTrackedEntityAttribute));

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ public String getTableType() {
* Returns the years' offset defined for the period generation. See {@link
* ANALYTICS_MAX_PERIOD_YEARS_OFFSET}.
*
* @return the offset defined in system settings.
* @return the offset defined in system settings, or null if nothing is set.
*/
public int getMaxPeriodYearsOffset() {
return systemSettingManager.getIntSetting(ANALYTICS_MAX_PERIOD_YEARS_OFFSET);
public Integer getMaxPeriodYearsOffset() {
Integer yearsOffset = systemSettingManager.getIntSetting(ANALYTICS_MAX_PERIOD_YEARS_OFFSET);
return yearsOffset < 0 ? null : yearsOffset;
}
}
Loading