aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-08-01 23:09:20 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-08-01 23:09:20 -0700
commit18eea3995305cab6dab641f7d3dfd11b9a5433be (patch)
tree53d17ad5ed2cbeea5d2837adc2b24c679e3ae870 /src/main
parentc733bc9f91e55df204d1626f6a2f88183184b041 (diff)
downloadtraccar-server-18eea3995305cab6dab641f7d3dfd11b9a5433be.tar.gz
traccar-server-18eea3995305cab6dab641f7d3dfd11b9a5433be.tar.bz2
traccar-server-18eea3995305cab6dab641f7d3dfd11b9a5433be.zip
Update reports API and add logging
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/traccar/api/DateParameterConverterProvider.java52
-rw-r--r--src/main/java/org/traccar/api/resource/PositionResource.java17
-rw-r--r--src/main/java/org/traccar/api/resource/ReportResource.java65
-rw-r--r--src/main/java/org/traccar/api/resource/StatisticsResource.java8
-rw-r--r--src/main/java/org/traccar/helper/LogAction.java18
-rw-r--r--src/main/java/org/traccar/web/WebServer.java8
6 files changed, 117 insertions, 51 deletions
diff --git a/src/main/java/org/traccar/api/DateParameterConverterProvider.java b/src/main/java/org/traccar/api/DateParameterConverterProvider.java
new file mode 100644
index 000000000..f07ece984
--- /dev/null
+++ b/src/main/java/org/traccar/api/DateParameterConverterProvider.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2020 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.api;
+
+import org.traccar.helper.DateUtil;
+
+import javax.ws.rs.ext.ParamConverter;
+import javax.ws.rs.ext.ParamConverterProvider;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Date;
+
+public class DateParameterConverterProvider implements ParamConverterProvider {
+
+ public static class DateParameterConverter implements ParamConverter<Date> {
+
+ @Override
+ public Date fromString(String value) {
+ return value != null ? DateUtil.parseDate(value) : null;
+ }
+
+ @Override
+ public String toString(Date value) {
+ return value != null ? DateUtil.formatDate(value) : null;
+ }
+
+ }
+
+ @Override
+ public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
+ if (Date.class.equals(rawType)) {
+ @SuppressWarnings("unchecked")
+ ParamConverter<T> paramConverter = (ParamConverter<T>) new DateParameterConverter();
+ return paramConverter;
+ }
+ return null;
+ }
+
+}
diff --git a/src/main/java/org/traccar/api/resource/PositionResource.java b/src/main/java/org/traccar/api/resource/PositionResource.java
index 67aa6dd32..e93feaccf 100644
--- a/src/main/java/org/traccar/api/resource/PositionResource.java
+++ b/src/main/java/org/traccar/api/resource/PositionResource.java
@@ -17,7 +17,6 @@ package org.traccar.api.resource;
import org.traccar.Context;
import org.traccar.api.BaseResource;
-import org.traccar.helper.DateUtil;
import org.traccar.model.Position;
import org.traccar.web.CsvBuilder;
import org.traccar.web.GpxBuilder;
@@ -35,6 +34,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.Date;
import java.util.List;
@Path("positions")
@@ -50,7 +50,7 @@ public class PositionResource extends BaseResource {
@GET
public Collection<Position> getJson(
@QueryParam("deviceId") long deviceId, @QueryParam("id") List<Long> positionIds,
- @QueryParam("from") String from, @QueryParam("to") String to)
+ @QueryParam("from") Date from, @QueryParam("to") Date to)
throws SQLException {
if (!positionIds.isEmpty()) {
ArrayList<Position> positions = new ArrayList<>();
@@ -65,8 +65,7 @@ public class PositionResource extends BaseResource {
} else {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
if (from != null && to != null) {
- return Context.getDataManager().getPositions(
- deviceId, DateUtil.parseDate(from), DateUtil.parseDate(to));
+ return Context.getDataManager().getPositions(deviceId, from, to);
} else {
return Collections.singleton(Context.getDeviceManager().getLastPosition(deviceId));
}
@@ -76,25 +75,23 @@ public class PositionResource extends BaseResource {
@GET
@Produces(TEXT_CSV)
public Response getCsv(
- @QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to)
+ @QueryParam("deviceId") long deviceId, @QueryParam("from") Date from, @QueryParam("to") Date to)
throws SQLException {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
CsvBuilder csv = new CsvBuilder();
csv.addHeaderLine(new Position());
- csv.addArray(Context.getDataManager().getPositions(
- deviceId, DateUtil.parseDate(from), DateUtil.parseDate(to)));
+ csv.addArray(Context.getDataManager().getPositions(deviceId, from, to));
return Response.ok(csv.build()).header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_CSV).build();
}
@GET
@Produces(GPX)
public Response getGpx(
- @QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to)
+ @QueryParam("deviceId") long deviceId, @QueryParam("from") Date from, @QueryParam("to") Date to)
throws SQLException {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
GpxBuilder gpx = new GpxBuilder(Context.getIdentityManager().getById(deviceId).getName());
- gpx.addPositions(Context.getDataManager().getPositions(
- deviceId, DateUtil.parseDate(from), DateUtil.parseDate(to)));
+ gpx.addPositions(Context.getDataManager().getPositions(deviceId, from, to));
return Response.ok(gpx.build()).header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_GPX).build();
}
diff --git a/src/main/java/org/traccar/api/resource/ReportResource.java b/src/main/java/org/traccar/api/resource/ReportResource.java
index d371cf987..7e9ce702e 100644
--- a/src/main/java/org/traccar/api/resource/ReportResource.java
+++ b/src/main/java/org/traccar/api/resource/ReportResource.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2020 Anton Tananaev (anton@traccar.org)
* Copyright 2016 - 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
+import java.util.Date;
import java.util.List;
import javax.activation.DataHandler;
@@ -39,7 +40,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Context;
import org.traccar.api.BaseResource;
-import org.traccar.helper.DateUtil;
+import org.traccar.helper.LogAction;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.reports.Events;
@@ -97,9 +98,9 @@ public class ReportResource extends BaseResource {
@GET
public Collection<Position> getRoute(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds,
- @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException {
- return Route.getObjects(getUserId(), deviceIds, groupIds,
- DateUtil.parseDate(from), DateUtil.parseDate(to));
+ @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException {
+ LogAction.logReport(getUserId(), "route", from, to, deviceIds, groupIds);
+ return Route.getObjects(getUserId(), deviceIds, groupIds, from, to);
}
@Path("route")
@@ -107,11 +108,11 @@ public class ReportResource extends BaseResource {
@Produces(XLSX)
public Response getRouteExcel(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds,
- @QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail)
+ @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("mail") boolean mail)
throws SQLException, IOException {
return executeReport(getUserId(), mail, stream -> {
- Route.getExcel(stream, getUserId(), deviceIds, groupIds,
- DateUtil.parseDate(from), DateUtil.parseDate(to));
+ LogAction.logReport(getUserId(), "route", from, to, deviceIds, groupIds);
+ Route.getExcel(stream, getUserId(), deviceIds, groupIds, from, to);
});
}
@@ -120,9 +121,9 @@ public class ReportResource extends BaseResource {
public Collection<Event> getEvents(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds,
@QueryParam("type") final List<String> types,
- @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException {
- return Events.getObjects(getUserId(), deviceIds, groupIds, types,
- DateUtil.parseDate(from), DateUtil.parseDate(to));
+ @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException {
+ LogAction.logReport(getUserId(), "events", from, to, deviceIds, groupIds);
+ return Events.getObjects(getUserId(), deviceIds, groupIds, types, from, to);
}
@Path("events")
@@ -131,11 +132,11 @@ public class ReportResource extends BaseResource {
public Response getEventsExcel(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds,
@QueryParam("type") final List<String> types,
- @QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail)
+ @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("mail") boolean mail)
throws SQLException, IOException {
return executeReport(getUserId(), mail, stream -> {
- Events.getExcel(stream, getUserId(), deviceIds, groupIds, types,
- DateUtil.parseDate(from), DateUtil.parseDate(to));
+ LogAction.logReport(getUserId(), "events", from, to, deviceIds, groupIds);
+ Events.getExcel(stream, getUserId(), deviceIds, groupIds, types, from, to);
});
}
@@ -143,9 +144,9 @@ public class ReportResource extends BaseResource {
@GET
public Collection<SummaryReport> getSummary(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds,
- @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException {
- return Summary.getObjects(getUserId(), deviceIds, groupIds,
- DateUtil.parseDate(from), DateUtil.parseDate(to));
+ @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException {
+ LogAction.logReport(getUserId(), "summary", from, to, deviceIds, groupIds);
+ return Summary.getObjects(getUserId(), deviceIds, groupIds, from, to);
}
@Path("summary")
@@ -153,11 +154,11 @@ public class ReportResource extends BaseResource {
@Produces(XLSX)
public Response getSummaryExcel(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds,
- @QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail)
+ @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("mail") boolean mail)
throws SQLException, IOException {
return executeReport(getUserId(), mail, stream -> {
- Summary.getExcel(stream, getUserId(), deviceIds, groupIds,
- DateUtil.parseDate(from), DateUtil.parseDate(to));
+ LogAction.logReport(getUserId(), "summary", from, to, deviceIds, groupIds);
+ Summary.getExcel(stream, getUserId(), deviceIds, groupIds, from, to);
});
}
@@ -166,9 +167,9 @@ public class ReportResource extends BaseResource {
@Produces(MediaType.APPLICATION_JSON)
public Collection<TripReport> getTrips(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds,
- @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException {
- return Trips.getObjects(getUserId(), deviceIds, groupIds,
- DateUtil.parseDate(from), DateUtil.parseDate(to));
+ @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException {
+ LogAction.logReport(getUserId(), "trips", from, to, deviceIds, groupIds);
+ return Trips.getObjects(getUserId(), deviceIds, groupIds, from, to);
}
@Path("trips")
@@ -176,11 +177,11 @@ public class ReportResource extends BaseResource {
@Produces(XLSX)
public Response getTripsExcel(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds,
- @QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail)
+ @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("mail") boolean mail)
throws SQLException, IOException {
return executeReport(getUserId(), mail, stream -> {
- Trips.getExcel(stream, getUserId(), deviceIds, groupIds,
- DateUtil.parseDate(from), DateUtil.parseDate(to));
+ LogAction.logReport(getUserId(), "trips", from, to, deviceIds, groupIds);
+ Trips.getExcel(stream, getUserId(), deviceIds, groupIds, from, to);
});
}
@@ -189,9 +190,9 @@ public class ReportResource extends BaseResource {
@Produces(MediaType.APPLICATION_JSON)
public Collection<StopReport> getStops(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds,
- @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException {
- return Stops.getObjects(getUserId(), deviceIds, groupIds,
- DateUtil.parseDate(from), DateUtil.parseDate(to));
+ @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException {
+ LogAction.logReport(getUserId(), "stops", from, to, deviceIds, groupIds);
+ return Stops.getObjects(getUserId(), deviceIds, groupIds, from, to);
}
@Path("stops")
@@ -199,11 +200,11 @@ public class ReportResource extends BaseResource {
@Produces(XLSX)
public Response getStopsExcel(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds,
- @QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail)
+ @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("mail") boolean mail)
throws SQLException, IOException {
return executeReport(getUserId(), mail, stream -> {
- Stops.getExcel(stream, getUserId(), deviceIds, groupIds,
- DateUtil.parseDate(from), DateUtil.parseDate(to));
+ LogAction.logReport(getUserId(), "stops", from, to, deviceIds, groupIds);
+ Stops.getExcel(stream, getUserId(), deviceIds, groupIds, from, to);
});
}
diff --git a/src/main/java/org/traccar/api/resource/StatisticsResource.java b/src/main/java/org/traccar/api/resource/StatisticsResource.java
index e801d4ff3..58073e7d1 100644
--- a/src/main/java/org/traccar/api/resource/StatisticsResource.java
+++ b/src/main/java/org/traccar/api/resource/StatisticsResource.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2020 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@ package org.traccar.api.resource;
import org.traccar.Context;
import org.traccar.api.BaseResource;
-import org.traccar.helper.DateUtil;
import org.traccar.model.Statistics;
import javax.ws.rs.Consumes;
@@ -28,6 +27,7 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.sql.SQLException;
import java.util.Collection;
+import java.util.Date;
@Path("statistics")
@Produces(MediaType.APPLICATION_JSON)
@@ -36,9 +36,9 @@ public class StatisticsResource extends BaseResource {
@GET
public Collection<Statistics> get(
- @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException {
+ @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException {
Context.getPermissionsManager().checkAdmin(getUserId());
- return Context.getDataManager().getStatistics(DateUtil.parseDate(from), DateUtil.parseDate(to));
+ return Context.getDataManager().getStatistics(from, to);
}
}
diff --git a/src/main/java/org/traccar/helper/LogAction.java b/src/main/java/org/traccar/helper/LogAction.java
index 16d55ec60..d16b25483 100644
--- a/src/main/java/org/traccar/helper/LogAction.java
+++ b/src/main/java/org/traccar/helper/LogAction.java
@@ -17,6 +17,10 @@
package org.traccar.helper;
import java.beans.Introspector;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -46,6 +50,7 @@ public final class LogAction {
private static final String PATTERN_LOGIN = "user: %d, action: %s";
private static final String PATTERN_LOGIN_FAILED = "login failed from: %s";
private static final String PATTERN_DEVICE_ACCUMULATORS = "user: %d, action: %s, deviceId: %d";
+ private static final String PATTERN_REPORT = "user: %d, report: %s, from: %s, to: %s, devices: %s, groups: %s";
public static void create(long userId, BaseModel object) {
logObjectAction(ACTION_CREATE, userId, object.getClass(), object.getId());
@@ -92,8 +97,8 @@ public final class LogAction {
PATTERN_OBJECT, userId, action, Introspector.decapitalize(clazz.getSimpleName()), objectId));
}
- private static void logLinkAction(String action, long userId,
- Class<?> owner, long ownerId, Class<?> property, long propertyId) {
+ private static void logLinkAction(
+ String action, long userId, Class<?> owner, long ownerId, Class<?> property, long propertyId) {
LOGGER.info(String.format(
PATTERN_LINK, userId, action,
Introspector.decapitalize(owner.getSimpleName()), ownerId,
@@ -104,4 +109,13 @@ public final class LogAction {
LOGGER.info(String.format(PATTERN_LOGIN, userId, action));
}
+ public static void logReport(
+ long userId, String report, Date from, Date to, List<Long> deviceIds, List<Long> groupIds) {
+ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+ LOGGER.info(String.format(
+ PATTERN_REPORT, userId, report,
+ dateFormat.format(from), dateFormat.format(to),
+ deviceIds.toString(), groupIds.toString()));
+ }
+
}
diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java
index 12fa80d10..7f0ec53b6 100644
--- a/src/main/java/org/traccar/web/WebServer.java
+++ b/src/main/java/org/traccar/web/WebServer.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 - 2019 Anton Tananaev (anton@traccar.org)
+ * Copyright 2012 - 2020 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.traccar.api.DateParameterConverterProvider;
import org.traccar.config.Config;
import org.traccar.api.AsyncSocketServlet;
import org.traccar.api.CorsResponseFilter;
@@ -161,8 +162,9 @@ public class WebServer {
}
ResourceConfig resourceConfig = new ResourceConfig();
- resourceConfig.registerClasses(JacksonFeature.class, ObjectMapperProvider.class, ResourceErrorHandler.class);
- resourceConfig.registerClasses(SecurityRequestFilter.class, CorsResponseFilter.class);
+ resourceConfig.registerClasses(
+ JacksonFeature.class, ObjectMapperProvider.class, ResourceErrorHandler.class,
+ SecurityRequestFilter.class, CorsResponseFilter.class, DateParameterConverterProvider.class);
resourceConfig.packages(ServerResource.class.getPackage().getName());
servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/api/*");
}