From cefc1542c359a1b6898c5d667242666ee42c612d Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 25 Nov 2024 10:27:13 +0400 Subject: [PATCH] #12 -- Divide TS presentation and forecasting --- .../ru/ulstu/controller/IndexController.java | 92 ++++++++++--------- .../java/ru/ulstu/datamodel/ChartForm.java | 10 ++ src/main/resources/templates/index.html | 5 +- src/main/resources/templates/method.html | 9 +- 4 files changed, 71 insertions(+), 45 deletions(-) diff --git a/src/main/java/ru/ulstu/controller/IndexController.java b/src/main/java/ru/ulstu/controller/IndexController.java index 11a630a..6a00753 100644 --- a/src/main/java/ru/ulstu/controller/IndexController.java +++ b/src/main/java/ru/ulstu/controller/IndexController.java @@ -54,53 +54,60 @@ public class IndexController { if (chartForm.getTimeSeriesMeta() != null && chartForm.getTimeSeriesMeta().getKey() != null && !chartForm.getTimeSeriesMeta().getKey().isEmpty()) { - addChartToModel(dbService.getTimeSeries(chartForm.getSet(), chartForm.getTimeSeriesMeta().getKey()), null, model); + addChartToModel(dbService.getTimeSeries(chartForm.getSet(), chartForm.getTimeSeriesMeta().getKey()), + null, + chartForm.isNeedForecast(), + model); } return "index"; } - private void addChartToModel(TimeSeries timeSeries, String method, Model model) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException { - int countForecastPoints = timeSeries.getLength() > 20 ? 10 : timeSeries.getLength() / 3; - TimeSeries timeSeriesModel; - ModelingResult modelingResult; - if (method == null) { - timeSeriesModel = timeSeriesService.smoothTimeSeries(timeSeries).getTimeSeries(); - modelingResult = timeSeriesService.getForecast(timeSeries, countForecastPoints); - } else { - timeSeriesModel = timeSeriesService.smoothTimeSeries(timeSeries, method).getTimeSeries(); - modelingResult = timeSeriesService.getForecast(timeSeries, method, countForecastPoints); - } - TimeSeries forecast = modelingResult.getTimeSeries(); - TimeSeries testForecast = modelingResult.getTestForecast(); - model.addAttribute("dates", getDatesForChart(timeSeries, forecast)); - model.addAttribute("timeSeries", timeSeries.getValues().stream().map(TimeSeriesValue::getValue).toArray()); - // если временной ряд был сжат моделью, то для графика нужно вставить пустые значения - TimeSeries modelWithSkips = new TimeSeries(timeSeriesModel.getKey()); - int j = 0; - for (int i = 0; i < timeSeries.getLength(); i++) { - if (timeSeries.getValue(i).getDate().equals(timeSeriesModel.getValue(j).getDate())) { - modelWithSkips.addValue(timeSeriesModel.getValue(j)); - j++; + private void addChartToModel(TimeSeries timeSeries, String method, boolean needForecast, Model model) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException { + if (needForecast) { + int countForecastPoints = timeSeries.getLength() > 20 ? 10 : timeSeries.getLength() / 3; + TimeSeries timeSeriesModel; + ModelingResult modelingResult; + if (method == null) { + timeSeriesModel = timeSeriesService.smoothTimeSeries(timeSeries).getTimeSeries(); + modelingResult = timeSeriesService.getForecast(timeSeries, countForecastPoints); } else { - modelWithSkips.addValue(new TimeSeriesValue((Double) null)); + timeSeriesModel = timeSeriesService.smoothTimeSeries(timeSeries, method).getTimeSeries(); + modelingResult = timeSeriesService.getForecast(timeSeries, method, countForecastPoints); } + TimeSeries forecast = modelingResult.getTimeSeries(); + TimeSeries testForecast = modelingResult.getTestForecast(); + // если временной ряд был сжат моделью, то для графика нужно вставить пустые значения + TimeSeries modelWithSkips = new TimeSeries(timeSeriesModel.getKey()); + int j = 0; + for (int i = 0; i < timeSeries.getLength(); i++) { + if (timeSeries.getValue(i).getDate().equals(timeSeriesModel.getValue(j).getDate())) { + modelWithSkips.addValue(timeSeriesModel.getValue(j)); + j++; + } else { + modelWithSkips.addValue(new TimeSeriesValue((Double) null)); + } + } + model.addAttribute("model", modelWithSkips.getValues().stream().map(TimeSeriesValue::getValue).toArray()); + timeSeries.getValues().remove(timeSeries.getValues().size() - 1); + + List forecastValues = timeSeries.getValues().stream().map(v -> (Double) null).collect(Collectors.toList()); + forecastValues.addAll(forecast.getValues().stream().map(TimeSeriesValue::getValue).collect(Collectors.toList())); + model.addAttribute("forecast", forecastValues.toArray()); + + List testForecastValues = timeSeries.getValues() + .stream() + .skip(countForecastPoints) + .map(v -> (Double) null) + .collect(Collectors.toList()); + testForecastValues.addAll(testForecast.getValues().stream().map(TimeSeriesValue::getValue).collect(Collectors.toList())); + model.addAttribute("testForecast", testForecastValues.toArray()); + model.addAttribute("forecastDescription", modelingResult); + model.addAttribute("statistic", statisticService.getStatistic(timeSeries)); + model.addAttribute("dates", getDatesForChart(timeSeries, forecast)); + } else { + model.addAttribute("dates", getDatesForChart(timeSeries, new TimeSeries())); } - model.addAttribute("model", modelWithSkips.getValues().stream().map(TimeSeriesValue::getValue).toArray()); - timeSeries.getValues().remove(timeSeries.getValues().size() - 1); - - List forecastValues = timeSeries.getValues().stream().map(v -> (Double) null).collect(Collectors.toList()); - forecastValues.addAll(forecast.getValues().stream().map(TimeSeriesValue::getValue).collect(Collectors.toList())); - model.addAttribute("forecast", forecastValues.toArray()); - - List testForecastValues = timeSeries.getValues() - .stream() - .skip(countForecastPoints) - .map(v -> (Double) null) - .collect(Collectors.toList()); - testForecastValues.addAll(testForecast.getValues().stream().map(TimeSeriesValue::getValue).collect(Collectors.toList())); - model.addAttribute("testForecast", testForecastValues.toArray()); - model.addAttribute("forecastDescription", modelingResult); - model.addAttribute("statistic", statisticService.getStatistic(timeSeries)); + model.addAttribute("timeSeries", timeSeries.getValues().stream().map(TimeSeriesValue::getValue).toArray()); } private List getDatesForChart(TimeSeries timeSeries, TimeSeries forecast) { @@ -132,7 +139,10 @@ public class IndexController { && !chartForm.getTimeSeriesMeta().getKey().isEmpty() && chartForm.getMethodClassName() != null && !chartForm.getMethodClassName().equals("")) { - addChartToModel(dbService.getTimeSeries(chartForm.getSet(), chartForm.getTimeSeriesMeta().getKey()), chartForm.getMethodClassName(), model); + addChartToModel(dbService.getTimeSeries(chartForm.getSet(), chartForm.getTimeSeriesMeta().getKey()), + chartForm.getMethodClassName(), + chartForm.isNeedForecast(), + model); } return "method"; } diff --git a/src/main/java/ru/ulstu/datamodel/ChartForm.java b/src/main/java/ru/ulstu/datamodel/ChartForm.java index eeeed6f..11956d4 100644 --- a/src/main/java/ru/ulstu/datamodel/ChartForm.java +++ b/src/main/java/ru/ulstu/datamodel/ChartForm.java @@ -8,6 +8,8 @@ public class ChartForm { private TimeSeriesMeta timeSeriesMeta; private String methodClassName = null; + private boolean needForecast; + public TimeSeriesSet getSet() { return set; } @@ -31,4 +33,12 @@ public class ChartForm { public void setMethodClassName(String methodClassName) { this.methodClassName = methodClassName; } + + public boolean isNeedForecast() { + return needForecast; + } + + public void setNeedForecast(boolean needForecast) { + this.needForecast = needForecast; + } } diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 5f5fae8..ce192a0 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -123,7 +123,10 @@ $('#select-ts').val([[*{timeSeriesMeta.key}]]); $('#select-ts').selectpicker('refresh'); - + +
Результаты моделирования:
diff --git a/src/main/resources/templates/method.html b/src/main/resources/templates/method.html index bdf89a4..244bc9a 100644 --- a/src/main/resources/templates/method.html +++ b/src/main/resources/templates/method.html @@ -120,7 +120,7 @@ +

Метод прогнозирования:

Оценка: