From 82018428014f7b06efed6c3c8e46bc836332f3ef Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 4 May 2022 14:42:40 +0400 Subject: [PATCH] #3 -- add validation messages to response --- .../java/ru/ulstu/controller/AdviceController.java | 11 +++++++++++ .../ru/ulstu/datamodel/response/ErrorConstants.java | 1 + src/main/java/ru/ulstu/method/Method.java | 5 +++++ .../java/ru/ulstu/service/MethodParamBruteForce.java | 10 ++++++++++ 4 files changed, 27 insertions(+) diff --git a/src/main/java/ru/ulstu/controller/AdviceController.java b/src/main/java/ru/ulstu/controller/AdviceController.java index 148ac8a..aaeb024 100644 --- a/src/main/java/ru/ulstu/controller/AdviceController.java +++ b/src/main/java/ru/ulstu/controller/AdviceController.java @@ -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 handleTimeSeriesValidateException(Throwable e) { + e.printStackTrace(); return handleException(ErrorConstants.TIME_SERIES_VALIDATE_ERROR, e.getMessage()); } @ExceptionHandler(ForecastValidateException.class) public ResponseExtended handleForecastValidateException(Throwable e) { + e.printStackTrace(); return handleException(ErrorConstants.FORECAST_PARAMS_ERROR, e.getMessage()); } + + @ExceptionHandler(ModelingException.class) + public ResponseExtended handleModelingException(Throwable e) { + e.printStackTrace(); + return handleException(ErrorConstants.MODELING_ERROR, e.getMessage()); + } } diff --git a/src/main/java/ru/ulstu/datamodel/response/ErrorConstants.java b/src/main/java/ru/ulstu/datamodel/response/ErrorConstants.java index 8672418..9bb21e4 100644 --- a/src/main/java/ru/ulstu/datamodel/response/ErrorConstants.java +++ b/src/main/java/ru/ulstu/datamodel/response/ErrorConstants.java @@ -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; diff --git a/src/main/java/ru/ulstu/method/Method.java b/src/main/java/ru/ulstu/method/Method.java index 99036c0..aa9f082 100644 --- a/src/main/java/ru/ulstu/method/Method.java +++ b/src/main/java/ru/ulstu/method/Method.java @@ -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 parameters) { try { ValidationUtils.validateTimeSeries(timeSeries); diff --git a/src/main/java/ru/ulstu/service/MethodParamBruteForce.java b/src/main/java/ru/ulstu/service/MethodParamBruteForce.java index 348d63d..f065f8f 100644 --- a/src/main/java/ru/ulstu/service/MethodParamBruteForce.java +++ b/src/main/java/ru/ulstu/service/MethodParamBruteForce.java @@ -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 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 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("Лучший метод не найден"));