Переглянути джерело

Added API 'driverIdUnitsGET'

Lukas Cerny 1 рік тому
батько
коміт
0b30748ce6

+ 30 - 0
src/main/java/cz/senslog/telemetry/database/repository/MapLogRepository.java

@@ -326,6 +326,36 @@ public class MapLogRepository implements SensLogRepository {
     }
 
     @Override
+    public Future<List<Unit>> findUnitsByDriverId(int driverId, OffsetDateTime from, OffsetDateTime to) {
+        String whereTimestampClause;
+        Tuple tupleParams;
+        if (from != null && to != null) {
+            whereTimestampClause = "WHERE dta.driver_id = $1 AND dta.from_time >= $2 AND dta.to_time < $3";
+            tupleParams = Tuple.of(driverId, from, to);
+        } else if (from != null) {
+            whereTimestampClause = "WHERE dta.driver_id = $1 AND dta.from_time >= $2";
+            tupleParams = Tuple.of(driverId, from);
+        } else if (to != null) {
+            whereTimestampClause = "WHERE dta.driver_id = $1 AND dta.to_time < $2";
+            tupleParams = Tuple.of(driverId, to);
+        } else {
+            whereTimestampClause = "WHERE dta.driver_id = $1";
+            tupleParams = Tuple.of(driverId);
+        }
+        return client.preparedQuery("SELECT u.unit_id, u.name, u.imei, u.description FROM maplog.driver_to_action AS dta " +
+                "JOIN maplog.unit AS u ON u.unit_id = dta.unit_id " + whereTimestampClause)
+                .execute(tupleParams)
+                .map(rs -> StreamSupport.stream(rs.spliterator(), false)
+                        .map(row -> Unit.of(
+                                row.getLong("unit_id"),
+                                row.getString("name"),
+                                row.getString("imei"),
+                                row.getString("description")
+                        )).collect(toList()))
+                .onFailure(logger::catching);
+    }
+
+    @Override
     public Future<List<CampaignUnit>> findUnitsByCampaignId(long campaignId) {
         return client.preparedQuery("SELECT u.unit_id, utc.camp_id, u.name, u.imei, u.unit_type_id, u.description, utc.from_time, utc.to_time " +
                         "FROM maplog.unit_to_campaign AS utc " +

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

@@ -176,6 +176,11 @@ public class MockMapLogRepository implements SensLogRepository {
     }
 
     @Override
+    public Future<List<Unit>> findUnitsByDriverId(int driverId, OffsetDateTime from, OffsetDateTime to) {
+        return Future.succeededFuture(Collections.emptyList());
+    }
+
+    @Override
     public Future<List<CampaignUnit>> findUnitsByCampaignId(long campaignId) {
         return Future.succeededFuture(Collections.emptyList());
     }

+ 2 - 0
src/main/java/cz/senslog/telemetry/database/repository/SensLogRepository.java

@@ -48,6 +48,8 @@ public interface SensLogRepository {
     Future<List<Driver>> allDrivers();
     Future<Driver> findDriverById(int driverId);
 
+
+    Future<List<Unit>> findUnitsByDriverId(int driverId, OffsetDateTime from, OffsetDateTime to);
     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);
 

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

@@ -80,6 +80,7 @@ public final class HttpVertxServer extends AbstractVerticle {
 
                     openAPIRouterBuilder.operation("driversGET").handler(apiHandler::driversGET);
                     openAPIRouterBuilder.operation("driverIdGET").handler(apiHandler::driverIdGET);
+                    openAPIRouterBuilder.operation("driverIdUnitsGET").handler(apiHandler::driverIdUnitsGET);
 
                     Router mainRouter = openAPIRouterBuilder.createRouter();
 //                    mainRouter.route().handler(LoggerHandler.create());

+ 27 - 0
src/main/java/cz/senslog/telemetry/server/OpenAPIHandler.java

@@ -841,4 +841,31 @@ public class OpenAPIHandler {
                         )).encode())
                         .onFailure(th -> rc.fail(400, th)));
     }
+
+    public void driverIdUnitsGET(RoutingContext rc) {
+        String host =  hostURLFull(rc.request());
+
+        int driverId = Integer.parseInt(rc.pathParam("driverId"));
+
+        List<String> paramFrom = rc.queryParam("from");
+        OffsetDateTime from = TernaryCondition.<OffsetDateTime>ternaryIf(paramFrom::isEmpty, () -> null, () -> OffsetDateTime.parse(paramFrom.get(0)));
+
+        List<String> paramTo = rc.queryParam("to");
+        OffsetDateTime to = TernaryCondition.<OffsetDateTime>ternaryIf(paramTo::isEmpty, () -> null, () -> OffsetDateTime.parse(paramTo.get(0)));
+
+        List<String> paramNavigationLinks = rc.queryParam("navigationLinks");
+        boolean navigationLinks = paramNavigationLinks.isEmpty() ? DEFAULT_NAVIGATION_LINKS : parseBoolean(paramNavigationLinks.get(0));
+
+        repo.findUnitsByDriverId(driverId, from, to)
+                .onSuccess(units -> rc.response().end(new JsonArray(
+                        units.stream().map(u -> (navigationLinks ? JsonObject.of(
+                                "DriverUnit@NavigationLink", String.format("%s/drivers/%d/units/%d",host, driverId, u.getUnitId())
+                        ) : JsonObject.of()).mergeIn(JsonObject.of(
+                                "unitId", u.getUnitId(),
+                                "name", u.getName(),
+                                "description", u.getDescription()
+                        ))).collect(toList())
+                ).encode()))
+                .onFailure(th -> rc.fail(400, th));
+    }
 }

+ 1 - 1
src/main/resources/openAPISpec.yaml

@@ -542,7 +542,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /drivers/{driverId}:
-    get:
+    get: # done
       operationId: driverIdGET
       summary: Publish detailed info about the driver
       parameters: