From 8bfb0b854f9ebb363c4ec8974a81f9153683e9e5 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 27 May 2021 14:25:00 +0400 Subject: [PATCH] split into two services --- .../controllers/TimeSeriesController.java | 26 --------- .../ru/ulstu/controllers/UtilController.java | 49 ++++++++++++++++ src/main/java/ru/ulstu/pages/IndexView.java | 6 +- .../ru/ulstu/services/TimeSeriesService.java | 44 --------------- .../java/ru/ulstu/services/UtilService.java | 56 +++++++++++++++++++ 5 files changed, 110 insertions(+), 71 deletions(-) create mode 100644 src/main/java/ru/ulstu/controllers/UtilController.java create mode 100644 src/main/java/ru/ulstu/services/UtilService.java diff --git a/src/main/java/ru/ulstu/controllers/TimeSeriesController.java b/src/main/java/ru/ulstu/controllers/TimeSeriesController.java index 8d500b3..889a9ee 100644 --- a/src/main/java/ru/ulstu/controllers/TimeSeriesController.java +++ b/src/main/java/ru/ulstu/controllers/TimeSeriesController.java @@ -3,11 +3,9 @@ package ru.ulstu.controllers; import io.swagger.annotations.ApiOperation; 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; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import ru.ulstu.configuration.ApiConfiguration; import ru.ulstu.models.ForecastParams; @@ -25,30 +23,6 @@ public class TimeSeriesController { this.timeSeriesService = timeSeriesService; } - @GetMapping("isAlive") - @ApiOperation("Проверка сервиса") - public ResponseEntity isAlive() { - return new ResponseEntity<>(true, HttpStatus.OK); - } - - @GetMapping("getRandom") - @ApiOperation("Получить временной ряд рандомной длины") - public ResponseEntity getRandomTimeSeries(@RequestParam("length") int length) { - return new ResponseEntity<>(timeSeriesService.getRandomTimeSeries(length), HttpStatus.OK); - } - - @GetMapping("getFromString") - @ApiOperation("Преобразовать строку с разделителями во временной ряд") - public ResponseEntity getTimeSeriesFromString(@RequestParam("tsString") String tsString) { - return new ResponseEntity<>(timeSeriesService.getTimeSeriesFromString(tsString), HttpStatus.OK); - } - - @PostMapping("timeSeriesToString") - @ApiOperation("Преобразовать временной ряд в строку с разделителями") - public ResponseEntity getTimeSeriesToString(@RequestBody TimeSeries timeSeries) { - return new ResponseEntity<>(timeSeriesService.getTimeSeriesToString(timeSeries), HttpStatus.OK); - } - @PostMapping("getForecast") @ApiOperation("Получить прогноз временного ряда") public ResponseEntity getForecastTimeSeries(@RequestBody ForecastParams forecastParams) throws ModelingException { diff --git a/src/main/java/ru/ulstu/controllers/UtilController.java b/src/main/java/ru/ulstu/controllers/UtilController.java new file mode 100644 index 0000000..3be9f09 --- /dev/null +++ b/src/main/java/ru/ulstu/controllers/UtilController.java @@ -0,0 +1,49 @@ +package ru.ulstu.controllers; + +import io.swagger.annotations.ApiOperation; +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; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.configuration.ApiConfiguration; +import ru.ulstu.models.TimeSeries; +import ru.ulstu.services.UtilService; + +@RestController +@RequestMapping(ApiConfiguration.API_1_0) +public class UtilController { + + private final UtilService utilService; + + public UtilController(UtilService utilService) { + this.utilService = utilService; + } + + @GetMapping("isAlive") + @ApiOperation("Проверка сервиса") + public ResponseEntity isAlive() { + return new ResponseEntity<>(true, HttpStatus.OK); + } + + @GetMapping("getRandom") + @ApiOperation("Получить временной ряд рандомной длины") + public ResponseEntity getRandomTimeSeries(@RequestParam("length") int length) { + return new ResponseEntity<>(utilService.getRandomTimeSeries(length), HttpStatus.OK); + } + + @GetMapping("getFromString") + @ApiOperation("Преобразовать строку с разделителями во временной ряд") + public ResponseEntity getTimeSeriesFromString(@RequestParam("tsString") String tsString) { + return new ResponseEntity<>(utilService.getTimeSeriesFromString(tsString), HttpStatus.OK); + } + + @PostMapping("timeSeriesToString") + @ApiOperation("Преобразовать временной ряд в строку с разделителями") + public ResponseEntity getTimeSeriesToString(@RequestBody TimeSeries timeSeries) { + return new ResponseEntity<>(utilService.getTimeSeriesToString(timeSeries), HttpStatus.OK); + } +} diff --git a/src/main/java/ru/ulstu/pages/IndexView.java b/src/main/java/ru/ulstu/pages/IndexView.java index 0038bbd..dfffcd4 100644 --- a/src/main/java/ru/ulstu/pages/IndexView.java +++ b/src/main/java/ru/ulstu/pages/IndexView.java @@ -12,6 +12,7 @@ import ru.ulstu.models.TimeSeries; import ru.ulstu.models.TimeSeriesValue; import ru.ulstu.models.exceptions.ModelingException; import ru.ulstu.services.TimeSeriesService; +import ru.ulstu.services.UtilService; import javax.annotation.PostConstruct; import javax.faces.view.ViewScoped; @@ -29,6 +30,9 @@ public class IndexView implements Serializable { @Autowired private transient TimeSeriesService timeSeriesService; + @Autowired + private transient UtilService utilService; + @PostConstruct public void init() { createChart(); @@ -40,7 +44,7 @@ public class IndexView implements Serializable { LineChartSeries series1 = new LineChartSeries(); series1.setLabel("Временной ряд"); - TimeSeries timeSeries = timeSeriesService.getTimeSeriesFromString(timeSeriesString); + TimeSeries timeSeries = utilService.getTimeSeriesFromString(timeSeriesString); for (TimeSeriesValue value : timeSeries.getValues()) { series1.set(DateTimeFormatter.ISO_LOCAL_DATE.format(value.getDate()), value.getValue()); } diff --git a/src/main/java/ru/ulstu/services/TimeSeriesService.java b/src/main/java/ru/ulstu/services/TimeSeriesService.java index 3c87442..4d5578f 100644 --- a/src/main/java/ru/ulstu/services/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/services/TimeSeriesService.java @@ -3,9 +3,7 @@ package ru.ulstu.services; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import ru.ulstu.TimeSeriesUtils; import ru.ulstu.models.TimeSeries; -import ru.ulstu.models.TimeSeriesValue; import ru.ulstu.models.exceptions.ModelingException; import ru.ulstu.tsMethods.TimeSeriesMethod; import ru.ulstu.tsMethods.exponential.AddTrendAddSeason; @@ -16,26 +14,11 @@ import ru.ulstu.tsMethods.exponential.param.Gamma; import ru.ulstu.tsMethods.exponential.param.Season; import ru.ulstu.tsMethods.exponential.param.TimeSeriesMethodParamValue; -import java.time.LocalDateTime; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - @Service public class TimeSeriesService { private static final Logger LOGGER = LoggerFactory.getLogger(TimeSeriesService.class); - public TimeSeries getRandomTimeSeries(int length) { - TimeSeries ts = new TimeSeries("Random time series"); - LocalDateTime dateStart = LocalDateTime.now().minusDays(length); - for (int i = 0; i < length; i++) { - ts.getValues().add(new TimeSeriesValue(dateStart, Math.random())); - dateStart = dateStart.plusDays(1); - } - return ts; - } - public TimeSeries getForecast(TimeSeries timeSeries, int countPoints) throws ModelingException { TimeSeriesMethod method; method = new NoTrendNoSeason(timeSeries, new TimeSeriesMethodParamValue<>(new Alpha(), 0.8)); @@ -58,31 +41,4 @@ public class TimeSeriesService { new TimeSeriesMethodParamValue<>(new Season(), 17)); return method.getModel(); } - - public TimeSeries getTimeSeriesFromString(String tsString) { - List tsValues = Arrays.stream(tsString.split("\n")) - .flatMap(v -> Arrays.stream(v.split(";"))) - .flatMap(v -> Arrays.stream(v.split(","))) - .flatMap(v -> Arrays.stream(v.split("
"))) - .filter(v -> { - try { - Double.parseDouble(v); - return true; - } catch (NumberFormatException e) { - return false; - } - }) - .map(Double::parseDouble) - .map(TimeSeriesValue::new) - .collect(Collectors.toList()); - return TimeSeriesUtils.fillDates(new TimeSeries(tsValues)); - } - - public String getTimeSeriesToString(TimeSeries timeSeries) { - return timeSeries - .getValues() - .stream() - .map(v -> v.getValue().toString().replace("\\.", ",")) - .collect(Collectors.joining(";")); - } } diff --git a/src/main/java/ru/ulstu/services/UtilService.java b/src/main/java/ru/ulstu/services/UtilService.java new file mode 100644 index 0000000..df78e69 --- /dev/null +++ b/src/main/java/ru/ulstu/services/UtilService.java @@ -0,0 +1,56 @@ +package ru.ulstu.services; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import ru.ulstu.TimeSeriesUtils; +import ru.ulstu.models.TimeSeries; +import ru.ulstu.models.TimeSeriesValue; + +import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + + +@Service +public class UtilService { + private static final Logger LOGGER = LoggerFactory.getLogger(UtilService.class); + + public TimeSeries getRandomTimeSeries(int length) { + TimeSeries ts = new TimeSeries("Random time series"); + LocalDateTime dateStart = LocalDateTime.now().minusDays(length); + for (int i = 0; i < length; i++) { + ts.getValues().add(new TimeSeriesValue(dateStart, Math.random())); + dateStart = dateStart.plusDays(1); + } + return ts; + } + + public TimeSeries getTimeSeriesFromString(String tsString) { + List tsValues = Arrays.stream(tsString.split("\n")) + .flatMap(v -> Arrays.stream(v.split(";"))) + .flatMap(v -> Arrays.stream(v.split(","))) + .flatMap(v -> Arrays.stream(v.split("
"))) + .filter(v -> { + try { + Double.parseDouble(v); + return true; + } catch (NumberFormatException e) { + return false; + } + }) + .map(Double::parseDouble) + .map(TimeSeriesValue::new) + .collect(Collectors.toList()); + return TimeSeriesUtils.fillDates(new TimeSeries(tsValues)); + } + + public String getTimeSeriesToString(TimeSeries timeSeries) { + return timeSeries + .getValues() + .stream() + .map(v -> v.getValue().toString().replace("\\.", ",")) + .collect(Collectors.joining(";")); + } +}