Beispiel #1
0
def get_routes_static_info(info_id):
    """Return routes static information by id."""
    result = Static.get_static_info(info_id)
    if result is None:
        message = "Couldn't retrieve data from database. Try again, please."
        return make_response(False, message, HTTPStatus.BAD_REQUEST)

    info = sorted(result, key=lambda x: -x["value"])
    return make_response(True, info, HTTPStatus.OK)
Beispiel #2
0
def get_route_coordinates(route):
    """Return route coordinates for the last collected time."""
    route = parse.unquote(route, encoding="utf-8")
    timeseries = Traffic.get_route_coordinates(route)
    if timeseries is None:
        message = "Couldn't retrieve data from database. Try again, please."
        return make_response(False, message, HTTPStatus.BAD_REQUEST)

    coordinates = timeseries[0] if timeseries else None
    return make_response(True, coordinates, HTTPStatus.OK)
Beispiel #3
0
def get_regions_congestion(region):
    """Return city region traffic congestion."""
    region = parse.unquote(region, encoding="utf-8")
    limit = request.args.get("limit", type=int, default=15)
    result = Congestion.get_region_congestion(region, limit)
    if result is None:
        message = "Couldn't retrieve data from database. Try again, please."
        return make_response(False, message, HTTPStatus.BAD_REQUEST)

    return make_response(True, result, HTTPStatus.OK)
Beispiel #4
0
def get_route_avg_distance(route):
    """Return aggregated routes timeseries by avg_distance."""
    route = parse.unquote(route, encoding="utf-8")
    delta = request.args.get("delta", type=float, default=3600)
    timeseries = Traffic.get_route_avg_distance(route, delta)
    if timeseries is None:
        message = "Couldn't retrieve data from database. Try again, please."
        return make_response(False, message, HTTPStatus.BAD_REQUEST)

    return make_response(True, timeseries, HTTPStatus.OK)
Beispiel #5
0
def get_stops():
    """Return stops suggestion by name."""
    query = request.args.get("query", "")
    limit = request.args.get("limit", type=int, default=10)

    stops = Stops.get_stops_by_name(query, limit)
    if stops is None:
        message = "Couldn't retrieve data from database. Try again, please."
        return make_response(False, message, HTTPStatus.BAD_REQUEST)

    return make_response(True, stops, HTTPStatus.OK)
Beispiel #6
0
def get_nearest_arrivals(stop_id):
    """Return the nearest arrivals for provided stop id."""
    stop = Stops.get_stop_by_id(stop_id)
    if stop is None:
        message = "Couldn't retrieve data from database. Try again, please."
        return make_response(False, message, HTTPStatus.BAD_REQUEST)

    time_start = get_time_integer(datetime.now().strftime(TIME_FORMAT))
    time_end = time_start + 3600
    nearest_arrivals = filter(
        lambda x: time_start <= x["arrival_time_integer"] <= time_end,
        stop["arrivals"])

    return make_response(True, list(nearest_arrivals), HTTPStatus.OK)
Beispiel #7
0
def get_routes_names():
    """Return json response with available routes from easyway for last period."""
    delta = request.args.get("delta", type=float, default=3600)
    result = Traffic.get_routes_names(delta)
    if result is None:
        message = "Couldn't retrieve data from database. Try again, please."
        return make_response(False, message, HTTPStatus.BAD_REQUEST)

    routes = []
    for route in result:
        route_names = sorted(route["route_names"])
        routes.append({"route_type": route["_id"], "route_names": route_names})

    routes = sorted(routes, key=lambda x: -len(x["route_names"]))
    return make_response(True, routes, HTTPStatus.OK)
Beispiel #8
0
def get_nearest_stops():
    """Return the nearest stops by provided latitude and longitude."""
    limit = request.args.get("limit", type=int, default=5)
    latitude = request.args.get("latitude", type=float)
    longitude = request.args.get("longitude", type=float)
    if not latitude or not longitude:
        message = "The required params latitude and longitude weren't provided."
        return make_response(False, message, HTTPStatus.BAD_REQUEST)

    nearest_stops = Stops.get_nearest_stops(latitude, longitude, limit)
    if nearest_stops is None:
        message = "Couldn't retrieve data from database. Try again, please."
        return make_response(False, message, HTTPStatus.BAD_REQUEST)

    return make_response(True, nearest_stops, HTTPStatus.OK)
Beispiel #9
0
def handle_500(error):
    """Return custom response for 500 http status code."""
    return make_response(
        False,
        "Something has gone wrong on the server side. Please, try again later.",
        error.code
    )
Beispiel #10
0
def handle_405(error):
    """Return custom response for 405 http status code."""
    return make_response(
        False,
        f"The method ({request.method}) you are trying to use for this URL could not be handled on the server.",
        error.code
    )
Beispiel #11
0
def handle_404(error):
    """Return custom response for 404 http status code."""
    return make_response(
        False,
        f"The endpoint ({request.path}) you are trying to access could not be found on the server.",
        error.code
    )
Beispiel #12
0
def get_health():
    """Return health OK http status."""
    return make_response(True, "OK", HTTPStatus.OK)