Esempio n. 1
0
def start_ride_txn(session, vehicle_id, user_email):
    """
    Start a vehicle ride (or continue if the vehicle is already in use).

    Arguments:
        session {.Session} -- The active session for the database connection.
        vehicle_id {String} -- The vehicle's `id` column.
    """
    # find the row where we want to start the ride.
    # SELECT * FROM vehicles WHERE id = <vehicle_id> AND in_use = false
    #         LIMIT 1;
    vehicle = session.query(Vehicle).filter(Vehicle.id == vehicle_id). \
                                     filter(Vehicle.in_use == False).first()

    if vehicle is None:
        return None

    # SELECT * FROM location_history WHERE vehicle_id = <vehicle_id>
    #      ORDER BY ts DESC LIMIT 1;
    last_chx = session.query(LocationHistory). \
                       filter(LocationHistory.vehicle_id ==
                              vehicle_id). \
                       order_by(LocationHistory.ts.desc()). \
                       first()
    new_location_history_id = str(uuid4())
    new_timestamp = func.now()
    new_location_history_row = LocationHistory(id=new_location_history_id,
                                               vehicle_id=vehicle_id,
                                               longitude=last_chx.longitude,
                                               latitude=last_chx.latitude,
                                               ts=new_timestamp)

    new_ride_id = str(uuid4())
    new_ride_row = Ride(id=new_ride_id,
                        vehicle_id=vehicle_id,
                        user_email=user_email,
                        start_ts=new_timestamp,
                        end_ts=None)

    # UPDATE vehicles SET in_use = true WHERE vehicles.id = <vehicle_id>
    vehicle.in_use = True
    vehicle.last_checkin = func.now()
    session.add(new_location_history_row)
    session.flush()
    session.add(new_ride_row)

    return True  # Just making it explicit that this worked.
Esempio n. 2
0
def start_ride_txn(session, city, rider_id, vehicle_id):
    """
    Insert a new row into the rides table and update a row of the vehicles table.

    Arguments:
        session {.Session} -- The active session for the database connection.
        city {String} -- The vehicle's city.
        rider_id {UUID} -- The user's unique ID.
        rider_city {String} -- The city in which the rider is registered.
        vehicle_id {UUID} -- The vehicle's unique ID.
    """
    v = session.query(Vehicle).filter(Vehicle.id == vehicle_id).first()
    r = Ride(city=city,
             id=str(uuid.uuid4()),
             rider_id=rider_id,
             vehicle_id=vehicle_id,
             start_location=v.last_location,
             start_time=datetime.datetime.now(datetime.timezone.utc))

    session.add(r)
    v.status = "unavailable"