#91 -- Group ts tendencies
This commit is contained in:
parent
41b23570c0
commit
f4600c2ba5
@ -22,6 +22,7 @@ public class TimeSeriesMarkupController {
|
||||
public String markupTs(Model model) {
|
||||
List<TimeSeries> ts = timeSeriesService.getAllTimeSeries();
|
||||
model.addAttribute("ts", ts);
|
||||
model.addAttribute("fts", timeSeriesService.getGroupedTendencies(ts.get(0)));
|
||||
return "markup";
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<TimeSeries> 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();
|
||||
}
|
||||
}
|
||||
|
18
src/main/java/ru/ulstu/extractor/util/JsonUtils.java
Normal file
18
src/main/java/ru/ulstu/extractor/util/JsonUtils.java
Normal file
@ -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<JSONObject> getListOfObjects(JSONArray jsonArray) {
|
||||
List<JSONObject> result = new ArrayList<>();
|
||||
for (int i = 0; i < jsonArray.length(); i++) {
|
||||
result.add(jsonArray.getJSONObject(i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user