aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2021-01-03 12:06:15 -0800
committerGravatar MarkKoz <[email protected]>2021-01-04 23:19:56 -0800
commit46973747f5f0dec03e3ea0f87ea07ebbda6b07da (patch)
tree9aae05ac9e93c29c2fe47eef48e1555bf5e83e03
parentHelpChannels: move function to the channel module (diff)
HelpChannels: refactor stat tracking
Move significant code related to stats to a separate module.
-rw-r--r--bot/exts/help_channels/_cog.py33
-rw-r--r--bot/exts/help_channels/_stats.py41
2 files changed, 47 insertions, 27 deletions
diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py
index f25bf132c..854696f87 100644
--- a/bot/exts/help_channels/_cog.py
+++ b/bot/exts/help_channels/_cog.py
@@ -11,7 +11,7 @@ from discord.ext import commands
from bot import constants
from bot.bot import Bot
-from bot.exts.help_channels import _caches, _channel, _cooldown, _message, _name
+from bot.exts.help_channels import _caches, _channel, _cooldown, _message, _name, _stats
from bot.utils import channel as channel_utils, lock
from bot.utils.scheduling import Scheduler
@@ -275,20 +275,10 @@ class HelpChannels(commands.Cog):
self.close_command.enabled = True
await self.init_available()
- self.report_stats()
+ _stats.report_counts()
log.info("Cog is ready!")
- def report_stats(self) -> None:
- """Report the channel count stats."""
- total_in_use = sum(1 for _ in _channel.get_category_channels(self.in_use_category))
- total_available = sum(1 for _ in _channel.get_category_channels(self.available_category))
- total_dormant = sum(1 for _ in _channel.get_category_channels(self.dormant_category))
-
- self.bot.stats.gauge("help.total.in_use", total_in_use)
- self.bot.stats.gauge("help.total.available", total_available)
- self.bot.stats.gauge("help.total.dormant", total_dormant)
-
async def move_idle_channel(self, channel: discord.TextChannel, has_task: bool = True) -> None:
"""
Make the `channel` dormant if idle or schedule the move if still active.
@@ -341,7 +331,7 @@ class HelpChannels(commands.Cog):
category_id=constants.Categories.help_available,
)
- self.report_stats()
+ _stats.report_counts()
async def move_to_dormant(self, channel: discord.TextChannel, caller: str) -> None:
"""
@@ -357,18 +347,7 @@ class HelpChannels(commands.Cog):
)
await self.unclaim_channel(channel)
-
- self.bot.stats.incr(f"help.dormant_calls.{caller}")
-
- in_use_time = await _channel.get_in_use_time(channel.id)
- if in_use_time:
- self.bot.stats.timing("help.in_use_time", in_use_time)
-
- unanswered = await _caches.unanswered.get(channel.id)
- if unanswered:
- self.bot.stats.incr("help.sessions.unanswered")
- elif unanswered is not None:
- self.bot.stats.incr("help.sessions.answered")
+ await _stats.report_complete_session(channel.id, caller)
log.trace(f"Position of #{channel} ({channel.id}) is actually {channel.position}.")
log.trace(f"Sending dormant message for #{channel} ({channel.id}).")
@@ -379,7 +358,7 @@ class HelpChannels(commands.Cog):
log.trace(f"Pushing #{channel} ({channel.id}) into the channel queue.")
self.channel_queue.put_nowait(channel)
- self.report_stats()
+ _stats.report_counts()
async def unclaim_channel(self, channel: discord.TextChannel) -> None:
"""
@@ -416,7 +395,7 @@ class HelpChannels(commands.Cog):
log.trace(f"Scheduling #{channel} ({channel.id}) to become dormant in {timeout} sec.")
self.scheduler.schedule_later(timeout, channel.id, self.move_idle_channel(channel))
- self.report_stats()
+ _stats.report_counts()
@commands.Cog.listener()
async def on_message(self, message: discord.Message) -> None:
diff --git a/bot/exts/help_channels/_stats.py b/bot/exts/help_channels/_stats.py
new file mode 100644
index 000000000..8e6ff8fe5
--- /dev/null
+++ b/bot/exts/help_channels/_stats.py
@@ -0,0 +1,41 @@
+import logging
+
+from more_itertools import ilen
+
+import bot
+from bot import constants
+from bot.exts.help_channels import _caches, _channel
+
+log = logging.getLogger(__name__)
+
+
+def report_counts() -> None:
+ """Report channel count stats of each help category."""
+ for name in ("in_use", "available", "dormant"):
+ id_ = getattr(constants.Categories, f"help_{name}")
+ category = bot.instance.get_channel(id_)
+
+ if category:
+ total = ilen(_channel.get_category_channels(category))
+ bot.instance.stats.gauge(f"help.total.{name}", total)
+ else:
+ log.warning(f"Couldn't find category {name!r} to track channel count stats.")
+
+
+async def report_complete_session(channel_id: int, caller: str) -> None:
+ """
+ Report stats for a completed help session channel `channel_id`.
+
+ `caller` is used to track stats on how `channel_id` was unclaimed (either 'auto' or 'command').
+ """
+ bot.instance.stats.incr(f"help.dormant_calls.{caller}")
+
+ in_use_time = await _channel.get_in_use_time(channel_id)
+ if in_use_time:
+ bot.instance.stats.timing("help.in_use_time", in_use_time)
+
+ unanswered = await _caches.unanswered.get(channel_id)
+ if unanswered:
+ bot.instance.stats.incr("help.sessions.unanswered")
+ elif unanswered is not None:
+ bot.instance.stats.incr("help.sessions.answered")