File size: 5,257 Bytes
900edd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import enum
from datetime import datetime, timezone

from sqlalchemy import (
    Boolean,
    Column,
    DateTime,
    Enum,
    Float,
    ForeignKey,
    Integer,
    String,
    UniqueConstraint,
)
from sqlalchemy.orm import relationship

from .connection import Base


def _utcnow() -> datetime:
    """Naive UTC timestamp for TIMESTAMP WITHOUT TIME ZONE columns.

    asyncpg rejects timezone-aware datetimes for naive TIMESTAMP columns, so we
    normalise to naive UTC at the application layer.
    """
    return datetime.now(timezone.utc).replace(tzinfo=None)


class UserRole(str, enum.Enum):
    admin = "admin"
    moderator = "moderator"
    participant = "participant"


class MeetingStatus(str, enum.Enum):
    active = "active"
    ended = "ended"


class MeetingRole(str, enum.Enum):
    """Role a user holds *within a specific meeting* (distinct from the global
    account role). The meeting creator is the host."""

    host = "host"
    moderator = "moderator"
    participant = "participant"


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    email = Column(String(255), unique=True, index=True, nullable=False)
    hashed_password = Column(String(255), nullable=False)
    role = Column(Enum(UserRole), default=UserRole.participant, nullable=False)
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=_utcnow)

    meetings = relationship(
        "Meeting",
        back_populates="host",
        foreign_keys="Meeting.host_id",
    )
    microphone_configs = relationship("MicrophoneConfig", back_populates="user")
    memberships = relationship(
        "MeetingMember",
        back_populates="user",
        cascade="all, delete-orphan",
    )


class Meeting(Base):
    __tablename__ = "meetings"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(200), nullable=False)
    description = Column(String(500), nullable=True)
    host_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    invite_token = Column(String(64), unique=True, index=True, nullable=True)
    created_at = Column(DateTime, default=_utcnow)
    ended_at = Column(DateTime, nullable=True)
    status = Column(Enum(MeetingStatus), default=MeetingStatus.active)

    host = relationship("User", back_populates="meetings", foreign_keys=[host_id])
    microphone_configs = relationship("MicrophoneConfig", back_populates="meeting")
    speech_logs = relationship("SpeechLog", back_populates="meeting")
    members = relationship(
        "MeetingMember",
        back_populates="meeting",
        cascade="all, delete-orphan",
    )


class MeetingMember(Base):
    """Per-meeting membership with a meeting-scoped role."""

    __tablename__ = "meeting_members"
    __table_args__ = (
        UniqueConstraint("meeting_id", "user_id", name="uq_meeting_member"),
    )

    id = Column(Integer, primary_key=True, index=True)
    meeting_id = Column(Integer, ForeignKey("meetings.id"), nullable=False, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    role = Column(
        Enum(MeetingRole), default=MeetingRole.participant, nullable=False
    )
    joined_at = Column(DateTime, default=_utcnow)

    meeting = relationship("Meeting", back_populates="members")
    user = relationship("User", back_populates="memberships")


class MicrophoneConfig(Base):
    __tablename__ = "microphone_configs"

    id = Column(Integer, primary_key=True, index=True)
    meeting_id = Column(Integer, ForeignKey("meetings.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    name = Column(String(100), nullable=False)
    priority = Column(Integer, default=5)
    is_chairman = Column(Boolean, default=False)
    is_muted = Column(Boolean, default=False)
    created_at = Column(DateTime, default=_utcnow)

    meeting = relationship("Meeting", back_populates="microphone_configs")
    user = relationship("User", back_populates="microphone_configs")
    speech_logs = relationship("SpeechLog", back_populates="microphone")


class SpeechLog(Base):
    __tablename__ = "speech_logs"

    id = Column(Integer, primary_key=True, index=True)
    meeting_id = Column(Integer, ForeignKey("meetings.id"), nullable=False)
    microphone_id = Column(
        Integer, ForeignKey("microphone_configs.id"), nullable=False
    )
    start_time = Column(DateTime, default=_utcnow)
    end_time = Column(DateTime, nullable=True)
    peak_db = Column(Float, default=-100.0)
    avg_db = Column(Float, default=-100.0)

    meeting = relationship("Meeting", back_populates="speech_logs")
    microphone = relationship("MicrophoneConfig", back_populates="speech_logs")


class SystemDiagnostic(Base):
    __tablename__ = "system_diagnostics"

    id = Column(Integer, primary_key=True, index=True)
    timestamp = Column(DateTime, default=_utcnow, index=True)
    cpu_usage = Column(Float, default=0.0)
    memory_usage = Column(Float, default=0.0)
    audio_latency_ms = Column(Float, default=0.0)
    buffer_health = Column(Float, default=100.0)
    packet_loss = Column(Float, default=0.0)