def get_update_by_id(system_update_id) -> views.SystemUpdate: system_update = systemqueries.get_update_by_pk(system_update_id) if system_update is None: raise exceptions.IdNotFoundError(models.SystemUpdate, id=str(system_update_id)) result = views.SystemUpdate.from_model(system_update) result.system = views.System.from_model(system_update.system) return result
def list_all_in_system(system_id) -> typing.List[views.Feed]: """ Get data on all feeds in a system. """ system = systemqueries.get_by_id(system_id, only_return_active=True) if system is None: raise exceptions.IdNotFoundError(models.System, system_id=system_id) return list( map(views.Feed.from_model, feedqueries.list_all_in_system(system_id)))
def get_update_in_feed_by_pk(system_id, feed_id, feed_update_pk): feed_update = feedqueries.get_update_by_pk(feed_update_pk) if feed_update is None: raise exceptions.IdNotFoundError( models.Feed, system_id=system_id, feed_id=feed_id, feed_update_id=str(feed_update_pk), ) return views.FeedUpdate.from_model(feed_update)
def list_all_in_system( system_id, alerts_detail: views.AlertsDetail = None ) -> typing.List[views.Agency]: system = systemqueries.get_by_id(system_id, only_return_active=True) if system is None: raise exceptions.IdNotFoundError(models.System, system_id=system_id) agencies = genericqueries.list_in_system(models.Agency, system_id) response = list(map(views.Agency.from_model, agencies)) helpers.add_alerts_to_views( response, agencies, alerts_detail or views.AlertsDetail.CAUSE_AND_EFFECT ) return response
def list_updates_in_feed(system_id, feed_id): """ List all of the updates for a feed. """ feed = feedqueries.get_in_system_by_id(system_id, feed_id) if feed is None: raise exceptions.IdNotFoundError(models.Feed, system_id=system_id, feed_id=feed_id) response = [] for feed_update in feedqueries.list_updates_in_feed(feed.pk): response.append(views.FeedUpdate.from_model(feed_update)) return response
def get_in_system_by_id( system_id, agency_id, alerts_detail: views.AlertsDetail = None ) -> views.AgencyLarge: agency = genericqueries.get_in_system_by_id(models.Agency, system_id, agency_id) if agency is None: raise exceptions.IdNotFoundError( models.Route, system_id=system_id, route_id=agency_id ) response = views.AgencyLarge.from_model(agency) helpers.add_alerts_to_views( [response], [agency], alerts_detail or views.AlertsDetail.MESSAGES ) response.routes = list(map(views.Route.from_model, agency.routes)) return response
def list_all_in_system(system_id, alerts_detail=None) -> typing.List[views.Stop]: system = systemqueries.get_by_id(system_id, only_return_active=True) if system is None: raise exceptions.IdNotFoundError(models.System, system_id=system_id) stops = stopqueries.list_all_in_system(system_id) response = list(map(views.Stop.from_model, stops)) helpers.add_alerts_to_views( response, stops, alerts_detail or views.AlertsDetail.NONE, ) return response
def list_all_transfers_in_system( system_id, from_stop_ids=None, to_stop_ids=None) -> typing.List[views.Transfer]: system = systemqueries.get_by_id(system_id, only_return_active=True) if system is None: raise exceptions.IdNotFoundError(models.System, system_id=system_id) return [ views.Transfer.from_model( transfer, views.Stop.from_model(transfer.from_stop), views.Stop.from_model(transfer.to_stop), ) for transfer in stopqueries.list_all_transfers_in_system( system_id, from_stop_ids=from_stop_ids, to_stop_ids=to_stop_ids) ]
def list_all_in_system( system_id, alerts_detail: views.AlertsDetail = None ) -> typing.List[views.Route]: system = systemqueries.get_by_id(system_id, only_return_active=True) if system is None: raise exceptions.IdNotFoundError(models.System, system_id=system_id) response = [] routes = list(routequeries.list_in_system(system_id)) for route in routes: route_response = views.Route.from_model(route) response.append(route_response) helpers.add_alerts_to_views( response, routes, alerts_detail or views.AlertsDetail.CAUSE_AND_EFFECT, ) return response
def get_in_system_by_id(system_id, feed_id) -> views.Feed: """ Get data on a specific feed in a system. """ feed = feedqueries.get_in_system_by_id(system_id, feed_id) if feed is None: raise exceptions.IdNotFoundError(models.Feed, system_id=system_id, feed_id=feed_id) response = views.FeedLarge.from_model(feed) response.updates = views.UpdatesInFeedLink.from_model(feed) end_time = datetime.datetime.utcnow() response.statistics = [] for minutes in [15, 60, 60 * 24]: start_time = end_time - datetime.timedelta(minutes=minutes) response.statistics.append( build_feed_windows([feed.pk], start_time, end_time)[feed.pk]) return response
def get_in_route_by_id( system_id, route_id, trip_id, alerts_detail: views.AlertsDetail = None ): trip = tripqueries.get_in_route_by_id(system_id, route_id, trip_id) if trip is None: raise exceptions.IdNotFoundError( models.Trip, system_id=system_id, route_id=route_id, trip_id=trip_id ) trip_response = views.Trip.from_model(trip) trip_response.route = views.Route.from_model(trip.route) trip_response.stop_times = [] for stop_time in trip.stop_times: stop_time_response = views.TripStopTime.from_model(stop_time) stop_time_response.stop = views.Stop.from_model(stop_time.stop) trip_response.stop_times.append(stop_time_response) helpers.add_alerts_to_views( [trip_response], [trip], alerts_detail or views.AlertsDetail.MESSAGES ) return trip_response
def get_in_system_by_id( system_id, route_id, alerts_detail: views.AlertsDetail = None ) -> views.RouteLarge: route = routequeries.get_in_system_by_id(system_id, route_id) if route is None: raise exceptions.IdNotFoundError( models.Route, system_id=system_id, route_id=route_id ) periodicity = routequeries.calculate_periodicity(route.pk) if periodicity is not None: periodicity = int(periodicity / 6) / 10 result = views.RouteLarge.from_model(route, periodicity) if route.agency is not None: result.agency = views.Agency.from_model(route.agency) helpers.add_alerts_to_views( [result], [route], alerts_detail or views.AlertsDetail.MESSAGES, ) result.service_maps = servicemapmanager.build_route_service_maps_response(route.pk) return result
def _create_and_execute_feed_update_helper( system_id, feed_id, update_manager_function, content=None, execute_async=False, ): """ Create a feed update for a feed in a system. """ feed_update_pk = update_manager_function(system_id, feed_id) if feed_update_pk is None: raise exceptions.IdNotFoundError(models.Feed, system_id=system_id, feed_id=feed_id) if execute_async: updatemanager.execute_feed_update_async.delay(feed_update_pk, content) else: updatemanager.execute_feed_update(feed_update_pk, content) return feed_update_pk
def get_by_id(system_id) -> views.SystemLarge: system = systemqueries.get_by_id(system_id) if system is None: raise exceptions.IdNotFoundError(models.System, system_id=system_id) response = views.SystemLarge.from_model(system) if system.status != system.SystemStatus.ACTIVE: return response for attr, view, relationship in ( ("agencies", views.AgenciesInSystem, models.System.agencies), ("stops", views.StopsInSystem, models.System.stops), ("routes", views.RoutesInSystem, models.System.routes), ("feeds", views.FeedsInSystem, models.System.feeds), ("transfers", views.TransfersInSystem, models.System.transfers), ): setattr( response, attr, view.from_model( system, genericqueries.count_number_of_related_entities(relationship, system), ), ) return response
def list_all_in_route( system_id, route_id, alerts_detail: views.AlertsDetail = None ) -> typing.List[views.Trip]: route = routequeries.get_in_system_by_id(system_id, route_id) if route is None: raise exceptions.IdNotFoundError( models.Route, system_id=system_id, route_id=route_id ) response = [] trips = tripqueries.list_all_in_route_by_pk(route.pk) trip_pk_to_last_stop = tripqueries.get_trip_pk_to_last_stop_map( trip.pk for trip in trips ) for trip in trips: trip_response = views.Trip.from_model(trip) last_stop = trip_pk_to_last_stop.get(trip.pk) if last_stop is not None: trip_response.last_stop = views.Stop.from_model(last_stop) response.append(trip_response) helpers.add_alerts_to_views( response, trips, alerts_detail or views.AlertsDetail.CAUSE_AND_EFFECT ) return response
def get_in_system_by_id( system_id, stop_id, return_only_stations=True, earliest_time=None, latest_time=None, minimum_number_of_trips=None, include_all_trips_within=None, exclude_trips_before=None, alerts_detail=None, ): """ Get information about a specific stop. """ stop = stopqueries.get_in_system_by_id(system_id, stop_id) if stop is None: raise exceptions.IdNotFoundError(models.Stop, system_id=system_id, stop_id=stop_id) stop_tree = _StopTree(stop, stopqueries.list_all_stops_in_stop_tree(stop.pk)) all_station_pks = set(stop.pk for stop in stop_tree.all_stations()) transfers = stopqueries.list_all_transfers_at_stops(all_station_pks) # The descendant stops are used as the source of trip stop times descendant_stop_pks = list(stop.pk for stop in stop_tree.descendents()) direction_name_matcher = _DirectionNameMatcher( stopqueries.list_direction_rules_for_stops(descendant_stop_pks)) trip_stop_times = stopqueries.list_stop_time_updates_at_stops( descendant_stop_pks, earliest_time=earliest_time, latest_time=latest_time, ) trip_pk_to_last_stop = tripqueries.get_trip_pk_to_last_stop_map( trip_stop_time.trip.pk for trip_stop_time in trip_stop_times) # On the other hand, the stop tree graph that is returned consists of all # stations in the stop's tree stop_pk_to_service_maps_response = servicemapmanager.build_stop_pk_to_service_maps_response( all_station_pks.union(transfer.to_stop_pk for transfer in transfers)) # Using the data retrieved, we then build the response response = views.StopLarge.from_model(stop) stop_tree_base: views.Stop = _build_stop_tree_response( stop_tree, stop_pk_to_service_maps_response, return_only_stations) response.parent_stop = stop_tree_base.parent_stop response.child_stops = stop_tree_base.child_stops response.service_maps = stop_tree_base.service_maps response.directions = list(direction_name_matcher.all_names()) response.transfers = _build_transfers_response( transfers, stop_pk_to_service_maps_response) stop_time_filter = _TripStopTimeFilter( inclusion_interval_start=exclude_trips_before, inclusion_interval_end=include_all_trips_within, min_trips_per_direction=minimum_number_of_trips, ) for trip_stop_time in trip_stop_times: direction = direction_name_matcher.match(trip_stop_time) if stop_time_filter.remove(trip_stop_time, direction): continue response.stop_times.append( _build_trip_stop_time_response(trip_stop_time, direction, trip_pk_to_last_stop)) helpers.add_alerts_to_views( [response], [stop], alerts_detail or views.AlertsDetail.CAUSE_AND_EFFECT, ) return response
def _get_transfer_config(config_id): config = transfersconfigqueries.get(config_id) if config is None: raise exceptions.IdNotFoundError(entity_type=models.TransfersConfig, id=config_id) return config