from datetime import datetime from sqlalchemy import DateTime, Integer, String, Text from sqlalchemy.dialects.sqlite import JSON from sqlalchemy.orm import Mapped, mapped_column from core_db import AuditMixin, Base, TenantMixin, VersionMixin from core_shared import JSONValue class TraceSpan(TenantMixin, AuditMixin, VersionMixin, Base): __tablename__ = "trace_span" run_id: Mapped[str] = mapped_column(String(36), index=True) node_run_id: Mapped[str | None] = mapped_column(String(36), nullable=True, index=True) parent_span_id: Mapped[str | None] = mapped_column(String(36), nullable=True, index=True) span_type: Mapped[str] = mapped_column(String(64), index=True) name: Mapped[str] = mapped_column(String(128)) status: Mapped[str] = mapped_column(String(32), default="running", index=True) started_time: Mapped[datetime] = mapped_column(DateTime) ended_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) duration_ms: Mapped[int | None] = mapped_column(Integer, nullable=True) attributes_json: Mapped[dict[str, JSONValue] | None] = mapped_column(JSON, nullable=True) error_code: Mapped[str | None] = mapped_column(String(64), nullable=True) error_message: Mapped[str | None] = mapped_column(Text, nullable=True)