From 9560190368aaa9d5cbe7a944eff5b66cc1502714 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 11 Aug 2021 14:24:45 +0400 Subject: [PATCH] #5 -- fix NPE --- .../ru/ulstu/controller/IndexController.java | 26 +++++++------------ src/main/java/ru/ulstu/score/Smape.java | 2 +- .../ulstu/service/MethodParamBruteForce.java | 10 +++++++ ...A работа по программе.csv | 9 +++++++ ...бота по программе.csv.meta | 1 + 5 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 time-series-db/aviastar/Mazak VCN 410A работа по программе.csv create mode 100644 time-series-db/aviastar/Mazak VCN 410A работа по программе.csv.meta diff --git a/src/main/java/ru/ulstu/controller/IndexController.java b/src/main/java/ru/ulstu/controller/IndexController.java index 09458db..0bc59df 100644 --- a/src/main/java/ru/ulstu/controller/IndexController.java +++ b/src/main/java/ru/ulstu/controller/IndexController.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import ru.ulstu.datamodel.ChartForm; import ru.ulstu.datamodel.ModelingResult; +import ru.ulstu.datamodel.exception.ModelingException; import ru.ulstu.datamodel.ts.TimeSeries; import ru.ulstu.datamodel.ts.TimeSeriesValue; import ru.ulstu.db.DbService; @@ -22,9 +23,11 @@ import ru.ulstu.service.UtilService; import springfox.documentation.annotations.ApiIgnore; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Set; +import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; @Controller @@ -52,7 +55,7 @@ public class IndexController { } @GetMapping("chart") - public String chart(@ModelAttribute ChartForm chartForm, Model model) throws IOException { + public String chart(@ModelAttribute ChartForm chartForm, Model model) throws IOException, ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { model.addAttribute("sets", dbService.getSets()); if (chartForm.getSet() != null) { model.addAttribute("listTimeSeries", dbService.getTimeSeriesMeta(chartForm.getSet())); @@ -65,21 +68,12 @@ public class IndexController { return "index.html"; } - private void addChartToModel(TimeSeries timeSeries, Model model) { - int countForecastPoints = 10; - TimeSeries timeSeriesModel = null; - ModelingResult modelingResult = null; - TimeSeries forecast = null; - TimeSeries testForecast = null; - try { - timeSeriesModel = timeSeriesService.smoothTimeSeries(timeSeries); - modelingResult = timeSeriesService.getForecast(timeSeries, countForecastPoints); - forecast = modelingResult.getTimeSeries(); - testForecast = modelingResult.getTestForecast(); - } catch (Exception e) { - LOG.warn(e.getMessage()); - } - + private void addChartToModel(TimeSeries timeSeries, Model model) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException { + int countForecastPoints = timeSeries.getLength() > 20 ? 10 : timeSeries.getLength() / 3; + TimeSeries timeSeriesModel = timeSeriesService.smoothTimeSeries(timeSeries); + ModelingResult modelingResult = timeSeriesService.getForecast(timeSeries, countForecastPoints); + TimeSeries forecast = modelingResult.getTimeSeries(); + TimeSeries testForecast = modelingResult.getTestForecast(); Set dates = timeSeries.getValues().stream() .map(v -> v.getDate().format(DateTimeFormatter.ISO_DATE)) .collect(Collectors.toSet()); diff --git a/src/main/java/ru/ulstu/score/Smape.java b/src/main/java/ru/ulstu/score/Smape.java index 4b5af9e..e3d9f90 100644 --- a/src/main/java/ru/ulstu/score/Smape.java +++ b/src/main/java/ru/ulstu/score/Smape.java @@ -28,7 +28,7 @@ public class Smape extends ScoreMethod { //double actualValue = getValueOnSameDate(modelValue, original).getValue(); double actualValue = Optional.ofNullable(tsValues.get(modelValue.getDate())) .orElseThrow(() -> new ModelingException("Значение модельного ряда не найдено в оригинальном ряде: " - + modelValue.getDate())); + + modelValue.getDate() + " " + tsValues + " " + model)); sum += abs(modelValue.getValue() - actualValue) / ((abs(actualValue) + abs(modelValue.getValue())) / 2); } diff --git a/src/main/java/ru/ulstu/service/MethodParamBruteForce.java b/src/main/java/ru/ulstu/service/MethodParamBruteForce.java index b78efce..897a788 100644 --- a/src/main/java/ru/ulstu/service/MethodParamBruteForce.java +++ b/src/main/java/ru/ulstu/service/MethodParamBruteForce.java @@ -60,6 +60,7 @@ public class MethodParamBruteForce { if (methodInstance.canMakeForecast(reducedTimeSeries, parametersValues, countPoints)) { results.add(executors.submit(() -> { TimeSeries forecast = methodInstance.getForecast(reducedTimeSeries, parametersValues, countPoints); + forecast = syncDates(forecast, timeSeries); return new ModelingResult(forecast, null, parametersValues, scoreMethod.getScore(tsValues, forecast), @@ -87,6 +88,15 @@ public class MethodParamBruteForce { bestResult.getTimeSeriesMethod()); } + private TimeSeries syncDates(TimeSeries forecast, TimeSeries timeSeries) { + List forecastValues = forecast.getValues(); + for (int i = 1; i <= forecastValues.size(); i++) { + forecastValues.get(forecastValues.size() - i) + .setDate(timeSeries.getValues().get(timeSeries.getValues().size() - i).getDate()); + } + return forecast; + } + public TimeSeries getForecastWithOptimalLength(TimeSeries timeSeries) { throw new RuntimeException("Not implemented"); } diff --git a/time-series-db/aviastar/Mazak VCN 410A работа по программе.csv b/time-series-db/aviastar/Mazak VCN 410A работа по программе.csv new file mode 100644 index 0000000..1f44546 --- /dev/null +++ b/time-series-db/aviastar/Mazak VCN 410A работа по программе.csv @@ -0,0 +1,9 @@ +2021-04-30T09:32:57.120554;37.13 +2021-05-31T09:32:57.120554;39.89 +2021-06-30T09:32:57.120554;41.6 +2021-07-30T09:32:57.120554;35.39 +2021-08-31T09:32:57.120554;33.46 +2021-09-30T09:32:57.120554;45.42 +2021-10-30T09:32:57.120554;41.99 +2021-11-30T09:32:57.120554;40.93 +2021-12-30T09:32:57.120554;36.07 \ No newline at end of file diff --git a/time-series-db/aviastar/Mazak VCN 410A работа по программе.csv.meta b/time-series-db/aviastar/Mazak VCN 410A работа по программе.csv.meta new file mode 100644 index 0000000..afaa5ce --- /dev/null +++ b/time-series-db/aviastar/Mazak VCN 410A работа по программе.csv.meta @@ -0,0 +1 @@ +{"key":"Mazak VCN 410A работа по программе","size":9,"hasDateTime":true} \ No newline at end of file