aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/traccar/api/resource/ReportResource.java39
-rw-r--r--src/main/java/org/traccar/reports/common/ReportExecutor.java25
-rw-r--r--src/main/java/org/traccar/reports/common/ReportMailer.java70
-rw-r--r--src/main/java/org/traccar/schedule/ScheduleManager.java10
-rw-r--r--src/main/java/org/traccar/schedule/TaskReports.java154
5 files changed, 262 insertions, 36 deletions
diff --git a/src/main/java/org/traccar/api/resource/ReportResource.java b/src/main/java/org/traccar/api/resource/ReportResource.java
index 70177dd4d..6944de9cb 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 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2016 - 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,27 +19,23 @@ package org.traccar.api.resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.api.BaseResource;
-import org.traccar.mail.MailManager;
import org.traccar.helper.LogAction;
import org.traccar.model.Event;
import org.traccar.model.Position;
-import org.traccar.model.User;
import org.traccar.model.UserRestrictions;
import org.traccar.reports.EventsReportProvider;
import org.traccar.reports.RouteReportProvider;
import org.traccar.reports.StopsReportProvider;
import org.traccar.reports.SummaryReportProvider;
import org.traccar.reports.TripsReportProvider;
+import org.traccar.reports.common.ReportExecutor;
+import org.traccar.reports.common.ReportMailer;
import org.traccar.reports.model.StopReportItem;
import org.traccar.reports.model.SummaryReportItem;
import org.traccar.reports.model.TripReportItem;
import org.traccar.storage.StorageException;
-import javax.activation.DataHandler;
import javax.inject.Inject;
-import javax.mail.MessagingException;
-import javax.mail.internet.MimeBodyPart;
-import javax.mail.util.ByteArrayDataSource;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@@ -51,9 +47,6 @@ import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
import java.util.Collection;
import java.util.Date;
import java.util.List;
@@ -83,31 +76,11 @@ public class ReportResource extends BaseResource {
private TripsReportProvider tripsReportProvider;
@Inject
- private MailManager mailManager;
+ private ReportMailer reportMailer;
- private interface ReportExecutor {
- void execute(OutputStream stream) throws StorageException, IOException;
- }
-
- private Response executeReport(
- long userId, boolean mail, ReportExecutor executor) {
+ private Response executeReport(long userId, boolean mail, ReportExecutor executor) {
if (mail) {
- new Thread(() -> {
- try {
- var stream = new ByteArrayOutputStream();
- executor.execute(stream);
-
- MimeBodyPart attachment = new MimeBodyPart();
- attachment.setFileName("report.xlsx");
- attachment.setDataHandler(new DataHandler(new ByteArrayDataSource(
- stream.toByteArray(), "application/octet-stream")));
-
- User user = permissionsService.getUser(userId);
- mailManager.sendMessage(user, "Report", "The report is in the attachment.", attachment);
- } catch (StorageException | IOException | MessagingException e) {
- LOGGER.warn("Report failed", e);
- }
- }).start();
+ reportMailer.sendAsync(userId, executor);
return Response.noContent().build();
} else {
StreamingOutput stream = output -> {
diff --git a/src/main/java/org/traccar/reports/common/ReportExecutor.java b/src/main/java/org/traccar/reports/common/ReportExecutor.java
new file mode 100644
index 000000000..aed4b8c23
--- /dev/null
+++ b/src/main/java/org/traccar/reports/common/ReportExecutor.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2023 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.reports.common;
+
+import org.traccar.storage.StorageException;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+public interface ReportExecutor {
+ void execute(OutputStream stream) throws StorageException, IOException;
+}
diff --git a/src/main/java/org/traccar/reports/common/ReportMailer.java b/src/main/java/org/traccar/reports/common/ReportMailer.java
new file mode 100644
index 000000000..32bbd4b17
--- /dev/null
+++ b/src/main/java/org/traccar/reports/common/ReportMailer.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2023 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.reports.common;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.traccar.api.security.PermissionsService;
+import org.traccar.mail.MailManager;
+import org.traccar.model.User;
+import org.traccar.storage.Storage;
+import org.traccar.storage.StorageException;
+import org.traccar.storage.query.Columns;
+import org.traccar.storage.query.Condition;
+import org.traccar.storage.query.Request;
+
+import javax.activation.DataHandler;
+import javax.inject.Inject;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.util.ByteArrayDataSource;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+public class ReportMailer {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ReportMailer.class);
+
+ private final Storage storage;
+ private final MailManager mailManager;
+
+ @Inject
+ public ReportMailer(Storage storage, MailManager mailManager) {
+ this.storage = storage;
+ this.mailManager = mailManager;
+ }
+
+ public void sendAsync(long userId, ReportExecutor executor) {
+ new Thread(() -> {
+ try {
+ var stream = new ByteArrayOutputStream();
+ executor.execute(stream);
+
+ MimeBodyPart attachment = new MimeBodyPart();
+ attachment.setFileName("report.xlsx");
+ attachment.setDataHandler(new DataHandler(new ByteArrayDataSource(
+ stream.toByteArray(), "application/octet-stream")));
+
+ User user = storage.getObject(
+ User.class, new Request(new Columns.All(), new Condition.Equals("id", userId)));
+ mailManager.sendMessage(user, "Report", "The report is in the attachment.", attachment);
+ } catch (StorageException | IOException | MessagingException e) {
+ LOGGER.warn("Email report failed", e);
+ }
+ }).start();
+ }
+
+}
diff --git a/src/main/java/org/traccar/schedule/ScheduleManager.java b/src/main/java/org/traccar/schedule/ScheduleManager.java
index 6412a186a..e1de3b3af 100644
--- a/src/main/java/org/traccar/schedule/ScheduleManager.java
+++ b/src/main/java/org/traccar/schedule/ScheduleManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2020 - 2023 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.
@@ -38,8 +38,12 @@ public class ScheduleManager implements LifecycleObject {
@Override
public void start() {
executor = Executors.newSingleThreadScheduledExecutor();
- List.of(TaskDeviceInactivityCheck.class, TaskWebSocketKeepalive.class, TaskHealthCheck.class)
- .forEach(task -> injector.getInstance(task).schedule(executor));
+ var tasks = List.of(
+ TaskReports.class,
+ TaskDeviceInactivityCheck.class,
+ TaskWebSocketKeepalive.class,
+ TaskHealthCheck.class);
+ tasks.forEach(task -> injector.getInstance(task).schedule(executor));
}
@Override
diff --git a/src/main/java/org/traccar/schedule/TaskReports.java b/src/main/java/org/traccar/schedule/TaskReports.java
new file mode 100644
index 000000000..259eb10d4
--- /dev/null
+++ b/src/main/java/org/traccar/schedule/TaskReports.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2023 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.schedule;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.traccar.model.BaseModel;
+import org.traccar.model.Calendar;
+import org.traccar.model.Device;
+import org.traccar.model.Group;
+import org.traccar.model.Report;
+import org.traccar.model.User;
+import org.traccar.reports.EventsReportProvider;
+import org.traccar.reports.RouteReportProvider;
+import org.traccar.reports.StopsReportProvider;
+import org.traccar.reports.SummaryReportProvider;
+import org.traccar.reports.TripsReportProvider;
+import org.traccar.reports.common.ReportMailer;
+import org.traccar.storage.Storage;
+import org.traccar.storage.StorageException;
+import org.traccar.storage.query.Columns;
+import org.traccar.storage.query.Condition;
+import org.traccar.storage.query.Request;
+
+import javax.inject.Inject;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+public class TaskReports implements ScheduleTask {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(TaskReports.class);
+
+ private static final long CHECK_PERIOD_MINUTES = 1;
+
+ private final Storage storage;
+ private final ReportMailer reportMailer;
+
+ @Inject
+ private EventsReportProvider eventsReportProvider;
+
+ @Inject
+ private RouteReportProvider routeReportProvider;
+
+ @Inject
+ private StopsReportProvider stopsReportProvider;
+
+ @Inject
+ private SummaryReportProvider summaryReportProvider;
+
+ @Inject
+ private TripsReportProvider tripsReportProvider;
+
+ @Inject
+ public TaskReports(Storage storage, ReportMailer reportMailer) {
+ this.storage = storage;
+ this.reportMailer = reportMailer;
+ }
+
+ @Override
+ public void schedule(ScheduledExecutorService executor) {
+ executor.scheduleAtFixedRate(this, CHECK_PERIOD_MINUTES, CHECK_PERIOD_MINUTES, TimeUnit.MINUTES);
+ }
+
+ @Override
+ public void run() {
+ Date currentCheck = new Date();
+ Date lastCheck = new Date(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(CHECK_PERIOD_MINUTES));
+
+ try {
+ for (Report report : storage.getObjects(Report.class, new Request(new Columns.All()))) {
+ Calendar calendar = storage.getObject(Calendar.class, new Request(
+ new Columns.All(), new Condition.Equals("id", report.getCalendarId())));
+ if (calendar.checkMoment(currentCheck) && !calendar.checkMoment(lastCheck)) {
+ executeReport(report);
+ }
+ }
+ } catch (StorageException e) {
+ LOGGER.warn("Scheduled reports error", e);
+ }
+ }
+
+ private void executeReport(Report report) throws StorageException {
+ var deviceIds = storage.getObjects(Device.class, new Request(
+ new Columns.Include("id"),
+ new Condition.Permission(Device.class, Report.class, report.getId())))
+ .stream().map(BaseModel::getId).collect(Collectors.toList());
+ var groupIds = storage.getObjects(Group.class, new Request(
+ new Columns.Include("id"),
+ new Condition.Permission(Group.class, Report.class, report.getId())))
+ .stream().map(BaseModel::getId).collect(Collectors.toList());
+ var users = storage.getObjects(User.class, new Request(
+ new Columns.Include("id"),
+ new Condition.Permission(User.class, Report.class, report.getId())));
+ for (User user : users) {
+ switch (report.getType()) {
+ case "events":
+ reportMailer.sendAsync(user.getId(), stream -> {
+ eventsReportProvider.getExcel(
+ stream, user.getId(), deviceIds, groupIds,
+ List.of(), report.getFrom(), report.getTo());
+ });
+ break;
+ case "route":
+ reportMailer.sendAsync(user.getId(), stream -> {
+ routeReportProvider.getExcel(
+ stream, user.getId(), deviceIds, groupIds,
+ report.getFrom(), report.getTo());
+ });
+ break;
+ case "summary":
+ reportMailer.sendAsync(user.getId(), stream -> {
+ summaryReportProvider.getExcel(
+ stream, user.getId(), deviceIds, groupIds,
+ report.getFrom(), report.getTo(), false);
+ });
+ break;
+ case "trips":
+ reportMailer.sendAsync(user.getId(), stream -> {
+ tripsReportProvider.getExcel(
+ stream, user.getId(), deviceIds, groupIds,
+ report.getFrom(), report.getTo());
+ });
+ break;
+ case "stops":
+ reportMailer.sendAsync(user.getId(), stream -> {
+ stopsReportProvider.getExcel(
+ stream, user.getId(), deviceIds, groupIds,
+ report.getFrom(), report.getTo());
+ });
+ break;
+ default:
+ LOGGER.warn("Unsupported report type {}", report.getType());
+ break;
+ }
+ }
+ }
+
+}