Skip to content

Commit

Permalink
Add scheduled reports logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Apr 8, 2024
1 parent 569bb18 commit b017ab2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
22 changes: 11 additions & 11 deletions src/main/java/org/traccar/api/resource/ReportResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Collection<CombinedReportItem> getCombined(
@QueryParam("from") Date from,
@QueryParam("to") Date to) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
LogAction.logReport(getUserId(), "combined", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "combined", from, to, deviceIds, groupIds);
return combinedReportProvider.getObjects(getUserId(), deviceIds, groupIds, from, to);
}

Expand All @@ -125,7 +125,7 @@ public Collection<Position> getRoute(
@QueryParam("from") Date from,
@QueryParam("to") Date to) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
LogAction.logReport(getUserId(), "route", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "route", from, to, deviceIds, groupIds);
return routeReportProvider.getObjects(getUserId(), deviceIds, groupIds, from, to);
}

Expand All @@ -140,7 +140,7 @@ public Response getRouteExcel(
@QueryParam("mail") boolean mail) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
return executeReport(getUserId(), mail, stream -> {
LogAction.logReport(getUserId(), "route", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "route", from, to, deviceIds, groupIds);
routeReportProvider.getExcel(stream, getUserId(), deviceIds, groupIds, from, to);
});
}
Expand All @@ -166,7 +166,7 @@ public Collection<Event> getEvents(
@QueryParam("from") Date from,
@QueryParam("to") Date to) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
LogAction.logReport(getUserId(), "events", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "events", from, to, deviceIds, groupIds);
return eventsReportProvider.getObjects(getUserId(), deviceIds, groupIds, types, from, to);
}

Expand All @@ -182,7 +182,7 @@ public Response getEventsExcel(
@QueryParam("mail") boolean mail) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
return executeReport(getUserId(), mail, stream -> {
LogAction.logReport(getUserId(), "events", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "events", from, to, deviceIds, groupIds);
eventsReportProvider.getExcel(stream, getUserId(), deviceIds, groupIds, types, from, to);
});
}
Expand All @@ -209,7 +209,7 @@ public Collection<SummaryReportItem> getSummary(
@QueryParam("to") Date to,
@QueryParam("daily") boolean daily) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
LogAction.logReport(getUserId(), "summary", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "summary", from, to, deviceIds, groupIds);
return summaryReportProvider.getObjects(getUserId(), deviceIds, groupIds, from, to, daily);
}

Expand All @@ -225,7 +225,7 @@ public Response getSummaryExcel(
@QueryParam("mail") boolean mail) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
return executeReport(getUserId(), mail, stream -> {
LogAction.logReport(getUserId(), "summary", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "summary", from, to, deviceIds, groupIds);
summaryReportProvider.getExcel(stream, getUserId(), deviceIds, groupIds, from, to, daily);
});
}
Expand All @@ -251,7 +251,7 @@ public Collection<TripReportItem> getTrips(
@QueryParam("from") Date from,
@QueryParam("to") Date to) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
LogAction.logReport(getUserId(), "trips", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "trips", from, to, deviceIds, groupIds);
return tripsReportProvider.getObjects(getUserId(), deviceIds, groupIds, from, to);
}

Expand All @@ -266,7 +266,7 @@ public Response getTripsExcel(
@QueryParam("mail") boolean mail) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
return executeReport(getUserId(), mail, stream -> {
LogAction.logReport(getUserId(), "trips", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "trips", from, to, deviceIds, groupIds);
tripsReportProvider.getExcel(stream, getUserId(), deviceIds, groupIds, from, to);
});
}
Expand All @@ -291,7 +291,7 @@ public Collection<StopReportItem> getStops(
@QueryParam("from") Date from,
@QueryParam("to") Date to) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
LogAction.logReport(getUserId(), "stops", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "stops", from, to, deviceIds, groupIds);
return stopsReportProvider.getObjects(getUserId(), deviceIds, groupIds, from, to);
}

Expand All @@ -306,7 +306,7 @@ public Response getStopsExcel(
@QueryParam("mail") boolean mail) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
return executeReport(getUserId(), mail, stream -> {
LogAction.logReport(getUserId(), "stops", from, to, deviceIds, groupIds);
LogAction.logReport(getUserId(), false, "stops", from, to, deviceIds, groupIds);
stopsReportProvider.getExcel(stream, getUserId(), deviceIds, groupIds, from, to);
});
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/traccar/helper/LogAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private LogAction() {
private static final String PATTERN_LOGIN = "user: %d, action: %s, from: %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";
private static final String PATTERN_REPORT = "user: %d, %s: %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());
Expand Down Expand Up @@ -113,10 +113,11 @@ private static void logLoginAction(String action, long userId, String remoteAddr
}

public static void logReport(
long userId, String report, Date from, Date to, List<Long> deviceIds, List<Long> groupIds) {
long userId, boolean scheduled, 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,
PATTERN_REPORT, userId, scheduled ? "scheduled" : "report", report,
dateFormat.format(from), dateFormat.format(to),
deviceIds.toString(), groupIds.toString()));
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/traccar/schedule/TaskReports.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2023 - 2024 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.
Expand All @@ -21,6 +21,7 @@
import net.fortuna.ical4j.model.Period;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.helper.LogAction;
import org.traccar.model.BaseModel;
import org.traccar.model.Calendar;
import org.traccar.model.Device;
Expand Down Expand Up @@ -110,6 +111,7 @@ private void executeReport(Report report, Date from, Date to) throws StorageExce
ReportMailer reportMailer = injector.getInstance(ReportMailer.class);

for (User user : users) {
LogAction.logReport(user.getId(), true, report.getType(), from, to, deviceIds, groupIds);
switch (report.getType()) {
case "events":
var eventsReportProvider = injector.getInstance(EventsReportProvider.class);
Expand Down

0 comments on commit b017ab2

Please sign in to comment.