From aa4248e73510c14eb64cfbb3c20cf65da805cd50 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 6 Oct 2020 17:06:04 -0700 Subject: Implement daily summary report --- .../org/traccar/api/resource/ReportResource.java | 10 +++-- src/main/java/org/traccar/reports/Summary.java | 47 ++++++++++++++++++--- .../java/org/traccar/reports/model/BaseReport.java | 22 ++++++++++ .../java/org/traccar/reports/model/StopReport.java | 20 --------- .../java/org/traccar/reports/model/TripReport.java | 20 --------- templates/export/summary.xlsx | Bin 15311 -> 12067 bytes 6 files changed, 68 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/traccar/api/resource/ReportResource.java b/src/main/java/org/traccar/api/resource/ReportResource.java index 7e9ce702e..7347bfd64 100644 --- a/src/main/java/org/traccar/api/resource/ReportResource.java +++ b/src/main/java/org/traccar/api/resource/ReportResource.java @@ -144,9 +144,10 @@ public class ReportResource extends BaseResource { @GET public Collection getSummary( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, - @QueryParam("from") Date from, @QueryParam("to") Date to) throws SQLException { + @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("daily") boolean daily) + throws SQLException { LogAction.logReport(getUserId(), "summary", from, to, deviceIds, groupIds); - return Summary.getObjects(getUserId(), deviceIds, groupIds, from, to); + return Summary.getObjects(getUserId(), deviceIds, groupIds, from, to, daily); } @Path("summary") @@ -154,11 +155,12 @@ public class ReportResource extends BaseResource { @Produces(XLSX) public Response getSummaryExcel( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, - @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("mail") boolean mail) + @QueryParam("from") Date from, @QueryParam("to") Date to, @QueryParam("daily") boolean daily, + @QueryParam("mail") boolean mail) throws SQLException, IOException { return executeReport(getUserId(), mail, stream -> { LogAction.logReport(getUserId(), "summary", from, to, deviceIds, groupIds); - Summary.getExcel(stream, getUserId(), deviceIds, groupIds, from, to); + Summary.getExcel(stream, getUserId(), deviceIds, groupIds, from, to, daily); }); } diff --git a/src/main/java/org/traccar/reports/Summary.java b/src/main/java/org/traccar/reports/Summary.java index 3883e7283..612761844 100644 --- a/src/main/java/org/traccar/reports/Summary.java +++ b/src/main/java/org/traccar/reports/Summary.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2020 Anton Tananaev (anton@traccar.org) * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,6 +22,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Calendar; import java.util.Collection; import java.util.Date; @@ -35,11 +36,10 @@ public final class Summary { private Summary() { } - private static SummaryReport calculateSummaryResult(long deviceId, Date from, Date to) throws SQLException { + private static SummaryReport calculateSummaryResult(long deviceId, Collection positions) { SummaryReport result = new SummaryReport(); result.setDeviceId(deviceId); result.setDeviceName(Context.getIdentityManager().getById(deviceId).getName()); - Collection positions = Context.getDataManager().getPositions(deviceId, from, to); if (positions != null && !positions.isEmpty()) { Position firstPosition = null; Position previousPosition = null; @@ -74,26 +74,59 @@ public final class Summary { result.setEndOdometer(previousPosition.getDouble(Position.KEY_TOTAL_DISTANCE)); } + result.setStartTime(firstPosition.getFixTime()); + result.setEndTime(previousPosition.getServerTime()); } return result; } + private static int getDay(long userId, Date date) { + Calendar calendar = Calendar.getInstance(ReportUtils.getTimezone(userId)); + calendar.setTime(date); + return calendar.get(Calendar.DAY_OF_MONTH); + } + + private static Collection calculateSummaryResults( + long userId, long deviceId, Date from, Date to, boolean daily) throws SQLException { + + ArrayList positions = new ArrayList<>(Context.getDataManager().getPositions(deviceId, from, to)); + + ArrayList results = new ArrayList<>(); + if (daily && !positions.isEmpty()) { + int startIndex = 0; + int startDay = getDay(userId, positions.iterator().next().getFixTime()); + for (int i = 0; i < positions.size(); i++) { + int currentDay = getDay(userId, positions.get(i).getFixTime()); + if (currentDay != startDay) { + results.add(calculateSummaryResult(deviceId, positions.subList(startIndex, i))); + startIndex = i; + startDay = currentDay; + } + } + results.add(calculateSummaryResult(deviceId, positions.subList(startIndex, positions.size()))); + } else { + results.add(calculateSummaryResult(deviceId, positions)); + } + + return results; + } + public static Collection getObjects(long userId, Collection deviceIds, - Collection groupIds, Date from, Date to) throws SQLException { + Collection groupIds, Date from, Date to, boolean daily) throws SQLException { ReportUtils.checkPeriodLimit(from, to); ArrayList result = new ArrayList<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - result.add(calculateSummaryResult(deviceId, from, to)); + result.addAll(calculateSummaryResults(userId, deviceId, from, to, daily)); } return result; } public static void getExcel(OutputStream outputStream, long userId, Collection deviceIds, Collection groupIds, - Date from, Date to) throws SQLException, IOException { + Date from, Date to, boolean daily) throws SQLException, IOException { ReportUtils.checkPeriodLimit(from, to); - Collection summaries = getObjects(userId, deviceIds, groupIds, from, to); + Collection summaries = getObjects(userId, deviceIds, groupIds, from, to, daily); String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/"); try (InputStream inputStream = new FileInputStream(templatePath + "/summary.xlsx")) { diff --git a/src/main/java/org/traccar/reports/model/BaseReport.java b/src/main/java/org/traccar/reports/model/BaseReport.java index 9f2d1188c..deb42bb8a 100644 --- a/src/main/java/org/traccar/reports/model/BaseReport.java +++ b/src/main/java/org/traccar/reports/model/BaseReport.java @@ -16,6 +16,8 @@ */ package org.traccar.reports.model; +import java.util.Date; + public class BaseReport { private long deviceId; @@ -103,4 +105,24 @@ public class BaseReport { this.endOdometer = endOdometer; } + private Date startTime; + + public Date getStartTime() { + return startTime; + } + + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + + private Date endTime; + + public Date getEndTime() { + return endTime; + } + + public void setEndTime(Date endTime) { + this.endTime = endTime; + } + } diff --git a/src/main/java/org/traccar/reports/model/StopReport.java b/src/main/java/org/traccar/reports/model/StopReport.java index fd8fa9335..28b7def62 100644 --- a/src/main/java/org/traccar/reports/model/StopReport.java +++ b/src/main/java/org/traccar/reports/model/StopReport.java @@ -50,26 +50,6 @@ public class StopReport extends BaseReport { this.longitude = longitude; } - private Date startTime; - - public Date getStartTime() { - return startTime; - } - - public void setStartTime(Date startTime) { - this.startTime = startTime; - } - - private Date endTime; - - public Date getEndTime() { - return endTime; - } - - public void setEndTime(Date endTime) { - this.endTime = endTime; - } - private String address; public String getAddress() { diff --git a/src/main/java/org/traccar/reports/model/TripReport.java b/src/main/java/org/traccar/reports/model/TripReport.java index 3140f3019..6c1c20953 100644 --- a/src/main/java/org/traccar/reports/model/TripReport.java +++ b/src/main/java/org/traccar/reports/model/TripReport.java @@ -80,16 +80,6 @@ public class TripReport extends BaseReport { this.endLon = endLon; } - private Date startTime; - - public Date getStartTime() { - return startTime; - } - - public void setStartTime(Date startTime) { - this.startTime = startTime; - } - private String startAddress; public String getStartAddress() { @@ -100,16 +90,6 @@ public class TripReport extends BaseReport { this.startAddress = address; } - private Date endTime; - - public Date getEndTime() { - return endTime; - } - - public void setEndTime(Date endTime) { - this.endTime = endTime; - } - private String endAddress; public String getEndAddress() { diff --git a/templates/export/summary.xlsx b/templates/export/summary.xlsx index be16e8931..3fcf461ff 100644 Binary files a/templates/export/summary.xlsx and b/templates/export/summary.xlsx differ -- cgit v1.2.3