-
Notifications
You must be signed in to change notification settings - Fork 0
/
devops.py
87 lines (66 loc) · 2.73 KB
/
devops.py
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
import cProfile
import pstats
import timeit
import discord
from discord.ext import commands
from utils.util import Pag
class DevOps(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
print(f"{self.__class__.__name__}: Ready")
@commands.command(aliases=["bps", "buildpaststats"], hidden=True)
@commands.has_role(603803993562677258)
async def build_past_stats(self, ctx, channel: discord.TextChannel = None):
"""Builds stats for the given channel using all of the currently stored messages"""
if not channel:
channel = self.bot.get_channel(602327123767590992)
start_time = timeit.default_timer()
async with ctx.typing():
with cProfile.Profile() as pr:
convos = await self.bot.manager.build_past_conversations(channel)
stats = pstats.Stats(pr)
stats.sort_stats(pstats.SortKey.TIME)
stats.dump_stats(filename="profile.prof")
total_messages = 0
for convo in convos:
total_messages += len(convo.messages)
elapsed = timeit.default_timer() - start_time
await ctx.send(
f"Total conversations: `{len(convos)}`\nTotal messages:`{total_messages}`\nElapsed seconds: {elapsed}"
)
@commands.command(name="commandstats", aliases=["cs"])
@commands.cooldown(1, 5, commands.BucketType.guild)
async def command_stats(self, ctx):
"""Show an overall usage for each command!"""
data = await self.bot.command_usage.get_all()
command_map = {item["_id"]: item["usage_count"] for item in data}
# get total commands run
total_commands_run = sum(command_map.values())
# Sort by value
sorted_list = sorted(command_map.items(), key=lambda x: x[1], reverse=True)
pages = []
cmd_per_page = 10
for i in range(0, len(sorted_list), cmd_per_page):
message = "Command Name: `Usage % | Num of command runs`\n\n"
next_commands = sorted_list[i : i + cmd_per_page]
for item in next_commands:
use_percent = item[1] / total_commands_run
message += (
f"**{item[0]}**: `{use_percent: .2%} | Ran {item[1]} times`\n"
)
pages.append(message)
if len(pages) == 1:
await ctx.send(
embed=discord.Embed(title="Command Usage Statistics!", color=0xC9B4F4)
)
else:
await Pag(
title="Command Usage Statistics!",
color=0xC9B4F4,
entries=pages,
length=1,
).start(ctx)
def setup(bot):
bot.add_cog(DevOps(bot))