1"""Attendance: marking who came to an event, bot-reported voice time, and the GM attendance report.
2
3See ``sidequest_api.lib.attendance`` for the SQL conditions ``attended``/no-show status feed into.
4"""
5
6from datetime import date, datetime
7from enum import StrEnum
8from uuid import UUID # pydantic needs it at runtime
9
10from pydantic import BaseModel, ConfigDict, Field
11
12from sidequest_common.schemas.events import EventKind, EventStatus, SignupRole, SignupStatus, WowClass
13
14
[docs]
15class AttendanceRange(StrEnum):
16 """A reporting window for ``GET /api/attendance/report`` and ``/trends``."""
17
18 LAST_30 = "last_30"
19 SEASON = "season"
20 ALL_TIME = "all_time"
21
22
[docs]
23class AttendanceEventSummary(BaseModel):
24 """One past event, as listed on the attendance tab's event picker."""
25
26 id: UUID
27 title: str
28 kind: EventKind
29 starts_at: datetime
30 status: EventStatus
31 attendance_taken_at: datetime | None
32 needs_attendance: bool = Field(
33 description="True when the event has happened, is not cancelled, and attendance has not been taken yet."
34 )
35
36
[docs]
37class WalkInCandidate(BaseModel):
38 """A guild member not currently signed up for the event, offered as a walk-in on the marking screen."""
39
40 member_id: int
41 display_name: str
42 avatar_url: str | None
43
44
[docs]
45class AttendanceSignup(BaseModel):
46 """One sign-up on the attendance marking screen, with the bot's reported voice time."""
47
48 model_config = ConfigDict(populate_by_name=True, serialize_by_alias=True)
49
50 signup_id: UUID
51 member_id: int
52 display_name: str
53 avatar_url: str | None
54 character: str | None
55 wow_class: WowClass | None = Field(alias="class")
56 role: SignupRole | None
57 status: SignupStatus
58 attended: bool | None
59 is_walk_in: bool
60 voice_minutes: int
61 suggested_attended: bool = Field(
62 description="The Came/No-show checkbox's pre-tick before an officer touches it: the stored attended value "
63 "once attendance has been taken, else True for a signed sign-up or anyone with voice_minutes at or above "
64 "the presence threshold, else False."
65 )
66
67
[docs]
68class AttendanceDetail(BaseModel):
69 """``GET/PUT /api/events/{id}/attendance`` (officer): the marking screen for one event."""
70
71 event: AttendanceEventSummary
72 signups: list[AttendanceSignup]
73 walk_in_candidates: list[WalkInCandidate]
74
75
[docs]
76class AttendanceMarkEntry(BaseModel):
77 """One sign-up's Came/No-show decision."""
78
79 signup_id: UUID
80 attended: bool
81
82
[docs]
83class WalkInCreate(BaseModel):
84 """A member who came without signing up. ``character``/``class``/``role`` are required for raid/dungeon events."""
85
86 model_config = ConfigDict(populate_by_name=True)
87
88 member_id: int
89 character: str | None = Field(default=None, min_length=1, max_length=24)
90 wow_class: WowClass | None = Field(default=None, alias="class")
91 role: SignupRole | None = None
92
93
[docs]
94class AttendanceMark(BaseModel):
95 """Body for ``PUT /api/events/{id}/attendance`` (officer). Sets ``attendance_taken_at`` to now.
96
97 Sign-ups not listed in ``entries`` are left as-is (unmarked, unless a previous save already marked them):
98 tentative/absent sign-ups default to not marked.
99 """
100
101 entries: list[AttendanceMarkEntry] = Field(default_factory=list)
102 walk_ins: list[WalkInCreate] = Field(default_factory=list)
103
104
[docs]
105class VoicePresenceEntry(BaseModel):
106 """One Discord member's cumulative voice minutes for an event, so far."""
107
108 discord_id: str
109 minutes: int = Field(ge=0)
110
111
[docs]
112class VoicePresenceReport(BaseModel):
113 """Bot -> API: cumulative voice minutes per member for one event, so far.
114
115 Idempotent: the API stores ``max(existing, reported)`` per ``discord_id`` listed (never an increment, and
116 never lowers a stored total), so the bot can push repeatedly -- including after its own in-memory counter has
117 reset on restart -- without double-counting or regressing a member's total.
118 """
119
120 event_id: UUID
121 entries: list[VoicePresenceEntry] = Field(default_factory=list)
122
123
[docs]
124class MemberAttendanceRow(BaseModel):
125 """One member's line on ``GET /api/attendance/report``."""
126
127 member_id: int
128 display_name: str
129 avatar_url: str | None
130 signed_up: int
131 came: int
132 no_shows: int
133 walk_ins: int
134 attendance_rate: float = Field(
135 description="Attended signed (non-walk-in) sign-ups divided by signed sign-ups. 0.0 if the member never "
136 "signed up in range. The came count includes walk-ins and stays separate, so it never raises this above 1.0."
137 )
138 last_attended: datetime | None
139
140
[docs]
141class AttendanceReport(BaseModel):
142 """``GET /api/attendance/report`` (GM)."""
143
144 range: AttendanceRange
145 raids_only: bool
146 rows: list[MemberAttendanceRow]
147
148
[docs]
149class AttendanceTrendPoint(BaseModel):
150 """One week's attendance on the trend chart."""
151
152 week_start: date
153 attendees: int = Field(description="Distinct members who came that week.")
154 signed_up: int
155 show_rate: float = Field(
156 description="Attended signed (non-walk-in) sign-ups divided by signed sign-ups for events starting that "
157 "week. 0.0 if nobody signed up. The attendees count includes walk-ins and stays separate, so it never "
158 "raises this above 1.0."
159 )
160
161
[docs]
162class AttendanceTrends(BaseModel):
163 """``GET /api/attendance/trends`` (GM): weekly attendance over the chosen range, oldest week first."""
164
165 range: AttendanceRange
166 raids_only: bool
167 points: list[AttendanceTrendPoint]