Search in sources :

Example 6 with ExternalServices

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

the class ValidateGTFS method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    int countSt = 0;
    int countNoSt = 0;
    int curSerTrips = 0;
    int tomSerTrips = 0;
    int countNoHs = 0;
    String agency = dao.getAllAgencies().iterator().next().getId();
    String name = dao.getAllAgencies().iterator().next().getName();
    AgencyAndId serviceAgencyAndId = new AgencyAndId();
    for (Trip trip : dao.getAllTrips()) {
        if (dao.getStopTimesForTrip(trip).size() == 0) {
            countNoSt++;
        } else {
            countSt++;
        }
        // check for current service
        Date today = removeTime(new Date());
        boolean hasCalDateException = false;
        // are there calendar dates?
        if (!dao.getCalendarDatesForServiceId(trip.getServiceId()).isEmpty()) {
            // calendar dates are not empty
            for (ServiceCalendarDate calDate : dao.getCalendarDatesForServiceId(trip.getServiceId())) {
                Date date = constructDate(calDate.getDate());
                if (date.equals(today)) {
                    hasCalDateException = true;
                    if (calDate.getExceptionType() == 1) {
                        // there is service for today
                        curSerTrips++;
                        break;
                    }
                    if (calDate.getExceptionType() == 2) {
                        // service has been excluded for today
                        break;
                    }
                }
            }
        }
        // if there are no entries in calendarDates, check serviceCalendar
        if (!hasCalDateException) {
            ServiceCalendar servCal = dao.getCalendarForServiceId(trip.getServiceId());
            if (servCal != null) {
                // check for current service using calendar
                Date start = removeTime(servCal.getStartDate().getAsDate());
                Date end = removeTime(servCal.getEndDate().getAsDate());
                if (today.equals(start) || today.equals(end) || (today.after(start) && today.before(end))) {
                    curSerTrips++;
                }
            }
        }
        // check for current service
        Date tomorrow = removeTime(addDays(new Date(), 1));
        hasCalDateException = false;
        // are there calendar dates?
        if (!dao.getCalendarDatesForServiceId(trip.getServiceId()).isEmpty()) {
            // calendar dates are not empty
            for (ServiceCalendarDate calDate : dao.getCalendarDatesForServiceId(trip.getServiceId())) {
                Date date = removeTime(calDate.getDate().getAsDate());
                if (date.equals(tomorrow)) {
                    hasCalDateException = true;
                    if (calDate.getExceptionType() == 1) {
                        // there is service for today
                        tomSerTrips++;
                        break;
                    }
                    if (calDate.getExceptionType() == 2) {
                        // service has been excluded for today
                        break;
                    }
                }
            }
        }
        // if there are no entries in calendarDates, check serviceCalendar
        if (!hasCalDateException) {
            ServiceCalendar servCal = dao.getCalendarForServiceId(trip.getServiceId());
            if (servCal != null) {
                // check for current service using calendar
                Date start = removeTime(servCal.getStartDate().getAsDate());
                Date end = removeTime(servCal.getEndDate().getAsDate());
                if (tomorrow.equals(start) || tomorrow.equals(end) || (tomorrow.after(start) && tomorrow.before(end))) {
                    tomSerTrips++;
                }
            }
        }
        if (trip.getTripHeadsign() == null) {
            countNoHs++;
        }
    }
    _log.info("Agency: {}, {}. Routes: {}, Trips: {}, Current Service: {}, " + "Stops: {}, Stop times {}, Trips w/ st: {}, Trips w/out st: {}, " + "Total trips w/out headsign: {}", agency, name, dao.getAllRoutes().size(), dao.getAllTrips().size(), curSerTrips, dao.getAllStops().size(), dao.getAllStopTimes().size(), countSt, countNoSt, countNoHs);
    ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
    String feed = CloudContextService.getLikelyFeedName(dao);
    HashSet<String> ids = new HashSet<String>();
    for (Stop stop : dao.getAllStops()) {
        // check for duplicate stop ids.
        if (ids.contains(stop.getId().getId())) {
            _log.error("Duplicate stop ids! Agency {} stop id {} stop code {}", agency, stop.getId().getId(), stop.getCode());
        } else {
            ids.add(stop.getId().getId());
        }
    }
    HashSet<String> codes = new HashSet<String>();
    for (Stop stop : dao.getAllStops()) {
        // check for duplicate stop ids.
        if (codes.contains(stop.getCode())) {
            _log.error("Duplicate stop codes! Agency {} stop codes {}", agency, stop.getCode());
        } else {
            codes.add(stop.getCode());
        }
    }
    es.publishMetric(CloudContextService.getNamespace(), "TripsWithServiceToday", "feed", feed, curSerTrips);
    es.publishMetric(CloudContextService.getNamespace(), "TripsWithServiceTomorrow", "feed", feed, tomSerTrips);
    if (curSerTrips + tomSerTrips < 1) {
        throw new IllegalStateException("There is no current service!!");
    }
    if (countNoHs > 0) {
        _log.error("There are trips with no headsign");
    }
    es.publishMetric(CloudContextService.getNamespace(), "TripsWithoutHeadsigns", "feed", feed, countNoHs);
}
Also used : ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ExternalServices(org.onebusaway.cloud.api.ExternalServices)

Example 7 with ExternalServices

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

the class UpdateStopIdFromControlStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
    RemoveEntityLibrary removeEntityLibrary = new RemoveEntityLibrary();
    File controlFile = new File((String) context.getParameter("controlFile"));
    ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
    String feed = CloudContextService.getLikelyFeedName(dao);
    if (!controlFile.exists()) {
        es.publishMultiDimensionalMetric(CloudContextService.getNamespace(), "MissingControlFiles", new String[] { "feed", "controlFileName" }, new String[] { feed, controlFile.getName() }, 1);
        throw new IllegalStateException("Control file does not exist: " + controlFile.getName());
    }
    List<String> controlLines = new InputLibrary().readList((String) context.getParameter("controlFile"));
    int matched = 0;
    int unmatched = 0;
    int duplicate = 0;
    int inCntrlRefNotAtis = 0;
    ArrayList<AgencyAndId> stopsToRemove = new ArrayList();
    // a map of the new id (from reference/control file) to the old id
    // so that stop times can be associated with new id
    HashMap<AgencyAndId, AgencyAndId> stopsUpdated = new HashMap<>();
    HashMap<String, Stop> referenceStops = new HashMap<>();
    for (Stop stop : reference.getAllStops()) {
        referenceStops.put(stop.getId().getId(), stop);
    }
    AgencyAndId agencyAndId = dao.getAllStops().iterator().next().getId();
    for (String controlLine : controlLines) {
        String[] controlArray = controlLine.split(",");
        if (controlArray == null || controlArray.length < 2) {
            _log.info("bad control line {}", controlLine);
            continue;
        }
        String referenceId = controlArray[LOCATION_NAME_INDEX];
        String direction = controlArray[DIRECTION];
        String atisId = controlArray[ATIS_ID_INDEX];
        Stop refStop;
        // find the reference stop based on the Id
        if (direction.isEmpty()) {
            refStop = referenceStops.get(referenceId);
            if (refStop == null) {
                if (!atisId.equals("0")) {
                // _log.info("missing reference stop {} for agency {} for ATIS id {}", referenceId, getReferenceAgencyId(reference), atisId);
                }
                unmatched++;
                continue;
            }
        } else {
            refStop = referenceStops.get(referenceId + direction);
            if (refStop == null) {
                if (!atisId.equals("0")) {
                // _log.info("missing reference stop {} for agency {} for ATIS id {}", referenceId, getReferenceAgencyId(reference), atisId);
                }
                unmatched++;
                continue;
            }
        }
        Stop atisStop = dao.getStopForId(new AgencyAndId(agencyAndId.getAgencyId(), atisId));
        // for example: there are two 128N ref stops in control file
        if (stopsUpdated.containsKey(refStop.getId())) {
            duplicate++;
            Stop persistStop = dao.getStopForId(stopsUpdated.get(refStop.getId()));
            stopsToRemove.add(new AgencyAndId(agencyAndId.getAgencyId(), atisId));
            // _log.info("Stop times for stop: {} stopTimes: {}", atisStop.getId().getId(), dao.getStopTimesForStop(atisStop).size());
            for (StopTime stopTime : dao.getStopTimesForStop(atisStop)) {
                stopTime.setStop(persistStop);
            }
            // _log.info("Duplicate stops keep: {}  remove: {} ", persistStop.getId().getId(), atisStop.getId().getId());
            continue;
        }
        if (atisStop == null) {
            if (!atisId.equals("0")) {
            // _log.info("missing atis stop {} for Reference id {}{}", atisId, referenceId, direction);
            }
            unmatched++;
            continue;
        } else {
            atisStop.setName(refStop.getName());
            atisStop.setDirection(refStop.getDirection());
            atisStop.setId(refStop.getId());
            atisStop.setParentStation(refStop.getParentStation());
            atisStop.setLocationType(refStop.getLocationType());
            stopsUpdated.put(atisStop.getId(), new AgencyAndId(agencyAndId.getAgencyId(), atisId));
            dao.updateEntity(atisStop);
            matched++;
        // _log.error("Updated stop: original ATIS id: {} Reference id: {} Id now: {}", atisId, referenceId, atisStop.getId().getId());
        }
    }
    _log.info("Complete with {} matched and {} unmatched and {} duplicates", matched, unmatched, duplicate);
    for (AgencyAndId id : stopsToRemove) {
        Stop stop = dao.getStopForId(id);
        // removeEntityLibrary.removeStop(dao, stop);
        dao.removeEntity(stop);
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) Stop(org.onebusaway.gtfs.model.Stop) ArrayList(java.util.ArrayList) ExternalServices(org.onebusaway.cloud.api.ExternalServices) File(java.io.File) StopTime(org.onebusaway.gtfs.model.StopTime)

Example 8 with ExternalServices

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

the class UpdateStopIdsFromFile method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    File controlFile = new File((String) context.getParameter("controlFile"));
    String feed = dao.getAllFeedInfos().iterator().next().getPublisherName();
    ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
    if (!controlFile.exists()) {
        es.publishMultiDimensionalMetric(CloudContextService.getNamespace(), "MissingControlFiles", new String[] { "feed", "controlFileName" }, new String[] { feed, controlFile.getName() }, 1);
        throw new IllegalStateException("Control file does not exist: " + controlFile.getName());
    }
    List<String> controlLines = new InputLibrary().readList((String) context.getParameter("controlFile"));
    int matched = 0;
    int unmatched = 0;
    int duplicate = 0;
    AgencyAndId agencyAndId = dao.getAllStops().iterator().next().getId();
    for (String controlLine : controlLines) {
        String[] controlArray = controlLine.split(",");
        if (controlArray == null || controlArray.length < 2) {
            _log.info("bad control line {}", controlLine);
            continue;
        }
        String oldId = controlArray[OLD_STOP_ID];
        String newId = controlArray[NEW_STOP_ID];
        String newCode = controlArray[NEW_STOP_CODE];
        Stop stop = dao.getStopForId(new AgencyAndId(agencyAndId.getAgencyId(), newId));
        if (stop == null) {
            if (!newId.equals("0")) {
                _log.info("missing stop for new id {}", newId);
            } else {
                _log.error("No stop found for id {}", newId);
            }
            unmatched++;
            continue;
        }
        matched++;
        _log.info("Setting existing new id {} to old id {}", newId, oldId);
        stop.setId(new AgencyAndId(stop.getId().getAgencyId(), oldId));
        stop.setCode(newCode);
    }
    _log.info("Complete with {} matched and {} unmatched and {} duplicates", matched, unmatched, duplicate);
}
Also used : ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) ExternalServices(org.onebusaway.cloud.api.ExternalServices) File(java.io.File)

Example 9 with ExternalServices

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

the class VerifyRouteService method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    String feed = CloudContextService.getLikelyFeedName(dao);
    ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
    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));
    int numRoutesToday = hasRouteServiceForDate(dao, today, es);
    if (!isWeekend(today)) {
        es.publishMetric(CloudContextService.getNamespace(), "WeekdayActiveSubwayRoutesToday", "feed", feed, numRoutesToday);
    }
    int numRoutesTommorrow = hasRouteServiceForDate(dao, tomorrow, es);
    if (!isWeekend(tomorrow)) {
        es.publishMetric(CloudContextService.getNamespace(), "WeekdayActiveSubwayRoutesTomorrow", "feed", feed, numRoutesTommorrow);
    }
    int numRoutesIn2Days = hasRouteServiceForDate(dao, nextDay, es);
    if (!isWeekend(nextDay)) {
        es.publishMetric(CloudContextService.getNamespace(), "WeekdayActiveSubwayRoutesIn2Days", "feed", feed, numRoutesIn2Days);
    }
    int numRoutesIn3Days = hasRouteServiceForDate(dao, dayAfterNext, es);
    if (!isWeekend(dayAfterNext)) {
        es.publishMetric(CloudContextService.getNamespace(), "WeekdayActiveSubwayRoutesIn3Days", "feed", feed, numRoutesIn3Days);
    }
    _log.info("Feed for metrics: {}", feed);
    _log.info("Active routes: {}: {}; {}: {}; {}: {}; {}: {}", formatDate(today), numRoutesToday, formatDate(tomorrow), numRoutesTommorrow, formatDate(nextDay), numRoutesIn2Days, formatDate(dayAfterNext), numRoutesIn3Days);
    es.publishMetric(CloudContextService.getNamespace(), "RoutesContainingTripsToday", "feed", feed, numRoutesToday);
    es.publishMetric(CloudContextService.getNamespace(), "RoutesContainingTripsTomorrow", "feed", feed, numRoutesTommorrow);
    es.publishMetric(CloudContextService.getNamespace(), "RoutesContainingTripsIn2Days", "feed", feed, numRoutesIn2Days);
    es.publishMetric(CloudContextService.getNamespace(), "RoutesContainingTripsIn3Days", "feed", feed, numRoutesIn3Days);
    if (numRoutesToday < 3) {
        throw new IllegalStateException("Route service missing in agency: " + dao.getAllAgencies().iterator().next());
    }
}
Also used : ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) ExternalServices(org.onebusaway.cloud.api.ExternalServices) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate)

Example 10 with ExternalServices

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

the class CountAndTest method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    int countSt = 0;
    int countCd = 0;
    int countNoSt = 0;
    int countNoCd = 0;
    int curSerTrips = 0;
    int tomSerTrips = 0;
    int countNoHs = 0;
    String agency = dao.getAllAgencies().iterator().next().getId();
    String name = dao.getAllAgencies().iterator().next().getName();
    AgencyAndId serviceAgencyAndId = new AgencyAndId();
    for (Trip trip : dao.getAllTrips()) {
        if (dao.getStopTimesForTrip(trip).size() == 0) {
            countNoSt++;
        } else {
            countSt++;
        }
        serviceAgencyAndId = trip.getServiceId();
        if (dao.getCalendarDatesForServiceId(serviceAgencyAndId).size() == 0) {
            countNoCd++;
        } else {
            countCd++;
        }
        // check for current service
        Date today = removeTime(new Date());
        boolean hasCalDateException = false;
        // are there calendar dates?
        if (!dao.getCalendarDatesForServiceId(trip.getServiceId()).isEmpty()) {
            // calendar dates are not empty
            for (ServiceCalendarDate calDate : dao.getCalendarDatesForServiceId(trip.getServiceId())) {
                Date date = constructDate(calDate.getDate());
                if (date.equals(today)) {
                    hasCalDateException = true;
                    if (calDate.getExceptionType() == 1) {
                        // there is service for today
                        curSerTrips++;
                        break;
                    }
                    if (calDate.getExceptionType() == 2) {
                        // service has been excluded for today
                        break;
                    }
                }
            }
        }
        // if there are no entries in calendarDates, check serviceCalendar
        if (!hasCalDateException) {
            ServiceCalendar servCal = dao.getCalendarForServiceId(trip.getServiceId());
            if (servCal != null) {
                // check for current service using calendar
                Date start = removeTime(servCal.getStartDate().getAsDate());
                Date end = removeTime(servCal.getEndDate().getAsDate());
                if (today.equals(start) || today.equals(end) || (today.after(start) && today.before(end))) {
                    curSerTrips++;
                }
            }
        }
        // check for current service
        Date tomorrow = removeTime(addDays(new Date(), 1));
        hasCalDateException = false;
        // are there calendar dates?
        if (!dao.getCalendarDatesForServiceId(trip.getServiceId()).isEmpty()) {
            // calendar dates are not empty
            for (ServiceCalendarDate calDate : dao.getCalendarDatesForServiceId(trip.getServiceId())) {
                Date date = removeTime(calDate.getDate().getAsDate());
                if (date.equals(tomorrow)) {
                    hasCalDateException = true;
                    if (calDate.getExceptionType() == 1) {
                        // there is service for today
                        tomSerTrips++;
                        break;
                    }
                    if (calDate.getExceptionType() == 2) {
                        // service has been excluded for today
                        break;
                    }
                }
            }
        }
        // if there are no entries in calendarDates, check serviceCalendar
        if (!hasCalDateException) {
            ServiceCalendar servCal = dao.getCalendarForServiceId(trip.getServiceId());
            if (servCal != null) {
                // check for current service using calendar
                Date start = removeTime(servCal.getStartDate().getAsDate());
                Date end = removeTime(servCal.getEndDate().getAsDate());
                if (tomorrow.equals(start) || tomorrow.equals(end) || (tomorrow.after(start) && tomorrow.before(end))) {
                    tomSerTrips++;
                }
            }
        }
        if (trip.getTripHeadsign() == null) {
            countNoHs++;
            _log.error("Trip {} has no headsign", trip.getId());
        }
    }
    _log.info("Agency: {}, {}. Routes: {}, Trips: {}, Current Service: {}, " + "Stops: {}, Stop times {}, Trips w/ st: {}, Trips w/out st: {}, " + "Total trips w/out headsign: {}", agency, name, dao.getAllRoutes().size(), dao.getAllTrips().size(), curSerTrips, dao.getAllStops().size(), dao.getAllStopTimes().size(), countSt, countNoSt, countNoHs);
    ExternalServices es = new ExternalServicesBridgeFactory().getExternalServices();
    String feed = CloudContextService.getLikelyFeedName(dao);
    HashSet<String> ids = new HashSet<String>();
    for (Stop stop : dao.getAllStops()) {
        // check for duplicate stop ids.
        if (ids.contains(stop.getId().getId())) {
            _log.error("Duplicate stop ids! Agency {} stop id {}", agency, stop.getId().getId());
            es.publishMultiDimensionalMetric(CloudContextService.getNamespace(), "DuplicateStopIds", new String[] { "feed", "stopId" }, new String[] { feed, stop.getId().toString() }, 1);
            throw new IllegalStateException("There are duplicate stop ids!");
        } else {
            ids.add(stop.getId().getId());
        }
    }
    es.publishMetric(CloudContextService.getNamespace(), "TripsInServiceToday", "feed", feed, curSerTrips);
    es.publishMetric(CloudContextService.getNamespace(), "TripsInServiceTomorrow", "feed", feed, tomSerTrips);
    if (curSerTrips + tomSerTrips < 1) {
        throw new IllegalStateException("There is no current service!!");
    }
    if (countNoHs > 0) {
        _log.error("There are trips with no headsign");
    }
    es.publishMetric(CloudContextService.getNamespace(), "TripsWithoutHeadsigns", "feed", feed, countNoHs);
}
Also used : ExternalServicesBridgeFactory(org.onebusaway.cloud.api.ExternalServicesBridgeFactory) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ExternalServices(org.onebusaway.cloud.api.ExternalServices)

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