Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly load/export/snapshot feeds w/ mixed calendar definitions. #287

Merged
merged 4 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 49 additions & 36 deletions src/main/java/com/conveyal/gtfs/loader/JdbcGtfsSnapshotter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static com.conveyal.gtfs.loader.JdbcGtfsLoader.createFeedRegistryIfNotExists;
import static com.conveyal.gtfs.loader.JdbcGtfsLoader.createSchema;
Expand Down Expand Up @@ -225,7 +227,7 @@ private TableLoadResult createScheduleExceptionsTable() {
Iterable<CalendarDate> calendarDates = calendarDatesReader.getAll();

// Keep track of calendars by service id in case we need to add dummy calendar entries.
Map<String, Calendar> calendarsByServiceId = new HashMap<>();
Map<String, Calendar> dummyCalendarsByServiceId = new HashMap<>();

// Iterate through calendar dates to build up to get maps from exceptions to their dates.
Multimap<String, String> removedServiceForDate = HashMultimap.create();
Expand All @@ -241,15 +243,15 @@ private TableLoadResult createScheduleExceptionsTable() {
addedServiceForDate.put(date, calendarDate.service_id);
// create (if needed) and extend range of dummy calendar that would need to be created if we are
// copying from a feed that doesn't have the calendar.txt file
Calendar calendar = calendarsByServiceId.getOrDefault(calendarDate.service_id, new Calendar());
Calendar calendar = dummyCalendarsByServiceId.getOrDefault(calendarDate.service_id, new Calendar());
calendar.service_id = calendarDate.service_id;
if (calendar.start_date == null || calendar.start_date.isAfter(calendarDate.date)) {
calendar.start_date = calendarDate.date;
}
if (calendar.end_date == null || calendar.end_date.isBefore(calendarDate.date)) {
calendar.end_date = calendarDate.date;
}
calendarsByServiceId.put(calendarDate.service_id, calendar);
dummyCalendarsByServiceId.put(calendarDate.service_id, calendar);
} else {
removedServiceForDate.put(date, calendarDate.service_id);
}
Expand All @@ -274,42 +276,53 @@ private TableLoadResult createScheduleExceptionsTable() {
}
scheduleExceptionsTracker.executeRemaining();

// determine if we appear to be working with a calendar_dates-only feed.
// If so, we must also add dummy entries to the calendar table
if (
feedIdToSnapshot != null &&
!tableExists(feedIdToSnapshot, "calendar") &&
calendarDatesReader.getRowCount() > 0
) {
sql = String.format(
"insert into %s (service_id, description, start_date, end_date, " +
"monday, tuesday, wednesday, thursday, friday, saturday, sunday)" +
"values (?, ?, ?, ?, 0, 0, 0, 0, 0, 0, 0)",
tablePrefix + "calendar"
// fetch all entries in the calendar table to generate set of serviceIds that exist in the calendar
// table.
JDBCTableReader<Calendar> calendarReader = new JDBCTableReader(
Table.CALENDAR,
dataSource,
feedIdToSnapshot + ".",
EntityPopulator.CALENDAR
);
Set<String> calendarServiceIds = new HashSet<>();
for (Calendar calendar : calendarReader.getAll()) {
calendarServiceIds.add(calendar.service_id);
}

// For service_ids that only existed in the calendar_dates table, insert auto-generated, "blank"
// (no days of week specified) calendar entries.
sql = String.format(
"insert into %s (service_id, description, start_date, end_date, " +
"monday, tuesday, wednesday, thursday, friday, saturday, sunday)" +
"values (?, ?, ?, ?, 0, 0, 0, 0, 0, 0, 0)",
tablePrefix + "calendar"
);
PreparedStatement calendarStatement = connection.prepareStatement(sql);
final BatchTracker calendarsTracker = new BatchTracker(
"calendar",
calendarStatement
);
for (Calendar dummyCalendar : dummyCalendarsByServiceId.values()) {
if (calendarServiceIds.contains(dummyCalendar.service_id)) {
// This service_id already exists in the calendar table. No need to create auto-generated entry.
continue;
}
calendarStatement.setString(1, dummyCalendar.service_id);
calendarStatement.setString(
2,
String.format("%s (auto-generated)", dummyCalendar.service_id)
);
PreparedStatement calendarStatement = connection.prepareStatement(sql);
final BatchTracker calendarsTracker = new BatchTracker(
"calendar",
calendarStatement
calendarStatement.setString(
3,
dummyCalendar.start_date.format(DateTimeFormatter.BASIC_ISO_DATE)
);
for (Calendar calendar : calendarsByServiceId.values()) {
calendarStatement.setString(1, calendar.service_id);
calendarStatement.setString(
2,
String.format("%s (auto-generated)", calendar.service_id)
);
calendarStatement.setString(
3,
calendar.start_date.format(DateTimeFormatter.BASIC_ISO_DATE)
);
calendarStatement.setString(
4,
calendar.end_date.format(DateTimeFormatter.BASIC_ISO_DATE)
);
calendarsTracker.addBatch();
}
calendarsTracker.executeRemaining();
calendarStatement.setString(
4,
dummyCalendar.end_date.format(DateTimeFormatter.BASIC_ISO_DATE)
);
calendarsTracker.addBatch();
}
calendarsTracker.executeRemaining();

connection.commit();
} catch (Exception e) {
Expand Down
Loading