File size: 4,637 Bytes
09801ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
Analytics & Visual Models — Dashboards, charts, stories, visual pipelines, and computer vision tasks.
"""

import uuid
from datetime import datetime
from typing import Optional

from sqlalchemy import String, Boolean, Text, Integer, Index, ForeignKey
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin


class Dashboard(UUIDPrimaryKeyMixin, TimestampMixin, Base):
    """User analytics dashboards."""

    __tablename__ = "dashboards"
    __table_args__ = (
        Index("ix_dashboards_user_id", "user_id"),
    )

    user_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("users.id", ondelete="CASCADE"),
        nullable=False,
    )
    title: Mapped[str] = mapped_column(String(255), default="Untitled Dashboard", nullable=False)
    description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    layout: Mapped[dict] = mapped_column(JSONB, default=list, nullable=False)
    is_public: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)

    # Relationships
    charts: Mapped[list["Chart"]] = relationship(
        back_populates="dashboard", cascade="all, delete-orphan", lazy="selectin"
    )

    def __repr__(self) -> str:
        return f"<Dashboard id={self.id} title={self.title}>"


class Chart(UUIDPrimaryKeyMixin, Base):
    """Individual chart component inside a dashboard."""

    __tablename__ = "charts"
    __table_args__ = (
        Index("ix_charts_dashboard_id", "dashboard_id"),
    )

    dashboard_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("dashboards.id", ondelete="CASCADE"),
        nullable=False,
    )
    title: Mapped[str] = mapped_column(String(255), nullable=False)
    chart_type: Mapped[str] = mapped_column(String(50), nullable=False)  # bar, line, scatter, pie
    data: Mapped[dict] = mapped_column(JSONB, nullable=False)
    config: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
    created_at: Mapped[datetime] = mapped_column(
        nullable=False, server_default="now()"
    )

    # Relationships
    dashboard: Mapped["Dashboard"] = relationship(back_populates="charts")

    def __repr__(self) -> str:
        return f"<Chart title={self.title} type={self.chart_type}>"


class ComputerVisionTask(UUIDPrimaryKeyMixin, TimestampMixin, Base):
    """Computer vision processing jobs (object detection, classification, segmentation)."""

    __tablename__ = "computer_vision_tasks"
    __table_args__ = (
        Index("ix_cv_tasks_user_id", "user_id"),
        Index("ix_cv_tasks_status", "status"),
    )

    user_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("users.id", ondelete="CASCADE"),
        nullable=False,
    )
    task_name: Mapped[str] = mapped_column(String(255), nullable=False)
    task_type: Mapped[str] = mapped_column(
        String(50), default="object_detection", nullable=False
    )  # object_detection, classification, ocr, segmentation
    model_name: Mapped[str] = mapped_column(String(100), default="yolov8n", nullable=False)
    status: Mapped[str] = mapped_column(String(20), default="pending", nullable=False)
    image_url: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    results_json: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
    detected_objects_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    confidence_threshold: Mapped[float] = mapped_column(default=0.25, nullable=False)

    def __repr__(self) -> str:
        return f"<ComputerVisionTask name={self.task_name} status={self.status}>"


class Notification(UUIDPrimaryKeyMixin, Base):
    """System and AI notifications for users."""

    __tablename__ = "notifications"
    __table_args__ = (
        Index("ix_notifications_user_id", "user_id"),
    )

    user_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("users.id", ondelete="CASCADE"),
        nullable=False,
    )
    title: Mapped[str] = mapped_column(String(255), nullable=False)
    message: Mapped[str] = mapped_column(Text, nullable=False)
    type: Mapped[str] = mapped_column(String(50), default="info", nullable=False)
    is_read: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    created_at: Mapped[datetime] = mapped_column(
        nullable=False, server_default="now()"
    )

    def __repr__(self) -> str:
        return f"<Notification title={self.title} read={self.is_read}>"