From 18eea3995305cab6dab641f7d3dfd11b9a5433be Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 1 Aug 2020 23:09:20 -0700 Subject: Update reports API and add logging --- .../api/DateParameterConverterProvider.java | 52 +++++++++++++++++ .../org/traccar/api/resource/PositionResource.java | 17 +++--- .../org/traccar/api/resource/ReportResource.java | 65 +++++++++++----------- .../traccar/api/resource/StatisticsResource.java | 8 +-- 4 files changed, 96 insertions(+), 46 deletions(-) create mode 100644 src/main/java/org/traccar/api/DateParameterConverterProvider.java (limited to 'src/main/java/org/traccar/api') 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 { + + @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 ParamConverter getConverter(Class rawType, Type genericType, Annotation[] annotations) { + if (Date.class.equals(rawType)) { + @SuppressWarnings("unchecked") + ParamConverter paramConverter = (ParamConverter) 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 getJson( @QueryParam("deviceId") long deviceId, @QueryParam("id") List positionIds, - @QueryParam("from") String from, @QueryParam("to") String to) + @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException { if (!positionIds.isEmpty()) { ArrayList 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 getRoute( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List 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 deviceIds, @QueryParam("groupId") final List 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 getEvents( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, @QueryParam("type") final List 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 deviceIds, @QueryParam("groupId") final List groupIds, @QueryParam("type") final List 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 getSummary( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List 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 deviceIds, @QueryParam("groupId") final List 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 getTrips( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List 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 deviceIds, @QueryParam("groupId") final List 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 getStops( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List 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 deviceIds, @QueryParam("groupId") final List 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 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); } } -- cgit v1.2.3