Przeglądaj źródła

Added API 'phenomenonIdGET'

Lukas Cerny 1 rok temu
rodzic
commit
f82f237126

+ 5 - 5
src/main/java/cz/senslog/telemetry/database/domain/Phenomenon.java

@@ -2,27 +2,27 @@ package cz.senslog.telemetry.database.domain;
 
 public class Phenomenon {
 
-    private final int id;
+    private final long id;
     private final String name;
     private final String uom;
     private final String uomLink;
 
-    public static Phenomenon of(int id, String name, String uom, String uomLink) {
+    public static Phenomenon of(long id, String name, String uom, String uomLink) {
         return new Phenomenon(id, name, uom, uomLink);
     }
 
-    public static Phenomenon of(int id, String name) {
+    public static Phenomenon of(long id, String name) {
         return new Phenomenon(id, name, null, null);
     }
 
-    private Phenomenon(int id, String name, String uom, String uomLink) {
+    private Phenomenon(long id, String name, String uom, String uomLink) {
         this.id = id;
         this.name = name;
         this.uom = uom;
         this.uomLink = uomLink;
     }
 
-    public int getId() {
+    public long getId() {
         return id;
     }
 

+ 16 - 1
src/main/java/cz/senslog/telemetry/database/repository/MapLogRepository.java

@@ -166,7 +166,7 @@ public class MapLogRepository implements SensLogRepository {
             row.getString("sensor_name"),
             row.getString("sensor_type"),
             row.getInteger("io_id"),
-            Phenomenon.of(row.getInteger("phenomenon_id"), null),
+            Phenomenon.of(row.getLong("phenomenon_id"), null),
             row.getString("description")
     );
 
@@ -202,6 +202,21 @@ public class MapLogRepository implements SensLogRepository {
                 );
     }
 
+    private static final Function<Row, Phenomenon> ROW_TO_PHENOMENON = (row) -> Phenomenon.of(
+            row.getLong("id"),
+            row.getString("name"),
+            row.getString("uom"),
+            row.getString("uom_link")
+    );
+
+    @Override
+    public Future<Phenomenon> findPhenomenonById(long phenomenonId) {
+        return client.preparedQuery("SELECT id, name, uom, uom_link FROM maplog.phenomenon WHERE id = $1")
+                .execute(Tuple.of(phenomenonId))
+                .map(RowSet::iterator)
+                .map(iterator -> iterator.hasNext() ? ROW_TO_PHENOMENON.apply(iterator.next()) : null);
+    }
+
     private static final Function<Row, Campaign> ROW_TO_BASIC_CAMPAIGN = (row) -> Campaign.of(
             row.getLong("campaign_id"),
             row.getString("description"),

+ 5 - 0
src/main/java/cz/senslog/telemetry/database/repository/MockMapLogRepository.java

@@ -126,6 +126,11 @@ public class MockMapLogRepository implements SensLogRepository {
     }
 
     @Override
+    public Future<Phenomenon> findPhenomenonById(long phenomenonId) {
+        return Future.succeededFuture(Phenomenon.of(phenomenonId, "mock(name)"));
+    }
+
+    @Override
     public Future<List<Campaign>> findCampaignsByUnitId(long unitId, ZoneId zone) {
         return Future.succeededFuture(Collections.emptyList());
     }

+ 10 - 13
src/main/java/cz/senslog/telemetry/database/repository/SensLogRepository.java

@@ -20,38 +20,35 @@ public interface SensLogRepository {
 
 
     Future<List<Unit>> allUnits();
-
-
     Future<Unit> findUnitById(long unitId);
-
     Future<Unit> findUnitByIMEI(String imei);
-
+    Future<List<Unit>> findUnitsBySensorId(long sensorId);
+    Future<List<CampaignUnit>> findUnitsByCampaignId(long campaignId);
+    Future<List<Long>> findUnitIdsByCampaignId(long campaignId);
 
     Future<List<Sensor>> allSensors();
     Future<Sensor> findSensorById(long sensorId);
-    Future<List<Unit>> findUnitsBySensorId(long sensorId);
     Future<Sensor> findSensorByIOAndUnitId(int ioID, long unitId);
+    Future<Map<Long, Sensor>> findSensorsByUnitIdGroupById(long unitId);
+    Future<List<Sensor>> findSensorsByUnitId(long unitId);
+
+
+    Future<Phenomenon> findPhenomenonById(long phenomenonId);
 
 
     Future<List<Campaign>> allCampaigns();
     Future<Campaign> findCampaignById(long campaignId);
-    Future<List<Long>> findUnitIdsByCampaignId(long campaignId);
+    Future<List<Campaign>> findCampaignsByUnitId(long unitId, ZoneId zone);
+
 
     Future<List<UnitTelemetry>> findObservationsByCampaignId(long campaignId, OffsetDateTime from, OffsetDateTime to, ZoneId zone, int offset, int limit);
     Future<PagingRetrieve<List<UnitTelemetry>>> findObservationsByCampaignIdWithPaging(long campaignId, OffsetDateTime from, OffsetDateTime to, ZoneId zone, int offset, int limit);
 
-
     Future<List<UnitTelemetry>> findObservationsByCampaignIdAndUnitId(long campaignId, long unitId, OffsetDateTime from, OffsetDateTime to, ZoneId zone, int offset, int limit);
     Future<PagingRetrieve<List<UnitTelemetry>>> findObservationsByCampaignIdAndUnitIdWithPaging(long campaignId, long unitId, OffsetDateTime from, OffsetDateTime to, ZoneId zone, int offset, int limit);
 
     Future<List<UnitLocation>> findLocationsByCampaignIdAndUnitId(long campaignId, long unitId, OffsetDateTime from, OffsetDateTime to, ZoneId zone, int offset, int limit);
     Future<PagingRetrieve<List<UnitLocation>>> findLocationsByCampaignIdAndUnitIdWithPaging(long campaignId, long unitId, OffsetDateTime from, OffsetDateTime to, ZoneId zone, int offset, int limit);
 
-    Future<Map<Long, Sensor>> findSensorsByUnitIdGroupById(long unitId);
-
-    Future<List<Sensor>> findSensorsByUnitId(long unitId);
-    Future<List<Campaign>> findCampaignsByUnitId(long unitId, ZoneId zone);
-
-    Future<List<CampaignUnit>> findUnitsByCampaignId(long campaignId);
     Future<List<UnitLocation>> findUnitsLocationsByCampaignId(long campaignId, int limitPerUnit, OffsetDateTime from, OffsetDateTime to, ZoneId zone, SortType sort);
 }

+ 2 - 0
src/main/java/cz/senslog/telemetry/server/HttpVertxServer.java

@@ -71,6 +71,8 @@ public final class HttpVertxServer extends AbstractVerticle {
                     openAPIRouterBuilder.operation("sensorIdGET").handler(apiHandler::sensorIdGET);
                     openAPIRouterBuilder.operation("sensorIdUnitsGET").handler(apiHandler::sensorIdUnitsGET);
 
+                    openAPIRouterBuilder.operation("phenomenonIdGET").handler(apiHandler::phenomenonIdGET);
+
                     Router mainRouter = openAPIRouterBuilder.createRouter();
 //                    mainRouter.route().handler(LoggerHandler.create());
 //                    mainRouter.route().handler(rc -> logger.info("HTTP Request '{}'.", rc.request().absoluteURI()));

+ 22 - 1
src/main/java/cz/senslog/telemetry/server/OpenAPIHandler.java

@@ -538,7 +538,7 @@ public class OpenAPIHandler {
                                 "Units@NavigationLink", String.format("%s/sensors/%d/units", host, s.getSensorId()  ),
                                 "Phenomenon@NavigationLink", String.format("%s/phenomenons/%d", host, s.getPhenomenon().getId())
                         ) : JsonObject.of()).mergeIn(JsonObject.of(
-                                "id", s.getSensorId(),
+                                "sensorId", s.getSensorId(),
                                 "name", s.getName(),
                                 "description", s.getDescription(),
                                 "type", s.getType(),
@@ -585,4 +585,25 @@ public class OpenAPIHandler {
                 ).encode()))
                 .onFailure(th -> rc.fail(400, th));
     }
+
+    public void phenomenonIdGET(RoutingContext rc) {
+        String host =  hostURLFull(rc.request());
+
+        long phenomenonId = Long.parseLong(rc.pathParam("phenomenonId"));
+
+        List<String> paramNavigationLinks = rc.queryParam("navigationLinks");
+        boolean navigationLinks = paramNavigationLinks.isEmpty() ? DEFAULT_NAVIGATION_LINKS : parseBoolean(paramNavigationLinks.get(0));
+
+        repo.findPhenomenonById(phenomenonId)
+                .onSuccess(p -> rc.response().end((navigationLinks ? JsonObject.of(
+                                "self@NavigationLink", String.format("%s/phenomenons/%d", host, p.getId()),
+                                "Sensors@NavigationLink", String.format("%s/phenomenons/%d/sensors", host, p.getId())
+                        ) : JsonObject.of()).mergeIn(JsonObject.of(
+                                "id", p.getId(),
+                                "name", p.getName(),
+                                "uom", p.getUom(),
+                                "uomLink", p.getUomLink()
+                        )).encode())
+                        .onFailure(th -> rc.fail(400, th)));
+    }
 }

+ 26 - 5
src/main/resources/openAPISpec.yaml

@@ -784,6 +784,13 @@ paths:
     get:
       operationId: phenomenonAllGET
       summary: Publish info about all phenomenons
+      parameters:
+        - in: query
+          name: navigationLinks
+          schema:
+            type: boolean
+            default: true
+          description: Option to disable @NavigationLinks in a response
       responses:
         200:
           description: JSON Array of info about phenomenons
@@ -800,10 +807,24 @@ paths:
               schema:
                 $ref: '#/components/schemas/Error'
 
-  /phenomenons/{id}:
+  /phenomenons/{phenomenonId}:
     get:
       operationId: phenomenonIdGET
       summary: Publish info about the phenomenon
+      parameters:
+        - in: path
+          name: phenomenonId
+          schema:
+            type: integer
+            format: int64
+          required: true
+          description: Numeric ID of the phenomenon to get
+        - in: query
+          name: navigationLinks
+          schema:
+            type: boolean
+            default: true
+          description: Option to disable @NavigationLinks in a response
       responses:
         200:
           description: JSON Object of info about the phenomenon
@@ -1577,12 +1598,11 @@ components:
     PhenomenonDetailInfo:
       type: object
       required:
-        - Phenomenon@NavigationLink
-        - Sensors@NavigationLink
         - id
         - name
+        - uom
       properties:
-        Phenomenon@NavigationLink:
+        self@NavigationLink:
           type: string
           format: uri
         Sensors@NavigationLink:
@@ -1600,7 +1620,8 @@ components:
           type: string
           format: uri
       example:
-        Phenomenon@NavigationLink: "<domain>/phenomenons/15"
+        self@NavigationLink: "<domain>/phenomenons/15"
+        Sensors@NavigationLink: "<domain>/phenomenons/15/sensors"
         id: 15
         name: "Temperature"
         uom: "uom"