aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2023-04-22 09:11:06 -0700
committerAnton Tananaev <anton@traccar.org>2023-04-22 09:11:06 -0700
commitc2637180549e69fc302f91f4685036e2697ae7bf (patch)
treeda7066ae24501623863d86995689736b36a4702b /src
parentf92bde2088001c34186eb3897cece90171a319d1 (diff)
downloadtrackermap-server-c2637180549e69fc302f91f4685036e2697ae7bf.tar.gz
trackermap-server-c2637180549e69fc302f91f4685036e2697ae7bf.tar.bz2
trackermap-server-c2637180549e69fc302f91f4685036e2697ae7bf.zip
Fix report scheduling
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/traccar/model/Calendar.java13
-rw-r--r--src/main/java/org/traccar/schedule/TaskReports.java12
-rw-r--r--src/test/java/org/traccar/calendar/CalendarTest.java15
3 files changed, 22 insertions, 18 deletions
diff --git a/src/main/java/org/traccar/model/Calendar.java b/src/main/java/org/traccar/model/Calendar.java
index 62c51cc4a..03f1995ba 100644
--- a/src/main/java/org/traccar/model/Calendar.java
+++ b/src/main/java/org/traccar/model/Calendar.java
@@ -34,6 +34,7 @@ import java.time.Duration;
import java.util.Collection;
import java.util.Date;
import java.util.List;
+import java.util.stream.Collectors;
@StorageName("tc_calendars")
public class Calendar extends ExtendedModel {
@@ -68,16 +69,22 @@ public class Calendar extends ExtendedModel {
return calendar;
}
- public Collection<VEvent> findEvents(Date date) {
+ private Collection<VEvent> findEvents(Date date) {
if (calendar != null) {
- Period period = new Period(new DateTime(date), Duration.ZERO);
- Filter<VEvent> filter = new Filter<>(new PeriodRule<>(period));
+ var filter = new Filter<VEvent>(new PeriodRule<>(new Period(new DateTime(date), Duration.ZERO)));
return filter.filter(calendar.getComponents(CalendarComponent.VEVENT));
} else {
return List.of();
}
}
+ public Collection<Period> findPeriods(Date date) {
+ var calendarDate = new net.fortuna.ical4j.model.Date(date);
+ return findEvents(date).stream()
+ .flatMap((event) -> event.getConsumedTime(calendarDate, calendarDate).stream())
+ .collect(Collectors.toSet());
+ }
+
public boolean checkMoment(Date date) {
return !findEvents(date).isEmpty();
}
diff --git a/src/main/java/org/traccar/schedule/TaskReports.java b/src/main/java/org/traccar/schedule/TaskReports.java
index 004a6078c..176b6d537 100644
--- a/src/main/java/org/traccar/schedule/TaskReports.java
+++ b/src/main/java/org/traccar/schedule/TaskReports.java
@@ -18,7 +18,7 @@ package org.traccar.schedule;
import com.google.inject.Injector;
import com.google.inject.servlet.RequestScoper;
import com.google.inject.servlet.ServletScopes;
-import net.fortuna.ical4j.model.component.VEvent;
+import net.fortuna.ical4j.model.Period;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.model.BaseModel;
@@ -77,16 +77,14 @@ public class TaskReports implements ScheduleTask {
Calendar calendar = storage.getObject(Calendar.class, new Request(
new Columns.All(), new Condition.Equals("id", report.getCalendarId())));
- var lastEvents = calendar.findEvents(lastCheck);
- var currentEvents = calendar.findEvents(currentCheck);
+ var lastEvents = calendar.findPeriods(lastCheck);
+ var currentEvents = calendar.findPeriods(currentCheck);
if (!lastEvents.isEmpty() && currentEvents.isEmpty()) {
- VEvent event = lastEvents.iterator().next();
- Date from = event.getStartDate().getDate();
- Date to = event.getEndDate().getDate();
+ Period period = lastEvents.iterator().next();
RequestScoper scope = ServletScopes.scopeRequest(Collections.emptyMap());
try (RequestScoper.CloseableScope ignored = scope.open()) {
- executeReport(report, from, to);
+ executeReport(report, period.getStart(), period.getEnd());
}
}
}
diff --git a/src/test/java/org/traccar/calendar/CalendarTest.java b/src/test/java/org/traccar/calendar/CalendarTest.java
index 6f4b30370..4106f89a9 100644
--- a/src/test/java/org/traccar/calendar/CalendarTest.java
+++ b/src/test/java/org/traccar/calendar/CalendarTest.java
@@ -11,6 +11,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CalendarTest {
@@ -45,14 +46,12 @@ public class CalendarTest {
calendar.setData(calendarString.getBytes());
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX");
- Date date = format.parse("2016-12-13 22:59:59+05");
- assertTrue(!calendar.checkMoment(date));
- date = format.parse("2016-12-13 23:00:01+05");
- assertTrue(calendar.checkMoment(date));
+ assertFalse(calendar.checkMoment(format.parse("2016-12-13 22:59:59+05")));
+ assertTrue(calendar.checkMoment(format.parse("2016-12-13 23:00:01+05")));
+ assertTrue(calendar.checkMoment(format.parse("2016-12-13 06:59:59+05")));
+ assertFalse(calendar.checkMoment(format.parse("2016-12-13 07:00:01+05")));
- date = format.parse("2016-12-13 06:59:59+05");
- assertTrue(calendar.checkMoment(date));
- date = format.parse("2016-12-13 07:00:01+05");
- assertTrue(!calendar.checkMoment(date));
+ var periods = calendar.findPeriods(format.parse("2016-12-13 06:59:59+05"));
+ assertFalse(periods.isEmpty());
}
}