From 704366f11ada7b4bf027e18e56b1390f9db1dac9 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 27 Feb 2023 23:35:31 +0400 Subject: [PATCH] #91 -- Group list of ts tendencies --- .../ru/ulstu/extractor/http/HttpService.java | 20 +++++++++++++++++++ .../TimeSeriesMarkupController.java | 2 +- .../ts/service/TimeSeriesService.java | 19 +++++++++--------- src/main/resources/templates/markup.html | 2 +- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/http/HttpService.java b/src/main/java/ru/ulstu/extractor/http/HttpService.java index 1e53488..97dd0df 100644 --- a/src/main/java/ru/ulstu/extractor/http/HttpService.java +++ b/src/main/java/ru/ulstu/extractor/http/HttpService.java @@ -41,6 +41,26 @@ public class HttpService { return response; } + public JSONArray post(String url, JSONArray postData) { + log.debug("Service call: {}", url); + JSONArray response = null; + try { + response = new JSONArray(Optional.ofNullable(client + .post() + .uri(url) + .contentType(MediaType.APPLICATION_JSON) + .body(BodyInserters.fromValue(postData.toString())) + .accept(MediaType.APPLICATION_JSON) + .retrieve() + .bodyToMono(String.class) + .toFuture().get()).orElse("[{response:\"empty\"}]")); + } catch (Exception e) { + return new JSONArray("[{response:\"empty\"}]"); + } + log.debug("Service response: {}", response); + return response; + } + public JSONArray get(String url) { log.debug("Service call: {}", url); try { diff --git a/src/main/java/ru/ulstu/extractor/ts/controller/TimeSeriesMarkupController.java b/src/main/java/ru/ulstu/extractor/ts/controller/TimeSeriesMarkupController.java index 32674c5..fbe7493 100644 --- a/src/main/java/ru/ulstu/extractor/ts/controller/TimeSeriesMarkupController.java +++ b/src/main/java/ru/ulstu/extractor/ts/controller/TimeSeriesMarkupController.java @@ -22,7 +22,7 @@ public class TimeSeriesMarkupController { public String markupTs(Model model) { List ts = timeSeriesService.getAllTimeSeries(); model.addAttribute("ts", ts); - model.addAttribute("fts", timeSeriesService.getGroupedTendencies(ts.get(0))); + model.addAttribute("groupedTs", timeSeriesService.getGroupedTendencies(ts)); return "markup"; } } diff --git a/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java index 1cdf4cc..dac66fe 100644 --- a/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java @@ -20,12 +20,14 @@ import ru.ulstu.extractor.ts.model.TimeSeriesValue; import ru.ulstu.extractor.ts.repository.TimeSeriesRepository; import ru.ulstu.extractor.ts.repository.TimeSeriesValueRepository; import ru.ulstu.extractor.ts.util.TimeSeriesDateMapper; +import ru.ulstu.extractor.util.JsonUtils; import javax.transaction.Transactional; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; @Service public class TimeSeriesService { @@ -138,15 +140,12 @@ public class TimeSeriesService { return Optional.empty(); } - public Optional getGroupedTendencies(TimeSeries ts) { - if (ts != null && ts.getValues().size() > 5) { - JSONObject response = httpService.post(TIME_SERIES_GROUPED_TENDENCIES_URL, new JSONObject(new JsonTimeSeries(ts))); - LOG.debug("Send to group time series tendencies"); - if (response.has("response") && response.getString("response").equals("empty")) { - return Optional.empty(); - } - return Optional.of(new TimeSeries(response)); - } - return Optional.empty(); + public List getGroupedTendencies(List tsList) { + JSONArray response = httpService.post(TIME_SERIES_GROUPED_TENDENCIES_URL, new JSONArray(tsList.stream().map(JsonTimeSeries::new).collect(Collectors.toList()))); + LOG.debug("Send to group time series tendencies"); + return JsonUtils.getListOfObjects(response) + .stream() + .map(TimeSeries::new) + .collect(Collectors.toList()); } } diff --git a/src/main/resources/templates/markup.html b/src/main/resources/templates/markup.html index 329d010..533e30a 100644 --- a/src/main/resources/templates/markup.html +++ b/src/main/resources/templates/markup.html @@ -14,7 +14,7 @@ - +