Skip to content

Commit

Permalink
chore: refactor to use Lombok's builder instead of manual one in Anal…
Browse files Browse the repository at this point in the history
…yticsTableUpdateParams [DHIS2-18144] (#18758)

* chore: refactor to use Lombok's builder instead of manual one in AnalyticsTableUpdateParams [DHIS2-18144]

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

* chore: refactor to use Lombok's builder instead of manual one in AnalyticsTableUpdateParams [DHIS2-18144]

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

---------

Signed-off-by: Giuseppe Nespolino <[email protected]>
  • Loading branch information
gnespolino authored Oct 8, 2024
1 parent 921f6b8 commit e8e4ff5
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
*/
package org.hisp.dhis.analytics;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.Builder;
import lombok.Getter;
import org.hisp.dhis.analytics.table.model.AnalyticsTablePartition;
import org.hisp.dhis.calendar.Calendar;
Expand All @@ -50,38 +49,40 @@
* @author Lars Helge Overland
*/
@Getter
@Builder(toBuilder = true, builderMethodName = "newBuilder")
public class AnalyticsTableUpdateParams {
/**
* Number of last years for which to update tables. A zero value indicates the "latest" data
* stored since last full analytics table generation.
*/
private Integer lastYears;
private final Integer lastYears;

/** Indicates whether to skip update of resource tables. */
private boolean skipResourceTables;
private final boolean skipResourceTables;

/** Indicates whether to skip update of analytics tables, outliers stats columns. */
private boolean skipOutliers;
private final boolean skipOutliers;

/** Analytics table types to skip. */
private Set<AnalyticsTableType> skipTableTypes = new HashSet<>();
@Builder.Default private final Set<AnalyticsTableType> skipTableTypes = new HashSet<>();

/** Analytics table programs to skip. */
private Set<String> skipPrograms = new HashSet<>();
@Builder.Default private final Set<String> skipPrograms = new HashSet<>();

/** Job ID. */
private JobConfiguration jobId;
private final JobConfiguration jobId;

/** Start time for update process. */
private Date startTime;
@Builder.Default private final Date startTime = new Date();

/** Time of last successful analytics table update. */
private Date lastSuccessfulUpdate;
private final Date lastSuccessfulUpdate;

/** Current date, only used for testing */
private Date today;
private final Date today;

private final Map<String, Object> extraParameters = new HashMap<>();
/** free key-value map for extra parameters. */
@Builder.Default private final Map<String, Object> extraParameters = new HashMap<>();

public void addExtraParam(String prefix, String key, Object value) {
extraParameters.put(prefix + key, value);
Expand All @@ -91,10 +92,6 @@ public Object getExtraParam(String prefix, String key) {
return extraParameters.get(prefix + key);
}

private AnalyticsTableUpdateParams() {
this.startTime = new Date();
}

// -------------------------------------------------------------------------
// Get methods
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -155,105 +152,7 @@ public Date getFromDate() {
return earliest;
}

// -------------------------------------------------------------------------
// Builder of immutable instances
// -------------------------------------------------------------------------

/** Returns a new instance of this parameter object. */
public AnalyticsTableUpdateParams instance() {
AnalyticsTableUpdateParams params = new AnalyticsTableUpdateParams();

params.lastYears = this.lastYears;
params.skipResourceTables = this.skipResourceTables;
params.skipOutliers = this.skipOutliers;
params.skipTableTypes = new HashSet<>(this.skipTableTypes);
params.skipPrograms = new HashSet<>(this.skipPrograms);
params.jobId = this.jobId;
params.startTime = this.startTime;
params.lastSuccessfulUpdate = this.lastSuccessfulUpdate;

return this;
}

public static Builder newBuilder() {
return new AnalyticsTableUpdateParams.Builder();
}

public static Builder newBuilder(AnalyticsTableUpdateParams analyticsTableUpdateParams) {
return new AnalyticsTableUpdateParams.Builder(analyticsTableUpdateParams);
}

/** Builder for {@link AnalyticsTableUpdateParams} instances. */
public static class Builder {
private final AnalyticsTableUpdateParams params;

protected Builder() {
this.params = new AnalyticsTableUpdateParams();
}

protected Builder(AnalyticsTableUpdateParams analyticsTableUpdateParams) {
this.params = analyticsTableUpdateParams.instance();
}

public Builder withLastYears(Integer lastYears) {
this.params.lastYears = lastYears;
return this;
}

public Builder withLatestPartition() {
this.params.lastYears = AnalyticsTablePartition.LATEST_PARTITION;
return this;
}

public Builder withSkipResourceTables(boolean skipResourceTables) {
this.params.skipResourceTables = skipResourceTables;
return this;
}

public Builder withSkipOutliers(boolean skipOutliers) {
this.params.skipOutliers = skipOutliers;
return this;
}

public Builder withSkipTableTypes(Set<AnalyticsTableType> skipTableTypes) {
this.params.skipTableTypes = skipTableTypes;
return this;
}

public Builder withSkipPrograms(Set<String> skipPrograms) {
this.params.skipPrograms = skipPrograms;
return this;
}

public Builder withJobId(JobConfiguration jobId) {
this.params.jobId = jobId;
return this;
}

public Builder withLastSuccessfulUpdate(Date lastSuccessfulUpdate) {
this.params.lastSuccessfulUpdate = lastSuccessfulUpdate;
return this;
}

public Builder withStartTime(Date startTime) {
this.params.startTime = startTime;
return this;
}

/**
* This builder property is only used for testing purposes.
*
* @param date A mock Date
*/
public Builder withToday(Date date) {

this.params.today = date;
return this;
}

public AnalyticsTableUpdateParams build() {
checkNotNull(this.params.startTime);
return this.params;
}
public AnalyticsTableUpdateParams withLatestPartition() {
return this.toBuilder().lastYears(AnalyticsTablePartition.LATEST_PARTITION).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public void generateAnalyticsTables(AnalyticsTableUpdateParams params0, JobProgr
.collect(Collectors.toSet());

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder(params0)
.withLastSuccessfulUpdate(lastSuccessfulUpdate)
.build();
params0.toBuilder().lastSuccessfulUpdate(lastSuccessfulUpdate).build();

log.info("Found {} analytics table types: {}", availableTypes.size(), availableTypes);
log.info("Analytics table update: {}", params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public void execute(JobConfiguration jobConfiguration, JobProgress progress) {

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder()
.withLastYears(parameters.getLastYears())
.withSkipResourceTables(parameters.isSkipResourceTables())
.withSkipOutliers(parameters.isSkipOutliers())
.withSkipTableTypes(parameters.getSkipTableTypes())
.withSkipPrograms(parameters.getSkipPrograms())
.withJobId(jobConfiguration)
.withStartTime(new Date())
.lastYears(parameters.getLastYears())
.skipResourceTables(parameters.isSkipResourceTables())
.skipOutliers(parameters.isSkipOutliers())
.skipTableTypes(parameters.getSkipTableTypes())
.skipPrograms(parameters.getSkipPrograms())
.jobId(jobConfiguration)
.startTime(new Date())
.build();

analyticsTableGenerator.generateAnalyticsTables(params, progress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ public void execute(JobConfiguration jobConfiguration, JobProgress progress) {

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder()
.withLastYears(parameters.getLastYears())
.withSkipResourceTables(false)
.withSkipOutliers(parameters.getSkipOutliers())
.withSkipTableTypes(parameters.getSkipTableTypes())
.withJobId(jobConfiguration)
.withStartTime(startTime)
.lastYears(parameters.getLastYears())
.skipResourceTables(false)
.skipOutliers(parameters.getSkipOutliers())
.skipTableTypes(parameters.getSkipTableTypes())
.jobId(jobConfiguration)
.startTime(startTime)
.build();

try {
Expand All @@ -119,13 +119,13 @@ public void execute(JobConfiguration jobConfiguration, JobProgress progress) {

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder()
.withLatestPartition()
.withSkipResourceTables(true)
.withSkipOutliers(parameters.getSkipOutliers())
.withSkipTableTypes(parameters.getSkipTableTypes())
.withJobId(jobConfiguration)
.withStartTime(startTime)
.build();
.skipResourceTables(true)
.skipOutliers(parameters.getSkipOutliers())
.skipTableTypes(parameters.getSkipTableTypes())
.jobId(jobConfiguration)
.startTime(startTime)
.build()
.withLatestPartition();

analyticsTableGenerator.generateAnalyticsTables(params, progress);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void testGetRegularAnalyticsTable() {
List<Integer> dataYears = List.of(2018, 2019);

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder().withStartTime(startTime).build();
AnalyticsTableUpdateParams.newBuilder().startTime(startTime).build();

when(analyticsTableSettings.getTableLogged()).thenReturn(UNLOGGED);
when(jdbcTemplate.queryForList(Mockito.anyString(), ArgumentMatchers.<Class<Integer>>any()))
Expand Down Expand Up @@ -165,7 +165,7 @@ void testGetRegularAnalyticsTableLogged() {
List<Integer> dataYears = List.of(2018, 2019);

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder().withStartTime(startTime).build();
AnalyticsTableUpdateParams.newBuilder().startTime(startTime).build();

when(analyticsTableSettings.getTableLogged()).thenReturn(LOGGED);
when(jdbcTemplate.queryForList(Mockito.anyString(), ArgumentMatchers.<Class<Integer>>any()))
Expand Down Expand Up @@ -206,10 +206,7 @@ void testGetLatestAnalyticsTable() {
Date startTime = new DateTime(2019, 3, 1, 10, 0).toDate();

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder()
.withStartTime(startTime)
.withLatestPartition()
.build();
AnalyticsTableUpdateParams.newBuilder().startTime(startTime).build().withLatestPartition();

List<Map<String, Object>> queryResp = new ArrayList<>();
queryResp.add(Map.of("dataelementid", 1));
Expand Down Expand Up @@ -245,10 +242,7 @@ void testGetLatestAnalyticsTableNoFullTableUpdate() {
Date startTime = new DateTime(2019, 3, 1, 10, 0).toDate();

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder()
.withStartTime(startTime)
.withLatestPartition()
.build();
AnalyticsTableUpdateParams.newBuilder().startTime(startTime).build().withLatestPartition();

when(settings.getLastSuccessfulResourceTablesUpdate()).thenReturn(new Date(0L));
when(settings.getLastSuccessfulAnalyticsTablesUpdate()).thenReturn(new Date(42L));
Expand All @@ -275,10 +269,8 @@ void testSwapTable() {
LOGGED);
table.addTablePartition(List.of(), 2023, new DateTime(2023, 1, 1, 0, 0).toDate(), null);
AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder()
.withStartTime(startTime)
.withLatestPartition()
.build();
AnalyticsTableUpdateParams.newBuilder().startTime(startTime).build().withLatestPartition();

when(jdbcTemplate.queryForList(any())).thenReturn(List.of(Map.of("table_name", "analytic")));

Table swappedPartition = table.getTablePartitions().get(0).swapFromStaging();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void verifyTeiTypeOrgUnitFetchesOuUidWhenPopulatingEventAnalyticsTable() {
when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(p1));

AnalyticsTableUpdateParams params =
AnalyticsTableUpdateParams.newBuilder().withLastYears(2).withStartTime(START_TIME).build();
AnalyticsTableUpdateParams.newBuilder().lastYears(2).startTime(START_TIME).build();

List<AnalyticsTable> analyticsTables = subject.getAnalyticsTables(params);
assertFalse(analyticsTables.isEmpty());
Expand Down
Loading

0 comments on commit e8e4ff5

Please sign in to comment.