|
|
@@ -1,20 +1,18 @@
|
|
|
package cz.senslog.telemetry.database.repository;
|
|
|
|
|
|
import cz.senslog.telemetry.database.DataNotFoundException;
|
|
|
-import cz.senslog.telemetry.database.PagingRetrieve;
|
|
|
import cz.senslog.telemetry.database.SortType;
|
|
|
import cz.senslog.telemetry.database.domain.*;
|
|
|
import cz.senslog.telemetry.database.domain.Filter;
|
|
|
import io.vertx.core.Future;
|
|
|
-import io.vertx.ext.auth.impl.UserImpl;
|
|
|
+import io.vertx.core.json.JsonObject;
|
|
|
import io.vertx.sqlclient.*;
|
|
|
|
|
|
-import java.sql.Array;
|
|
|
import java.time.OffsetDateTime;
|
|
|
-import java.time.ZoneId;
|
|
|
import java.util.*;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
import java.util.stream.StreamSupport;
|
|
|
|
|
|
import static java.lang.String.format;
|
|
|
@@ -33,6 +31,18 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
return new MapLogRepository(client);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ private static JsonObject convertSensorIdToName(Map<Long, String> sensors, JsonObject observedValues) {
|
|
|
+ JsonObject res = new JsonObject(new HashMap<>(observedValues.size()));
|
|
|
+ for (Map.Entry<String, Object> val : observedValues) {
|
|
|
+ Long sensorId = Long.parseLong(val.getKey());
|
|
|
+ if (sensors.containsKey(sensorId)) {
|
|
|
+ res.put(sensors.get(sensorId), val.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Future<Integer> updateEvent(EntityAction data) {
|
|
|
Tuple params = Tuple.of(data.getTimestamp(), data.getEntityId(), data.getActionId(), data.getUnitId());
|
|
|
@@ -309,7 +319,7 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
return client.preparedQuery("SELECT s.sensor_id, s.name, s.type " +
|
|
|
"FROM maplog.unit_to_sensor AS uts " +
|
|
|
"JOIN maplog.sensor s on s.sensor_id = uts.sensor_id " +
|
|
|
- "WHERE UTS.unit_id = $1 ORDER BY s.sensor_id")
|
|
|
+ "WHERE uts.unit_id = $1 AND s.io_id != 0 ORDER BY s.sensor_id")
|
|
|
.execute(Tuple.of(unitId))
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map((row) -> Sensor.of(
|
|
|
@@ -1318,7 +1328,14 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
"WHERE utc.campaign_id = $1 AND " + whereTimestampClause + whereFiltersClause + " " +
|
|
|
"ORDER BY tel.time_stamp OFFSET $2 LIMIT $3;";
|
|
|
|
|
|
- return client.preparedQuery(sql)
|
|
|
+ return client.preparedQuery("SELECT s.sensor_id, s.name FROM maplog.sensor s " +
|
|
|
+ "JOIN maplog.unit_to_sensor uts ON uts.sensor_id = s.sensor_id " +
|
|
|
+ "JOIN maplog.unit_to_campaign utc ON utc.unit_id = uts.unit_id " +
|
|
|
+ "WHERE utc.campaign_id = $1")
|
|
|
+ .execute(Tuple.of(campaignId))
|
|
|
+ .map(r -> StreamSupport.stream(r.spliterator(), false)
|
|
|
+ .collect(Collectors.toMap(row -> row.getLong("sensor_id"), row -> row.getString("name"))))
|
|
|
+ .flatMap(sensors -> client.preparedQuery(sql)
|
|
|
.execute(tupleParams)
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(r -> UnitTelemetry.of(
|
|
|
@@ -1331,24 +1348,10 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
r.getFloat("alt"),
|
|
|
r.getFloat("angle")),
|
|
|
r.getInteger("speed"),
|
|
|
- r.getJsonObject("observed_values")
|
|
|
+ convertSensorIdToName(sensors, r.getJsonObject("observed_values"))
|
|
|
))
|
|
|
.collect(toList())
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Future<PagingRetrieve<List<UnitTelemetry>>> findObservationsByCampaignIdWithPaging(
|
|
|
- long campaignId, OffsetDateTime from, OffsetDateTime to, int offset, int limit, List<Filter> filters
|
|
|
- ) {
|
|
|
- return findObservationsByCampaignId(campaignId, from, to, offset, limit+1, filters)
|
|
|
- .map(data -> {
|
|
|
- boolean hasNext = data.size() > limit;
|
|
|
- if (hasNext) {
|
|
|
- data.remove(data.size() - 1);
|
|
|
- }
|
|
|
- return new PagingRetrieve<>(hasNext, data.size(), data);
|
|
|
- });
|
|
|
+ ));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -1408,7 +1411,14 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
"WHERE utc.campaign_id = $1 AND su.identity = $4 AND " + whereTimestampClause + whereFiltersClause + " " +
|
|
|
"ORDER BY tel.time_stamp OFFSET $2 LIMIT $3;";
|
|
|
|
|
|
- return client.preparedQuery(sql)
|
|
|
+ return client.preparedQuery("SELECT s.sensor_id, s.name FROM maplog.sensor s " +
|
|
|
+ "JOIN maplog.unit_to_sensor uts ON uts.sensor_id = s.sensor_id " +
|
|
|
+ "JOIN maplog.unit_to_campaign utc ON utc.unit_id = uts.unit_id " +
|
|
|
+ "WHERE utc.campaign_id = $1")
|
|
|
+ .execute(Tuple.of(campaignId))
|
|
|
+ .map(r -> StreamSupport.stream(r.spliterator(), false)
|
|
|
+ .collect(Collectors.toMap(row -> row.getLong("sensor_id"), row -> row.getString("name"))))
|
|
|
+ .flatMap(sensors -> client.preparedQuery(sql)
|
|
|
.execute(tupleParams)
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(r -> UnitTelemetry.of(
|
|
|
@@ -1421,10 +1431,10 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
r.getFloat("alt"),
|
|
|
r.getFloat("angle")),
|
|
|
r.getInteger("speed"),
|
|
|
- r.getJsonObject("observed_values")
|
|
|
+ convertSensorIdToName(sensors, r.getJsonObject("observed_values"))
|
|
|
))
|
|
|
.collect(toList())
|
|
|
- );
|
|
|
+ ));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -1480,7 +1490,13 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
"WHERE utc.campaign_id = $1 AND utc.unit_id = $2 AND " + whereTimestampClause + whereFiltersClause + " " +
|
|
|
"ORDER BY tel.time_stamp OFFSET $3 LIMIT $4;";
|
|
|
|
|
|
- return client.preparedQuery(sql)
|
|
|
+ return client.preparedQuery("SELECT s.sensor_id, s.name FROM maplog.sensor s " +
|
|
|
+ "JOIN maplog.unit_to_sensor uts ON uts.sensor_id = s.sensor_id " +
|
|
|
+ "WHERE uts.unit_id = $1")
|
|
|
+ .execute(Tuple.of(unitId))
|
|
|
+ .map(r -> StreamSupport.stream(r.spliterator(), false)
|
|
|
+ .collect(Collectors.toMap(row -> row.getLong("sensor_id"), row -> row.getString("name"))))
|
|
|
+ .flatMap(sensors -> client.preparedQuery(sql)
|
|
|
.execute(tupleParams)
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(r -> UnitTelemetry.of(
|
|
|
@@ -1493,23 +1509,10 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
r.getFloat("alt"),
|
|
|
r.getFloat("angle")),
|
|
|
r.getInteger("speed"),
|
|
|
- r.getJsonObject("observed_values")))
|
|
|
+ convertSensorIdToName(sensors, r.getJsonObject("observed_values"))
|
|
|
+ ))
|
|
|
.collect(toList())
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Future<PagingRetrieve<List<UnitTelemetry>>> findObservationsByCampaignIdAndUnitIdWithPaging(
|
|
|
- long campaignId, long unitId, OffsetDateTime from, OffsetDateTime to, int offset, int limit, List<Filter> filters
|
|
|
- ) {
|
|
|
- return findObservationsByCampaignIdAndUnitId(campaignId, unitId, from, to, offset, limit+1, filters)
|
|
|
- .map(data -> {
|
|
|
- boolean hasNext = data.size() > limit;
|
|
|
- if (hasNext) {
|
|
|
- data.remove(data.size() - 1);
|
|
|
- }
|
|
|
- return new PagingRetrieve<>(hasNext, data.size(), data);
|
|
|
- });
|
|
|
+ ));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -1568,7 +1571,13 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
"WHERE utc.campaign_id = $1 AND utc.unit_id = $2 AND su.identity = $5 AND " + whereTimestampClause + whereFiltersClause + " " +
|
|
|
"ORDER BY tel.time_stamp OFFSET $3 LIMIT $4";
|
|
|
|
|
|
- return client.preparedQuery(sql)
|
|
|
+ return client.preparedQuery("SELECT s.sensor_id, s.name FROM maplog.sensor s " +
|
|
|
+ "JOIN maplog.unit_to_sensor uts ON uts.sensor_id = s.sensor_id " +
|
|
|
+ "WHERE uts.unit_id = $1")
|
|
|
+ .execute(Tuple.of(unitId))
|
|
|
+ .map(r -> StreamSupport.stream(r.spliterator(), false)
|
|
|
+ .collect(Collectors.toMap(row -> row.getLong("sensor_id"), row -> row.getString("name"))))
|
|
|
+ .flatMap(sensors -> client.preparedQuery(sql)
|
|
|
.execute(tupleParams)
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(r -> UnitTelemetry.of(
|
|
|
@@ -1581,9 +1590,10 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
r.getFloat("alt"),
|
|
|
r.getFloat("angle")),
|
|
|
r.getInteger("speed"),
|
|
|
- r.getJsonObject("observed_values")))
|
|
|
+ convertSensorIdToName(sensors, r.getJsonObject("observed_values"))
|
|
|
+ ))
|
|
|
.collect(toList())
|
|
|
- );
|
|
|
+ ));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -1629,8 +1639,7 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- String sql = "SELECT tel.id, tel.unit_id, tel.observed_values::json, tel.speed, " +
|
|
|
- "tel.time_stamp, $4 AS zone_id, " + // ::timestamp with time zone at time zone $4 AS time_stamp
|
|
|
+ String sql = "SELECT tel.id, tel.unit_id, tel.observed_values::json, tel.speed, tel.time_stamp, " +
|
|
|
"ST_X (ST_Transform (tel.the_geom, 4326)) AS long, " +
|
|
|
"ST_Y (ST_Transform (tel.the_geom, 4326)) AS lat, " +
|
|
|
"ST_Z (ST_Transform (tel.the_geom, 4326)) AS alt, " +
|
|
|
@@ -1640,7 +1649,14 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
"WHERE e.id = $1 AND " + whereTimestampClause + whereFiltersClause + " " +
|
|
|
"ORDER BY tel.time_stamp OFFSET $2 LIMIT $3";
|
|
|
|
|
|
- return client.preparedQuery(sql)
|
|
|
+ return client.preparedQuery("SELECT s.sensor_id, s.name FROM maplog.sensor s " +
|
|
|
+ "JOIN maplog.unit_to_sensor uts ON uts.sensor_id = s.sensor_id " +
|
|
|
+ "JOIN maplog.event e ON e.unit_id = uts.unit_id " +
|
|
|
+ "WHERE e.id = $1")
|
|
|
+ .execute(Tuple.of(eventId))
|
|
|
+ .map(r -> StreamSupport.stream(r.spliterator(), false)
|
|
|
+ .collect(Collectors.toMap(row -> row.getLong("sensor_id"), row -> row.getString("name"))))
|
|
|
+ .flatMap(sensors -> client.preparedQuery(sql)
|
|
|
.execute(tupleParams)
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(r -> UnitTelemetry.of(
|
|
|
@@ -1653,9 +1669,10 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
r.getFloat("alt"),
|
|
|
r.getFloat("angle")),
|
|
|
r.getInteger("speed"),
|
|
|
- r.getJsonObject("observed_values")))
|
|
|
+ convertSensorIdToName(sensors, r.getJsonObject("observed_values"))
|
|
|
+ ))
|
|
|
.collect(toList())
|
|
|
- );
|
|
|
+ ));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -1714,7 +1731,14 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
"WHERE e.id = $1 AND su.identity = $4 AND " + whereTimestampClause + whereFiltersClause + " " +
|
|
|
"ORDER BY tel.time_stamp OFFSET $2 LIMIT $3";
|
|
|
|
|
|
- return client.preparedQuery(sql)
|
|
|
+ return client.preparedQuery("SELECT s.sensor_id, s.name FROM maplog.sensor s " +
|
|
|
+ "JOIN maplog.unit_to_sensor uts ON uts.sensor_id = s.sensor_id " +
|
|
|
+ "JOIN maplog.event e ON e.unit_id = uts.unit_id " +
|
|
|
+ "WHERE e.id = $1")
|
|
|
+ .execute(Tuple.of(eventId))
|
|
|
+ .map(r -> StreamSupport.stream(r.spliterator(), false)
|
|
|
+ .collect(Collectors.toMap(row -> row.getLong("sensor_id"), row -> row.getString("name"))))
|
|
|
+ .flatMap(sensors -> client.preparedQuery(sql)
|
|
|
.execute(tupleParams)
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(r -> UnitTelemetry.of(
|
|
|
@@ -1727,9 +1751,10 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
r.getFloat("alt"),
|
|
|
r.getFloat("angle")),
|
|
|
r.getInteger("speed"),
|
|
|
- r.getJsonObject("observed_values")))
|
|
|
+ convertSensorIdToName(sensors, r.getJsonObject("observed_values"))
|
|
|
+ ))
|
|
|
.collect(toList())
|
|
|
- );
|
|
|
+ ));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -1775,8 +1800,7 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- String sql = "SELECT tel.id, (tel.observed_values::jsonb ->> $3::bigint::text::varchar)::integer AS value, tel.speed, " +
|
|
|
- "tel.time_stamp, $6 AS zone_id, " + // ::timestamp with time zone at time zone $6 AS time_stamp
|
|
|
+ String sql = "SELECT tel.id, (tel.observed_values::jsonb ->> $3::bigint::text::varchar)::integer AS value, tel.speed, tel.time_stamp, " +
|
|
|
"ST_X (ST_Transform (tel.the_geom, 4326)) AS long, " +
|
|
|
"ST_Y (ST_Transform (tel.the_geom, 4326)) AS lat, " +
|
|
|
"ST_Z (ST_Transform (tel.the_geom, 4326)) AS alt, " +
|
|
|
@@ -1922,7 +1946,7 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- String sql = "SELECT tel.unit_id, tel.time_stamp, $5 AS zone_id, " + // ::timestamp with time zone at time zone $5 AS time_stamp
|
|
|
+ String sql = "SELECT tel.unit_id, tel.time_stamp, " +
|
|
|
"ST_X (ST_Transform (tel.the_geom, 4326)) AS long, " +
|
|
|
"ST_Y (ST_Transform (tel.the_geom, 4326)) AS lat, " +
|
|
|
"ST_Z (ST_Transform (tel.the_geom, 4326)) AS alt " +
|
|
|
@@ -2063,7 +2087,7 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- String sql = "SELECT tel.unit_id, tel.time_stamp, $4 AS zone_id, " + // ::timestamp with time zone at time zone $4 AS time_stamp
|
|
|
+ String sql = "SELECT tel.unit_id, tel.time_stamp, " +
|
|
|
"ST_X (ST_Transform (tel.the_geom, 4326)) AS long, " +
|
|
|
"ST_Y (ST_Transform (tel.the_geom, 4326)) AS lat, " +
|
|
|
"ST_Z (ST_Transform (tel.the_geom, 4326)) AS alt " +
|
|
|
@@ -2201,7 +2225,7 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- String sql = "SELECT unit_id, time_stamp, $3 AS zone_id, " + // ::timestamp with time zone at time zone $5 AS time_stamp
|
|
|
+ String sql = "SELECT unit_id, time_stamp, " +
|
|
|
"ST_X (ST_Transform (the_geom, 4326)) AS long, " +
|
|
|
"ST_Y (ST_Transform (the_geom, 4326)) AS lat, " +
|
|
|
"ST_Z (ST_Transform (the_geom, 4326)) AS alt, " +
|
|
|
@@ -2384,14 +2408,12 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
statusFilter = Set.of(AlertStatus.CREATED, AlertStatus.INFORMED, AlertStatus.IN_PROCESS, AlertStatus.SOLVED);
|
|
|
}
|
|
|
|
|
|
- String statuses = String.join(",", statusFilter.stream().map(s -> "'"+s.name()+"'").collect(toSet()));
|
|
|
-
|
|
|
return client.preparedQuery("SELECT a.id, a.message, a.status, a.time_stamp FROM maplog.alert AS a " +
|
|
|
"JOIN maplog.event AS e ON e.unit_id = a.unit_id " +
|
|
|
"WHERE e.from_time <= a.time_stamp AND (CASE WHEN e.to_time IS NULL THEN now() ELSE e.to_time END) >= a.time_stamp " +
|
|
|
- "AND e.id = $1 AND a.status IN ("+statuses+") " + // TODO better solution IN (array)
|
|
|
+ "AND e.id = $1 AND a.status = ANY($2) " +
|
|
|
"ORDER BY a.time_stamp "+sortType.name())
|
|
|
- .execute(Tuple.of(eventId))
|
|
|
+ .execute(Tuple.of(eventId, statusFilter))
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(r -> Alert.of(
|
|
|
r.getLong("id"),
|
|
|
@@ -2473,22 +2495,20 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
statusFilter = Set.of(AlertStatus.CREATED, AlertStatus.INFORMED, AlertStatus.IN_PROCESS, AlertStatus.SOLVED);
|
|
|
}
|
|
|
|
|
|
- String statuses = String.join(",", statusFilter.stream().map(s -> "'"+s.name()+"'").collect(toSet()));
|
|
|
-
|
|
|
String whereTimestampClause;
|
|
|
Tuple tupleParams;
|
|
|
if (from != null && to != null) {
|
|
|
- whereTimestampClause = "WHERE utc.campaign_id = $1 AND a.status IN ("+statuses+") AND a.time_stamp >= (CASE WHEN utc.from_time > $2 THEN utc.from_time ELSE $2 END) AND a.time_stamp <= (CASE WHEN utc.to_time < $3 THEN utc.to_time ELSE $3 END)";
|
|
|
- tupleParams = Tuple.of(campaignId, from, to);
|
|
|
+ whereTimestampClause = "WHERE utc.campaign_id = $1 AND a.status = ANY($2) AND a.time_stamp >= (CASE WHEN utc.from_time > $3 THEN utc.from_time ELSE $3 END) AND a.time_stamp <= (CASE WHEN utc.to_time < $4 THEN utc.to_time ELSE $4 END)";
|
|
|
+ tupleParams = Tuple.of(campaignId, statusFilter, from, to);
|
|
|
} else if (from != null) {
|
|
|
- whereTimestampClause = "WHERE utc.campaign_id = $1 AND a.status IN ("+statuses+") AND a.time_stamp >= (CASE WHEN utc.from_time > $2 THEN utc.from_time ELSE $2 END) AND a.time_stamp <= utc.to_time";
|
|
|
- tupleParams = Tuple.of(campaignId, from);
|
|
|
+ whereTimestampClause = "WHERE utc.campaign_id = $1 AND a.status = ANY($2) AND a.time_stamp >= (CASE WHEN utc.from_time > $3 THEN utc.from_time ELSE $3 END) AND a.time_stamp <= utc.to_time";
|
|
|
+ tupleParams = Tuple.of(campaignId, statusFilter, from);
|
|
|
} else if (to != null) {
|
|
|
- whereTimestampClause = "WHERE utc.campaign_id = $1 AND a.status IN ("+statuses+") AND a.time_stamp >= utc.from_time AND a.time_stamp <= (CASE WHEN utc.to_time < $2 THEN utc.to_time ELSE $2 END)";
|
|
|
- tupleParams = Tuple.of(campaignId, to);
|
|
|
+ whereTimestampClause = "WHERE utc.campaign_id = $1 AND a.status = ANY($2) AND a.time_stamp >= utc.from_time AND a.time_stamp <= (CASE WHEN utc.to_time < $3 THEN utc.to_time ELSE $3 END)";
|
|
|
+ tupleParams = Tuple.of(campaignId, statusFilter, to);
|
|
|
} else {
|
|
|
- whereTimestampClause = "WHERE utc.campaign_id = $1 AND a.status IN ("+statuses+")";
|
|
|
- tupleParams = Tuple.of(campaignId);
|
|
|
+ whereTimestampClause = "WHERE utc.campaign_id = $1 AND a.status = ANY($2)";
|
|
|
+ tupleParams = Tuple.of(campaignId, statusFilter);
|
|
|
}
|
|
|
|
|
|
return client.preparedQuery("SELECT a.id, utc.campaign_id, a.unit_id, a.message, a.time_stamp, a.status FROM maplog.alert AS a " +
|
|
|
@@ -2556,22 +2576,20 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
statusFilter = Set.of(AlertStatus.CREATED, AlertStatus.INFORMED, AlertStatus.IN_PROCESS, AlertStatus.SOLVED);
|
|
|
}
|
|
|
|
|
|
- String statuses = String.join(",", statusFilter.stream().map(s -> "'"+s.name()+"'").collect(toSet()));
|
|
|
-
|
|
|
String whereTimestampClause;
|
|
|
Tuple tupleParams;
|
|
|
if (from != null && to != null) {
|
|
|
- whereTimestampClause = "WHERE utc.campaign_id = $1 AND utc.unit_id = $2 AND a.status IN ("+statuses+") AND a.time_stamp >= (CASE WHEN utc.from_time > $3 THEN utc.from_time ELSE $3 END) AND a.time_stamp <= (CASE WHEN utc.to_time < $4 THEN utc.to_time ELSE $4 END)";
|
|
|
- tupleParams = Tuple.of(campaignId, unitId, from, to);
|
|
|
+ whereTimestampClause = "WHERE utc.campaign_id = $1 AND utc.unit_id = $2 AND a.status = ANY($3) AND a.time_stamp >= (CASE WHEN utc.from_time > $4 THEN utc.from_time ELSE $4 END) AND a.time_stamp <= (CASE WHEN utc.to_time < $5 THEN utc.to_time ELSE $5 END)";
|
|
|
+ tupleParams = Tuple.of(campaignId, unitId, statusFilter, from, to);
|
|
|
} else if (from != null) {
|
|
|
- whereTimestampClause = "WHERE utc.campaign_id = $1 AND utc.unit_id = $2 AND a.status IN ("+statuses+") AND a.time_stamp >= (CASE WHEN utc.from_time > $3 THEN utc.from_time ELSE $3 END) AND a.time_stamp <= utc.to_time";
|
|
|
- tupleParams = Tuple.of(campaignId, unitId, from);
|
|
|
+ whereTimestampClause = "WHERE utc.campaign_id = $1 AND utc.unit_id = $2 AND a.status = ANY($3) AND a.time_stamp >= (CASE WHEN utc.from_time > $4 THEN utc.from_time ELSE $4 END) AND a.time_stamp <= utc.to_time";
|
|
|
+ tupleParams = Tuple.of(campaignId, unitId, statusFilter, from);
|
|
|
} else if (to != null) {
|
|
|
- whereTimestampClause = "WHERE utc.campaign_id = $1 AND utc.unit_id = $2 AND a.status IN ("+statuses+") AND a.time_stamp >= utc.from_time AND a.time_stamp <= (CASE WHEN utc.to_time < $3 THEN utc.to_time ELSE $3 END)";
|
|
|
- tupleParams = Tuple.of(campaignId, unitId, to);
|
|
|
+ whereTimestampClause = "WHERE utc.campaign_id = $1 AND utc.unit_id = $2 AND a.status = ANY($3) AND a.time_stamp >= utc.from_time AND a.time_stamp <= (CASE WHEN utc.to_time < $4 THEN utc.to_time ELSE $4 END)";
|
|
|
+ tupleParams = Tuple.of(campaignId, unitId, statusFilter, to);
|
|
|
} else {
|
|
|
- whereTimestampClause = "WHERE utc.campaign_id = $1 AND utc.unit_id = $2 AND a.status IN ("+statuses+")";
|
|
|
- tupleParams = Tuple.of(campaignId, unitId);
|
|
|
+ whereTimestampClause = "WHERE utc.campaign_id = $1 AND utc.unit_id = $2 AND a.status = ANY($3)";
|
|
|
+ tupleParams = Tuple.of(campaignId, unitId, statusFilter);
|
|
|
}
|
|
|
|
|
|
return client.preparedQuery("SELECT a.id, a.unit_id, a.message, a.time_stamp, a.status FROM maplog.alert AS a " +
|
|
|
@@ -2637,15 +2655,13 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
statusFilter = Set.of(AlertStatus.CREATED, AlertStatus.INFORMED, AlertStatus.IN_PROCESS, AlertStatus.SOLVED);
|
|
|
}
|
|
|
|
|
|
- String statuses = String.join(",", statusFilter.stream().map(s -> "'"+s.name()+"'").collect(toSet()));
|
|
|
-
|
|
|
return client.preparedQuery("SELECT a.id, a.message, a.time_stamp, a.status FROM maplog.alert AS a " +
|
|
|
"JOIN maplog.event AS e on a.unit_id = e.unit_id " +
|
|
|
"WHERE e.entity_id = $1 AND e.action_id = $2 AND e.unit_id = $3 " +
|
|
|
"AND e.from_time <= a.time_stamp AND (CASE WHEN e.to_time IS NULL THEN now() ELSE e.to_time END) >= a.time_stamp " +
|
|
|
- "AND a.status IN ("+statuses+")" +
|
|
|
+ "AND a.status = ANY($4)" +
|
|
|
"ORDER BY a.time_stamp "+sort.name())
|
|
|
- .execute(Tuple.of(entityId, actionId, unitId))
|
|
|
+ .execute(Tuple.of(entityId, actionId, unitId, statusFilter))
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(r -> Alert.of(
|
|
|
r.getLong("id"),
|