88import re
99import statistics
1010import time
11- from typing import Sequence
11+ from typing import cast , Sequence
1212
1313import discord
1414import prometheus_client # type: ignore
3838PROM_REQUESTS_SEEN = prometheus_client .Counter (
3939 f"{ PROM_PREFIX } _requests_seen_total" , "requests seen" , ["track" ]
4040)
41+ PROM_MENTEES_SEEN = prometheus_client .Counter (
42+ f"{ PROM_PREFIX } _mentees_seen_total" , "mentees seen" , ["track" ]
43+ )
4144
4245EXERCISM_TRACK_POLL_MIN_SECONDS = 5 * 60 # 5 minutes
4346EXERCISM_TRACK_POLL_MAX_SECONDS = 30 * 60 # 30 minutes
@@ -88,6 +91,7 @@ def __init__(
8891 self .request_timestamps : dict [str , list [int ]] = {track : [] for track in self .tracks }
8992 self .request_sum_delay : dict [str , int ] = {track : 0 for track in self .tracks }
9093 self .request_counts : dict [str , int ] = {track : 0 for track in self .tracks }
94+ self .mentees : dict [str , set [str ]] = {track : set () for track in self .tracks }
9195
9296 self .task_manager .start () # pylint: disable=E1101
9397
@@ -187,15 +191,13 @@ async def fetch_track_requests(self, track: str) -> None:
187191 add_requests = set (requests ) - set (self .messages [track ])
188192 del_requests = set (self .messages [track ]) - set (requests )
189193 self .requests [track ] = {
190- request_id : message
191- for request_id , ( timestamp , message ) in requests .items ()
194+ request_id : cast ( str , requests [ request_id ][ " message" ])
195+ for request_id , message in requests .items ()
192196 }
193197
194198 if add_requests :
195199 new_request_timestamps = [
196- timestamp
197- for request_id , (timestamp , message ) in requests .items ()
198- if request_id in add_requests
200+ cast (int , requests [request_id ]["timestamp" ]) for request_id in add_requests
199201 ]
200202 new_request_timestamps .sort (reverse = True )
201203
@@ -213,6 +215,13 @@ async def fetch_track_requests(self, track: str) -> None:
213215 )
214216 PROM_REQUESTS_SEEN .labels (track ).inc (len (add_requests ))
215217
218+ prior_count = len (self .mentees [track ])
219+ self .mentees [track ].update (
220+ cast (str , requests [request_id ]["mentee" ]) for request_id in add_requests
221+ )
222+ if delta := len (self .mentees [track ]) - prior_count :
223+ PROM_MENTEES_SEEN .labels (track ).inc (delta )
224+
216225 # Add the new timestamps the the running tally of the last N.
217226 self .request_timestamps [track ] = sorted (
218227 self .request_timestamps [track ] + new_request_timestamps ,
@@ -338,7 +347,7 @@ async def load_data(self) -> None:
338347 self .threads [track ] = thread
339348 await asyncio .sleep (2 )
340349
341- async def get_requests (self , track_slug : str ) -> dict [str , tuple [ int , str ]]:
350+ async def get_requests (self , track_slug : str ) -> dict [str , dict [ str , str | int ]]:
342351 """Return formatted mentor requests."""
343352 requests = {}
344353 for req in await self .exercism .mentor_requests (track_slug ):
@@ -356,5 +365,9 @@ async def get_requests(self, track_slug: str) -> dict[str, tuple[int, str]]:
356365 msg += f"({ student_handle } )"
357366
358367 timestamp = int (datetime .datetime .fromisoformat (req ["updated_at" ]).timestamp ())
359- requests [req ["uuid" ]] = (timestamp , msg )
368+ requests [req ["uuid" ]] = {
369+ "timestamp" : timestamp ,
370+ "message" : msg ,
371+ "mentee" : student_handle ,
372+ }
360373 return requests
0 commit comments