From c339719936fee5101be8a0f2d15cc7c4ff9ab12f Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 30 Nov 2026 11:16:24 +0100 Subject: [PATCH] Annotate the ORM columns with `orm.Mapped` Since SQLAlchemy 2.0, `orm.mapped_column()` with an annotation lets `mypy` see `pixel.id` as an `int` instead of a `Column[int]`. --- src/velofy/db/addresses.py | 62 +++++-- src/velofy/db/addresses_addresses.py | 36 +++- src/velofy/db/addresses_pixels.py | 20 ++- src/velofy/db/cities.py | 47 +++-- src/velofy/db/couriers.py | 39 +++- src/velofy/db/customers.py | 6 +- src/velofy/db/forecasts.py | 63 +++++-- src/velofy/db/grids.py | 13 +- src/velofy/db/orders.py | 257 ++++++++++++++++++++++----- src/velofy/db/pixels.py | 24 ++- src/velofy/db/replay/orders.py | 108 ++++++++--- src/velofy/db/replay/simulations.py | 23 ++- src/velofy/db/restaurants.py | 25 ++- 13 files changed, 588 insertions(+), 135 deletions(-) diff --git a/src/velofy/db/addresses.py b/src/velofy/db/addresses.py index af13c98..c858862 100644 --- a/src/velofy/db/addresses.py +++ b/src/velofy/db/addresses.py @@ -2,6 +2,7 @@ from __future__ import annotations +import datetime as dt import functools from typing import Any @@ -22,17 +23,56 @@ class Address(meta.Base): __tablename__ = "addresses" # Columns - id = sa.Column(sa.Integer, primary_key=True, autoincrement=False) - primary_id = sa.Column(sa.Integer, nullable=False, index=True) - created_at = sa.Column(sa.DateTime, nullable=False) - place_id = sa.Column(sa.Unicode(length=120), nullable=False, index=True) - latitude = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False) - longitude = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False) - city_id = sa.Column(sa.SmallInteger, nullable=False, index=True) - city_name = sa.Column("city", sa.Unicode(length=25), nullable=False) - zip_code = sa.Column(sa.Integer, nullable=False, index=True) - street = sa.Column(sa.Unicode(length=80), nullable=False) - floor = sa.Column(sa.SmallInteger) + id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + autoincrement=False, + ) + primary_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + index=True, + ) + created_at: orm.Mapped[dt.datetime] = orm.mapped_column( + sa.DateTime, + nullable=False, + ) + place_id: orm.Mapped[str] = orm.mapped_column( + sa.Unicode(length=120), + nullable=False, + index=True, + ) + latitude: orm.Mapped[float] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=False, + ) + longitude: orm.Mapped[float] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=False, + ) + city_id: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + index=True, + ) + city_name: orm.Mapped[str] = orm.mapped_column( + "city", + sa.Unicode(length=25), + nullable=False, + ) + zip_code: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + index=True, + ) + street: orm.Mapped[str] = orm.mapped_column( + sa.Unicode(length=80), + nullable=False, + ) + floor: orm.Mapped[int | None] = orm.mapped_column( + sa.SmallInteger, + nullable=True, + ) # Constraints __table_args__ = ( diff --git a/src/velofy/db/addresses_addresses.py b/src/velofy/db/addresses_addresses.py index ef965ab..0ab22f1 100644 --- a/src/velofy/db/addresses_addresses.py +++ b/src/velofy/db/addresses_addresses.py @@ -39,16 +39,38 @@ class Path(meta.Base): __tablename__ = "addresses_addresses" # Columns - first_address_id = sa.Column(sa.Integer, primary_key=True) - second_address_id = sa.Column(sa.Integer, primary_key=True) - city_id = sa.Column(sa.SmallInteger, nullable=False) + first_address_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + ) + second_address_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + ) + city_id: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) # Distances are measured in meters. - air_distance = sa.Column(sa.Integer, nullable=False) - bicycle_distance = sa.Column(sa.Integer, nullable=True) + air_distance: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + ) + bicycle_distance: orm.Mapped[int | None] = orm.mapped_column( + sa.Integer, + nullable=True, + ) # The duration is measured in seconds. - bicycle_duration = sa.Column(sa.Integer, nullable=True) + bicycle_duration: orm.Mapped[int | None] = orm.mapped_column( + sa.Integer, + nullable=True, + ) # An array of latitude-longitude pairs approximating a courier's way. - _directions = sa.Column("directions", postgresql.JSON, nullable=True) + _directions: orm.Mapped[str | None] = orm.mapped_column( + "directions", + postgresql.JSON, + nullable=True, + ) # Constraints __table_args__ = ( diff --git a/src/velofy/db/addresses_pixels.py b/src/velofy/db/addresses_pixels.py index d75377b..60133a5 100644 --- a/src/velofy/db/addresses_pixels.py +++ b/src/velofy/db/addresses_pixels.py @@ -19,10 +19,22 @@ class AddressPixelAssociation(meta.Base): __tablename__ = "addresses_pixels" # Columns - address_id = sa.Column(sa.Integer, primary_key=True) - city_id = sa.Column(sa.SmallInteger, nullable=False) - grid_id = sa.Column(sa.SmallInteger, nullable=False) - pixel_id = sa.Column(sa.Integer, primary_key=True) + address_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + ) + city_id: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) + grid_id: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) + pixel_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + ) # Constraints __table_args__ = ( diff --git a/src/velofy/db/cities.py b/src/velofy/db/cities.py index f8b1e39..ce7b8b9 100644 --- a/src/velofy/db/cities.py +++ b/src/velofy/db/cities.py @@ -21,22 +21,49 @@ class City(meta.Base): __tablename__ = "cities" # Generic columns - id = sa.Column( + id: orm.Mapped[int] = orm.mapped_column( sa.SmallInteger, primary_key=True, autoincrement=False, ) - name = sa.Column(sa.Unicode(length=10), nullable=False) - kml = sa.Column(sa.UnicodeText, nullable=False) + name: orm.Mapped[str] = orm.mapped_column( + sa.Unicode(length=10), + nullable=False, + ) + kml: orm.Mapped[str] = orm.mapped_column( + sa.UnicodeText, + nullable=False, + ) # Google Maps related columns - center_latitude = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False) - center_longitude = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False) - northeast_latitude = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False) - northeast_longitude = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False) - southwest_latitude = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False) - southwest_longitude = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False) - initial_zoom = sa.Column(sa.SmallInteger, nullable=False) + center_latitude: orm.Mapped[float] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=False, + ) + center_longitude: orm.Mapped[float] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=False, + ) + northeast_latitude: orm.Mapped[float] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=False, + ) + northeast_longitude: orm.Mapped[float] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=False, + ) + southwest_latitude: orm.Mapped[float] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=False, + ) + southwest_longitude: orm.Mapped[float] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=False, + ) + initial_zoom: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) # Relationships addresses = orm.relationship("Address", back_populates="city") diff --git a/src/velofy/db/couriers.py b/src/velofy/db/couriers.py index ea7f588..94ad329 100644 --- a/src/velofy/db/couriers.py +++ b/src/velofy/db/couriers.py @@ -1,5 +1,7 @@ """Provide the ORM's `Courier` model.""" +import datetime as dt + import sqlalchemy as sa from sqlalchemy import orm from sqlalchemy.dialects import postgresql @@ -13,13 +15,36 @@ class Courier(meta.Base): __tablename__ = "couriers" # Columns - id = sa.Column(sa.Integer, primary_key=True, autoincrement=False) - created_at = sa.Column(sa.DateTime, nullable=False) - vehicle = sa.Column(sa.Unicode(length=10), nullable=False) - historic_speed = sa.Column("speed", postgresql.DOUBLE_PRECISION, nullable=False) - capacity = sa.Column(sa.SmallInteger, nullable=False) - pay_per_hour = sa.Column(sa.SmallInteger, nullable=False) - pay_per_order = sa.Column(sa.SmallInteger, nullable=False) + id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + autoincrement=False, + ) + created_at: orm.Mapped[dt.datetime] = orm.mapped_column( + sa.DateTime, + nullable=False, + ) + vehicle: orm.Mapped[str] = orm.mapped_column( + sa.Unicode(length=10), + nullable=False, + ) + historic_speed: orm.Mapped[float] = orm.mapped_column( + "speed", + postgresql.DOUBLE_PRECISION, + nullable=False, + ) + capacity: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) + pay_per_hour: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) + pay_per_order: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) # Constraints __table_args__ = ( diff --git a/src/velofy/db/customers.py b/src/velofy/db/customers.py index 4aa0328..da701f5 100644 --- a/src/velofy/db/customers.py +++ b/src/velofy/db/customers.py @@ -17,7 +17,11 @@ class Customer(meta.Base): __tablename__ = "customers" # Columns - id = sa.Column(sa.Integer, primary_key=True, autoincrement=False) + id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + autoincrement=False, + ) def __repr__(self) -> str: """Non-literal text representation.""" diff --git a/src/velofy/db/forecasts.py b/src/velofy/db/forecasts.py index 1296978..f33a0dd 100644 --- a/src/velofy/db/forecasts.py +++ b/src/velofy/db/forecasts.py @@ -2,6 +2,7 @@ from __future__ import annotations +import datetime as dt import math from typing import List @@ -23,25 +24,63 @@ class Forecast(meta.Base): __tablename__ = "forecasts" # Columns - id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) - pixel_id = sa.Column(sa.Integer, nullable=False, index=True) - start_at = sa.Column(sa.DateTime, nullable=False) - time_step = sa.Column(sa.SmallInteger, nullable=False) - train_horizon = sa.Column(sa.SmallInteger, nullable=False) - model = sa.Column(sa.Unicode(length=20), nullable=False) + id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + autoincrement=True, + ) + pixel_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + index=True, + ) + start_at: orm.Mapped[dt.datetime] = orm.mapped_column( + sa.DateTime, + nullable=False, + ) + time_step: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) + train_horizon: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) + model: orm.Mapped[str] = orm.mapped_column( + sa.Unicode(length=20), + nullable=False, + ) # We also store the actual order counts for convenient retrieval. # A `UniqueConstraint` below ensures that redundant values that # are to be expected are consistent across rows. - actual = sa.Column(sa.SmallInteger, nullable=False) + actual: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) # Raw `.prediction`s are stored as `float`s (possibly negative). # The rounding is then done on the fly if required. - prediction = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False) + prediction: orm.Mapped[float] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=False, + ) # The confidence intervals are treated like the `.prediction`s # but they may be nullable as some methods do not calculate them. - low80 = sa.Column(postgresql.DOUBLE_PRECISION, nullable=True) - high80 = sa.Column(postgresql.DOUBLE_PRECISION, nullable=True) - low95 = sa.Column(postgresql.DOUBLE_PRECISION, nullable=True) - high95 = sa.Column(postgresql.DOUBLE_PRECISION, nullable=True) + low80: orm.Mapped[float | None] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=True, + ) + high80: orm.Mapped[float | None] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=True, + ) + low95: orm.Mapped[float | None] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=True, + ) + high95: orm.Mapped[float | None] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=True, + ) # Constraints __table_args__ = ( diff --git a/src/velofy/db/grids.py b/src/velofy/db/grids.py index dae6013..9821ade 100644 --- a/src/velofy/db/grids.py +++ b/src/velofy/db/grids.py @@ -22,13 +22,20 @@ class Grid(meta.Base): __tablename__ = "grids" # Columns - id = sa.Column( + id: orm.Mapped[int] = orm.mapped_column( sa.SmallInteger, primary_key=True, autoincrement=True, ) - city_id = sa.Column(sa.SmallInteger, nullable=False) - side_length = sa.Column(sa.SmallInteger, nullable=False, unique=True) + city_id: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) + side_length: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + unique=True, + ) # Constraints __table_args__ = ( diff --git a/src/velofy/db/orders.py b/src/velofy/db/orders.py index cf198ff..a66fb2c 100644 --- a/src/velofy/db/orders.py +++ b/src/velofy/db/orders.py @@ -1,6 +1,7 @@ """Provide the ORM's `Order` model.""" import datetime +import datetime as dt import folium import sqlalchemy as sa @@ -18,64 +19,228 @@ class Order(meta.Base): __tablename__ = "orders" # Generic columns - id = sa.Column(sa.Integer, primary_key=True, autoincrement=False) - _delivery_id = sa.Column("delivery_id", sa.Integer, index=True, unique=True) - customer_id = sa.Column(sa.Integer, nullable=False, index=True) - placed_at = sa.Column(sa.DateTime, nullable=False, index=True) - ad_hoc = sa.Column(sa.Boolean, nullable=False) - scheduled_delivery_at = sa.Column(sa.DateTime, index=True) - scheduled_delivery_at_corrected = sa.Column(sa.Boolean, index=True) - first_estimated_delivery_at = sa.Column(sa.DateTime) - cancelled = sa.Column(sa.Boolean, nullable=False, index=True) - cancelled_at = sa.Column(sa.DateTime) - cancelled_at_corrected = sa.Column(sa.Boolean, index=True) + id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + autoincrement=False, + ) + _delivery_id: orm.Mapped[int | None] = orm.mapped_column( + "delivery_id", + sa.Integer, + nullable=True, + index=True, + unique=True, + ) + customer_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + index=True, + ) + placed_at: orm.Mapped[dt.datetime] = orm.mapped_column( + sa.DateTime, + nullable=False, + index=True, + ) + ad_hoc: orm.Mapped[bool] = orm.mapped_column( + sa.Boolean, + nullable=False, + ) + scheduled_delivery_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + index=True, + ) + scheduled_delivery_at_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) + first_estimated_delivery_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + cancelled: orm.Mapped[bool] = orm.mapped_column( + sa.Boolean, + nullable=False, + index=True, + ) + cancelled_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + cancelled_at_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) # Price-related columns - sub_total = sa.Column(sa.Integer, nullable=False) - delivery_fee = sa.Column(sa.SmallInteger, nullable=False) - total = sa.Column(sa.Integer, nullable=False) + sub_total: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + ) + delivery_fee: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) + total: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + ) # Restaurant-related columns - restaurant_id = sa.Column(sa.SmallInteger, nullable=False, index=True) - restaurant_notified_at = sa.Column(sa.DateTime) - restaurant_notified_at_corrected = sa.Column(sa.Boolean, index=True) - restaurant_confirmed_at = sa.Column(sa.DateTime) - restaurant_confirmed_at_corrected = sa.Column(sa.Boolean, index=True) - estimated_prep_duration = sa.Column(sa.Integer, index=True) - estimated_prep_duration_corrected = sa.Column(sa.Boolean, index=True) - estimated_prep_buffer = sa.Column(sa.Integer, nullable=False, index=True) + restaurant_id: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + index=True, + ) + restaurant_notified_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + restaurant_notified_at_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) + restaurant_confirmed_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + restaurant_confirmed_at_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) + estimated_prep_duration: orm.Mapped[int | None] = orm.mapped_column( + sa.Integer, + nullable=True, + index=True, + ) + estimated_prep_duration_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) + estimated_prep_buffer: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + index=True, + ) # Dispatch-related columns - courier_id = sa.Column(sa.Integer, index=True) - dispatch_at = sa.Column(sa.DateTime) - dispatch_at_corrected = sa.Column(sa.Boolean, index=True) - courier_notified_at = sa.Column(sa.DateTime) - courier_notified_at_corrected = sa.Column(sa.Boolean, index=True) - courier_accepted_at = sa.Column(sa.DateTime) - courier_accepted_at_corrected = sa.Column(sa.Boolean, index=True) - utilization = sa.Column(sa.SmallInteger, nullable=False) + courier_id: orm.Mapped[int | None] = orm.mapped_column( + sa.Integer, + nullable=True, + index=True, + ) + dispatch_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + dispatch_at_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) + courier_notified_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + courier_notified_at_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) + courier_accepted_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + courier_accepted_at_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) + utilization: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) # Pickup-related columns - pickup_address_id = sa.Column(sa.Integer, nullable=False, index=True) - reached_pickup_at = sa.Column(sa.DateTime) - pickup_at = sa.Column(sa.DateTime) - pickup_at_corrected = sa.Column(sa.Boolean, index=True) - pickup_not_confirmed = sa.Column(sa.Boolean) - left_pickup_at = sa.Column(sa.DateTime) - left_pickup_at_corrected = sa.Column(sa.Boolean, index=True) + pickup_address_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + index=True, + ) + reached_pickup_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + pickup_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + pickup_at_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) + pickup_not_confirmed: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + ) + left_pickup_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + left_pickup_at_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) # Delivery-related columns - delivery_address_id = sa.Column(sa.Integer, nullable=False, index=True) - reached_delivery_at = sa.Column(sa.DateTime) - delivery_at = sa.Column(sa.DateTime) - delivery_at_corrected = sa.Column(sa.Boolean, index=True) - delivery_not_confirmed = sa.Column(sa.Boolean) - _courier_waited_at_delivery = sa.Column("courier_waited_at_delivery", sa.Boolean) + delivery_address_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + index=True, + ) + reached_delivery_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + delivery_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + delivery_at_corrected: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + index=True, + ) + delivery_not_confirmed: orm.Mapped[bool | None] = orm.mapped_column( + sa.Boolean, + nullable=True, + ) + _courier_waited_at_delivery: orm.Mapped[bool | None] = orm.mapped_column( + "courier_waited_at_delivery", + sa.Boolean, + nullable=True, + ) # Statistical columns - logged_delivery_distance = sa.Column(sa.SmallInteger, nullable=True) - logged_avg_speed = sa.Column(postgresql.DOUBLE_PRECISION, nullable=True) - logged_avg_speed_distance = sa.Column(sa.SmallInteger, nullable=True) + logged_delivery_distance: orm.Mapped[int | None] = orm.mapped_column( + sa.SmallInteger, + nullable=True, + ) + logged_avg_speed: orm.Mapped[float | None] = orm.mapped_column( + postgresql.DOUBLE_PRECISION, + nullable=True, + ) + logged_avg_speed_distance: orm.Mapped[int | None] = orm.mapped_column( + sa.SmallInteger, + nullable=True, + ) # Constraints __table_args__ = ( diff --git a/src/velofy/db/pixels.py b/src/velofy/db/pixels.py index 10b1901..1d0f791 100644 --- a/src/velofy/db/pixels.py +++ b/src/velofy/db/pixels.py @@ -28,10 +28,26 @@ class Pixel(meta.Base): __tablename__ = "pixels" # Columns - id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) - grid_id = sa.Column(sa.SmallInteger, nullable=False, index=True) - n_x = sa.Column(sa.SmallInteger, nullable=False, index=True) - n_y = sa.Column(sa.SmallInteger, nullable=False, index=True) + id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + autoincrement=True, + ) + grid_id: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + index=True, + ) + n_x: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + index=True, + ) + n_y: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + index=True, + ) # Constraints __table_args__ = ( diff --git a/src/velofy/db/replay/orders.py b/src/velofy/db/replay/orders.py index 001d7ab..4e32ceb 100644 --- a/src/velofy/db/replay/orders.py +++ b/src/velofy/db/replay/orders.py @@ -2,6 +2,8 @@ from __future__ import annotations +import datetime as dt + import sqlalchemy as sa from sqlalchemy import orm @@ -14,43 +16,107 @@ class ReplayedOrder(meta.Base): __tablename__ = "replayed_orders" # Generic columns - simulation_id = sa.Column(sa.Integer, primary_key=True) - actual_order_id = sa.Column(sa.Integer, primary_key=True) + simulation_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + ) + actual_order_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + ) # `Order`-type related columns # `.ad_hoc` is part of a `ForeignKeyConstraint` to ensure the type # of the `ReplayedOrder` (i.e., ad-hoc or scheduled) is in line # with the `.actual` order. - ad_hoc = sa.Column(sa.Boolean, nullable=False) - placed_at = sa.Column(sa.DateTime, nullable=False) + ad_hoc: orm.Mapped[bool] = orm.mapped_column( + sa.Boolean, + nullable=False, + ) + placed_at: orm.Mapped[dt.datetime] = orm.mapped_column( + sa.DateTime, + nullable=False, + ) # Depending on `.ad_hoc`, `.scheduled_delivery_at` is either filled in or not. - scheduled_delivery_at = sa.Column(sa.DateTime) + scheduled_delivery_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) # If an order is cancelled in a simulation, # some of the columns below do not apply any more. - cancelled_at = sa.Column(sa.DateTime) + cancelled_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) # Restaurant-related columns - estimated_prep_duration = sa.Column(sa.SmallInteger) - restaurant_notified_at = sa.Column(sa.DateTime) - restaurant_confirmed_at = sa.Column(sa.DateTime) - restaurant_ready_at = sa.Column(sa.DateTime) + estimated_prep_duration: orm.Mapped[int | None] = orm.mapped_column( + sa.SmallInteger, + nullable=True, + ) + restaurant_notified_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + restaurant_confirmed_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + restaurant_ready_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) # Dispatch-related columns - dispatch_at = sa.Column(sa.DateTime) - first_estimated_delivery_at = sa.Column(sa.DateTime) - courier_id = sa.Column(sa.Integer, index=True) - courier_notified_at = sa.Column(sa.DateTime) - courier_accepted_at = sa.Column(sa.DateTime) - utilization = sa.Column(sa.SmallInteger) + dispatch_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + first_estimated_delivery_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + courier_id: orm.Mapped[int | None] = orm.mapped_column( + sa.Integer, + nullable=True, + index=True, + ) + courier_notified_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + courier_accepted_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + utilization: orm.Mapped[int | None] = orm.mapped_column( + sa.SmallInteger, + nullable=True, + ) # Pickup-related columns - reached_pickup_at = sa.Column(sa.DateTime) - pickup_at = sa.Column(sa.DateTime) - left_pickup_at = sa.Column(sa.DateTime) + reached_pickup_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + pickup_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + left_pickup_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) # Delivery-related columns - reached_delivery_at = sa.Column(sa.DateTime) - delivery_at = sa.Column(sa.DateTime) + reached_delivery_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) + delivery_at: orm.Mapped[dt.datetime | None] = orm.mapped_column( + sa.DateTime, + nullable=True, + ) # Constraints __table_args__ = ( diff --git a/src/velofy/db/replay/simulations.py b/src/velofy/db/replay/simulations.py index 798a69c..f6bb815 100644 --- a/src/velofy/db/replay/simulations.py +++ b/src/velofy/db/replay/simulations.py @@ -15,11 +15,26 @@ class ReplaySimulation(meta.Base): __tablename__ = "replay_simulations" # Columns - id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) - city_id = sa.Column(sa.SmallInteger, nullable=False, index=True) - strategy = sa.Column(sa.Unicode(length=100), nullable=False, index=True) + id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + primary_key=True, + autoincrement=True, + ) + city_id: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + index=True, + ) + strategy: orm.Mapped[str] = orm.mapped_column( + sa.Unicode(length=100), + nullable=False, + index=True, + ) # `.run` may be used as random seed. - run = sa.Column(sa.SmallInteger, nullable=False) + run: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) # Constraints __table_args__ = ( diff --git a/src/velofy/db/restaurants.py b/src/velofy/db/restaurants.py index 50a0cce..db13cf6 100644 --- a/src/velofy/db/restaurants.py +++ b/src/velofy/db/restaurants.py @@ -2,6 +2,8 @@ from __future__ import annotations +import datetime as dt + import folium import sqlalchemy as sa from sqlalchemy import orm @@ -22,15 +24,28 @@ class Restaurant(meta.Base): __tablename__ = "restaurants" # Columns - id = sa.Column( + id: orm.Mapped[int] = orm.mapped_column( sa.SmallInteger, primary_key=True, autoincrement=False, ) - created_at = sa.Column(sa.DateTime, nullable=False) - name = sa.Column(sa.Unicode(length=45), nullable=False) - address_id = sa.Column(sa.Integer, nullable=False, index=True) - estimated_prep_duration = sa.Column(sa.SmallInteger, nullable=False) + created_at: orm.Mapped[dt.datetime] = orm.mapped_column( + sa.DateTime, + nullable=False, + ) + name: orm.Mapped[str] = orm.mapped_column( + sa.Unicode(length=45), + nullable=False, + ) + address_id: orm.Mapped[int] = orm.mapped_column( + sa.Integer, + nullable=False, + index=True, + ) + estimated_prep_duration: orm.Mapped[int] = orm.mapped_column( + sa.SmallInteger, + nullable=False, + ) # Constraints __table_args__ = (