split into two services

This commit is contained in:
Anton Romanov 2021-05-27 14:25:00 +04:00
parent b182181780
commit 8bfb0b854f
5 changed files with 110 additions and 71 deletions

View File

@ -3,11 +3,9 @@ package ru.ulstu.controllers;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; 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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.configuration.ApiConfiguration; import ru.ulstu.configuration.ApiConfiguration;
import ru.ulstu.models.ForecastParams; import ru.ulstu.models.ForecastParams;
@ -25,30 +23,6 @@ public class TimeSeriesController {
this.timeSeriesService = timeSeriesService; this.timeSeriesService = timeSeriesService;
} }
@GetMapping("isAlive")
@ApiOperation("Проверка сервиса")
public ResponseEntity<Boolean> isAlive() {
return new ResponseEntity<>(true, HttpStatus.OK);
}
@GetMapping("getRandom")
@ApiOperation("Получить временной ряд рандомной длины")
public ResponseEntity<TimeSeries> getRandomTimeSeries(@RequestParam("length") int length) {
return new ResponseEntity<>(timeSeriesService.getRandomTimeSeries(length), HttpStatus.OK);
}
@GetMapping("getFromString")
@ApiOperation("Преобразовать строку с разделителями во временной ряд")
public ResponseEntity<TimeSeries> getTimeSeriesFromString(@RequestParam("tsString") String tsString) {
return new ResponseEntity<>(timeSeriesService.getTimeSeriesFromString(tsString), HttpStatus.OK);
}
@PostMapping("timeSeriesToString")
@ApiOperation("Преобразовать временной ряд в строку с разделителями")
public ResponseEntity<String> getTimeSeriesToString(@RequestBody TimeSeries timeSeries) {
return new ResponseEntity<>(timeSeriesService.getTimeSeriesToString(timeSeries), HttpStatus.OK);
}
@PostMapping("getForecast") @PostMapping("getForecast")
@ApiOperation("Получить прогноз временного ряда") @ApiOperation("Получить прогноз временного ряда")
public ResponseEntity<TimeSeries> getForecastTimeSeries(@RequestBody ForecastParams forecastParams) throws ModelingException { public ResponseEntity<TimeSeries> getForecastTimeSeries(@RequestBody ForecastParams forecastParams) throws ModelingException {

View File

@ -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<Boolean> isAlive() {
return new ResponseEntity<>(true, HttpStatus.OK);
}
@GetMapping("getRandom")
@ApiOperation("Получить временной ряд рандомной длины")
public ResponseEntity<TimeSeries> getRandomTimeSeries(@RequestParam("length") int length) {
return new ResponseEntity<>(utilService.getRandomTimeSeries(length), HttpStatus.OK);
}
@GetMapping("getFromString")
@ApiOperation("Преобразовать строку с разделителями во временной ряд")
public ResponseEntity<TimeSeries> getTimeSeriesFromString(@RequestParam("tsString") String tsString) {
return new ResponseEntity<>(utilService.getTimeSeriesFromString(tsString), HttpStatus.OK);
}
@PostMapping("timeSeriesToString")
@ApiOperation("Преобразовать временной ряд в строку с разделителями")
public ResponseEntity<String> getTimeSeriesToString(@RequestBody TimeSeries timeSeries) {
return new ResponseEntity<>(utilService.getTimeSeriesToString(timeSeries), HttpStatus.OK);
}
}

View File

@ -12,6 +12,7 @@ import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.TimeSeriesValue; import ru.ulstu.models.TimeSeriesValue;
import ru.ulstu.models.exceptions.ModelingException; import ru.ulstu.models.exceptions.ModelingException;
import ru.ulstu.services.TimeSeriesService; import ru.ulstu.services.TimeSeriesService;
import ru.ulstu.services.UtilService;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped; import javax.faces.view.ViewScoped;
@ -29,6 +30,9 @@ public class IndexView implements Serializable {
@Autowired @Autowired
private transient TimeSeriesService timeSeriesService; private transient TimeSeriesService timeSeriesService;
@Autowired
private transient UtilService utilService;
@PostConstruct @PostConstruct
public void init() { public void init() {
createChart(); createChart();
@ -40,7 +44,7 @@ public class IndexView implements Serializable {
LineChartSeries series1 = new LineChartSeries(); LineChartSeries series1 = new LineChartSeries();
series1.setLabel("Временной ряд"); series1.setLabel("Временной ряд");
TimeSeries timeSeries = timeSeriesService.getTimeSeriesFromString(timeSeriesString); TimeSeries timeSeries = utilService.getTimeSeriesFromString(timeSeriesString);
for (TimeSeriesValue value : timeSeries.getValues()) { for (TimeSeriesValue value : timeSeries.getValues()) {
series1.set(DateTimeFormatter.ISO_LOCAL_DATE.format(value.getDate()), value.getValue()); series1.set(DateTimeFormatter.ISO_LOCAL_DATE.format(value.getDate()), value.getValue());
} }

View File

@ -3,9 +3,7 @@ package ru.ulstu.services;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.TimeSeriesUtils;
import ru.ulstu.models.TimeSeries; import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.TimeSeriesValue;
import ru.ulstu.models.exceptions.ModelingException; import ru.ulstu.models.exceptions.ModelingException;
import ru.ulstu.tsMethods.TimeSeriesMethod; import ru.ulstu.tsMethods.TimeSeriesMethod;
import ru.ulstu.tsMethods.exponential.AddTrendAddSeason; 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.Season;
import ru.ulstu.tsMethods.exponential.param.TimeSeriesMethodParamValue; 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 @Service
public class TimeSeriesService { public class TimeSeriesService {
private static final Logger LOGGER = LoggerFactory.getLogger(TimeSeriesService.class); 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 { public TimeSeries getForecast(TimeSeries timeSeries, int countPoints) throws ModelingException {
TimeSeriesMethod method; TimeSeriesMethod method;
method = new NoTrendNoSeason(timeSeries, new TimeSeriesMethodParamValue<>(new Alpha(), 0.8)); method = new NoTrendNoSeason(timeSeries, new TimeSeriesMethodParamValue<>(new Alpha(), 0.8));
@ -58,31 +41,4 @@ public class TimeSeriesService {
new TimeSeriesMethodParamValue<>(new Season(), 17)); new TimeSeriesMethodParamValue<>(new Season(), 17));
return method.getModel(); return method.getModel();
} }
public TimeSeries getTimeSeriesFromString(String tsString) {
List<TimeSeriesValue> tsValues = Arrays.stream(tsString.split("\n"))
.flatMap(v -> Arrays.stream(v.split(";")))
.flatMap(v -> Arrays.stream(v.split(",")))
.flatMap(v -> Arrays.stream(v.split("<br>")))
.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(";"));
}
} }

View File

@ -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<TimeSeriesValue> tsValues = Arrays.stream(tsString.split("\n"))
.flatMap(v -> Arrays.stream(v.split(";")))
.flatMap(v -> Arrays.stream(v.split(",")))
.flatMap(v -> Arrays.stream(v.split("<br>")))
.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(";"));
}
}