Search in sources :

Example 16 with ExternalServices

use of org.onebusaway.cloud.api.ExternalServices in project onebusaway-gtfs-modules by OneBusAway.

the class VerifyBusService method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
    ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
    String feed = CloudContextService.getLikelyFeedName(dao);
    CalendarService refCalendarService = CalendarServiceDataFactoryImpl.createService(reference);
    AgencyAndId refAgencyAndId = reference.getAllTrips().iterator().next().getId();
    int curSerRoute = 0;
    int alarmingRoutes = 0;
    Date today = removeTime(new Date());
    // list of all routes in ATIS
    Set<String> ATISrouteIds = new HashSet<>();
    // check for route specific current service
    for (Route route : dao.getAllRoutes()) {
        ATISrouteIds.add(route.getId().getId());
        _log.info("Adding route: {}", route.getId().getId());
        curSerRoute = 0;
        triploop: for (Trip trip1 : dao.getTripsForRoute(route)) {
            for (ServiceCalendarDate calDate : dao.getCalendarDatesForServiceId(trip1.getServiceId())) {
                Date date = constructDate(calDate.getDate());
                if (calDate.getExceptionType() == 1 && date.equals(today)) {
                    _log.info("ATIS has current service for route: {}", route.getId().getId());
                    curSerRoute++;
                    break triploop;
                }
            }
        }
        if (curSerRoute == 0) {
            _log.error("No current service for {}", route.getId().getId());
            // if there is no current service, check that it should have service
            // there are certain routes that don't run on the weekend or won't have service in reference
            ServiceDate sToday = createServiceDate(today);
            Route refRoute = reference.getRouteForId(new AgencyAndId(refAgencyAndId.getAgencyId(), route.getId().getId()));
            reftriploop: for (Trip refTrip : reference.getTripsForRoute(refRoute)) {
                Set<ServiceDate> activeDates = refCalendarService.getServiceDatesForServiceId(refTrip.getServiceId());
                if (activeDates.contains(sToday)) {
                    _log.info("Reference has service for this bus route today but ATIS does not: {}", route.getId());
                    // This would be the site to add to a bulk metric, missingBus:
                    alarmingRoutes++;
                }
            }
        }
    }
    es.publishMetric(CloudContextService.getNamespace(), "RoutesMissingTripsFromAtisButInRefToday", "feed", feed, alarmingRoutes);
    es.publishMetric(CloudContextService.getNamespace(), "RoutesContainingTripsToday", "feed", feed, curSerRoute);
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) Trip(org.onebusaway.gtfs.model.Trip) ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) Set(java.util.Set) HashSet(java.util.HashSet) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ExternalServices(org.onebusaway.cloud.api.ExternalServices) Route(org.onebusaway.gtfs.model.Route) HashSet(java.util.HashSet)

Example 17 with ExternalServices

use of org.onebusaway.cloud.api.ExternalServices in project onebusaway-gtfs-modules by OneBusAway.

the class UpdateStopTimesForTime method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    RemoveEntityLibrary removeEntityLibrary = new RemoveEntityLibrary();
    StopTime currentStop = new StopTime();
    int negativeTimes = 0;
    ArrayList<Trip> tripsToRemove = new ArrayList<Trip>();
    // For now, for any trip with stop_times that go back in time, remove the trip.
    for (Trip trip : dao.getAllTrips()) {
        StopTime previousStop = new StopTime();
        previousStop.setArrivalTime(0);
        for (StopTime stopTime : dao.getStopTimesForTrip(trip)) {
            currentStop = stopTime;
            // handle the cases where there is no stop time (stop time is negative)
            if (currentStop.getArrivalTime() < 0) {
                _log.error("Ignoring negative stop time for {}", currentStop.toString());
            } else {
                // handle the case of decreasing stop time
                if (previousStop.getArrivalTime() > currentStop.getArrivalTime()) {
                    _log.info("Time travel! previous arrival time {} this stop {}", previousStop.displayArrival(), currentStop.toString());
                    tripsToRemove.add(trip);
                    negativeTimes++;
                    break;
                }
                previousStop = currentStop;
            }
        }
    }
    _log.info("Decreasing times: {}, TripsToRemove: {}", negativeTimes, tripsToRemove.size());
    ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
    String feed = CloudContextService.getLikelyFeedName(dao);
    es.publishMetric(CloudContextService.getNamespace(), "TripsWithDecreasingStopTimes", "feed", feed, tripsToRemove.size());
    StringBuffer illegalTripList = new StringBuffer();
    for (Trip trip : tripsToRemove) {
        illegalTripList.append(trip.getId().toString()).append(" ");
        removeEntityLibrary.removeTrip(dao, trip);
    }
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) ExternalServices(org.onebusaway.cloud.api.ExternalServices) ArrayList(java.util.ArrayList) StopTime(org.onebusaway.gtfs.model.StopTime)

Aggregations

ExternalServices (org.onebusaway.cloud.api.ExternalServices)17 ExternalServicesBridgeFactory (org.onebusaway.cloud.api.ExternalServicesBridgeFactory)17 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)10 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)8 HashMap (java.util.HashMap)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)4 Stop (org.onebusaway.gtfs.model.Stop)4 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)4 Date (java.util.Date)3 HashSet (java.util.HashSet)3 CSVLibrary (org.onebusaway.csv_entities.CSVLibrary)3 Trip (org.onebusaway.gtfs.model.Trip)3 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)2