fix(bot): ✨ Improve p.stats.

This commit is contained in:
Nyeki.py
2024-10-17 23:37:13 -03:00
parent 62e2d30fdf
commit 0fbf7d86dc
10 changed files with 191 additions and 45 deletions
+90 -17
View File
@@ -1,3 +1,5 @@
from typing import Awaitable
from datetime import datetime, timezone, timedelta
import sys
@@ -13,7 +15,7 @@ from payphone import bot
from payphone.db import models, utils
from payphone.cogs import base
from payphone.cogs.mods import embeds, views
from payphone.core import checks, converters, constants
from payphone.core import checks, converters, constants, formatting as fmt
class Mods(base.Cog):
@@ -291,29 +293,100 @@ class Mods(base.Cog):
@commands.command()
@commands.is_owner()
async def stats(self, ctx: commands.Context) -> None:
async def stats(
self, ctx: commands.Context, iterations: int = 5, wait: int = 5
) -> None:
"""
Shows all Payphone's stats.
Options:
- `<iterations>`: The amount of iterations (to display progress). Defaults to 5.
- `<wait>`: The amount of seconds to wait between iterations. Defaults to 5.
"""
stats = [
f"On-call Channels: {await self.bot.redis.scard(constants.CHANNELS_ONCALL_SET)}",
f"Searching Channels: {await self.bot.redis.scard(constants.CHANNELS_SEARCH_QUEUE)}",
f"Messages: {await models.Message.all().count()}",
f"Message Contents: {await models.MessageContent.all().count()}",
f"Calls: {await models.Call.all().count()}",
f"Ongoing Calls: {await models.Call.filter(ended_at=None).count()}",
f"Bans: {await models.Ban.all().count()}",
f"Reports: {await models.Report.all().count()}",
f"On-review Reports: {await models.Report.filter(status=models.ReportStatus.ON_REVIEW).count()}",
f"Users: {await models.User.all().count()}",
f"Profiles: {await models.Profile.all().count()}",
f"Servers: {await models.Server.all().count()}",
f"Shards: {self.bot.shard_count}",
f"Current Shard: {ctx.guild.shard_id + 1}",
(
"On-call Channels",
lambda: self.bot.redis.scard(constants.CHANNELS_ONCALL_SET),
),
(
"Searching Channels",
lambda: self.bot.redis.scard(constants.CHANNELS_SEARCH_QUEUE),
),
(
"Messages",
lambda: models.Message.all().count(),
),
(
"Message Contents",
lambda: models.MessageContent.all().count(),
),
("Calls", lambda: models.Call.all().count()),
(
"Ongoing Calls",
lambda: models.Call.filter(ended_at=None).count(),
),
("Bans", lambda: models.Ban.all().count()),
("Reports", lambda: models.Report.all().count()),
(
"On-review Reports",
lambda: models.Report.filter(
status=models.ReportStatus.ON_REVIEW
).count(),
),
("Users", lambda: models.User.all().count()),
(
"Profiles",
lambda: models.Profile.all().count(),
),
("Servers", lambda: models.Server.all().count()),
("Shards", lambda: self.bot.shard_count),
("Current Shards", lambda: ctx.guild.shard_id + 1),
]
await ctx.send(f"```\n{"\n".join(stats)}\n```")
rows = [[stat[0]] for stat in stats]
rows_with_diff = [[stat[0]] for stat in stats]
i = 0
message = None
while i < iterations:
i += 1
for stat_i, stat in enumerate(stats):
value = stat[1]()
if isinstance(value, Awaitable):
value = await value
rows[stat_i].append(value)
prev_value = rows[stat_i][-2]
if isinstance(prev_value, str):
rows_with_diff[stat_i].append(value)
else:
diff = value - prev_value
rows_with_diff[stat_i].append(
str(value)
+ (f" ({"+" if diff > 0 else ""}{diff})" if diff != 0 else "")
)
table = fmt.monospace_table(
rows_with_diff,
[""] + [f"{wait * j}s" for j in range(i)],
padding=2,
)
if message is None:
message = await ctx.send(f"```\n{table}\n```")
else:
message = await message.edit(f"```\n{table}\n```")
if i < iterations:
await asyncio.sleep(wait)
def setup(b: bot.Payphone) -> None: