-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
164 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,51 @@ | ||
from typing import Dict, Any | ||
|
||
from sqlalchemy.orm import Session | ||
from sqlalchemy import create_engine | ||
|
||
|
||
def create_db_session(db_config): | ||
def create_db_session(db_config: Dict[str, Any]) -> Session: | ||
""" | ||
Create a SQLAlchemy Database Connection session from to the database using the database configuration | ||
:param db_config: database specific section of the app_config.yaml | ||
:return: SQLAlchmey database connection | ||
""" | ||
engine = create_engine(db_config['url']) | ||
session = Session(engine) | ||
return session | ||
|
||
|
||
def save_to_db_session(session, item): | ||
def save_to_db_session(session: Session, item: Any) -> None: | ||
""" | ||
Stage an object to the session | ||
:param session: Database Connection Session | ||
:param item: Object to be added to session updates | ||
""" | ||
session.add(item) | ||
|
||
|
||
def commit_db_session(session): | ||
def commit_db_session(session: Session) -> None: | ||
""" | ||
Commit session updates to database | ||
:param session: Database Connection Session | ||
""" | ||
session.commit() | ||
|
||
|
||
def close_db_session(session): | ||
def close_db_session(session: Session) -> None: | ||
""" | ||
Close database connection session | ||
:param session: Database Connection Session | ||
""" | ||
session.close() | ||
|
||
|
||
def save_and_commit_to_db(session, item): | ||
def save_and_commit_to_db(session: Session, item: Any): | ||
""" | ||
Stage and commit and object to a database in one action | ||
:param session: Database connection session | ||
:param item: Object to add to database | ||
""" | ||
save_to_db_session(session, item) | ||
commit_db_session(session) | ||
return item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,48 @@ | ||
class InvalidTripException(Exception): | ||
""" | ||
Raised if trip is trivially infeasible when constructed | ||
""" | ||
pass | ||
|
||
|
||
class InvalidConfigException(Exception): | ||
""" | ||
Raised when there is an issue with the configuration | ||
""" | ||
pass | ||
|
||
|
||
class RevenueCalculationException(Exception): | ||
""" | ||
Raised when Revenue Table is missing the level of service for a trip | ||
""" | ||
pass | ||
|
||
|
||
class SolutionNotFoundException(Exception): | ||
""" | ||
Raised when the Optimizer could not find a solution after N attempts | ||
""" | ||
pass | ||
|
||
|
||
class UnknownDriverException(Exception): | ||
""" | ||
Raised when a driver ID was passed in that was not part of the original CSV or Database set of drivers | ||
""" | ||
pass | ||
|
||
|
||
class MissingTripDetailsException(Exception): | ||
""" | ||
Raised when the Trip Dataframe is missing necessary details | ||
""" | ||
pass | ||
|
||
|
||
class InvalidRevenueRateMilageException(Exception): | ||
class InvalidRevenueRateMileageException(Exception): | ||
""" | ||
Raised with there is an issue with the revenue calculation. (i.e. there is not mileage window for the input number | ||
of miles) | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,53 @@ | ||
import datetime | ||
from datetime import datetime, timedelta | ||
from typing import Optional | ||
|
||
ONE_MINUTE = 0.00069444444 | ||
|
||
def get_time_window_by_hours_minutes(hours, minutes): | ||
|
||
def get_time_window_by_hours_minutes(hours: int, minutes: int) -> float: | ||
""" | ||
Get a fractional of a day representation given the hours and minutes | ||
:param hours: number of hours | ||
:param minutes: number of minutes | ||
:return: fraction of a day equivalent if the given hours and minutes passed from midnight | ||
""" | ||
return ONE_MINUTE * (60 * hours + minutes) | ||
|
||
|
||
def fifteen_minutes(): | ||
""" | ||
:return: Get a precalculated 15 minute fraction of day equivalent | ||
""" | ||
return get_time_window_by_hours_minutes(0, 15) | ||
|
||
def timedelta_to_hhmmss(td): | ||
|
||
def timedelta_to_hhmmss(td: timedelta) -> str: | ||
""" | ||
Convert time delta object to a HH:MM:SS string | ||
:param td: input timedelta | ||
:return: HH:MM:SS rounded string of time delta | ||
""" | ||
return str(td).split('.')[0] | ||
|
||
def date_to_day_of_week(date): | ||
|
||
def date_to_day_of_week(date: Optional[str] = None) -> int: | ||
""" | ||
Convert a date into day of week | ||
:param date: Optional. If not passed in, then current day is used. | ||
:return: Day of the week starting with [0=Monday, 6=Sunday] | ||
""" | ||
if date is None: | ||
day_of_week = datetime.datetime.now().timetuple().tm_wday | ||
day_of_week = datetime.now().timetuple().tm_wday | ||
else: | ||
m, d, y = date.split('-') | ||
day_of_week = datetime.datetime(int(y), int(m), int(d)).timetuple().tm_wday | ||
day_of_week = datetime(int(y), int(m), int(d)).timetuple().tm_wday | ||
return day_of_week | ||
|
||
def timedelta_to_fraction_of_day(td): | ||
|
||
def timedelta_to_fraction_of_day(td: timedelta) -> float: | ||
""" | ||
Convert timedelta object to a fraction of a day completed representation | ||
:param td: timedelta object | ||
:return: fraction of day passed by timedelta | ||
""" | ||
return td.total_seconds() / (60 * 60 * 24) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,30 @@ | ||
import datetime | ||
|
||
from pandas import Series | ||
|
||
def generate_html_label_for_addr(trips, addr): | ||
from avicena.models import Driver | ||
|
||
|
||
def generate_html_label_for_addr(trips: Series, addr: str) -> str: | ||
""" | ||
Generate specialized string label with HTML tags of trips passing by a specific address | ||
:param trips: Trips passing by address | ||
:param addr: Original address of location | ||
:return: HTML Formatted details about trips going through specified address | ||
""" | ||
data = "<br>".join( | ||
"0" * (10 - len(str(t['trip_id']))) + str(t['trip_id']) + " | " + str( | ||
datetime.timedelta(days=float(t['est_pickup_time']))).split('.')[0] + | ||
" | " + str(t['driver_id']) for t in trips | ||
) | ||
return addr + "<br><b>TripID, Time, DriverID </b><br>" + data | ||
|
||
def generate_html_label_for_driver_addr(d): | ||
return d.address[:-4] + "<br>Driver " + str(d.id) + " Home" | ||
|
||
def generate_html_label_for_driver_addr(d: Driver) -> str: | ||
""" | ||
Generate a driver HTML Label for visualization | ||
:param d: Driver object | ||
:return: HTML formatted driver address | ||
""" | ||
return d.address.get_clean_address() + "<br>Driver " + str(d.id) + " Home" | ||
|