1"""Officer-facing server stats: ``GET /api/stats``.
2
3Aggregated from ``member_activity_hour`` (chat/voice minutes and the XP they earned), ``XPGrant`` (event/group
4XP), the codebase's shared attendance rule (``sidequest_api.lib.attendance.attended_condition``), and the same
5counted-group rule used for group XP (``peak_size >= 2`` and ``grouped_minutes``/member ``minutes >=
6COUNTED_GROUP_MINUTES``). Times, days, and weekday/hour buckets are America/Chicago.
7"""
8
9from datetime import date, datetime # pydantic needs them at runtime
10from enum import StrEnum
11from typing import Literal
12
13from pydantic import BaseModel, Field
14
15
[docs]
16class StatsRange(StrEnum):
17 """The window ``GET /api/stats`` reports over."""
18
19 SEVEN_DAYS = "7d"
20 THIRTY_DAYS = "30d"
21 NINETY_DAYS = "90d"
22 ALL = "all"
23
24
[docs]
25class StatsPerson(BaseModel):
26 """Someone on a stats list. ``member_id`` is set when they have a site account (links to their profile)."""
27
28 discord_id: str
29 display_name: str
30 avatar_url: str | None = None
31 member_id: int | None = None
32
33
[docs]
34class StatsLeader(BaseModel):
35 """One line on a top-10 leaderboard; what ``value`` counts depends on which list it's on."""
36
37 person: StatsPerson
38 value: int
39
40
[docs]
41class StatsTotals(BaseModel):
42 """Guild-wide activity totals over a window."""
43
44 active_members: int = Field(description="Distinct members with any message, voice minute, or attended event.")
45 messages: int
46 voice_minutes: int
47 events_attended: int
48 groups_joined: int = Field(description="Counted #lfg group joins (peak size 2+, 10+ minutes), like group XP.")
49 xp_gained: int = Field(description="Chat and voice XP earned, plus event/group XP paid out, in the window.")
50
51
[docs]
52class StatsTrendPoint(BaseModel):
53 """One point on the activity trend chart: a day or a Monday-start week, per ``GuildStats.trend_bucket``."""
54
55 start: date
56 messages: int
57 voice_minutes: int
58 active_members: int
59
60
[docs]
61class StatsHourCell(BaseModel):
62 """Activity in one weekday/hour slot, America/Chicago. ``weekday`` 0 is Monday."""
63
64 weekday: int = Field(ge=0, le=6)
65 hour: int = Field(ge=0, le=23)
66 messages: int
67 voice_minutes: int
68
69
[docs]
70class StatsQuietMember(BaseModel):
71 """A roster member still in the guild with no activity in the last 30 days."""
72
73 person: StatsPerson
74 last_active_at: datetime | None = Field(description="Most recent message, voice minute, or attended event ever.")
75 level: int
76
77
[docs]
78class GuildStats(BaseModel):
79 """``GET /api/stats?range=`` (officer): totals, trend, heatmap, leaderboards, and quiet members."""
80
81 range: StatsRange
82 since: datetime | None = Field(description="Start of the window; `null` for `all`.")
83 tracking_since: datetime | None = Field(description="Earliest hour the bot has ever recorded activity for.")
84 totals: StatsTotals
85 previous: StatsTotals | None = Field(
86 description="The same-length window right before `since`, for a trend comparison; `null` for `all`."
87 )
88 trend_bucket: Literal["day", "week"] = Field(description="`day` for 7d/30d, Monday-start `week` for 90d/all.")
89 trend: list[StatsTrendPoint] = Field(
90 description="Oldest first, zero-filled across the window (`all`: from `tracking_since`)."
91 )
92 heatmap: list[StatsHourCell] = Field(description="Non-zero weekday/hour cells only.")
93 top_chatters: list[StatsLeader] = Field(description="Highest message count, max 10.")
94 top_voice: list[StatsLeader] = Field(description="Most voice minutes, max 10.")
95 top_events: list[StatsLeader] = Field(description="Most attended events, max 10.")
96 top_groups: list[StatsLeader] = Field(description="Most counted #lfg groups joined, max 10.")
97 top_xp: list[StatsLeader] = Field(description="Most XP gained in the window, max 10.")
98 gone_quiet: list[StatsQuietMember] = Field(
99 description=(
100 "Roster members still in the guild with no messages, voice, or attended event in the last 30 days "
101 "(independent of `range`). Never-active first, then oldest last-active, max 25."
102 )
103 )