From 3391112e40f22b3bfd6f067700d31b7c0d33a8d3 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 16 Apr 2022 10:53:16 +0400 Subject: [PATCH] #1 -- show available time series methods --- .../ru/ulstu/controller/IndexController.java | 2 +- .../controller/TimeSeriesController.java | 32 ++++++++++++++----- src/main/java/ru/ulstu/method/Method.java | 5 ++- .../ulstu/service/MethodParamBruteForce.java | 6 +++- .../ru/ulstu/service/TimeSeriesService.java | 10 ++++-- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/main/java/ru/ulstu/controller/IndexController.java b/src/main/java/ru/ulstu/controller/IndexController.java index be301fa..b0dc6fd 100644 --- a/src/main/java/ru/ulstu/controller/IndexController.java +++ b/src/main/java/ru/ulstu/controller/IndexController.java @@ -63,7 +63,7 @@ public class IndexController { 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); + TimeSeries timeSeriesModel = timeSeriesService.smoothTimeSeries(timeSeries).getTimeSeries(); ModelingResult modelingResult = timeSeriesService.getForecast(timeSeries, countForecastPoints); TimeSeries forecast = modelingResult.getTimeSeries(); TimeSeries testForecast = modelingResult.getTestForecast(); diff --git a/src/main/java/ru/ulstu/controller/TimeSeriesController.java b/src/main/java/ru/ulstu/controller/TimeSeriesController.java index 0d4d6bf..b4cdcb9 100644 --- a/src/main/java/ru/ulstu/controller/TimeSeriesController.java +++ b/src/main/java/ru/ulstu/controller/TimeSeriesController.java @@ -11,6 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -21,11 +22,12 @@ import ru.ulstu.datamodel.ForecastParams; import ru.ulstu.datamodel.ModelingResult; import ru.ulstu.datamodel.exception.ModelingException; import ru.ulstu.datamodel.ts.TimeSeries; -import ru.ulstu.service.MethodParamBruteForce; +import ru.ulstu.method.Method; import ru.ulstu.service.TimeSeriesService; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.InvocationTargetException; +import java.util.List; import java.util.concurrent.ExecutionException; @RestController @@ -34,16 +36,13 @@ public class TimeSeriesController { private final static Logger LOGGER = LoggerFactory.getLogger(TimeSeriesController.class); private final TimeSeriesService timeSeriesService; - private final MethodParamBruteForce methodParamBruteForce; - public TimeSeriesController(TimeSeriesService timeSeriesService, - MethodParamBruteForce methodParamBruteForce) { + public TimeSeriesController(TimeSeriesService timeSeriesService) { this.timeSeriesService = timeSeriesService; - this.methodParamBruteForce = methodParamBruteForce; } @PostMapping("getForecast") - @Operation(description = "Получить прогноз временного ряда") + @Operation(description = "Получить прогноз временного ряда любым методом") public ResponseEntity getForecastTimeSeries(@RequestBody ForecastParams forecastParams, HttpServletRequest request) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException { LOGGER.info("User ip: " + HttpUtils.getUserIp(request)); LOGGER.info("Forecast: " + forecastParams); @@ -54,12 +53,29 @@ public class TimeSeriesController { } @PostMapping("getSmoothed") - @Operation(description = "Получить сглаженный временной ряд") + @Operation(description = "Получить сглаженный временной ряд любым методом") public ResponseEntity getSmoothedTimeSeries(@RequestBody TimeSeries timeSeries, HttpServletRequest request) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { LOGGER.info("User ip: " + HttpUtils.getUserIp(request)); LOGGER.info("Time series for smoothing: " + timeSeries); - ResponseEntity result = new ResponseEntity<>(methodParamBruteForce.getSmoothedTimeSeries(timeSeries), HttpStatus.OK); + ResponseEntity result = new ResponseEntity<>(timeSeriesService.smoothTimeSeries(timeSeries), HttpStatus.OK); LOGGER.info("Smoothing complete"); return result; } + + @PostMapping("getSpecificMethodForecast") + @Operation(description = "Получить прогноз временного ряда указанным методом") + public ResponseEntity getForecastTimeSeriesSpecificMethod(@RequestBody ForecastParams forecastParams, HttpServletRequest request) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException { + LOGGER.info("User ip: " + HttpUtils.getUserIp(request)); + LOGGER.info("Forecast: " + forecastParams); + ResponseEntity result = new ResponseEntity<>(timeSeriesService.getForecast(forecastParams.getOriginalTimeSeries(), + forecastParams.getCountForecast()), HttpStatus.OK); + LOGGER.info("Forecast result complete"); + return result; + } + + @GetMapping("availableMethods") + @Operation(description = "Получить список доступных методов моделирования") + public ResponseEntity> getAvailableMethods() { + return new ResponseEntity<>(timeSeriesService.getAvailableMethods(), HttpStatus.OK); + } } diff --git a/src/main/java/ru/ulstu/method/Method.java b/src/main/java/ru/ulstu/method/Method.java index e1e33ab..3be54a6 100644 --- a/src/main/java/ru/ulstu/method/Method.java +++ b/src/main/java/ru/ulstu/method/Method.java @@ -116,10 +116,13 @@ public abstract class Method { } @Override - //@JsonProperty("name") public String toString() { return getName(); } + public String getId() { + return getClass().getSimpleName(); + } + public abstract String getName(); } diff --git a/src/main/java/ru/ulstu/service/MethodParamBruteForce.java b/src/main/java/ru/ulstu/service/MethodParamBruteForce.java index f528aff..6688440 100644 --- a/src/main/java/ru/ulstu/service/MethodParamBruteForce.java +++ b/src/main/java/ru/ulstu/service/MethodParamBruteForce.java @@ -33,7 +33,7 @@ import java.util.concurrent.Future; import java.util.stream.Collectors; @Service -public class MethodParamBruteForce { +class MethodParamBruteForce { private final int DEFAULT_THREAD_COUNT = 50; private final List methods; private final ScoreMethod scoreMethod = new Smape(); @@ -178,4 +178,8 @@ public class MethodParamBruteForce { } return true; } + + public List getAvailableMethods() { + return methods; + } } diff --git a/src/main/java/ru/ulstu/service/TimeSeriesService.java b/src/main/java/ru/ulstu/service/TimeSeriesService.java index f548bf0..1e6d84e 100644 --- a/src/main/java/ru/ulstu/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/service/TimeSeriesService.java @@ -10,8 +10,10 @@ import org.springframework.stereotype.Service; import ru.ulstu.datamodel.ModelingResult; import ru.ulstu.datamodel.exception.ModelingException; import ru.ulstu.datamodel.ts.TimeSeries; +import ru.ulstu.method.Method; import java.lang.reflect.InvocationTargetException; +import java.util.List; import java.util.concurrent.ExecutionException; @@ -27,7 +29,11 @@ public class TimeSeriesService { return methodParamBruteForce.getForecast(timeSeries, countPoints); } - public TimeSeries smoothTimeSeries(TimeSeries timeSeries) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { - return methodParamBruteForce.getSmoothedTimeSeries(timeSeries).getTimeSeries(); + public ModelingResult smoothTimeSeries(TimeSeries timeSeries) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + return methodParamBruteForce.getSmoothedTimeSeries(timeSeries); + } + + public List getAvailableMethods() { + return methodParamBruteForce.getAvailableMethods(); } }