File size: 6,880 Bytes
4a2ab42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ae946d
 
 
 
 
 
4a2ab42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ae946d
 
 
4a2ab42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ae946d
 
 
4a2ab42
 
 
 
4ae946d
 
 
4a2ab42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ae946d
 
 
4a2ab42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ae946d
 
 
4a2ab42
 
 
 
 
 
 
 
 
 
 
 
 
 
4ae946d
 
 
4a2ab42
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
AI Data Models
Database models for AI-generated insights, decisions, and interactions
"""

from datetime import UTC, datetime

from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Integer, String, Text
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship


class Base(DeclarativeBase):
    pass


class AIDecision(Base):
    """AI-generated decisions and reasoning"""

    __tablename__ = "ai_decisions"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    decision_id: Mapped[str] = mapped_column(String(100), unique=True, index=True)
    decision_type: Mapped[str] = mapped_column(String(50))
    confidence_level: Mapped[str] = mapped_column(String(20))
    decision: Mapped[str] = mapped_column(Text)
    reasoning: Mapped[str] = mapped_column(Text)  # JSON array of reasoning strings
    evidence: Mapped[str] = mapped_column(Text)  # JSON object
    alternatives: Mapped[str] = mapped_column(Text)  # JSON array of alternatives
    risk_assessment: Mapped[str] = mapped_column(Text)  # JSON object
    model_version: Mapped[str] = mapped_column(String(50))
    processing_time: Mapped[float] = mapped_column(Float)
    human_override_required: Mapped[bool] = mapped_column(Boolean, default=False)
    human_override_reason: Mapped[str | None] = mapped_column(Text, nullable=True)

    # Foreign keys
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"))
    tenant_id: Mapped[int] = mapped_column(Integer, ForeignKey("tenants.id"))

    # Timestamps
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now(UTC)
    )
    resolved_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True
    )

    # Relationships
    user: Mapped["User"] = relationship("User", back_populates="ai_decisions")


class AIPrediction(Base):
    """AI-generated predictions and forecasts"""

    __tablename__ = "ai_predictions"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    insight_id: Mapped[str] = mapped_column(String(100), unique=True, index=True)
    insight_type: Mapped[str] = mapped_column(String(50))
    prediction: Mapped[str] = mapped_column(Text)  # JSON-serializable prediction
    confidence_score: Mapped[float] = mapped_column(Float)
    confidence_interval_lower: Mapped[float] = mapped_column(Float)
    confidence_interval_upper: Mapped[float] = mapped_column(Float)
    timeframe: Mapped[str] = mapped_column(String(20))
    business_impact: Mapped[str] = mapped_column(String(50))
    recommended_actions: Mapped[str] = mapped_column(Text)  # JSON array
    data_quality_score: Mapped[float] = mapped_column(Float)
    model_used: Mapped[str] = mapped_column(String(50))

    # Foreign keys
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"))
    tenant_id: Mapped[int] = mapped_column(Integer, ForeignKey("tenants.id"))

    # Timestamps
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now(UTC)
    )


class AIInteraction(Base):
    """Human-AI interaction records"""

    __tablename__ = "ai_interactions"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    interaction_id: Mapped[str] = mapped_column(String(100), unique=True, index=True)
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"))
    tenant_id: Mapped[int] = mapped_column(Integer, ForeignKey("tenants.id"))
    interaction_type: Mapped[str] = mapped_column(String(30))
    user_input: Mapped[str] = mapped_column(Text)
    ai_response: Mapped[str] = mapped_column(Text)
    context: Mapped[str] = mapped_column(Text)  # JSON object
    collaboration_mode: Mapped[str] = mapped_column(String(20))
    confidence_score: Mapped[float] = mapped_column(Float)
    user_feedback: Mapped[str | None] = mapped_column(
        Text, nullable=True
    )  # JSON object
    processing_time: Mapped[float] = mapped_column(Float)
    outcome: Mapped[str | None] = mapped_column(String(50), nullable=True)

    # Timestamps
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now(UTC)
    )


class AIScalingEvent(Base):
    """AI-driven scaling events"""

    __tablename__ = "ai_scaling_events"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    event_id: Mapped[str] = mapped_column(String(100), unique=True, index=True)
    resource_type: Mapped[str] = mapped_column(String(50))
    decision: Mapped[str] = mapped_column(String(20))
    current_capacity: Mapped[float] = mapped_column(Float)
    target_capacity: Mapped[float] = mapped_column(Float)
    reason: Mapped[str] = mapped_column(Text)
    confidence_score: Mapped[float] = mapped_column(Float)
    estimated_cost_impact: Mapped[float] = mapped_column(Float)
    execution_time: Mapped[float | None] = mapped_column(Float, nullable=True)
    success: Mapped[bool] = mapped_column(Boolean, default=False)

    # Timestamps
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now(UTC)
    )


class AIWorkflowOptimization(Base):
    """AI-generated workflow optimizations"""

    __tablename__ = "ai_workflow_optimizations"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    workflow_id: Mapped[str] = mapped_column(String(100), index=True)
    augmentation_type: Mapped[str] = mapped_column(String(50))
    description: Mapped[str] = mapped_column(Text)
    ai_suggestions: Mapped[str] = mapped_column(Text)  # JSON array
    human_tasks: Mapped[str] = mapped_column(Text)  # JSON array
    estimated_benefits: Mapped[str] = mapped_column(Text)  # JSON object
    implementation_complexity: Mapped[str] = mapped_column(String(20))
    confidence_score: Mapped[float] = mapped_column(Float)

    # Foreign keys
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"))
    tenant_id: Mapped[int] = mapped_column(Integer, ForeignKey("tenants.id"))

    # Timestamps
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now(UTC)
    )


# Update existing User model to include AI relationships
# Note: This would need to be integrated with the existing User model
class User(Base):
    """Extended User model with AI relationships"""

    __tablename__ = "users"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    email: Mapped[str] = mapped_column(String(255), unique=True)
    full_name: Mapped[str] = mapped_column(String(100))

    # AI relationships
    ai_decisions: Mapped[list[AIDecision]] = relationship(
        "AIDecision", back_populates="user"
    )


# Alembic migration for AI tables would be created separately
# This file defines the models that would be included in the migration