Skip to content

Commit

Permalink
merged main to dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamad11Dab committed May 27, 2024
2 parents 5608091 + f4c75c5 commit c8dc727
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 8 deletions.
7 changes: 5 additions & 2 deletions application/app/views/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,27 @@ def haversine(lon1, lat1, lon2, lat2):
distance = radius_earth_km * c
return distance

def distance_query(set_long, set_lat, distance):
def distance_query(set_long, set_lat, distance, offers):
nearby_requests = []
## Only search for PENDING to reduce search space
trip_requests = db.session.execute(db.select(TripRequest).filter_by(status='PENDING').order_by(TripRequest.requested_time)).scalars().all()

for request in trip_requests:
start_point = to_shape(request.pickup_location)
end_point = to_shape(request.dropoff_location)

# Calculate distance between set point and pickup location of the request
dist = haversine(start_point.x, start_point.y, set_long, set_lat)

if abs(dist) <= distance:
nearby_requests.append(request)

if len(nearby_requests) == offers:
break

return [trip.to_dict() for trip in trip_requests]



def link_trip_request_to_trip(trip_id: str, trip_request_id: str) -> bool:
trip = db.session.execute(db.select(Trip).filter_by(id=trip_id)).scalars().first()
trip_req = db.session.execute(db.select(TripRequest).filter_by(id=trip_request_id)).scalars().first()
Expand Down
69 changes: 65 additions & 4 deletions application/app/views/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,21 @@ def post(self):
return make_response("There is no trip under this ID.", 400)
start_point = to_shape(trip.start_location)
willing_distance_to_travel = trip.seats_remaining
seats_remaining = trip.seats_remaining

if trip.seats_remaining is not None:
if trip.seats_remaining == 0:
return make_response([], 200)
willing_distance_to_travel = trip.distance_addition / trip.seats_remaining
else:
willing_distance_to_travel = trip.distance_addition / trip.driver.car.max_available_seats

choices = distance_query(start_point.x, start_point.y, willing_distance_to_travel)
if (seats_remaining):
choices = distance_query(start_point.x, start_point.y, willing_distance_to_travel, (2 * seats_remaining))
else:
choices = []
return make_response(choices, 200)


class GetApprovedTripRequests(Resource):
def post(self):
contents = nearby_trip_requests_parser.parse_args()
Expand All @@ -338,7 +342,7 @@ def post(self):
trip_query = db.session.execute(db.select(Trip).filter_by(id=contents.get("trip_id"))).scalars().first()
if trip_query:
ans = [trip.id for trip in trip_query.trip_requests]
return make_response(f"Accepted trip requests are: {ans}", 200)
return make_response({"accepted trips": ans}, 200)
else:
return make_response(f"This is not a valid trip id.", 400)
else:
Expand Down Expand Up @@ -381,7 +385,7 @@ def post(self):

class Test(Resource):
def get(self):
result = distance_query( -123.4194, 37.7749, 90)
result = distance_query(-123.4194, 37.7749, 90)

worked = link_trip_request_to_trip("a5cade10-3b8b-4ff2-80db-eab01946e4c8", "735eb991-1c24-4108-896d-0f08c32eb226")
worked2 = link_trip_request_to_trip("a5cade10-3b8b-4ff2-80db-eab01946e4c8", "50eb1230-8e50-4ec9-93ac-179121c254a1")
Expand All @@ -391,6 +395,41 @@ def get(self):
return make_response("Num trip requests" + str(len(trip.trip_requests)), 205)
# return make_response(f"distance = {result}", 200)


class GetTripPositions(Resource):
def post(self):
contents = nearby_trip_requests_parser.parse_args()
trip = db.session.execute(db.select(Trip).filter_by(id=contents.get("trip_id"))).scalars().first()
if trip is None:
return make_response("There is no trip with this id.", 200)

start_point = to_shape(trip.start_location)
passengers = trip.trip_requests
# Sort passengers by the Haversine distance from the start point
sorted_passengers = sorted(
passengers,
key=lambda p: haversine(
start_point.x, start_point.y,
to_shape(p.pickup_location).x, to_shape(p.pickup_location).y
)
)

# Construct the response
response = {
"trip_time": trip.start_time,
"passengers": [
{
"lat": to_shape(p.pickup_location).y,
"long": to_shape(p.pickup_location).x,
"name": p.passenger.user[0].name,
"pickup": p.start_address
}
for p in sorted_passengers
]
}

return make_response(response, 200)

### Resources for methods that have POST and specific get methods ###
api.add_resource(Health, "/health")
api.add_resource(CreateDriver, "/driver/create")
Expand All @@ -406,4 +445,26 @@ def get(self):
api.add_resource(GetNearbyTripRequests, "/trip/get/pending_nearby")
api.add_resource(GetApprovedTripRequests, "/trip/get/approved")
api.add_resource(ApproveRequest, "/trip/post/approved")
api.add_resource(GetTripPositions, "/trip/get_route_positions")


### trip/get_route_pos
### trip id --> list of lat/long + start time of trip + name of passanger (sort them based on distance chains)

# {
# "time": start_time,
# "passanger": [
# { "lat"
# "long"
# "name"
# "pickup or dropoff"
# }
# ,{

# }
# ]
# }

######

api.add_resource(Test, "/test")
1 change: 1 addition & 0 deletions application/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
environment:
CELERY_BROKER_URL: redis://redis:6379
SQLALCHEMY_DATABASE_URI: postgresql+psycopg://administrator:verySecretPassword@database:5432/ride
# ROUTING_API_URL: trwst.com
command: celery --app tasks.celery_app worker --loglevel=info -Q matching.fifo --uid=nobody --gid=nogroup
depends_on:
- redis
Expand Down
48 changes: 46 additions & 2 deletions endpoint.http
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ Content-Type: application/json
"email": "[email protected]"
}

### Create a passenger for Bailey
POST {{baseUrl}}/passenger/create
Content-Type: application/json

{
"username": "Bailey45",
"password": "53%32",
"name": "Bailey Doe",
"phone_number": "47383926",
"email": "[email protected]"
}


### Get a user by username
POST {{baseUrl}}/profile
Content-Type: application/json
Expand Down Expand Up @@ -106,6 +119,19 @@ Content-Type: application/json
"pickup_window_end": "2024-05-10T08:00:00"
}

### Add trip request
POST {{baseUrl}}/trip_request/create
Content-Type: application/json

{
"username": "Bailey45",
"requested_time": "2024-05-10T08:01:00",
"pickup_location": {"latitude": 37.7749, "longitude": -123.4193, "address": "123 street road, brisbane, qld"},
"dropoff_location": {"latitude": 37.7749, "longitude": -123.4193, "address": "125 street road, brisbane, qld"},
"pickup_window_start": "2024-05-10T07:31:00",
"pickup_window_end": "2024-05-10T08:00:00"
}

### Get all trip requests for a user
POST {{baseUrl}}/trip_requests/get/all
Content-Type: application/json
Expand All @@ -114,6 +140,15 @@ Content-Type: application/json
"username": "jDoe12"
}

### Get all trip requests for a user
POST {{baseUrl}}/trip_requests/get/all
Content-Type: application/json

{
"username": "Bailey45"
}


### Get a trip requests by its id
POST {{baseUrl}}/trip_requests/get
Content-Type: application/json
Expand Down Expand Up @@ -155,9 +190,18 @@ Content-Type: application/json

{
"username": "jDoe12",
"trip_request_id": "0789e17c-0b53-4d1f-ba91-c381b28ef9dd",
"trip_request_id": "e7f8fc4b-8cca-4ca0-97d1-725a4cefe4fd",
"trip_id": "c84fbfc3-3d2f-4f3f-a3ff-15286d31c51c"
}

### GET THE TEST
GET {{baseUrl}}/test
GET {{baseUrl}}/test

### GET NEW END POINT POSITIONING ALONG ROUTES
POST {{baseUrl}}/trip/get_route_positions
Content-Type: application/json

{
"trip_id": "c84fbfc3-3d2f-4f3f-a3ff-15286d31c51c",
"username": "jDoe12"
}
35 changes: 35 additions & 0 deletions model/adrs/temp
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,38 @@ Dependence on community support and updates for new features and optimizations.
Negative:
Potentially higher learning curve for developers not familiar with geospatial databases.
Increased complexity in database management due to the advanced features and functions of PostGIS.
------------------------------------------------------------------------------------------------------------------
Title: Adoption of Self-Hosted Nominatim API for Geocoding
Date: 24/05/2024
Status: Superseded

Summary:
This ADR revises our previous decision to use the Nominatim API as an external service by transitioning to a self-hosted solution on an EC2 instance. This shift will provide us with greater control over the geocoding process and data, enhancing our ability to customize and optimize the service for our specific needs in the ride-sharing application.

Context:
As our application's scale and complexity grow, so does the need for more controlled and reliable geocoding services. Self-hosting allows us to manage the geocoding data directly, offering better customization and potentially improved performance.

Decision:
We will deploy the Nominatim API on an Amazon EC2 instance to handle our application's geocoding needs. This approach will give us full control over the geocoding process, including data management, updates, and system configuration.

Rationale for Self-Hosting:
Although previously deemed impractical for a smaller scale project, our evolving requirements and growth prospects justify the investment in self-hosting. The decision is driven by:

Increased need for customization and control over the geocoding data.
Potential cost savings in the long term with increased usage.
Elimination of reliance on external API's availability and performance constraints.
Consequences:

Positive:

Full control over the geocoding process allows for tailored configurations and optimizations.
Direct access to manage and update the data as needed without waiting for third-party updates.
Potentially better performance and reliability by optimizing server resources and configurations.
Neutral:

Increased initial setup and maintenance efforts required.
Need for in-house expertise to manage and operate the geocoding server effectively.
Negative:

Higher upfront costs for server setup and ongoing operational expenses.
Increased responsibility for ensuring high availability and data integrity, which could require additional resources and planning.

0 comments on commit c8dc727

Please sign in to comment.