Skip to content

Commit

Permalink
Merge pull request #914 from msvinaykumar/errorMap
Browse files Browse the repository at this point in the history
Error map for updateResults
  • Loading branch information
dinogun authored Aug 23, 2023
2 parents 7074335 + 386edc9 commit db95c01
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*******************************************************************************/
package com.autotune.analyzer.experiment;

import com.autotune.analyzer.exceptions.KruizeResponse;
import com.autotune.analyzer.kruizeObject.KruizeObject;
import com.autotune.analyzer.performanceProfiles.PerformanceProfileInterface.PerfProfileInterface;
import com.autotune.analyzer.serviceObjects.Converters;
import com.autotune.analyzer.serviceObjects.UpdateResultsAPIObject;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.analyzer.utils.AnalyzerErrorConstants;
import com.autotune.common.data.ValidationOutputData;
import com.autotune.common.data.result.ExperimentResultData;
import com.autotune.database.service.ExperimentDBService;
Expand All @@ -32,6 +34,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.ArrayList;
Expand All @@ -50,6 +53,30 @@ public class ExperimentInitiator {
List<UpdateResultsAPIObject> failedUpdateResultsAPIObjects = new ArrayList<>();
private ValidationOutputData validationOutputData;

public static List<KruizeResponse> getErrorMap(List<String> errorMessages) {
List<KruizeResponse> responses;
if (null != errorMessages) {
responses = new ArrayList<>();
errorMessages.forEach(
(errorText) -> {
if (AnalyzerErrorConstants.APIErrors.updateResultsAPI.ERROR_CODE_MAP.containsKey(errorText)) {
responses.add(
new KruizeResponse(errorText, AnalyzerErrorConstants.APIErrors.updateResultsAPI.ERROR_CODE_MAP.get(errorText), "", "ERROR", null)
);

} else {
responses.add(
new KruizeResponse(errorText, HttpServletResponse.SC_BAD_REQUEST, "", "ERROR", null)
);
}
}
);
} else {
responses = null;
}
return responses;
}

/**
* Initiate Experiment validation
*
Expand Down Expand Up @@ -93,7 +120,6 @@ public void generateAndAddRecommendations(KruizeObject kruizeObject, List<Experi
}
}


public void validateAndAddExperimentResults(List<UpdateResultsAPIObject> updateResultsAPIObjects) {
List<UpdateResultsAPIObject> failedDBObjects = new ArrayList<>();
Validator validator = Validation.byProvider(HibernateValidator.class)
Expand All @@ -117,7 +143,7 @@ public void validateAndAddExperimentResults(List<UpdateResultsAPIObject> updateR
errorReasons.add(violation.getMessage());
}
}
object.setErrorReasons(errorReasons);
object.setErrors(getErrorMap(errorReasons));
failedUpdateResultsAPIObjects.add(object);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package com.autotune.analyzer.serviceObjects;

import com.autotune.analyzer.exceptions.KruizeResponse;
import com.autotune.analyzer.serviceObjects.verification.annotators.CompareDate;
import com.autotune.analyzer.serviceObjects.verification.annotators.KubernetesElementsCheck;
import com.autotune.analyzer.serviceObjects.verification.annotators.PerformanceProfileCheck;
Expand All @@ -29,7 +30,6 @@
import java.sql.Timestamp;
import java.util.List;


@CompareDate(groups = BaseSO.InitialValidation.class, message = AnalyzerErrorConstants.AutotuneObjectErrors.WRONG_TIMESTAMP)
@TimeDifferenceCheck(groups = UpdateResultsAPIObject.EvaluateRemainingConstraints.class, message = AnalyzerErrorConstants.AutotuneObjectErrors.MEASUREMENT_DURATION_ERROR)
@PerformanceProfileCheck(groups = UpdateResultsAPIObject.EvaluateRemainingConstraints.class)
Expand All @@ -50,7 +50,7 @@ public class UpdateResultsAPIObject extends BaseSO {
@SerializedName(KruizeConstants.JSONKeys.KUBERNETES_OBJECTS)
private List<KubernetesAPIObject> kubernetesAPIObjects;

private List<String> errorReasons;
private List<KruizeResponse> errors;

public Timestamp getStartTimestamp() {
return startTimestamp;
Expand All @@ -76,12 +76,12 @@ public void setKubernetesObjects(List<KubernetesAPIObject> kubernetesAPIObjects)
this.kubernetesAPIObjects = kubernetesAPIObjects;
}

public List<String> getErrorReasons() {
return errorReasons;
public List<KruizeResponse> getErrors() {
return errors;
}

public void setErrorReasons(List<String> errorReasons) {
this.errorReasons = errorReasons;
public void setErrors(List<KruizeResponse> errors) {
this.errors = errors;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.autotune.operator.KruizeDeploymentInfo;
import com.autotune.utils.KruizeConstants;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
* Contains strings describing the errors encountered
*/
Expand Down Expand Up @@ -111,6 +114,16 @@ private RunExperimentMessages() {
public static final class APIErrors {
private APIErrors() {

}

public static final class updateResultsAPI {
public static final String RESULTS_ALREADY_EXISTS = "An entry for this record already exists!";

public static final HashMap<String, Integer> ERROR_CODE_MAP = new HashMap<String, Integer>() {{
put(RESULTS_ALREADY_EXISTS, HttpServletResponse.SC_CONFLICT);
}};


}

public static final class ListRecommendationsAPI {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.autotune.analyzer.kruizeObject.KruizeObject;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.analyzer.utils.AnalyzerErrorConstants;
import com.autotune.common.data.ValidationOutputData;
import com.autotune.database.helper.DBConstants;
import com.autotune.database.init.KruizeHibernateUtil;
Expand Down Expand Up @@ -120,8 +121,7 @@ public List<KruizeResultsEntry> addToDBAndFetchFailedResults(List<KruizeResultsE
try {
session.merge(entry);
} catch (PersistenceException e) {
entry.setErrorReasons(List.of(String.format("A record with the name %s already exists within the timestamp range starting from %s and ending on %s.", entry.getExperiment_name(), new SimpleDateFormat(KruizeConstants.DateFormats.STANDARD_JSON_DATE_FORMAT).format(entry.getInterval_start_time()),
new SimpleDateFormat(KruizeConstants.DateFormats.STANDARD_JSON_DATE_FORMAT).format(entry.getInterval_end_time()))));
entry.setErrorReasons(List.of(AnalyzerErrorConstants.APIErrors.updateResultsAPI.RESULTS_ALREADY_EXISTS));
failedResultsEntries.add(entry);
} catch (Exception e) {
entry.setErrorReasons(List.of(e.getMessage()));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/autotune/database/helper/DBHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import java.text.SimpleDateFormat;
import java.util.*;

import static com.autotune.analyzer.experiment.ExperimentInitiator.getErrorMap;

/**
* Helper functions used by the DB to create entity objects.
*/
Expand Down Expand Up @@ -491,7 +493,7 @@ public static List<UpdateResultsAPIObject> convertResultEntryToUpdateResultsAPIO
updateResultsAPIObject.setExperimentName(kruizeResultsEntry.getExperiment_name());
updateResultsAPIObject.setStartTimestamp(kruizeResultsEntry.getInterval_start_time());
updateResultsAPIObject.setEndTimestamp(kruizeResultsEntry.getInterval_end_time());
updateResultsAPIObject.setErrorReasons(kruizeResultsEntry.getErrorReasons());
updateResultsAPIObject.setErrors(getErrorMap(kruizeResultsEntry.getErrorReasons()));
JsonNode extendedDataNode = kruizeResultsEntry.getExtended_data();
JsonNode k8sObjectsNode = extendedDataNode.get(KruizeConstants.JSONKeys.KUBERNETES_OBJECTS);
List<K8sObject> k8sObjectList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,12 @@ def test_update_results_with_same_result(cluster_type):
interval_end_time = result_json_data[0]['interval_end_time']
interval_start_time = result_json_data[0]['interval_start_time']

TIMESTAMP_PRESENT_MSG = 'A record with the name %s already exists within the timestamp range starting ' \
'from %s and ending on %s.' % (experiment_name, interval_start_time, interval_end_time)
TIMESTAMP_PRESENT_MSG = 'An entry for this record already exists!'

print(TIMESTAMP_PRESENT_MSG)
print(data['data'][0]['errorReasons'][0])
print(data['data'][0]['errors'][0]['message'])
assert data['message'] == 'Out of a total of 1 records, 1 failed to save'
assert data['data'][0]['errorReasons'][0] == TIMESTAMP_PRESENT_MSG
assert data['data'][0]['errors'][0]['message'] == TIMESTAMP_PRESENT_MSG

response = delete_experiment(input_json_file)
print("delete exp = ", response.status_code)
Expand Down Expand Up @@ -535,12 +534,12 @@ def test_update_results_with_valid_and_invalid_interval_duration(test_name, inte
assert response.status_code == ERROR_STATUS_CODE
assert data['status'] == ERROR_STATUS
assert data['message'] == 'Out of a total of 1 records, 1 failed to save'
assert data['data'][0]['errorReasons'][0] == UPDATE_RESULTS_DATE_PRECEDE_ERROR_MSG
assert data['data'][0]['errors'][0]['message'] == UPDATE_RESULTS_DATE_PRECEDE_ERROR_MSG
else:
assert response.status_code == ERROR_STATUS_CODE
assert data['status'] == ERROR_STATUS
assert data['message'] == 'Out of a total of 1 records, 1 failed to save'
assert data['data'][0]['errorReasons'][0] == INVALID_INTERVAL_DURATION_MSG
assert data['data'][0]['errors'][0]['message'] == INVALID_INTERVAL_DURATION_MSG

response = delete_experiment(input_json_file)
print("delete exp = ", response.status_code)

0 comments on commit db95c01

Please sign in to comment.