add member count metrics

This commit is contained in:
io mintz 2021-04-06 04:00:17 +00:00
parent f9d272fee2
commit 553267d9d8
2 changed files with 21 additions and 0 deletions

15
bot.py
View File

@ -58,6 +58,8 @@ class Bot(Bot):
utils.SUCCESS_EMOJIS = utils.misc.SUCCESS_EMOJIS = (
self.config.get('response_emojis', {}).get('success', default))
# Metrics
async def on_command(self, ctx):
user_id_md5 = hashlib.md5(ctx.author.id.to_bytes(8, byteorder='big'), usedforsecurity=False).digest()
await self.pool.execute(
@ -65,6 +67,19 @@ class Bot(Bot):
ctx.guild.id, user_id_md5, ctx.command.qualified_name,
)
# we use on_shard_ready rather than on_ready because the latter is a bit less reliable
async def on_shard_ready(self, shard_id):
member_count = sum(guild.member_count for guild in self.guilds if guild.shard_id == shard_id)
await self.pool.execute(
"""
INSERT INTO shard_member_counts (shard_id, member_count)
VALUES ($1, $2)
ON CONFLICT (shard_id) DO UPDATE
SET member_count = EXCLUDED.member_count
""",
shard_id, member_count,
)
def main():
import sys

View File

@ -11,3 +11,9 @@ CREATE TABLE invokes (
);
CREATE INDEX invokes_invoked_at_idx ON invokes (invoked_at);
CREATE TABLE shard_member_counts (
shard_id INT2 PRIMARY KEY,
-- sum(guild.member_count for guild in shard)
member_count INT4 NOT NULL
);