#3 -- add validation messages to response

This commit is contained in:
Anton Romanov 2022-05-04 14:42:40 +04:00
parent 4417fc2ceb
commit 8201842801
4 changed files with 27 additions and 0 deletions

View File

@ -2,13 +2,16 @@ package ru.ulstu.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.client.HttpServerErrorException;
import ru.ulstu.datamodel.exception.ForecastValidateException;
import ru.ulstu.datamodel.exception.ModelingException;
import ru.ulstu.datamodel.exception.TimeSeriesValidateException;
import ru.ulstu.datamodel.response.ErrorConstants;
import ru.ulstu.datamodel.response.ResponseExtended;
@ControllerAdvice
public class AdviceController {
private final Logger log = LoggerFactory.getLogger(AdviceController.class);
@ -30,11 +33,19 @@ public class AdviceController {
@ExceptionHandler(TimeSeriesValidateException.class)
public ResponseExtended<String> handleTimeSeriesValidateException(Throwable e) {
e.printStackTrace();
return handleException(ErrorConstants.TIME_SERIES_VALIDATE_ERROR, e.getMessage());
}
@ExceptionHandler(ForecastValidateException.class)
public ResponseExtended<String> handleForecastValidateException(Throwable e) {
e.printStackTrace();
return handleException(ErrorConstants.FORECAST_PARAMS_ERROR, e.getMessage());
}
@ExceptionHandler(ModelingException.class)
public ResponseExtended<String> handleModelingException(Throwable e) {
e.printStackTrace();
return handleException(ErrorConstants.MODELING_ERROR, e.getMessage());
}
}

View File

@ -4,6 +4,7 @@ public enum ErrorConstants {
UNKNOWN(0, "Unknown error"),
TIME_SERIES_VALIDATE_ERROR(10, "Некорректный временной ряд"),
FORECAST_PARAMS_ERROR(11, "Некорректные параметры для прогнозирования"),
MODELING_ERROR(13, "Ошибка моделирования"),
HTTP_CLIENT_ERROR(66, "Http client error");
private final int code;

View File

@ -62,6 +62,11 @@ public abstract class Method {
return true;
}
public void validateForForecast(TimeSeries timeSeries, int countPoints) throws ModelingException {
ValidationUtils.validateTimeSeries(timeSeries);
validateForecastParams(countPoints);
}
public boolean canMakeModel(TimeSeries timeSeries, List<MethodParamValue> parameters) {
try {
ValidationUtils.validateTimeSeries(timeSeries);

View File

@ -44,6 +44,12 @@ class MethodParamBruteForce {
TimeSeries reducedTimeSeries = new TimeSeries(timeSeries.getValues().stream().limit(timeSeries.getLength() - countPoints).collect(Collectors.toList()),
"test part of " + timeSeries.getKey());
try {
ValidationUtils.validateTimeSeries(reducedTimeSeries);
} catch (ModelingException ex) {
throw new ModelingException("Тестовая часть временного ряда не прошла валидацию: " + ex.getMessage());
}
Map<LocalDateTime, Double> tsValues = timeSeries.getValues().stream()
.collect(Collectors.toMap(TimeSeriesValue::getDate, TimeSeriesValue::getValue));
@ -74,6 +80,7 @@ class MethodParamBruteForce {
.filter(m -> m.getClass().getSimpleName().equals(methodClassName))
.findAny()
.orElseThrow(() -> new ModelingException("Неизвестный метод прогнозирования"));
method.validateForForecast(timeSeries, countPointsForecast);
return getForecastByMethods(timeSeries, List.of(method), countPointsForecast);
}
@ -88,6 +95,9 @@ class MethodParamBruteForce {
private ModelingResult getBestResultForecast(List<ModelingResult> modelingResults,
TimeSeries timeSeries,
int countPoints) throws ModelingException {
if (modelingResults.size() == 0) {
throw new ModelingException("Нет результатов моделирования");
}
ModelingResult bestResult = modelingResults.stream()
.min(Comparator.comparing(modelingResult -> modelingResult.getScore().getDoubleValue()))
.orElseThrow(() -> new ModelingException("Лучший метод не найден"));