Spaces:
Paused
Paused
Download models/ticket.py from 3SCError404/nexus: direct link, hf CLI and curl.
- Browser
- Download file 4.48 kB
-
https://huggingface.co/spaces/3SCError404/nexus/resolve/main/models/ticket.py
- Command line
-
hf download hf://spaces/3SCError404/nexus/models/ticket.py
-
curl -L -o ticket.py https://huggingface.co/spaces/3SCError404/nexus/resolve/main/models/ticket.py
4.48 kB
| """ | |
| Ticket model — core issue tracking entity. | |
| Status flow: Open → Acknowledged → In Progress → Resolved → Closed | |
| """ | |
| import enum | |
| from sqlalchemy import Column, String, Text, Enum, ForeignKey, DateTime, Index, Computed | |
| from sqlalchemy.dialects.postgresql import UUID, JSONB, TSVECTOR | |
| from sqlalchemy.orm import relationship | |
| from pgvector.sqlalchemy import Vector | |
| from core.database import Base | |
| from core.config import get_settings | |
| from models.base import PrimaryKeyMixin, TimestampMixin, SoftDeleteMixin | |
| settings = get_settings() | |
| class TicketStatus(str, enum.Enum): | |
| OPEN = "OPEN" | |
| ACKNOWLEDGED = "ACKNOWLEDGED" | |
| IN_PROGRESS = "IN_PROGRESS" | |
| RESOLVED = "RESOLVED" | |
| CLOSED = "CLOSED" | |
| class TicketPriority(str, enum.Enum): | |
| P0 = "P0" | |
| P1 = "P1" | |
| P2 = "P2" | |
| P3 = "P3" | |
| # Valid status transitions | |
| ALLOWED_TRANSITIONS = { | |
| TicketStatus.OPEN: [TicketStatus.ACKNOWLEDGED], | |
| TicketStatus.ACKNOWLEDGED: [TicketStatus.IN_PROGRESS, TicketStatus.OPEN], | |
| TicketStatus.IN_PROGRESS: [TicketStatus.RESOLVED, TicketStatus.ACKNOWLEDGED], | |
| TicketStatus.RESOLVED: [TicketStatus.CLOSED, TicketStatus.IN_PROGRESS], | |
| TicketStatus.CLOSED: [], # Terminal state | |
| } | |
| class Ticket(Base, PrimaryKeyMixin, TimestampMixin, SoftDeleteMixin): | |
| __tablename__ = "tickets" | |
| workspace_id = Column(UUID(as_uuid=True), ForeignKey("workspaces.id"), nullable=False, index=True) | |
| created_by = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False, index=True) | |
| assigned_to = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=True, index=True) | |
| project_id = Column(UUID(as_uuid=True), ForeignKey("projects.id"), nullable=True, index=True) | |
| title = Column(String(500), nullable=False) | |
| description = Column(Text, nullable=False) | |
| category = Column(String(100), nullable=True) | |
| priority = Column(Enum(TicketPriority), nullable=False, default=TicketPriority.P2) | |
| status = Column(Enum(TicketStatus), nullable=False, default=TicketStatus.OPEN) | |
| sla_deadline = Column(DateTime(timezone=False), nullable=True) | |
| estimated_effort_hours = Column(JSONB, nullable=True) # Will store {value, complexity} | |
| expected_resolution_at = Column(DateTime(timezone=False), nullable=True) | |
| resolved_at = Column(DateTime(timezone=False), nullable=True) | |
| embedding = Column(Vector(settings.AI_EMBEDDING_DIM), nullable=True) | |
| # AI-populated metadata: classification confidence, routing info, predicted resolution time | |
| ai_metadata = Column(JSONB, default={}) | |
| is_escalated = Column(DateTime(timezone=False), nullable=True) # Tracks if SLA notification was sent | |
| duplicate_of_id = Column(UUID(as_uuid=True), ForeignKey("tickets.id"), nullable=True) | |
| # --- Search Optimization --- | |
| # 1. Computed TSVector for GIN search (Keyword Engine) | |
| search_vector = Column( | |
| TSVECTOR, | |
| Computed("to_tsvector('english', coalesce(title, '') || ' ' || coalesce(description, ''))", persisted=True) | |
| ) | |
| # 2. Indexes for 3-branch search | |
| __table_args__ = ( | |
| Index('idx_ticket_search_vector', "search_vector", postgresql_using='gin'), | |
| Index( | |
| 'idx_ticket_embedding_hnsw', | |
| "embedding", | |
| postgresql_using='hnsw', | |
| postgresql_with={'m': 16, 'ef_construction': 64}, | |
| postgresql_ops={'embedding': 'vector_cosine_ops'} | |
| ), | |
| # Optimizes range/reporting queries: select ... where workspace_id=X and created_at > Y | |
| Index('idx_ticket_workspace_created', "workspace_id", "created_at"), | |
| ) | |
| # Resolution Metadata | |
| resolution_text = Column(Text, nullable=True) | |
| resolved_by = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=True) | |
| # Relationships | |
| workspace = relationship("Workspace", back_populates="tickets") | |
| creator = relationship("User", back_populates="created_tickets", foreign_keys=[created_by]) | |
| assignee = relationship("User", back_populates="assigned_tickets", foreign_keys=[assigned_to]) | |
| resolver = relationship("User", back_populates="resolved_tickets", foreign_keys=[resolved_by]) # Added for N+1 fix | |
| project = relationship("Project", back_populates="tickets") | |
| messages = relationship("Message", back_populates="ticket", lazy="selectin", order_by="Message.created_at") | |
| attachments = relationship("Attachment", back_populates="ticket", lazy="selectin") | |
| feedback = relationship("Feedback", back_populates="ticket", uselist=False) | |