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

Skipped timepoint predictions, improvements to Ridership #19

Merged
merged 7 commits into from
Feb 12, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pom.xml.releaseBackup
*.iml
*.class
git.properties
.DS_Store
14 changes: 7 additions & 7 deletions onebusaway-admin-webapp/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@
<param name="namespace">/</param>
<param name="actionName">index</param>
</result>
<!--<result name="errorHandler" type="chain">-->
<!--<param name="actionName">error</param>-->
<!--<param name="namespace">/</param>-->
<!--</result>-->
<result name="errorHandler" type="chain">
<param name="actionName">error</param>
<param name="namespace">/</param>
</result>
</global-results>
<!--<global-exception-mappings>-->
<!--<exception-mapping exception="java.lang.Exception" result="errorHandler"/>-->
<!--</global-exception-mappings>-->
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="errorHandler"/>
</global-exception-mappings>
</package>
</struts>
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
package org.onebusaway.api.model.transit;

import org.onebusaway.realtime.api.OccupancyStatus;
import org.onebusaway.transit_data.HistoricalRidershipBean;

import java.io.Serializable;
import java.util.List;

public class ArrivalAndDepartureV2Bean implements Serializable {

private static final long serialVersionUID = 3L;
private static final long serialVersionUID = 4L;

private String routeId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.onebusaway.collections.CollectionsLibrary;
import org.onebusaway.geospatial.model.CoordinatePoint;
import org.onebusaway.geospatial.model.EncodedPolylineBean;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.realtime.api.OccupancyStatus;
import org.onebusaway.transit_data.HistoricalRidershipBean;
import org.onebusaway.transit_data.OccupancyStatusBean;
Expand Down Expand Up @@ -208,8 +207,11 @@ public ListWithReferencesBean<TripDetailsV2Bean> getTripDetailsResponse(
ListBean<TripDetailsBean> trips) {

List<TripDetailsV2Bean> beans = new ArrayList<TripDetailsV2Bean>();
for (TripDetailsBean trip : trips.getList())
beans.add(getTripDetails(trip));
for (TripDetailsBean trip : trips.getList()){
if(trip != null)
beans.add(getTripDetails(trip));
}

return list(beans, trips.isLimitExceeded(), false);
}

Expand All @@ -236,8 +238,13 @@ public EntryWithReferencesBean<VehicleStatusV2Bean> getVehicleStatusResponse(
return entry(getVehicleStatus(vehicleStatus));
}

public EntryWithReferencesBean<ListBean<OccupancyStatusBean>> getHistoricalOccupancyResponse(List<OccupancyStatusBean> occ) {
return entry(new ListBean<>(occ, false));
public ListWithReferencesBean<HistoricalRidershipBean> getHistoricalOccupancyResponse(
List<OccupancyStatusBean> beans) {
List<HistoricalRidershipBean> rid = new ArrayList<>();
for (OccupancyStatusBean bean : beans) {
rid.add(new HistoricalRidershipBean(bean.getOccupancyStatus()));
}
return list(rid, false, false);
}

public EntryWithReferencesBean<SituationV2Bean> getResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
import org.onebusaway.api.model.transit.BeanFactoryV2;
import org.onebusaway.exceptions.ServiceException;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.realtime.api.OccupancyStatus;
import org.onebusaway.transit_data.HistoricalRidershipBean;
import org.onebusaway.transit_data.OccupancyStatusBean;
import org.onebusaway.transit_data.model.*;
import org.onebusaway.transit_data.services.TransitDataService;
import org.onebusaway.transit_data_federation.model.bundle.HistoricalRidership;
import org.onebusaway.transit_data_federation.services.AgencyAndIdLibrary;
import org.onebusaway.util.SystemTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
Expand All @@ -41,10 +38,6 @@ public class HistoricalOccupancyByStopAction extends ApiActionSupport {
private static final long serialVersionUID = 1L;
private static final int V2 = 2;

// private String _id;
//
// private String _agencyId;

private HistoricalOccupancyByStopQueryBean _query = new HistoricalOccupancyByStopQueryBean();

public HistoricalOccupancyByStopAction() { super(V2); }
Expand Down Expand Up @@ -79,6 +72,7 @@ public long getServiceDate() {
private TransitDataService _service;

public DefaultHttpHeaders show() throws IOException, ServiceException {

if (!isVersion(V2))
return setUnknownVersionResponse();
if (hasErrors())
Expand All @@ -87,70 +81,44 @@ public DefaultHttpHeaders show() throws IOException, ServiceException {
AgencyAndId routeId = AgencyAndId.convertFromString(_query.getRouteId());
AgencyAndId tripId = AgencyAndId.convertFromString(_query.getTripId());
AgencyAndId stopId = AgencyAndId.convertFromString(_query.getStopId());

long serviceDate = _query.getServiceDate();
/**
* Attempting to make this customizable.
* If a route and/or a trip is in the query,
* If a route, trip, and/or service date is in the query,
* then return only the ridership values for the stop
* which are associated with them
*/
List<HistoricalRidershipBean> hrs = new ArrayList<>();
List<HistoricalRidershipBean> fil = new ArrayList<>();
List<OccupancyStatusBean> hrs = _service.getHistoricalRidershipForStop(_query);
List<OccupancyStatusBean> fil = new ArrayList<>();

// If we have both route and trip, get specific ridership
if (_query.getRouteId() != null && _query.getTripId() != null) {
hrs = _service.getHistoricalRiderships(routeId, tripId, stopId);
hrs = _service.getHistoricalRiderships(routeId, tripId, stopId, serviceDate);

// Otherwise, get all ridership for this stop, and filter with whats given
} else {
// Otherwise, get all ridership for this stop, and filter with whats given
hrs = _service.getHistoricalRidershipForStop(_query);
// filter for given route
if (_query.getRouteId() != null) {
for( HistoricalRidershipBean hr : hrs ) {
if(hr.getRouteId().getId().equals(routeId.getId())) {
fil.add(hr);
for( OccupancyStatusBean occ : hrs ) {
if(occ.getRouteId().getId().equals(routeId.getId())) {
fil.add(occ);
}
}
hrs = new ArrayList<>(fil);
}
// filter for given trip
else if(_query.getTripId() != null) {
for( HistoricalRidershipBean hr : hrs ) {
if(hr.getTripId().getId().equals(tripId.getId())) {
fil.add(hr);
for( OccupancyStatusBean occ : hrs ) {
if(occ.getTripId().getId().equals(tripId.getId())) {
fil.add(occ);
}
}
hrs = new ArrayList<>(fil);
}
}
// Then filter for the Service Date
if(_query.getServiceDate() != 0) {
// Gather the list of routes, from the schedule for this Stop, on this Service Date
StopScheduleBean schedule = _service.getScheduleForStop(stopId.toString(), new Date(_query.getServiceDate()));
List<String> schedRoutes = new ArrayList<>();
for (StopRouteScheduleBean srsb : schedule.getRoutes()) {
schedRoutes.add(srsb.getRoute().getId());
}
fil = new ArrayList<>();
for (HistoricalRidershipBean hr : hrs) {
if (schedRoutes.contains(hr.getRouteId().toString())) {
fil.add(hr);
}
}
hrs = new ArrayList<>(fil);
}


// convert to OccupancyStatus enums
List<OccupancyStatusBean> occ = new ArrayList<>();
for (HistoricalRidershipBean hr : hrs) {
OccupancyStatusBean bean = new OccupancyStatusBean();
bean.setStatus(OccupancyStatus.toEnum(hr.getLoadFactor()));
occ.add(bean);
}

BeanFactoryV2 factory = getBeanFactoryV2();

return setOkResponse(factory.getHistoricalOccupancyResponse(occ));
return setOkResponse(factory.getHistoricalOccupancyResponse(hrs));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public List<VehicleActivityStructure> getVehicleActivityForRoute(
ListBean<TripDetailsBean> trips = getAllTripsForRoute(routeId,
currentTime);
for (TripDetailsBean tripDetails : trips.getList()) {
if(tripDetails == null) continue;
// filter out interlined routes
if (routeId != null
&& !tripDetails.getTrip().getRoute().getId()
Expand Down Expand Up @@ -550,6 +551,7 @@ public boolean getVehiclesInServiceForRoute(String routeId,
ListBean<TripDetailsBean> trips = getAllTripsForRoute(routeId,
currentTime);
for (TripDetailsBean tripDetails : trips.getList()) {
if(tripDetails == null) continue;
// filter out interlined routes
if (routeId != null
&& !tripDetails.getTrip().getRoute().getId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
package org.onebusaway.api.model.where;

import org.onebusaway.realtime.api.OccupancyStatus;
import org.onebusaway.transit_data.HistoricalRidershipBean;

import java.io.Serializable;
import java.util.List;

public class ArrivalAndDepartureBeanV1 implements Serializable {

private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2L;

private String routeId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private List<Vehicle> getVehiclesForRoute(String routeId,

List<Vehicle> vehiclesList = new ArrayList<Vehicle>();
for (TripDetailsBean tripDetails : trips.getList()) {

if(tripDetails == null) continue;
TripStatusBean tripStatus = tripDetails.getStatus();

// filter out interlined routes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public List<VehicleActivityStructure> getVehicleActivityForRoute(String routeId,

ListBean<TripDetailsBean> trips = getAllTripsForRoute(routeId, currentTime);
for(TripDetailsBean tripDetails : trips.getList()) {
if(tripDetails == null)
continue;

// filter out interlined routes
if(routeId != null && !tripDetails.getTrip().getRoute().getId().equals(routeId))
Expand Down Expand Up @@ -322,6 +324,8 @@ private List<TimepointPredictionRecord> createTimePredictionRecordsForStop(
public boolean getVehiclesInServiceForRoute(String routeId, String directionId, long currentTime) {
ListBean<TripDetailsBean> trips = getAllTripsForRoute(routeId, currentTime);
for(TripDetailsBean tripDetails : trips.getList()) {
if(tripDetails == null)
continue;
// filter out interlined routes
if(routeId != null && !tripDetails.getTrip().getRoute().getId().equals(routeId))
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (C) 2019 Cambridge Systematics, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onebusaway.realtime.api;

/**
* Current status of a vehicle in relation to schedule.
*/
public enum EVehicleStatus {

/**
* vehicle is mapped to a schedule
*/
SCHEDULED,
/**
* vehicle is mapped to a schedule but skipping a set of stops
*/
SKIPPED,
/**
* vehicle is not supplying data
*/
NO_DATA,
/**
* status is unknown
*/
UNKNOWN;

public String toLabel() {
return toString().toLowerCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ public class TimepointPredictionRecord implements Serializable {

private long timepointPredictedDepartureTime = -1;

private ScheduleRelationship scheduleRelationship;

public enum ScheduleRelationship {
SCHEDULED(0),
SKIPPED(1),
NO_DATA(2);

private int value;

ScheduleRelationship(int i) {
this.value = i;
}

public static ScheduleRelationship toEnum(int val) {
switch(val) {
case 0: return SCHEDULED;
case 1: return SKIPPED;
case 2: return NO_DATA;
default: return SCHEDULED;
}
}
public int getValue() {
return this.value;
}
}

public TimepointPredictionRecord() {

}
Expand All @@ -52,6 +78,7 @@ public TimepointPredictionRecord(TimepointPredictionRecord r) {
this.timepointPredictedArrivalTime = r.timepointPredictedArrivalTime;
this.timepointPredictedDepartureTime = r.timepointPredictedDepartureTime;
this.timepointScheduledTime = r.timepointScheduledTime;
this.scheduleRelationship = r.scheduleRelationship;
}

public AgencyAndId getTimepointId() {
Expand Down Expand Up @@ -102,4 +129,14 @@ public void setTimepointPredictedDepartureTime(
long timepointPredictedDepartureTime) {
this.timepointPredictedDepartureTime = timepointPredictedDepartureTime;
}
public void setScheduleRealtionship(int status) {
this.scheduleRelationship = ScheduleRelationship.toEnum(status);
}
public ScheduleRelationship getScheduleRelationship() {
return scheduleRelationship;
}
public boolean isSkipped() {
return (this.scheduleRelationship != null
&& this.scheduleRelationship.getValue() == ScheduleRelationship.SKIPPED.getValue());
}
}
Loading