|
|
@@ -1079,4 +1079,81 @@ public class OpenAPIHandler {
|
|
|
)).encode())
|
|
|
.onFailure(th -> rc.fail(400, th)));
|
|
|
}
|
|
|
+
|
|
|
+ public void eventIdObservationsGET(RoutingContext rc) {
|
|
|
+ String host = hostURLFull(rc.request());
|
|
|
+ JsonObject paramsJson = new JsonObject();
|
|
|
+
|
|
|
+ int eventId = Integer.parseInt(rc.pathParam("eventId"));
|
|
|
+
|
|
|
+ List<String> paramFrom = rc.queryParam("from");
|
|
|
+ OffsetDateTime from = TernaryCondition.<OffsetDateTime>ternaryIf(paramFrom::isEmpty, () -> null, () -> {
|
|
|
+ paramsJson.put("from", paramFrom.get(0));
|
|
|
+ return OffsetDateTime.parse(paramFrom.get(0));
|
|
|
+ });
|
|
|
+
|
|
|
+ List<String> paramTo = rc.queryParam("to");
|
|
|
+ OffsetDateTime to = TernaryCondition.<OffsetDateTime>ternaryIf(paramTo::isEmpty, () -> null,() -> {
|
|
|
+ paramsJson.put("to", paramTo.get(0));
|
|
|
+ return OffsetDateTime.parse(paramTo.get(0));
|
|
|
+ });
|
|
|
+
|
|
|
+ List<String> paramZone = rc.queryParam("zone");
|
|
|
+ ZoneId zone = TernaryCondition.<ZoneId>ternaryIf(paramZone::isEmpty, ZoneId.of("UTC"), () -> {
|
|
|
+ paramsJson.put("zone", paramZone.get(0));
|
|
|
+ return ZoneId.of(paramZone.get(0));
|
|
|
+ });
|
|
|
+
|
|
|
+ List<String> paramOffset = rc.queryParam("offset");
|
|
|
+ int offset = TernaryCondition.<Integer>ternaryIf(paramOffset::isEmpty, 0, () -> {
|
|
|
+ paramsJson.put("offset", paramOffset.get(0));
|
|
|
+ return Integer.parseInt(paramOffset.get(0));
|
|
|
+ });
|
|
|
+
|
|
|
+ List<String> paramLimit = rc.queryParam("limit");
|
|
|
+ int limit = TernaryCondition.<Integer>ternaryIf(paramLimit::isEmpty, DEFAULT_MAX_DATA_LIMIT, () -> {
|
|
|
+ paramsJson.put("limit", paramLimit.get(0));
|
|
|
+ return Integer.parseInt(paramLimit.get(0));
|
|
|
+ });
|
|
|
+
|
|
|
+ List<String> paramNavigationLinks = rc.queryParam("navigationLinks");
|
|
|
+ boolean navigationLinks = TernaryCondition.<Boolean>ternaryIf(paramNavigationLinks::isEmpty, true, () -> {
|
|
|
+ paramsJson.put("navigationLinks", paramNavigationLinks.get(0));
|
|
|
+ return parseBoolean(paramNavigationLinks.get(0));
|
|
|
+ });
|
|
|
+
|
|
|
+ JsonObject navLinks = navigationLinks ? JsonObject.of(
|
|
|
+ "Event@NavigationLink", String.format("%s/events/%d", host, eventId)
|
|
|
+ ) : JsonObject.of();
|
|
|
+
|
|
|
+ Function<Long, String> createNextNavLink = dataSize -> {
|
|
|
+ String urlParams = paramsJson.stream()
|
|
|
+ .filter(e -> !e.getKey().equals("offset"))
|
|
|
+ .map(e -> String.format("%s=%s", e.getKey(), e.getValue()))
|
|
|
+ .collect(Collectors.joining("&"));
|
|
|
+ long newOffset = offset + dataSize;
|
|
|
+ return String.format("%s/events/%d/observations?offset=%d%s", host, eventId, newOffset, (urlParams.isEmpty() ? "" : "&"+urlParams));
|
|
|
+ };
|
|
|
+
|
|
|
+ repo.findObservationsByEventIdWithPaging(eventId, from, to, zone, offset, limit)
|
|
|
+ .onSuccess(paging -> rc.response().end(navLinks.mergeIn(navigationLinks && paging.hasNext() ? JsonObject.of(
|
|
|
+ "next@NavigationLink", createNextNavLink.apply(paging.size())
|
|
|
+ ) : JsonObject.of()).mergeIn(JsonObject.of(
|
|
|
+ "params", paramsJson,
|
|
|
+ "size", paging.size(),
|
|
|
+ "offset", offset,
|
|
|
+ "hasNext", paging.hasNext(),
|
|
|
+ "data", new JsonArray(
|
|
|
+ paging.data().stream().map(o -> JsonObject.of(
|
|
|
+ "timestamp", OffsetDateTime.ofInstant(o.getTimestamp().toInstant(), zone).format(ISO_OFFSET_DATE_TIME),
|
|
|
+ "speed", o.getSpeed(),
|
|
|
+ "location", JsonObject.of(
|
|
|
+ "longitude", o.getLocation().getLongitude(),
|
|
|
+ "latitude", o.getLocation().getLatitude(),
|
|
|
+ "altitude", o.getLocation().getAltitude()),
|
|
|
+ "observedValues", o.getObservedValues()
|
|
|
+ )).collect(toList()))
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> rc.fail(400, th));
|
|
|
+ }
|
|
|
}
|