aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-10-06 17:06:04 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-10-06 17:06:04 -0700
commitaa4248e73510c14eb64cfbb3c20cf65da805cd50 (patch)
treef15d4828125679ed5eac28feda132354e599ddeb
parent87e9932ff6eec10dec78300cd9fb2532d1bfa538 (diff)
downloadtraccar-server-aa4248e73510c14eb64cfbb3c20cf65da805cd50.tar.gz
traccar-server-aa4248e73510c14eb64cfbb3c20cf65da805cd50.tar.bz2
traccar-server-aa4248e73510c14eb64cfbb3c20cf65da805cd50.zip
Implement daily summary report
-rw-r--r--src/main/java/org/traccar/api/resource/ReportResource.java10
-rw-r--r--src/main/java/org/traccar/reports/Summary.java47
-rw-r--r--src/main/java/org/traccar/reports/model/BaseReport.java22
-rw-r--r--src/main/java/org/traccar/reports/model/StopReport.java20
-rw-r--r--src/main/java/org/traccar/reports/model/TripReport.java20
-rw-r--r--templates/export/summary.xlsxbin15311 -> 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<SummaryReport> getSummary(
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> 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<Long> deviceIds, @QueryParam("groupId") final List<Long> 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<Position> positions) {
SummaryReport result = new SummaryReport();
result.setDeviceId(deviceId);
result.setDeviceName(Context.getIdentityManager().getById(deviceId).getName());
- Collection<Position> 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<SummaryReport> calculateSummaryResults(
+ long userId, long deviceId, Date from, Date to, boolean daily) throws SQLException {
+
+ ArrayList<Position> positions = new ArrayList<>(Context.getDataManager().getPositions(deviceId, from, to));
+
+ ArrayList<SummaryReport> 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<SummaryReport> getObjects(long userId, Collection<Long> deviceIds,
- Collection<Long> groupIds, Date from, Date to) throws SQLException {
+ Collection<Long> groupIds, Date from, Date to, boolean daily) throws SQLException {
ReportUtils.checkPeriodLimit(from, to);
ArrayList<SummaryReport> 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<Long> deviceIds, Collection<Long> groupIds,
- Date from, Date to) throws SQLException, IOException {
+ Date from, Date to, boolean daily) throws SQLException, IOException {
ReportUtils.checkPeriodLimit(from, to);
- Collection<SummaryReport> summaries = getObjects(userId, deviceIds, groupIds, from, to);
+ Collection<SummaryReport> 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
--- a/templates/export/summary.xlsx
+++ b/templates/export/summary.xlsx
Binary files differ