From 886efe231e79255aa86a0056e7bf912fff6069fb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 24 May 2023 15:37:04 -0700 Subject: Query positions in report utils --- .../org/traccar/reports/StopsReportProvider.java | 11 ++---- .../org/traccar/reports/TripsReportProvider.java | 11 ++---- .../org/traccar/reports/common/ReportUtils.java | 10 +++--- .../java/org/traccar/reports/ReportUtilsTest.java | 39 +++++++++++++--------- 4 files changed, 32 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/traccar/reports/StopsReportProvider.java b/src/main/java/org/traccar/reports/StopsReportProvider.java index a23cee48b..57c57079d 100644 --- a/src/main/java/org/traccar/reports/StopsReportProvider.java +++ b/src/main/java/org/traccar/reports/StopsReportProvider.java @@ -20,7 +20,6 @@ import org.apache.poi.ss.util.WorkbookUtil; import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.helper.model.DeviceUtil; -import org.traccar.helper.model.PositionUtil; import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.reports.common.ReportUtils; @@ -56,12 +55,6 @@ public class StopsReportProvider { this.storage = storage; } - private Collection detectStops(Device device, Date from, Date to) throws StorageException { - boolean ignoreOdometer = config.getBoolean(Keys.REPORT_IGNORE_ODOMETER); - var positions = PositionUtil.getPositions(storage, device.getId(), from, to); - return reportUtils.detectTripsAndStops(device, positions, ignoreOdometer, StopReportItem.class); - } - public Collection getObjects( long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws StorageException { @@ -69,7 +62,7 @@ public class StopsReportProvider { ArrayList result = new ArrayList<>(); for (Device device: DeviceUtil.getAccessibleDevices(storage, userId, deviceIds, groupIds)) { - result.addAll(detectStops(device, from, to)); + result.addAll(reportUtils.detectTripsAndStops(device, from, to, StopReportItem.class)); } return result; } @@ -82,7 +75,7 @@ public class StopsReportProvider { ArrayList devicesStops = new ArrayList<>(); ArrayList sheetNames = new ArrayList<>(); for (Device device: DeviceUtil.getAccessibleDevices(storage, userId, deviceIds, groupIds)) { - Collection stops = detectStops(device, from, to); + Collection stops = reportUtils.detectTripsAndStops(device, from, to, StopReportItem.class); DeviceReportSection deviceStops = new DeviceReportSection(); deviceStops.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceStops.getDeviceName())); diff --git a/src/main/java/org/traccar/reports/TripsReportProvider.java b/src/main/java/org/traccar/reports/TripsReportProvider.java index 2d8989b7a..e6c3e7ffd 100644 --- a/src/main/java/org/traccar/reports/TripsReportProvider.java +++ b/src/main/java/org/traccar/reports/TripsReportProvider.java @@ -20,7 +20,6 @@ import org.apache.poi.ss.util.WorkbookUtil; import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.helper.model.DeviceUtil; -import org.traccar.helper.model.PositionUtil; import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.reports.common.ReportUtils; @@ -56,12 +55,6 @@ public class TripsReportProvider { this.storage = storage; } - private Collection detectTrips(Device device, Date from, Date to) throws StorageException { - boolean ignoreOdometer = config.getBoolean(Keys.REPORT_IGNORE_ODOMETER); - var positions = PositionUtil.getPositions(storage, device.getId(), from, to); - return reportUtils.detectTripsAndStops(device, positions, ignoreOdometer, TripReportItem.class); - } - public Collection getObjects( long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws StorageException { @@ -69,7 +62,7 @@ public class TripsReportProvider { ArrayList result = new ArrayList<>(); for (Device device: DeviceUtil.getAccessibleDevices(storage, userId, deviceIds, groupIds)) { - result.addAll(detectTrips(device, from, to)); + result.addAll(reportUtils.detectTripsAndStops(device, from, to, TripReportItem.class)); } return result; } @@ -82,7 +75,7 @@ public class TripsReportProvider { ArrayList devicesTrips = new ArrayList<>(); ArrayList sheetNames = new ArrayList<>(); for (Device device: DeviceUtil.getAccessibleDevices(storage, userId, deviceIds, groupIds)) { - Collection trips = detectTrips(device, from, to); + Collection trips = reportUtils.detectTripsAndStops(device, from, to, TripReportItem.class); DeviceReportSection deviceTrips = new DeviceReportSection(); deviceTrips.setDeviceName(device.getName()); sheetNames.add(WorkbookUtil.createSafeSheetName(deviceTrips.getDeviceName())); diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java index 6c2d5cd72..a930f9439 100644 --- a/src/main/java/org/traccar/reports/common/ReportUtils.java +++ b/src/main/java/org/traccar/reports/common/ReportUtils.java @@ -58,7 +58,6 @@ import java.io.OutputStream; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; -import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Locale; @@ -291,14 +290,15 @@ public class ReportUtils { return positions.get(index).getBoolean(Position.KEY_MOTION); } - public Collection detectTripsAndStops( - Device device, List positions, boolean ignoreOdometer, - Class reportClass) throws StorageException { + public List detectTripsAndStops( + Device device, Date from, Date to, Class reportClass) throws StorageException { - Collection result = new ArrayList<>(); + List result = new ArrayList<>(); TripsConfig tripsConfig = new TripsConfig( new AttributeUtil.StorageProvider(config, storage, permissionsService, device)); + boolean ignoreOdometer = config.getBoolean(Keys.REPORT_IGNORE_ODOMETER); + var positions = PositionUtil.getPositions(storage, device.getId(), from, to); if (!positions.isEmpty()) { boolean trips = reportClass.equals(TripReportItem.class); diff --git a/src/test/java/org/traccar/reports/ReportUtilsTest.java b/src/test/java/org/traccar/reports/ReportUtilsTest.java index afb0a516f..d11d2b2e1 100644 --- a/src/test/java/org/traccar/reports/ReportUtilsTest.java +++ b/src/test/java/org/traccar/reports/ReportUtilsTest.java @@ -2,7 +2,6 @@ package org.traccar.reports; import org.apache.velocity.app.VelocityEngine; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.traccar.BaseTest; import org.traccar.api.security.PermissionsService; @@ -21,7 +20,6 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; -import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.List; @@ -33,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -43,7 +42,7 @@ public class ReportUtilsTest extends BaseTest { @BeforeEach public void init() throws StorageException { storage = mock(Storage.class); - when(storage.getObject(any(), any())).thenReturn(mock(Device.class)); + when(storage.getObject(eq(Device.class), any())).thenReturn(mock(Device.class)); } private Date date(String time) throws ParseException { @@ -114,12 +113,13 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:05:00.000", 0, 3000), position("2016-01-01 00:15:00.000", 0, 3000), position("2016-01-01 00:25:00.000", 0, 3000)); + when(storage.getObjects(eq(Position.class), any())).thenReturn(data); Device device = mockDevice(500, 300, 180, 900, false); ReportUtils reportUtils = new ReportUtils( mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null); - var trips = reportUtils.detectTripsAndStops(device, data, false, TripReportItem.class); + var trips = reportUtils.detectTripsAndStops(device, new Date(), new Date(), TripReportItem.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -133,7 +133,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(3000, itemTrip.getDistance(), 0.01); - var stops = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class); + var stops = reportUtils.detectTripsAndStops(device, new Date(), new Date(), StopReportItem.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -166,6 +166,7 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:05:00.000", 0, 3000), position("2016-01-01 00:15:00.000", 0, 3000), position("2016-01-01 00:25:00.000", 0, 3000)); + when(storage.getObjects(eq(Position.class), any())).thenReturn(data); data.get(5).set(Position.KEY_IGNITION, false); @@ -173,7 +174,7 @@ public class ReportUtilsTest extends BaseTest { ReportUtils reportUtils = new ReportUtils( mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null); - var trips = reportUtils.detectTripsAndStops(device, data, false, TripReportItem.class); + var trips = reportUtils.detectTripsAndStops(device, new Date(), new Date(), TripReportItem.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -187,7 +188,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(3000, itemTrip.getDistance(), 0.01); - trips = reportUtils.detectTripsAndStops(device, data, false, TripReportItem.class); + trips = reportUtils.detectTripsAndStops(device, new Date(), new Date(), TripReportItem.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -201,7 +202,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(3000, itemTrip.getDistance(), 0.01); - var stops = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class); + var stops = reportUtils.detectTripsAndStops(device, new Date(), new Date(), StopReportItem.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -238,12 +239,13 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:09:00.000", 0, 7000), position("2016-01-01 00:19:00.000", 0, 7000), position("2016-01-01 00:29:00.000", 0, 7000)); + when(storage.getObjects(eq(Position.class), any())).thenReturn(data); Device device = mockDevice(500, 300, 180, 900, false); ReportUtils reportUtils = new ReportUtils( mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null); - var trips = reportUtils.detectTripsAndStops(device, data, false, TripReportItem.class); + var trips = reportUtils.detectTripsAndStops(device, new Date(), new Date(), TripReportItem.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -257,7 +259,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(7000, itemTrip.getDistance(), 0.01); - var stops = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class); + var stops = reportUtils.detectTripsAndStops(device, new Date(), new Date(), StopReportItem.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -288,12 +290,13 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:03:00.000", 0, 0), position("2016-01-01 00:04:00.000", 1, 0), position("2016-01-01 00:05:00.000", 0, 0)); + when(storage.getObjects(eq(Position.class), any())).thenReturn(data); Device device = mockDevice(500, 300, 200, 900, false); ReportUtils reportUtils = new ReportUtils( mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null); - var result = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class); + var result = reportUtils.detectTripsAndStops(device, new Date(), new Date(), StopReportItem.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -316,12 +319,13 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:03:00.000", 0, 0), position("2016-01-01 00:04:00.000", 1, 0), position("2016-01-01 00:05:00.000", 2, 0)); + when(storage.getObjects(eq(Position.class), any())).thenReturn(data); Device device = mockDevice(500, 300, 200, 900, false); ReportUtils reportUtils = new ReportUtils( mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null); - var result = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class); + var result = reportUtils.detectTripsAndStops(device, new Date(), new Date(), StopReportItem.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -344,12 +348,13 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:12:00.000", 0, 0), position("2016-01-01 00:22:00.000", 0, 0), position("2016-01-01 00:32:00.000", 0, 0)); + when(storage.getObjects(eq(Position.class), any())).thenReturn(data); Device device = mockDevice(500, 300, 200, 900, false); ReportUtils reportUtils = new ReportUtils( mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null); - var result = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class); + var result = reportUtils.detectTripsAndStops(device, new Date(), new Date(), StopReportItem.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -372,12 +377,13 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:03:00.000", 5, 0), position("2016-01-01 00:04:00.000", 5, 0), position("2016-01-01 00:05:00.000", 5, 0)); + when(storage.getObjects(eq(Position.class), any())).thenReturn(data); Device device = mockDevice(500, 300, 200, 900, false); ReportUtils reportUtils = new ReportUtils( mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null); - var result = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class); + var result = reportUtils.detectTripsAndStops(device, new Date(), new Date(), StopReportItem.class); assertNotNull(result); assertTrue(result.isEmpty()); @@ -396,12 +402,13 @@ public class ReportUtilsTest extends BaseTest { position("2016-01-01 00:23:00.000", 2, 700), position("2016-01-01 00:24:00.000", 5, 800), position("2016-01-01 00:25:00.000", 5, 900)); + when(storage.getObjects(eq(Position.class), any())).thenReturn(data); Device device = mockDevice(500, 200, 200, 900, false); ReportUtils reportUtils = new ReportUtils( mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null); - var trips = reportUtils.detectTripsAndStops(device, data, false, TripReportItem.class); + var trips = reportUtils.detectTripsAndStops(device, new Date(), new Date(), TripReportItem.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -415,7 +422,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(7, itemTrip.getMaxSpeed(), 0.01); assertEquals(600, itemTrip.getDistance(), 0.01); - var stops = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class); + var stops = reportUtils.detectTripsAndStops(device, new Date(), new Date(), StopReportItem.class); assertNotNull(stops); assertFalse(stops.isEmpty()); -- cgit v1.2.3