From f4600c2ba5d144ca3b7de486fa780f86016df3a0 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 27 Feb 2023 14:47:51 +0400 Subject: [PATCH] #91 -- Group ts tendencies --- .../controller/TimeSeriesMarkupController.java | 1 + .../ulstu/extractor/ts/model/TimeSeries.java | 12 ++++++++++++ .../extractor/ts/model/TimeSeriesValue.java | 13 +++++++++++++ .../ts/service/TimeSeriesService.java | 13 +++++++++++++ .../ru/ulstu/extractor/util/JsonUtils.java | 18 ++++++++++++++++++ 5 files changed, 57 insertions(+) create mode 100644 src/main/java/ru/ulstu/extractor/util/JsonUtils.java 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 5caad5c..32674c5 100644 --- a/src/main/java/ru/ulstu/extractor/ts/controller/TimeSeriesMarkupController.java +++ b/src/main/java/ru/ulstu/extractor/ts/controller/TimeSeriesMarkupController.java @@ -22,6 +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))); return "markup"; } } diff --git a/src/main/java/ru/ulstu/extractor/ts/model/TimeSeries.java b/src/main/java/ru/ulstu/extractor/ts/model/TimeSeries.java index 76abf70..34a2b4c 100644 --- a/src/main/java/ru/ulstu/extractor/ts/model/TimeSeries.java +++ b/src/main/java/ru/ulstu/extractor/ts/model/TimeSeries.java @@ -2,6 +2,7 @@ package ru.ulstu.extractor.ts.model; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; +import org.json.JSONObject; import ru.ulstu.extractor.branch.model.Branch; import ru.ulstu.extractor.core.BaseEntity; @@ -15,6 +16,9 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; + +import static ru.ulstu.extractor.util.JsonUtils.getListOfObjects; @Entity public class TimeSeries extends BaseEntity { @@ -51,6 +55,14 @@ public class TimeSeries extends BaseEntity { this.branch = branch; } + public TimeSeries(JSONObject timeSeries) { + this.name = timeSeries.getString("name"); + this.values = getListOfObjects(timeSeries.getJSONArray("values")) + .stream() + .map(TimeSeriesValue::new) + .collect(Collectors.toList()); + } + public String getName() { return name; } diff --git a/src/main/java/ru/ulstu/extractor/ts/model/TimeSeriesValue.java b/src/main/java/ru/ulstu/extractor/ts/model/TimeSeriesValue.java index 9b77f2a..e9ac965 100644 --- a/src/main/java/ru/ulstu/extractor/ts/model/TimeSeriesValue.java +++ b/src/main/java/ru/ulstu/extractor/ts/model/TimeSeriesValue.java @@ -1,8 +1,11 @@ package ru.ulstu.extractor.ts.model; +import org.json.JSONObject; import ru.ulstu.extractor.core.BaseEntity; import javax.persistence.Entity; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Date; @Entity @@ -28,6 +31,16 @@ public class TimeSeriesValue extends BaseEntity { this.value = value; } + public TimeSeriesValue(JSONObject jsonValue) { + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss"); + try { + this.date = formatter.parse(jsonValue.getString("date")); + } catch (ParseException e) { + e.printStackTrace(); + } + this.value = jsonValue.getDouble("value"); + } + public Date getDate() { return date; } 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 d5e12e6..1cdf4cc 100644 --- a/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/extractor/ts/service/TimeSeriesService.java @@ -36,6 +36,7 @@ public class TimeSeriesService { private final HttpService httpService; private final static String TIME_SERIES_SAVE_SERVICE_URL = "http://time-series.athene.tech/api/1.0/add-time-series?setKey=git-extractor"; private final static String TIME_SERIES_TENDENCY_URL = "http://time-series.athene.tech/api/1.0/getSpecificMethodSmoothed"; + private final static String TIME_SERIES_GROUPED_TENDENCIES_URL = "http://time-series.athene.tech/api/1.0/getGroupedTendencies"; public TimeSeriesService(TimeSeriesRepository timeSeriesRepository, TimeSeriesValueRepository timeSeriesValueRepository, @@ -136,4 +137,16 @@ 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(); + } } diff --git a/src/main/java/ru/ulstu/extractor/util/JsonUtils.java b/src/main/java/ru/ulstu/extractor/util/JsonUtils.java new file mode 100644 index 0000000..6ac8927 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/util/JsonUtils.java @@ -0,0 +1,18 @@ +package ru.ulstu.extractor.util; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +public class JsonUtils { + + public static List getListOfObjects(JSONArray jsonArray) { + List result = new ArrayList<>(); + for (int i = 0; i < jsonArray.length(); i++) { + result.add(jsonArray.getJSONObject(i)); + } + return result; + } +}