Use period from calendar

This commit is contained in:
Anton Tananaev 2023-01-26 15:19:47 -08:00
parent 7d1a4120eb
commit 18387265cd
4 changed files with 34 additions and 61 deletions

View File

@ -25,12 +25,6 @@
<column name="calendarid" type="INT"> <column name="calendarid" type="INT">
<constraints nullable="false" /> <constraints nullable="false" />
</column> </column>
<column name="from" type="TIMESTAMP">
<constraints nullable="false" />
</column>
<column name="to" type="TIMESTAMP">
<constraints nullable="false" />
</column>
<column name="attributes" type="VARCHAR(4000)"> <column name="attributes" type="VARCHAR(4000)">
<constraints nullable="false" /> <constraints nullable="false" />
</column> </column>

View File

@ -24,6 +24,7 @@ import net.fortuna.ical4j.filter.predicate.PeriodRule;
import net.fortuna.ical4j.model.DateTime; import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Period; import net.fortuna.ical4j.model.Period;
import net.fortuna.ical4j.model.component.CalendarComponent; import net.fortuna.ical4j.model.component.CalendarComponent;
import net.fortuna.ical4j.model.component.VEvent;
import org.traccar.storage.QueryIgnore; import org.traccar.storage.QueryIgnore;
import org.traccar.storage.StorageName; import org.traccar.storage.StorageName;
@ -32,6 +33,7 @@ import java.io.IOException;
import java.time.Duration; import java.time.Duration;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.List;
@StorageName("tc_calendars") @StorageName("tc_calendars")
public class Calendar extends ExtendedModel { public class Calendar extends ExtendedModel {
@ -66,14 +68,18 @@ public class Calendar extends ExtendedModel {
return calendar; return calendar;
} }
public boolean checkMoment(Date date) { public Collection<VEvent> findEvents(Date date) {
if (calendar != null) { if (calendar != null) {
Period period = new Period(new DateTime(date), Duration.ZERO); Period period = new Period(new DateTime(date), Duration.ZERO);
Filter<CalendarComponent> filter = new Filter<>(new PeriodRule<>(period)); Filter<VEvent> filter = new Filter<>(new PeriodRule<>(period));
Collection<CalendarComponent> events = filter.filter(calendar.getComponents(CalendarComponent.VEVENT)); return filter.filter(calendar.getComponents(CalendarComponent.VEVENT));
return events != null && !events.isEmpty(); } else {
return List.of();
} }
return false; }
public boolean checkMoment(Date date) {
return !findEvents(date).isEmpty();
} }
} }

View File

@ -17,8 +17,6 @@ package org.traccar.model;
import org.traccar.storage.StorageName; import org.traccar.storage.StorageName;
import java.util.Date;
@StorageName("tc_reports") @StorageName("tc_reports")
public class Report extends ScheduledModel { public class Report extends ScheduledModel {
@ -42,24 +40,4 @@ public class Report extends ScheduledModel {
this.description = description; this.description = description;
} }
private Date from;
public Date getFrom() {
return from;
}
public void setFrom(Date from) {
this.from = from;
}
private Date to;
public Date getTo() {
return to;
}
public void setTo(Date to) {
this.to = to;
}
} }

View File

@ -15,6 +15,7 @@
*/ */
package org.traccar.schedule; package org.traccar.schedule;
import net.fortuna.ical4j.model.component.VEvent;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.traccar.model.BaseModel; import org.traccar.model.BaseModel;
@ -86,8 +87,15 @@ public class TaskReports implements ScheduleTask {
for (Report report : storage.getObjects(Report.class, new Request(new Columns.All()))) { for (Report report : storage.getObjects(Report.class, new Request(new Columns.All()))) {
Calendar calendar = storage.getObject(Calendar.class, new Request( Calendar calendar = storage.getObject(Calendar.class, new Request(
new Columns.All(), new Condition.Equals("id", report.getCalendarId()))); new Columns.All(), new Condition.Equals("id", report.getCalendarId())));
if (calendar.checkMoment(currentCheck) && !calendar.checkMoment(lastCheck)) {
executeReport(report); var lastEvents = calendar.findEvents(lastCheck);
var currentEvents = calendar.findEvents(currentCheck);
if (!lastEvents.isEmpty() && currentEvents.isEmpty()) {
VEvent event = lastEvents.iterator().next();
Date from = event.getStartDate().getDate();
Date to = event.getEndDate().getDate();
executeReport(report, from, to);
} }
} }
} catch (StorageException e) { } catch (StorageException e) {
@ -95,7 +103,8 @@ public class TaskReports implements ScheduleTask {
} }
} }
private void executeReport(Report report) throws StorageException { private void executeReport(Report report, Date from, Date to) throws StorageException {
var deviceIds = storage.getObjects(Device.class, new Request( var deviceIds = storage.getObjects(Device.class, new Request(
new Columns.Include("id"), new Columns.Include("id"),
new Condition.Permission(Device.class, Report.class, report.getId()))) new Condition.Permission(Device.class, Report.class, report.getId())))
@ -107,42 +116,28 @@ public class TaskReports implements ScheduleTask {
var users = storage.getObjects(User.class, new Request( var users = storage.getObjects(User.class, new Request(
new Columns.Include("id"), new Columns.Include("id"),
new Condition.Permission(User.class, Report.class, report.getId()))); new Condition.Permission(User.class, Report.class, report.getId())));
for (User user : users) { for (User user : users) {
switch (report.getType()) { switch (report.getType()) {
case "events": case "events":
reportMailer.sendAsync(user.getId(), stream -> { reportMailer.sendAsync(user.getId(), stream -> eventsReportProvider.getExcel(
eventsReportProvider.getExcel( stream, user.getId(), deviceIds, groupIds, List.of(), from, to));
stream, user.getId(), deviceIds, groupIds,
List.of(), report.getFrom(), report.getTo());
});
break; break;
case "route": case "route":
reportMailer.sendAsync(user.getId(), stream -> { reportMailer.sendAsync(user.getId(), stream -> routeReportProvider.getExcel(
routeReportProvider.getExcel( stream, user.getId(), deviceIds, groupIds, from, to));
stream, user.getId(), deviceIds, groupIds,
report.getFrom(), report.getTo());
});
break; break;
case "summary": case "summary":
reportMailer.sendAsync(user.getId(), stream -> { reportMailer.sendAsync(user.getId(), stream -> summaryReportProvider.getExcel(
summaryReportProvider.getExcel( stream, user.getId(), deviceIds, groupIds, from, to, false));
stream, user.getId(), deviceIds, groupIds,
report.getFrom(), report.getTo(), false);
});
break; break;
case "trips": case "trips":
reportMailer.sendAsync(user.getId(), stream -> { reportMailer.sendAsync(user.getId(), stream -> tripsReportProvider.getExcel(
tripsReportProvider.getExcel( stream, user.getId(), deviceIds, groupIds, from, to));
stream, user.getId(), deviceIds, groupIds,
report.getFrom(), report.getTo());
});
break; break;
case "stops": case "stops":
reportMailer.sendAsync(user.getId(), stream -> { reportMailer.sendAsync(user.getId(), stream -> stopsReportProvider.getExcel(
stopsReportProvider.getExcel( stream, user.getId(), deviceIds, groupIds, from, to));
stream, user.getId(), deviceIds, groupIds,
report.getFrom(), report.getTo());
});
break; break;
default: default:
LOGGER.warn("Unsupported report type {}", report.getType()); LOGGER.warn("Unsupported report type {}", report.getType());