From b3ab0496f8182c805c941dc245a91517b5c203c9 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 25 Feb 2023 14:12:44 +0400 Subject: [PATCH] group time series tendencies --- .../controller/TimeSeriesController.java | 7 ++++++ .../ru/ulstu/service/TimeSeriesService.java | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/main/java/ru/ulstu/controller/TimeSeriesController.java b/src/main/java/ru/ulstu/controller/TimeSeriesController.java index a6c6cd0..eef1580 100644 --- a/src/main/java/ru/ulstu/controller/TimeSeriesController.java +++ b/src/main/java/ru/ulstu/controller/TimeSeriesController.java @@ -85,4 +85,11 @@ public class TimeSeriesController { public ResponseEntity> getAvailableMethods() { return new ResponseEntity<>(timeSeriesService.getAvailableMethods(), HttpStatus.OK); } + + @PostMapping("getGroupedTendencies") + @Operation(description = "Получить список сгруппированных тенденций") + public ResponseEntity getGroupedTendencies(@RequestBody TimeSeries timeSeries) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + return new ResponseEntity<>(timeSeriesService.getGroupedTendencies(timeSeries), HttpStatus.OK); + } + } diff --git a/src/main/java/ru/ulstu/service/TimeSeriesService.java b/src/main/java/ru/ulstu/service/TimeSeriesService.java index 525eb79..10c7eea 100644 --- a/src/main/java/ru/ulstu/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/service/TimeSeriesService.java @@ -42,4 +42,29 @@ public class TimeSeriesService { public List getAvailableMethods() { return methodParamBruteForce.getAvailableMethods(); } + + public TimeSeries getGroupedTendencies(TimeSeries timeSeries) throws ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + timeSeries = smoothTimeSeries(timeSeries, "FTransform").getTimeSeries(); + int i = 2; + double prevDiff = timeSeries.getNumericValue(1) - + timeSeries.getNumericValue(0); + while (i < timeSeries.getLength()) { + double diff = timeSeries.getNumericValue(i) - + timeSeries.getNumericValue(i - 1); + //если тенденция сохранилась + if (tsTendencyNotChanged(diff, prevDiff)) { + timeSeries.getValues().remove(i - 1); + } else { + i++; + } + prevDiff = diff; + } + return timeSeries; + } + + private boolean tsTendencyNotChanged(double diff, double prevDiff) { + return (diff > 0 && prevDiff > 0) + || ((diff < 0 && prevDiff < 0) + || ((diff == 0 && prevDiff == 0))); + } }