1"""Auction house prices synced from Battle.net, member price watches, and the bot's alert queue.
2
3``AuctionHousePriceRow`` holds each house's current price per item, replaced wholesale for a house on every sync.
4``AuctionHouseState`` tracks which houses are currently part of the guild's faction, so a house dropped from the
5faction (or gone empty) can have its stale price rows pruned. ``AuctionDailyHistory`` keeps one lowest-price row per
6(item, day) for the trend chart, pruned past ``AUCTION_HISTORY_DAYS``. ``AuctionWatch``/``AuctionAlert`` back a
7member's price alerts.
8"""
9
10from datetime import date, datetime # SQLAlchemy resolves Mapped[] annotations at runtime
11from uuid import UUID # SQLAlchemy resolves Mapped[] annotations at runtime
12
13from advanced_alchemy.base import UUIDAuditBase
14from advanced_alchemy.types import DateTimeUTC
15from sqlalchemy import BigInteger, Boolean, Date, ForeignKey, Integer, Text, UniqueConstraint
16from sqlalchemy.orm import Mapped, mapped_column
17
18
[docs]
19class AuctionHousePriceRow(UUIDAuditBase):
20 """One auction house's current price for one item, keyed by ``(house_id, item_id)``.
21
22 Replaced wholesale for a house on every sync: the sync deletes a house's existing rows and bulk-inserts the
23 freshly aggregated ones in one transaction, so a price that drops off the house never lingers.
24 """
25
26 __tablename__ = "auction_house_price"
27 __table_args__ = (
28 UniqueConstraint("house_id", "item_id", name="uq_auction_house_price_house_item"),
29 {"comment": "Each auction house's current lowest price per item; replaced wholesale on every sync."},
30 )
31
32 house_id: Mapped[int] = mapped_column(Integer, index=True)
33 item_id: Mapped[int] = mapped_column(Integer, index=True)
34 house_name: Mapped[str] = mapped_column(Text)
35 unit_price: Mapped[int] = mapped_column(BigInteger)
36 quantity: Mapped[int] = mapped_column(BigInteger)
37 listings: Mapped[int] = mapped_column(Integer)
38
39
[docs]
40class AuctionHouseState(UUIDAuditBase):
41 """Sync bookkeeping for one auction house: whether it's currently part of the guild's faction, and counts.
42
43 ``is_active`` lets a sync prune ``AuctionHousePriceRow`` rows for a house that dropped out of the faction set
44 (Battle.net could in principle reshuffle which houses are Alliance/Horde/Blackwater).
45 """
46
47 __tablename__ = "auction_house_state"
48 __table_args__ = {"comment": "Per-house sync bookkeeping: whether it's part of the guild's faction, and counts."}
49
50 house_id: Mapped[int] = mapped_column(Integer, unique=True, index=True)
51 house_name: Mapped[str] = mapped_column(Text)
52 is_active: Mapped[bool] = mapped_column(Boolean, default=True)
53 auctions_count: Mapped[int] = mapped_column(Integer, default=0)
54 commodities_count: Mapped[int] = mapped_column(Integer, default=0)
55 last_synced_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
56
57
[docs]
58class AuctionDailyHistory(UUIDAuditBase):
59 """One item's lowest unit price and latest total quantity for one UTC day, across the faction's houses."""
60
61 __tablename__ = "auction_daily_history"
62 __table_args__ = (
63 UniqueConstraint("item_id", "day", name="uq_auction_daily_history_item_day"),
64 {"comment": "Daily lowest price and latest quantity per item, across the faction's houses."},
65 )
66
67 item_id: Mapped[int] = mapped_column(Integer, index=True)
68 day: Mapped[date] = mapped_column(Date, index=True)
69 unit_price: Mapped[int] = mapped_column(BigInteger)
70 quantity_total: Mapped[int] = mapped_column(BigInteger)
71
72
[docs]
73class AuctionWatch(UUIDAuditBase):
74 """One member's price watch on an item: alert when the lowest unit price is at or below ``max_unit_price``."""
75
76 __tablename__ = "auction_watch"
77 __table_args__ = (
78 UniqueConstraint("member_id", "item_id", name="uq_auction_watch_member_item"),
79 {"comment": "A member's price-watch on an item. Deleted with the member."},
80 )
81
82 member_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("member.id", ondelete="CASCADE"), index=True)
83 item_id: Mapped[int] = mapped_column(Integer, index=True)
84 max_unit_price: Mapped[int] = mapped_column(BigInteger)
85 last_alerted_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
86 last_alerted_price: Mapped[int | None] = mapped_column(BigInteger, default=None)
87
88
[docs]
89class AuctionAlert(UUIDAuditBase):
90 """One queued price alert for a watch, the bot DMs and acks (``posted_at``).
91
92 ``discord_user_id``/``member_name`` are snapshotted at queue time so the alert reads correctly even if the
93 member later renames or unlinks. ``created_at`` (from the audit base) orders the pending queue oldest first.
94 """
95
96 __tablename__ = "auction_alert"
97 __table_args__ = {"comment": "Queued price-watch alerts for the bot to DM; posted_at is set once acked."}
98
99 watch_id: Mapped[UUID] = mapped_column(ForeignKey("auction_watch.id", ondelete="CASCADE"), index=True)
100 discord_user_id: Mapped[int] = mapped_column(BigInteger, index=True)
101 member_name: Mapped[str] = mapped_column(Text)
102 item_id: Mapped[int] = mapped_column(Integer, index=True)
103 unit_price: Mapped[int] = mapped_column(BigInteger)
104 max_unit_price: Mapped[int] = mapped_column(BigInteger)
105 quantity: Mapped[int] = mapped_column(BigInteger)
106 house_name: Mapped[str] = mapped_column(Text)
107 posted_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None, index=True)