#91 -- Group list of ts tendencies

This commit is contained in:
Anton Romanov 2023-02-27 23:35:31 +04:00
parent f4600c2ba5
commit 704366f11a
4 changed files with 31 additions and 12 deletions

View File

@ -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 {

View File

@ -22,7 +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)));
model.addAttribute("groupedTs", timeSeriesService.getGroupedTendencies(ts));
return "markup";
}
}

View File

@ -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<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();
public List<TimeSeries> getGroupedTendencies(List<TimeSeries> 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());
}
}

View File

@ -14,7 +14,7 @@
</tr>
</thead>
<tbody>
<tr th:each="t: ${ts}">
<tr th:each="t: ${groupedTs}">
<td><span class="badge badge-success" th:text="${t.name}"></span></td>
<td><span class="badge badge-success" th:text="${#lists.size(t.values)}"></span></td>
</tr>