/* * Copyright (C) 2021 Anton Romanov - All Rights Reserved * You may use, distribute and modify this code, please write to: romanov73@gmail.com. * */ package ru.ulstu.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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; import ru.ulstu.service.TimeSeriesService; 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.concurrent.ExecutionException; import java.util.stream.Collectors; @Controller @ApiIgnore public class IndexController { private final UtilService utilService; private final TimeSeriesService timeSeriesService; private final DbService dbService; private final static Logger LOG = LoggerFactory.getLogger(IndexController.class); public IndexController(UtilService utilService, TimeSeriesService timeSeriesService, DbService dbService) { this.utilService = utilService; this.timeSeriesService = timeSeriesService; this.dbService = dbService; } @GetMapping("/") public String index(Model model) throws IOException { model.addAttribute("sets", dbService.getSets()); model.addAttribute("chartForm", new ChartForm()); return "index.html"; } @GetMapping("chart") 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())); } if (chartForm.getTimeSeriesMeta() != null && chartForm.getTimeSeriesMeta().getKey() != null && !chartForm.getTimeSeriesMeta().getKey().isEmpty()) { addChartToModel(dbService.getTimeSeries(chartForm.getSet(), chartForm.getTimeSeriesMeta().getKey()), model); } return "index.html"; } 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(); List dates = timeSeries.getValues().stream() .map(v -> v.getDate().format(DateTimeFormatter.ISO_DATE)) .distinct() .collect(Collectors.toList()); dates.addAll(forecast.getValues().stream() .map(v -> v.getDate().format(DateTimeFormatter.ISO_DATE)) .collect(Collectors.toSet())); model.addAttribute("dates", dates); model.addAttribute("timeSeries", timeSeries.getValues().stream().map(TimeSeriesValue::getValue).toArray()); model.addAttribute("model", timeSeriesModel.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); } }