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

feat: tracker import errors to job tracking error forward [DHIS2-15276] #15723

Merged
merged 6 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -47,6 +47,7 @@
import lombok.*;
import lombok.experimental.Accessors;
import org.hisp.dhis.feedback.ErrorCode;
import org.hisp.dhis.tracker.imports.validation.ValidationCode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to your changes but there are some wildcard imports in this file. maybe can clean up later.


/**
*
Expand Down Expand Up @@ -147,6 +148,14 @@
addError(code, uid, type, List.of(args));
}

default void addError(
@Nonnull ValidationCode code,
@CheckForNull String uid,
@Nonnull String type,
String... args) {
addError(code, uid, type, List.of(args));
}

default void addError(
@Nonnull ErrorCode code,
@CheckForNull String uid,
Expand All @@ -156,6 +165,15 @@
// default is to not collect errors
}

default void addError(
@Nonnull ValidationCode code,
Fixed Show fixed Hide fixed
@CheckForNull String uid,
Fixed Show fixed Hide fixed
@Nonnull String type,
Fixed Show fixed Hide fixed
@Nonnull List<String> args) {
Fixed Show fixed Hide fixed
// is overridden by a tracker that collects errors
// default is to not collect errors
}

/*
* Tracking API:
*/
Expand Down Expand Up @@ -580,7 +598,7 @@
@Nonnull
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private final Map<String, Map<ErrorCode, Queue<Error>>> errors;
private final Map<String, Map<String, Queue<Error>>> errors;

public Progress() {
this.sequence = new ConcurrentLinkedDeque<>();
Expand All @@ -590,7 +608,7 @@
@JsonCreator
public Progress(
@Nonnull @JsonProperty("sequence") Deque<Process> sequence,
@CheckForNull @JsonProperty("errors") Map<String, Map<ErrorCode, Queue<Error>>> errors) {
@CheckForNull @JsonProperty("errors") Map<String, Map<String, Queue<Error>>> errors) {
this.sequence = sequence;
this.errors = errors == null ? Map.of() : errors;
}
Expand All @@ -609,7 +627,7 @@
return !errors.isEmpty();
}

public Set<ErrorCode> getErrorCodes() {
public Set<String> getErrorCodes() {
return errors.values().stream()
.flatMap(e -> e.keySet().stream())
.collect(toUnmodifiableSet());
Expand All @@ -620,7 +638,7 @@
@Accessors(chain = true)
final class Error {

@Nonnull @JsonProperty private final ErrorCode code;
@Nonnull @JsonProperty private final String code;

/** The object that has the error */
@Nonnull @JsonProperty private final String id;
Expand All @@ -641,7 +659,7 @@

@JsonCreator
public Error(
@Nonnull @JsonProperty("code") ErrorCode code,
@Nonnull @JsonProperty("code") String code,
@Nonnull @JsonProperty("id") String id,
@Nonnull @JsonProperty("type") String type,
@Nonnull @JsonProperty("args") List<String> args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package org.hisp.dhis.scheduling;

import static org.hisp.dhis.scheduling.JobType.TRACKER_IMPORT_JOB;
import static org.hisp.dhis.scheduling.JobType.values;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -72,6 +73,7 @@
import org.hisp.dhis.schema.Property;
import org.hisp.dhis.setting.SettingKey;
import org.hisp.dhis.setting.SystemSettingManager;
import org.hisp.dhis.tracker.imports.validation.ValidationCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -260,6 +262,7 @@ public List<JsonObject> findJobRunErrors(@Nonnull JobRunErrorsParams params) {
json -> {
JsonObject obj = JsonMixed.of(json);
List<JsonNode> flatErrors = new ArrayList<>();
JobType type = obj.getString("type").parsed(JobType::valueOf);
JsonObject errors = obj.getObject("errors");
errors
.node()
Expand All @@ -275,22 +278,7 @@ public List<JsonObject> findJobRunErrors(@Nonnull JobRunErrorsParams params) {
.getValue()
.elements()
.forEach(
error -> {
ErrorCode code =
ErrorCode.valueOf(
JsonMixed.of(error).getString("code").string());
Object[] args =
JsonMixed.of(error)
.getArray("args")
.stringValues()
.toArray(new String[0]);
String msg =
MessageFormat.format(code.getMessage(), args);
flatErrors.add(
error
.extract()
.addMembers(e -> e.addString("message", msg)));
})));
error -> flatErrors.add(errorWithMessage(type, error)))));
return JsonMixed.of(
errors
.node()
Expand All @@ -300,6 +288,17 @@ public List<JsonObject> findJobRunErrors(@Nonnull JobRunErrorsParams params) {
return jobConfigurationStore.findJobRunErrors(params).map(toObject).toList();
}

private static JsonNode errorWithMessage(JobType type, JsonNode error) {
String codeName = JsonMixed.of(error).getString("code").string();
String template =
type == TRACKER_IMPORT_JOB
? ValidationCode.valueOf(codeName).getMessage()
: ErrorCode.valueOf(codeName).getMessage();
Object[] args = JsonMixed.of(error).getArray("args").stringValues().toArray(new String[0]);
String msg = MessageFormat.format(template, args);
return error.extract().addMembers(e -> e.addString("message", msg));
}

@Override
@Transactional(readOnly = true)
public Map<String, Map<String, Property>> getJobParametersSchema() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import lombok.extern.slf4j.Slf4j;
import org.hisp.dhis.commons.util.DebugUtils;
import org.hisp.dhis.eventhook.EventHookPublisher;
import org.hisp.dhis.feedback.ErrorCode;
import org.hisp.dhis.feedback.NotFoundException;
import org.hisp.dhis.leader.election.LeaderManager;
import org.hisp.dhis.message.MessageService;
Expand Down Expand Up @@ -284,8 +283,7 @@ private void updateProgress(@Nonnull String jobId) {
if (job == null) return;
try {
JobProgress.Progress progress = job.getProgress();
String errorCodes =
progress.getErrorCodes().stream().map(ErrorCode::name).sorted().collect(joining(" "));
String errorCodes = progress.getErrorCodes().stream().sorted().collect(joining(" "));
jobConfigurationStore.updateProgress(
jobId, jsonMapper.writeValueAsString(progress), errorCodes);
} catch (JsonProcessingException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.text.MessageFormat;
import java.util.*;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hisp.dhis.feedback.ConflictException;
import org.hisp.dhis.feedback.ErrorCode;
import org.hisp.dhis.feedback.NotFoundException;
import org.hisp.dhis.scheduling.JobProgress.Progress;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -127,14 +125,12 @@ public List<JobProgress.Error> getErrors(@Nonnull String jobId) {
if (json == null) return List.of();
Progress progress = mapToProgress("{\"sequence\":[],\"errors\":" + json + "}");
if (progress == null) return List.of();
Map<String, Map<ErrorCode, Queue<JobProgress.Error>>> map = progress.getErrors();
Map<String, Map<String, Queue<JobProgress.Error>>> map = progress.getErrors();
if (map.isEmpty()) return List.of();
List<JobProgress.Error> errors =
map.values().stream()
.flatMap(e -> e.values().stream().flatMap(Collection::stream))
.toList();
errors.forEach(
e -> e.setMessage(MessageFormat.format(e.getCode().getMessage(), e.getArgs().toArray())));
return errors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ public void addError(
@CheckForNull String uid,
@Nonnull String type,
@Nonnull List<String> args) {
addError(code.name(), uid, type, args);
}

private void addError(
@Nonnull String code,
@CheckForNull String uid,
@Nonnull String type,
@Nonnull List<String> args) {
try {
// Note: we use empty string in case the UID is not known/defined yet to allow use in maps
progress.addError(new Error(code, uid == null ? "" : uid, type, args));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import lombok.Builder;
import lombok.Value;
import org.hisp.dhis.common.OpenApi;
Expand All @@ -37,43 +40,24 @@
@Builder
@OpenApi.Shared(name = "TrackerImportError")
public class Error {
private final String errorMessage;

private final String errorCode;

private final String trackerType;

private final String uid;
@Nonnull @JsonProperty String message;
@Nonnull @JsonProperty String errorCode;
@Nonnull @JsonProperty String trackerType;
@Nonnull @JsonProperty String uid;
@Nonnull @JsonProperty List<String> args;

@JsonCreator
public Error(
@JsonProperty("message") String errorMessage,
@JsonProperty("errorCode") String errorCode,
@JsonProperty("trackerType") String trackerType,
@JsonProperty("uid") String uid) {
this.errorMessage = errorMessage;
@Nonnull @JsonProperty("message") String message,
@Nonnull @JsonProperty("errorCode") String errorCode,
@Nonnull @JsonProperty("trackerType") String trackerType,
@Nonnull @JsonProperty("uid") String uid,
@CheckForNull @JsonProperty("uid") List<String> args) {
this.message = message;
this.errorCode = errorCode;
this.trackerType = trackerType;
this.uid = uid;
}

@JsonProperty
public String getErrorCode() {
return errorCode;
}

@JsonProperty
public String getMessage() {
return errorMessage;
}

@JsonProperty
public String getTrackerType() {
return trackerType;
}

@JsonProperty
public String getUid() {
return uid;
this.args = args == null ? List.of() : args;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ private static List<Error> convertToError(List<Validation> errors) {
.map(
e ->
Error.builder()
.errorMessage(e.getMessage())
.message(e.getMessage())
.errorCode(e.getCode())
.trackerType(e.getType())
.uid(e.getUid())
.args(e.getArgs())
.build())
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
*/
package org.hisp.dhis.tracker.imports.validation;

import java.util.List;
import lombok.Value;
import org.hisp.dhis.tracker.TrackerType;

@Value
public class Error implements Validation {
String message;

String message;
ValidationCode code;

TrackerType type;

String uid;
List<Object> args;

public ValidationCode getErrorCode() {
return code;
Expand Down Expand Up @@ -67,4 +67,9 @@ public String getType() {
public String getUid() {
return uid;
}

@Override
public List<String> getArgs() {
return args.stream().map(Object::toString).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,15 @@ private <T extends TrackerDto> void addErrorsForChildren(
}

private static Error error(ValidationCode code, TrackerDto notPersistable, TrackerDto reason) {
String message =
MessageFormat.format(
code.getMessage(),
notPersistable.getTrackerType().getName(),
notPersistable.getUid(),
reason.getTrackerType().getName(),
reason.getUid());

return new Error(message, code, notPersistable.getTrackerType(), notPersistable.getUid());
Object[] args = {
notPersistable.getTrackerType().getName(),
notPersistable.getUid(),
reason.getTrackerType().getName(),
reason.getUid()
};
String message = MessageFormat.format(code.getMessage(), args);
return new Error(
message, code, notPersistable.getTrackerType(), notPersistable.getUid(), List.of(args));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ public boolean addError(TrackerDto dto, ValidationCode code, Object... args) {
MessageFormatter.format(idSchemes, code.getMessage(), args),
code,
dto.getTrackerType(),
dto.getUid()));
dto.getUid(),
List.of(args)));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
package org.hisp.dhis.tracker.imports.validation;

import java.util.List;

/**
* Validation represents an issue found by the validation process. It contains information that help
* the client to understand and fix the problem.
Expand All @@ -39,4 +41,8 @@ public interface Validation {
String getType();

String getUid();

default List<String> getArgs() {
return List.of();
}
}
Loading
Loading