25 lines
928 B
Python
25 lines
928 B
Python
|
|
from datetime import datetime
|
||
|
|
from sqlalchemy import DateTime, String, ForeignKey, Index
|
||
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
||
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||
|
|
|
||
|
|
from app.database import Base
|
||
|
|
|
||
|
|
|
||
|
|
class Event(Base):
|
||
|
|
__tablename__ = "events"
|
||
|
|
|
||
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
||
|
|
vehicle_id: Mapped[int] = mapped_column(ForeignKey("vehicles.id", ondelete="CASCADE"))
|
||
|
|
timestamp: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
|
||
|
|
type: Mapped[str] = mapped_column(String(50), nullable=False) # LONG_STOP, OVERSPEED, CONNECTION_LOST
|
||
|
|
payload: Mapped[dict] = mapped_column(JSONB, default=dict)
|
||
|
|
|
||
|
|
# Relationships
|
||
|
|
vehicle: Mapped["Vehicle"] = relationship(back_populates="events")
|
||
|
|
|
||
|
|
__table_args__ = (
|
||
|
|
Index("idx_events_vehicle_ts", "vehicle_id", "timestamp"),
|
||
|
|
Index("idx_events_type", "type"),
|
||
|
|
)
|