diff options
author | Anton Tananaev <anton@traccar.org> | 2022-06-18 12:22:21 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-06-18 12:22:21 -0700 |
commit | 24140619789c88d96364673240b172bbd2ae2c82 (patch) | |
tree | 30b0ec3b091bf95e6edea42297b4163d15b491c4 /src/main/java/org/traccar | |
parent | 895e89504ff0ef6fe05e2a74847f9aa582a9d270 (diff) | |
download | trackermap-server-24140619789c88d96364673240b172bbd2ae2c82.tar.gz trackermap-server-24140619789c88d96364673240b172bbd2ae2c82.tar.bz2 trackermap-server-24140619789c88d96364673240b172bbd2ae2c82.zip |
Refactor reports dependencies
Diffstat (limited to 'src/main/java/org/traccar')
8 files changed, 54 insertions, 51 deletions
diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index dba0357c9..5f555858d 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -23,7 +23,6 @@ import org.traccar.database.PermissionsManager; import org.traccar.helper.Log; import org.traccar.model.BaseModel; import org.traccar.model.Device; -import org.traccar.storage.Storage; public final class Context { diff --git a/src/main/java/org/traccar/database/PermissionsManager.java b/src/main/java/org/traccar/database/PermissionsManager.java index 0ff20775a..c52c72c4d 100644 --- a/src/main/java/org/traccar/database/PermissionsManager.java +++ b/src/main/java/org/traccar/database/PermissionsManager.java @@ -22,11 +22,7 @@ import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.model.Permission; 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 java.util.HashMap; import java.util.HashSet; diff --git a/src/main/java/org/traccar/reports/EventsReportProvider.java b/src/main/java/org/traccar/reports/EventsReportProvider.java index 69d95d1be..878c0265d 100644 --- a/src/main/java/org/traccar/reports/EventsReportProvider.java +++ b/src/main/java/org/traccar/reports/EventsReportProvider.java @@ -73,11 +73,10 @@ public class EventsReportProvider { long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Collection<String> types, Date from, Date to) throws StorageException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList<Event> result = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - Collection<Event> events = getEvents(deviceId, from, to); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + Collection<Event> events = getEvents(device.getId(), from, to); boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS); for (Event event : events) { if (all || types.contains(event.getType())) { @@ -98,14 +97,13 @@ public class EventsReportProvider { OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Collection<String> types, Date from, Date to) throws StorageException, IOException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList<DeviceReportSection> devicesEvents = new ArrayList<>(); ArrayList<String> sheetNames = new ArrayList<>(); HashMap<Long, String> geofenceNames = new HashMap<>(); HashMap<Long, String> maintenanceNames = new HashMap<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - Collection<Event> events = getEvents(deviceId, from, to); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + Collection<Event> events = getEvents(device.getId(), from, to); boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS); for (Iterator<Event> iterator = events.iterator(); iterator.hasNext();) { Event event = iterator.next(); @@ -132,7 +130,6 @@ public class EventsReportProvider { } } DeviceReportSection deviceEvents = new DeviceReportSection(); - Device device = reportUtils.getDevice(deviceId); deviceEvents.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceEvents.getDeviceName())); if (device.getGroupId() > 0) { diff --git a/src/main/java/org/traccar/reports/RouteReportProvider.java b/src/main/java/org/traccar/reports/RouteReportProvider.java index 2364cc0f3..0f618822e 100644 --- a/src/main/java/org/traccar/reports/RouteReportProvider.java +++ b/src/main/java/org/traccar/reports/RouteReportProvider.java @@ -58,11 +58,10 @@ public class RouteReportProvider { public Collection<Position> getObjects(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws StorageException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList<Position> result = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - result.addAll(PositionUtil.getPositions(storage, deviceId, from, to)); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + result.addAll(PositionUtil.getPositions(storage, device.getId(), from, to)); } return result; } @@ -71,14 +70,12 @@ public class RouteReportProvider { long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws StorageException, IOException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList<DeviceReportSection> devicesRoutes = new ArrayList<>(); ArrayList<String> sheetNames = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - var positions = PositionUtil.getPositions(storage, deviceId, from, to); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + var positions = PositionUtil.getPositions(storage, device.getId(), from, to); DeviceReportSection deviceRoutes = new DeviceReportSection(); - Device device = reportUtils.getDevice(deviceId); deviceRoutes.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceRoutes.getDeviceName())); if (device.getGroupId() > 0) { diff --git a/src/main/java/org/traccar/reports/StopsReportProvider.java b/src/main/java/org/traccar/reports/StopsReportProvider.java index 3b1f3c1fe..6e4cd74d6 100644 --- a/src/main/java/org/traccar/reports/StopsReportProvider.java +++ b/src/main/java/org/traccar/reports/StopsReportProvider.java @@ -65,11 +65,10 @@ public class StopsReportProvider { long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws StorageException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList<StopReportItem> result = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - result.addAll(detectStops(deviceId, from, to)); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + result.addAll(detectStops(device.getId(), from, to)); } return result; } @@ -78,14 +77,12 @@ public class StopsReportProvider { OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws StorageException, IOException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList<DeviceReportSection> devicesStops = new ArrayList<>(); ArrayList<String> sheetNames = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - Collection<StopReportItem> stops = detectStops(deviceId, from, to); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + Collection<StopReportItem> stops = detectStops(device.getId(), from, to); DeviceReportSection deviceStops = new DeviceReportSection(); - Device device = reportUtils.getDevice(deviceId); deviceStops.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceStops.getDeviceName())); if (device.getGroupId() > 0) { diff --git a/src/main/java/org/traccar/reports/SummaryReportProvider.java b/src/main/java/org/traccar/reports/SummaryReportProvider.java index 1f136adeb..26d79c899 100644 --- a/src/main/java/org/traccar/reports/SummaryReportProvider.java +++ b/src/main/java/org/traccar/reports/SummaryReportProvider.java @@ -23,6 +23,7 @@ import org.traccar.config.Keys; import org.traccar.helper.UnitsConverter; import org.traccar.helper.model.PositionUtil; import org.traccar.helper.model.UserUtil; +import org.traccar.model.Device; import org.traccar.model.Position; import org.traccar.reports.common.ReportUtils; import org.traccar.reports.model.SummaryReportItem; @@ -145,11 +146,11 @@ public class SummaryReportProvider { long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to, boolean daily) throws StorageException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList<SummaryReportItem> result = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - Collection<SummaryReportItem> deviceResults = calculateSummaryResults(userId, deviceId, from, to, daily); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + Collection<SummaryReportItem> deviceResults = + calculateSummaryResults(userId, device.getId(), from, to, daily); for (SummaryReportItem summaryReport : deviceResults) { if (summaryReport.getStartTime() != null && summaryReport.getEndTime() != null) { result.add(summaryReport); diff --git a/src/main/java/org/traccar/reports/TripsReportProvider.java b/src/main/java/org/traccar/reports/TripsReportProvider.java index 95612ab30..7d10879b7 100644 --- a/src/main/java/org/traccar/reports/TripsReportProvider.java +++ b/src/main/java/org/traccar/reports/TripsReportProvider.java @@ -64,11 +64,10 @@ public class TripsReportProvider { public Collection<TripReportItem> getObjects(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws StorageException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList<TripReportItem> result = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - result.addAll(detectTrips(deviceId, from, to)); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + result.addAll(detectTrips(device.getId(), from, to)); } return result; } @@ -77,14 +76,12 @@ public class TripsReportProvider { long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws StorageException, IOException { reportUtils.checkPeriodLimit(from, to); - reportUtils.checkPermissions(userId, deviceIds, groupIds); ArrayList<DeviceReportSection> devicesTrips = new ArrayList<>(); ArrayList<String> sheetNames = new ArrayList<>(); - for (long deviceId: reportUtils.getDeviceList(deviceIds, groupIds)) { - Collection<TripReportItem> trips = detectTrips(deviceId, from, to); + for (Device device: reportUtils.getAccessibleDevices(userId, deviceIds, groupIds)) { + Collection<TripReportItem> trips = detectTrips(device.getId(), from, to); DeviceReportSection deviceTrips = new DeviceReportSection(); - Device device = reportUtils.getDevice(deviceId); deviceTrips.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceTrips.getDeviceName())); if (device.getGroupId() > 0) { diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java index ab009161f..3960546e9 100644 --- a/src/main/java/org/traccar/reports/common/ReportUtils.java +++ b/src/main/java/org/traccar/reports/common/ReportUtils.java @@ -26,7 +26,6 @@ import org.jxls.formula.StandardFormulaProcessor; import org.jxls.transform.Transformer; import org.jxls.transform.poi.PoiTransformer; import org.jxls.util.TransformerFactory; -import org.traccar.Context; import org.traccar.api.security.PermissionsService; import org.traccar.config.Config; import org.traccar.config.Keys; @@ -62,11 +61,14 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Date; -import java.util.LinkedHashSet; +import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; public class ReportUtils { @@ -114,22 +116,39 @@ public class ReportUtils { } } - public void checkPermissions( + public Collection<Device> getAccessibleDevices( long userId, Collection<Long> deviceIds, Collection<Long> groupIds) throws StorageException { - for (long deviceId : deviceIds) { - permissionsService.checkPermission(Device.class, userId, deviceId); - } - for (long groupId : groupIds) { - permissionsService.checkPermission(Group.class, userId, groupId); - } - } - public Collection<Long> getDeviceList(Collection<Long> deviceIds, Collection<Long> groupIds) { - Collection<Long> result = new LinkedHashSet<>(deviceIds); - for (long groupId : groupIds) { - result.addAll(Context.getPermissionsManager().getGroupDevices(groupId)); + var devices = storage.getObjects(Device.class, new Request( + new Columns.All(), + new Condition.Permission(User.class, userId, Device.class))); + var deviceById = devices.stream() + .collect(Collectors.toUnmodifiableMap(Device::getId, x -> x)); + var devicesByGroup = devices.stream() + .filter(x -> x.getGroupId() > 0) + .collect(Collectors.groupingBy(Device::getGroupId)); + + var groups = storage.getObjects(Group.class, new Request( + new Columns.All(), + new Condition.Permission(User.class, userId, Group.class))); + var groupsByGroup = groups.stream() + .filter(x -> x.getGroupId() > 0) + .collect(Collectors.groupingBy(Group::getGroupId)); + + var results = deviceIds.stream() + .map(deviceById::get) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + + var groupQueue = new LinkedList<>(groupIds); + while (!groupQueue.isEmpty()) { + long groupId = groupQueue.pop(); + results.addAll(devicesByGroup.getOrDefault(groupId, Collections.emptyList())); + groupQueue.addAll(groupsByGroup.getOrDefault(groupId, Collections.emptyList()) + .stream().map(Group::getId).collect(Collectors.toUnmodifiableList())); } - return result; + + return results; } public double calculateFuel(Position firstPosition, Position lastPosition) { |