File size: 3,826 Bytes
0ddcb86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""SQLAlchemy ORM models."""
from __future__ import annotations
from datetime import datetime
from typing import Optional
from sqlalchemy import String, Float, Integer, DateTime, Boolean, JSON, Text, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base


class RoutePrediction(Base):
    __tablename__ = "route_predictions"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    user_id: Mapped[Optional[str]] = mapped_column(String(128), index=True, nullable=True)

    origin_lat: Mapped[float] = mapped_column(Float)
    origin_lng: Mapped[float] = mapped_column(Float)
    origin_name: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)

    destination_lat: Mapped[float] = mapped_column(Float)
    destination_lng: Mapped[float] = mapped_column(Float)
    destination_name: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)

    vehicle_type: Mapped[str] = mapped_column(String(32))
    preference: Mapped[str] = mapped_column(String(32))

    chosen_route_id: Mapped[str] = mapped_column(String(64))
    eta_predicted_min: Mapped[float] = mapped_column(Float)
    eta_actual_min: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    fuel_predicted_l: Mapped[float] = mapped_column(Float)
    distance_km: Mapped[float] = mapped_column(Float)

    ranked_payload: Mapped[dict] = mapped_column(JSON)
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, index=True)

    feedback = relationship("RouteFeedback", back_populates="prediction", uselist=False)


class RouteFeedback(Base):
    __tablename__ = "route_feedback"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    prediction_id: Mapped[Optional[int]] = mapped_column(
        ForeignKey("route_predictions.id"), nullable=True, index=True
    )
    route_id: Mapped[str] = mapped_column(String(64), index=True)
    user_id: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
    actual_eta_min: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    actual_fuel_l: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    rating: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    completed: Mapped[bool] = mapped_column(Boolean, default=True)
    notes: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)

    prediction = relationship("RoutePrediction", back_populates="feedback")


class TrafficSnapshot(Base):
    """One TomTom flow data point — accumulated for model retraining."""
    __tablename__ = "traffic_snapshots"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    lat: Mapped[float] = mapped_column(Float, index=True)
    lng: Mapped[float] = mapped_column(Float, index=True)
    current_speed_kmh: Mapped[float] = mapped_column(Float)
    free_flow_speed_kmh: Mapped[float] = mapped_column(Float)
    traffic_level: Mapped[float] = mapped_column(Float)
    hour_of_day: Mapped[int] = mapped_column(Integer)
    day_of_week: Mapped[int] = mapped_column(Integer)
    captured_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, index=True)


class TrainingRun(Base):
    __tablename__ = "training_runs"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    samples: Mapped[int] = mapped_column(Integer)
    eta_metrics: Mapped[dict] = mapped_column(JSON)
    fuel_metrics: Mapped[dict] = mapped_column(JSON)
    model_type: Mapped[str] = mapped_column(String(32))
    artifact_path: Mapped[str] = mapped_column(String(256))
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, index=True)