"""Durable rate-limit event records.""" from __future__ import annotations from datetime import datetime, timezone from uuid import uuid4 from sqlalchemy import DateTime, Index, String, func from sqlalchemy.orm import Mapped, mapped_column from ..db import Base def _utc_now() -> datetime: return datetime.now(timezone.utc) class RateLimitEvent(Base): """One recorded rate-limit hit for an operation + key (no secrets).""" __tablename__ = "rate_limit_events" __table_args__ = ( Index("ix_rate_limit_op_key_created", "operation", "key", "created_at"), Index("ix_rate_limit_org_created", "organization_id", "created_at"), ) id: Mapped[str] = mapped_column( String(64), primary_key=True, default=lambda: f"rl_{uuid4().hex}" ) operation: Mapped[str] = mapped_column(String(120), nullable=False) key: Mapped[str] = mapped_column(String(255), nullable=False, index=True) organization_id: Mapped[str | None] = mapped_column(String(64), nullable=True) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False, default=_utc_now, server_default=func.now() )