Skip to content

Commit

Permalink
Fix overlapping report schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Apr 20, 2024
1 parent 0a56837 commit 7b3fda2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/main/java/org/traccar/model/Calendar.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 - 2022 Anton Tananaev ([email protected])
* Copyright 2016 - 2024 Anton Tananaev ([email protected])
* Copyright 2016 Andrey Kunitsyn ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -79,9 +79,8 @@ private Collection<VEvent> findEvents(Date date) {
}

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())
.flatMap((e) -> e.calculateRecurrenceSet(new Period(new DateTime(date), Duration.ZERO)).stream())
.collect(Collectors.toSet());
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/traccar/schedule/TaskReports.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void run() {
var lastEvents = calendar.findPeriods(lastCheck);
var currentEvents = calendar.findPeriods(currentCheck);

if (!lastEvents.isEmpty() && currentEvents.isEmpty()) {
if (!lastEvents.isEmpty() && !currentEvents.equals(lastEvents)) {
Period period = lastEvents.iterator().next();
RequestScoper scope = ServletScopes.scopeRequest(Collections.emptyMap());
try (RequestScoper.CloseableScope ignored = scope.open()) {
Expand Down
38 changes: 35 additions & 3 deletions src/test/java/org/traccar/calendar/CalendarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import org.traccar.model.Calendar;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class CalendarTest {

@Test
public void testCalendar() throws IOException, ParserException, ParseException, SQLException {
public void testCalendar() throws IOException, ParserException, ParseException {
String calendarString = "BEGIN:VCALENDAR\n" +
"PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN\n" +
"VERSION:2.0\n" +
Expand Down Expand Up @@ -54,4 +54,36 @@ public void testCalendar() throws IOException, ParserException, ParseException,
var periods = calendar.findPeriods(format.parse("2016-12-13 06:59:59+05"));
assertFalse(periods.isEmpty());
}

@Test
public void testCalendarOverlap() throws IOException, ParserException, ParseException {
String calendarString = "BEGIN:VCALENDAR\n" +
"VERSION:2.0\n" +
"PRODID:-//Traccar//NONSGML Traccar//EN\n" +
"BEGIN:VEVENT\n" +
"UID:00000000-0000-0000-0000-000000000000\n" +
"DTSTART;TZID=America/Los_Angeles:20240420T060000\n" +
"DTEND;TZID=America/Los_Angeles:20240421T060000\n" +
"RRULE:FREQ=DAILY\n" +
"SUMMARY:Event\n" +
"END:VEVENT\n" +
"END:VCALENDAR";
Calendar calendar = new Calendar();
calendar.setData(calendarString.getBytes());
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX");

var periods0 = calendar.findPeriods(format.parse("2014-05-13 07:00:00-07"));
var periods1 = calendar.findPeriods(format.parse("2024-05-13 05:00:00-07"));
var periods2 = calendar.findPeriods(format.parse("2024-05-13 07:00:00-07"));
var periods3 = calendar.findPeriods(format.parse("2024-05-13 08:00:00-07"));

assertEquals(periods0.size(), 0);
assertEquals(periods1.size(), 1);
assertEquals(periods2.size(), 1);
assertEquals(periods3.size(), 1);

assertNotEquals(periods0, periods1);
assertNotEquals(periods1, periods2);
assertEquals(periods2, periods3);
}
}

0 comments on commit 7b3fda2

Please sign in to comment.