Search in sources :

Example 1 with CSVLibrary

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;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) CSVListener(org.onebusaway.csv_entities.CSVListener) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) CSVLibrary(org.onebusaway.csv_entities.CSVLibrary) File(java.io.File) IOException(java.io.IOException)

Example 2 with CSVLibrary

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]);
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) URL(java.net.URL) FileInputStream(java.io.FileInputStream) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) BufferedInputStream(java.io.BufferedInputStream) ExternalServices(org.onebusaway.cloud.api.ExternalServices) CSVLibrary(org.onebusaway.csv_entities.CSVLibrary)

Example 3 with CSVLibrary

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]);
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) BufferedInputStream(java.io.BufferedInputStream) ExternalServices(org.onebusaway.cloud.api.ExternalServices) CSVLibrary(org.onebusaway.csv_entities.CSVLibrary)

Example 4 with CSVLibrary

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;
}
Also used : CSVLibrary(org.onebusaway.csv_entities.CSVLibrary) URL(java.net.URL)

Example 5 with CSVLibrary

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();
}
Also used : FareAttribute(org.onebusaway.gtfs.model.FareAttribute) InputStream(java.io.InputStream) FareRule(org.onebusaway.gtfs.model.FareRule) CSVLibrary(org.onebusaway.csv_entities.CSVLibrary) URL(java.net.URL) HashSet(java.util.HashSet)

Aggregations

CSVLibrary (org.onebusaway.csv_entities.CSVLibrary)5 InputStream (java.io.InputStream)3 URL (java.net.URL)3 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 ExternalServices (org.onebusaway.cloud.api.ExternalServices)2 ExternalServicesBridgeFactory (org.onebusaway.cloud.api.ExternalServicesBridgeFactory)2 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)2 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)2 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 CSVListener (org.onebusaway.csv_entities.CSVListener)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 FareAttribute (org.onebusaway.gtfs.model.FareAttribute)1 FareRule (org.onebusaway.gtfs.model.FareRule)1