-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
225 lines (192 loc) · 9.85 KB
/
Copy pathmonitor.py
File metadata and controls
225 lines (192 loc) · 9.85 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import asyncio
import logging
import time
from datetime import datetime, timezone, timedelta
import zoneinfo
import aiohttp
from telegram import Bot
from blizzard_api import BlizzardAPI
import database
from bluesky_fetcher import BlueskyFetcher
logger = logging.getLogger(__name__)
MIN_POLL_INTERVAL = 60
FALLBACK_POLL_INTERVAL = 300
BLUESKY_POLL_INTERVAL = 300
class MonitorService:
"""Monitors WoW realm statuses and Bluesky updates."""
def __init__(self, blizzard_api: BlizzardAPI, bot: Bot, bsky_fetcher: BlueskyFetcher, bsky_wow_fetcher: BlueskyFetcher = None, bsky_classic_fetcher: BlueskyFetcher = None):
self.api = blizzard_api
self.bot = bot
self.bsky = bsky_fetcher
self.bsky_wow = bsky_wow_fetcher
self.bsky_classic = bsky_classic_fetcher
self._last_status: dict[tuple[str, str, str], str | None] = {}
self._running = False
self.start_time = time.time()
self.blizzard_fetches = 0
self.bluesky_fetches = 0
async def broadcast_telegram(self, chat_ids: list[int], message: str):
"""Send a message to multiple users, strictly rate-limited."""
for cid in chat_ids:
try:
await self.bot.send_message(chat_id=cid, text=message, parse_mode="HTML")
except Exception as e:
logger.error("Failed to send to %s: %s", cid, e)
await asyncio.sleep(0.05) # Telegram limit: ~30 msgs/sec
async def check_realms(self, session: aiohttp.ClientSession) -> int:
unique_realms = await database.get_unique_realms()
if not unique_realms:
return FALLBACK_POLL_INTERVAL
min_cache_age = FALLBACK_POLL_INTERVAL
for region, slug, original_name, game_version in unique_realms:
realm_key = (region, slug, game_version)
try:
self.blizzard_fetches += 1
realm_data, cache_max_age = await self.api.get_realm_status(session, region, slug, game_version)
if cache_max_age > 0:
min_cache_age = min(min_cache_age, cache_max_age)
if realm_data is None:
continue
current_status = realm_data["status"]
previous_status = self._last_status.get(realm_key)
realm_name = str(realm_data.get("name", original_name)).title()
region_upper = region.upper()
if previous_status is not None and current_status != previous_status:
logger.info("Status change: %s -> %s for %s-%s (%s)", previous_status, current_status, region_upper, slug, game_version)
users = await database.get_users_for_realm(region, slug, game_version)
if users:
v_tag = f"[{game_version.title()}] " if game_version != "retail" else ""
tz_groups = await database.get_users_by_timezone(users)
for tz_name, user_group in tz_groups.items():
try:
tz = zoneinfo.ZoneInfo(tz_name)
except Exception:
tz = zoneinfo.ZoneInfo("UTC")
now_local = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
if current_status == "UP":
local_msg = f"🟢 <b>Realm {v_tag}\"{realm_name}\" ({region_upper}) is back ONLINE</b>\n🕐 {now_local}"
else:
local_msg = f"🔴 <b>Realm {v_tag}\"{realm_name}\" ({region_upper}) went OFFLINE</b>\n🕐 {now_local}"
await self.broadcast_telegram(user_group, local_msg)
self._last_status[realm_key] = current_status
except Exception as e:
logger.error("Error checking realm %s-%s (%s): %s", region.upper(), slug, game_version, e)
return max(min_cache_age, MIN_POLL_INTERVAL)
async def check_bluesky(self):
try:
self.bluesky_fetches += 1
posts = await self.bsky.fetch_new_posts()
if posts:
logger.info("Found %d new posts from %s", len(posts), self.bsky.target_account)
for post in posts:
logger.info("Processing post from %s: %s...", post['author_name'], post['text'][:50])
if post['is_maintenance']:
targets = await database.get_bluesky_subscribers(['maintenance', 'all'])
else:
targets = await database.get_bluesky_subscribers(['all'])
if targets:
tz_groups = await database.get_users_by_timezone(targets)
for tz_name, user_group in tz_groups.items():
try:
tz = zoneinfo.ZoneInfo(tz_name)
except Exception:
tz = zoneinfo.ZoneInfo("UTC")
now_local = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
msg = (
f"🐦 <b>{post['author_name']} Update</b>\n\n"
f"{post['text']}\n\n"
f"🕐 {now_local}\n"
f"👉 <a href='{post['post_url']}'>View on Bluesky</a>"
)
await self.broadcast_telegram(user_group, msg)
except Exception as e:
logger.error("Error checking Bluesky: %s", e)
async def check_wow_bluesky(self):
if not self.bsky_wow:
return
try:
self.bluesky_fetches += 1
posts = await self.bsky_wow.fetch_new_posts()
if posts:
logger.info("Found %d new posts from %s", len(posts), self.bsky_wow.target_account)
for post in posts:
logger.info("Processing post from %s: %s...", post['author_name'], post['text'][:50])
targets = await database.get_wow_bluesky_subscribers(['all'])
if targets:
tz_groups = await database.get_users_by_timezone(targets)
for tz_name, user_group in tz_groups.items():
try:
tz = zoneinfo.ZoneInfo(tz_name)
except Exception:
tz = zoneinfo.ZoneInfo("UTC")
now_local = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
msg = (
f"🗡 <b>{post['author_name']} Update</b>\n\n"
f"{post['text']}\n\n"
f"🕐 {now_local}\n"
f"👉 <a href='{post['post_url']}'>View on Bluesky</a>"
)
await self.broadcast_telegram(user_group, msg)
except Exception as e:
logger.error("Error checking WoW Bluesky: %s", e)
async def check_classic_bluesky(self):
if not self.bsky_classic:
return
try:
self.bluesky_fetches += 1
posts = await self.bsky_classic.fetch_new_posts()
if posts:
logger.info("Found %d new posts from %s", len(posts), self.bsky_classic.target_account)
for post in posts:
logger.info("Processing post from %s: %s...", post['author_name'], post['text'][:50])
targets = await database.get_classic_bluesky_subscribers(['all'])
if targets:
tz_groups = await database.get_users_by_timezone(targets)
for tz_name, user_group in tz_groups.items():
try:
tz = zoneinfo.ZoneInfo(tz_name)
except Exception:
tz = zoneinfo.ZoneInfo("UTC")
now_local = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
msg = (
f"🛡 <b>{post['author_name']} Update</b>\n\n"
f"{post['text']}\n\n"
f"🕐 {now_local}\n"
f"👉 <a href='{post['post_url']}'>View on Bluesky</a>"
)
await self.broadcast_telegram(user_group, msg)
except Exception as e:
logger.error("Error checking Classic Bluesky: %s", e)
async def _realm_loop(self):
async with aiohttp.ClientSession() as session:
# First check purely populates the baseline state if we just restarted
next_interval = await self.check_realms(session)
while self._running:
await asyncio.sleep(next_interval)
next_interval = await self.check_realms(session)
async def _bluesky_loop(self):
while self._running:
await self.check_bluesky()
await self.check_wow_bluesky()
await self.check_classic_bluesky()
await asyncio.sleep(BLUESKY_POLL_INTERVAL)
async def run(self):
self._running = True
logger.info("Starting background monitor service...")
# Run both tasks concurrently
await asyncio.gather(
self._realm_loop(),
self._bluesky_loop()
)
def stop(self):
self._running = False
def get_stats(self) -> dict:
"""Calculate and return system stats."""
uptime = time.time() - self.start_time
up_minutes = max(1.0, float(uptime / 60.0))
td = timedelta(seconds=int(uptime))
return {
"uptime": str(td),
"blizzard_rpm": f"{self.blizzard_fetches / up_minutes:.2f}",
"bluesky_rpm": f"{self.bluesky_fetches / up_minutes:.2f}"
}