use of org.onebusaway.csv_entities.CSVLibrary in project onebusaway-application-modules by camsys.
the class StifTask method loadTripToDSCOverrides.
private Map<AgencyAndId, String> loadTripToDSCOverrides(String path) throws Exception {
final Map<AgencyAndId, String> results = new HashMap<AgencyAndId, String>();
CSVListener listener = new CSVListener() {
int count = 0;
int tripIdIndex;
int dscIndex;
@Override
public void handleLine(List<String> line) throws Exception {
if (line.size() != 2)
throw new Exception("Each Trip ID to DSC CSV line must contain two columns.");
if (count == 0) {
count++;
tripIdIndex = line.indexOf("trip_id");
dscIndex = line.indexOf("dsc");
if (tripIdIndex == -1 || dscIndex == -1) {
throw new Exception("Trip ID to DSC CSV must contain a header with column names 'trip_id' and 'dsc'.");
}
return;
}
results.put(AgencyAndIdLibrary.convertFromString(line.get(tripIdIndex)), line.get(dscIndex));
}
};
File source = new File(path);
new CSVLibrary().parse(source, listener);
return results;
}
use of org.onebusaway.csv_entities.CSVLibrary in project onebusaway-gtfs-modules by OneBusAway.
the class VerifyFutureRouteService method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
Collection<String> problemRoutes = new HashSet<String>();
ProblemRouteListener listener = new ProblemRouteListener();
try {
if (problemRoutesUrl != null) {
URL url = new URL(problemRoutesUrl);
try (InputStream is = url.openStream()) {
new CSVLibrary().parse(is, listener);
}
}
if (problemRoutesFile != null) {
InputStream is = new BufferedInputStream(new FileInputStream(problemRoutesFile));
new CSVLibrary().parse(is, listener);
}
} catch (Exception e) {
e.printStackTrace();
}
problemRoutes = listener.returnRouteIds();
GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
CalendarService refCalendarService = CalendarServiceDataFactoryImpl.createService(reference);
String feed = CloudContextService.getLikelyFeedName(dao);
ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
String agencyName = dao.getAllAgencies().iterator().next().getName();
int[] tripsToday;
int[] tripsTomorrow;
int[] tripsNextDay;
int[] tripsDayAfterNext;
Date today = removeTime(new Date());
Date tomorrow = removeTime(addDays(new Date(), 1));
Date nextDay = removeTime(addDays(new Date(), 2));
Date dayAfterNext = removeTime(addDays(new Date(), 3));
tripsToday = hasRouteServiceForDate(dao, reference, refCalendarService, today, problemRoutes);
tripsTomorrow = hasRouteServiceForDate(dao, reference, refCalendarService, tomorrow, problemRoutes);
tripsNextDay = hasRouteServiceForDate(dao, reference, refCalendarService, nextDay, problemRoutes);
tripsDayAfterNext = hasRouteServiceForDate(dao, reference, refCalendarService, dayAfterNext, problemRoutes);
_log.info("Feed for metrics: {}, agency name: {}", feed, agencyName);
_log.info("Active routes {}: {}, {}: {}, {}: {}, {}: {}", today, tripsToday[ACTIVE_ROUTES], tomorrow, tripsTomorrow[ACTIVE_ROUTES], nextDay, tripsNextDay[ACTIVE_ROUTES], dayAfterNext, tripsDayAfterNext[ACTIVE_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RoutesContainingTripsToday", "feed", feed, tripsToday[ACTIVE_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RoutesNoTripsInAtisButInRefToday", "feed", feed, tripsToday[ALARMING_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RoutesContainingTripsTomorrow", "feed", feed, tripsTomorrow[ACTIVE_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RoutesNoTripsInAtisButInRefTomorrow", "feed", feed, tripsTomorrow[ALARMING_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RoutesContainingTripsIn2Days", "feed", feed, tripsNextDay[ACTIVE_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RoutesNoTripsInAtisButInRefIn2Days", "feed", feed, tripsNextDay[ALARMING_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RoutesContainingTripsIn3Days", "feed", feed, tripsDayAfterNext[ACTIVE_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RoutesNoTripsInAtisButInRefIn3Days", "feed", feed, tripsDayAfterNext[ALARMING_ROUTES]);
}
use of org.onebusaway.csv_entities.CSVLibrary in project onebusaway-gtfs-modules by OneBusAway.
the class VerifyReferenceService method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
CalendarService refCalendarService = CalendarServiceDataFactoryImpl.createService(reference);
String feed = CloudContextService.getLikelyFeedName(reference);
ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
Collection<String> problemRoutes;
ProblemRouteListener listener = new ProblemRouteListener();
try {
if (problemRoutesFile != null) {
InputStream is = new BufferedInputStream(new FileInputStream(problemRoutesFile));
new CSVLibrary().parse(is, listener);
}
} catch (Exception e) {
e.printStackTrace();
}
problemRoutes = listener.returnRouteIds();
for (String route : problemRoutes) {
_log.info("ProblemRoutes includes: " + route);
}
int[] tripsToday;
int[] tripsTomorrow;
int[] tripsNextDay;
int[] tripsDayAfterNext;
Date today = removeTime(new Date());
Date tomorrow = removeTime(addDays(new Date(), 1));
Date nextDay = removeTime(addDays(new Date(), 2));
Date dayAfterNext = removeTime(addDays(new Date(), 3));
tripsToday = hasRouteServiceForDate(dao, reference, refCalendarService, today, problemRoutes);
tripsTomorrow = hasRouteServiceForDate(dao, reference, refCalendarService, tomorrow, problemRoutes);
tripsNextDay = hasRouteServiceForDate(dao, reference, refCalendarService, nextDay, problemRoutes);
tripsDayAfterNext = hasRouteServiceForDate(dao, reference, refCalendarService, dayAfterNext, problemRoutes);
_log.info("Feed for metrics: {}", feed);
_log.info("Active routes {}: {}, {}: {}, {}: {}, {}: {}", today, tripsToday, tomorrow, tripsTomorrow, nextDay, tripsNextDay, dayAfterNext, tripsDayAfterNext);
es.publishMetric(CloudContextService.getNamespace(), "RefRoutesContainingTripsToday", "feed", feed, tripsTomorrow[ACTIVE_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RefRoutesMissingTripsFromRefButInAtisToday", "feed", feed, tripsTomorrow[ALARMING_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RefRoutesContainingTripsTomorrow", "feed", feed, tripsTomorrow[ACTIVE_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RefRoutesMissingTripsFromRefButInAtisTomorrow", "feed", feed, tripsTomorrow[ALARMING_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RefRoutesContainingTripsIn2Days", "feed", feed, tripsNextDay[ACTIVE_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RefRoutesMissingTripsFromRefButInAtisIn2Days", "feed", feed, tripsNextDay[ALARMING_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RefRoutesContainingTripsIn3Days", "feed", feed, tripsDayAfterNext[ACTIVE_ROUTES]);
es.publishMetric(CloudContextService.getNamespace(), "RefRoutesMissingTripsFromRefButInAtisIn3Days", "feed", feed, tripsDayAfterNext[ALARMING_ROUTES]);
}
use of org.onebusaway.csv_entities.CSVLibrary in project onebusaway-gtfs-modules by OneBusAway.
the class AnomalyCheckFutureTripCounts method readCsvFrom.
private CSVListener readCsvFrom(CSVListener listener, String urlSource, String fileSource) {
try {
if (urlSource != null) {
URL url = new URL(urlSource);
try (InputStream is = url.openStream()) {
new CSVLibrary().parse(is, listener);
}
}
if (fileSource != null) {
InputStream is = new BufferedInputStream(new FileInputStream(fileSource));
new CSVLibrary().parse(is, listener);
}
} catch (Exception e) {
_log.error(e.getMessage());
}
return listener;
}
use of org.onebusaway.csv_entities.CSVLibrary in project onebusaway-gtfs-modules by OneBusAway.
the class StopMatrixFareModificationStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
// remove rules for route
for (FareRule rule : new HashSet<FareRule>(dao.getAllFareRules())) {
if (rule.getRoute() != null && rule.getRoute().getId().getId().equals(routeId)) {
if (!isExemplarSet()) {
setAttributesFromExemplar(rule.getFare());
}
route = rule.getRoute();
dao.removeEntity(rule);
}
}
for (FareAttribute attr : new HashSet<FareAttribute>(dao.getAllFareAttributes())) {
if (dao.getFareRulesForFareAttribute(attr).isEmpty()) {
dao.removeEntity(attr);
}
}
// add new rules
FareCreationListener listener = new FareCreationListener();
listener.setDao(dao);
try {
URL url = new URL(csvUrl);
try (InputStream is = url.openStream()) {
new CSVLibrary().parse(is, listener);
}
} catch (Exception e) {
e.printStackTrace();
}
listener.flushNewFares();
}
Aggregations