diff --git a/cogs/stats.py b/cogs/stats.py index a528f01..c312efd 100644 --- a/cogs/stats.py +++ b/cogs/stats.py @@ -1,3 +1,5 @@ +import cProfile +import pstats import timeit import discord @@ -43,7 +45,12 @@ async def build_past_stats(self, ctx, channel: discord.TextChannel = None): """Builds stats for the given channel using all of the currently stored messages""" start_time = timeit.default_timer() async with ctx.typing(): - convos = await self.bot.manager.build_past_conversations(channel) + 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: diff --git a/conversations/dataclasses.py b/conversations/dataclasses.py index eb5187d..89fb80f 100644 --- a/conversations/dataclasses.py +++ b/conversations/dataclasses.py @@ -62,8 +62,10 @@ def get_average_time_per_convo(self) -> int: Returns the average amount of minutes spent per support conversation """ - time_in_seconds = sum(self.conversation_length) / len(self.conversation_length) - return round(time_in_seconds / 60) + time_in_seconds = sum(self.conversation_length, datetime.timedelta(0)) / len( + self.conversation_length + ) + return round(time_in_seconds.total_seconds() / 60) class Plots(Enum): diff --git a/conversations/datastore/sqlite/datastore.db b/conversations/datastore/sqlite/datastore.db index 14071c0..d198228 100644 Binary files a/conversations/datastore/sqlite/datastore.db and b/conversations/datastore/sqlite/datastore.db differ diff --git a/conversations/datastore/sqlite/sqlite.py b/conversations/datastore/sqlite/sqlite.py index ad1cf4b..12e9182 100644 --- a/conversations/datastore/sqlite/sqlite.py +++ b/conversations/datastore/sqlite/sqlite.py @@ -118,15 +118,8 @@ async def fetch_helpers(self) -> List[Helper]: helpers = [] helpers_raw = await cursor.fetchall() for val in helpers_raw: - try: - per_convo_messages = val[3] - except IndexError: - per_convo_messages = [] - - try: - convos = [timedelta(seconds=s) for s in val[4]] - except IndexError: - convos = [] + per_convo_messages = [item[3] for item in val] + convos = [timedelta(seconds=s[4]) for s in val] helpers.append( Helper( @@ -153,22 +146,33 @@ async def fetch_helper(self, identifier: int) -> Helper: "LEFT JOIN Helper_convo_length Hcl " " ON H.identifier = Hcl.helper_id " "WHERE " - " H.identifier=:identifier " + " H.identifier=:identifier ", + {"identifier": identifier} + # TODO Add args here ) as cursor: val = await cursor.fetchone() + x = await cursor.fetchall() + + per_convo_messages = [item[3] for item in x] + convos = [timedelta(seconds=s[4]) for s in x] + + """ try: per_convo_messages = val[3] except IndexError: per_convo_messages = [] try: + print(val[4]) convos = [timedelta(seconds=s) for s in val[4]] except IndexError: convos = [] + """ + return Helper( identifier=val[0], total_messages=val[1], total_conversations=val[2], - messages_per_conversation=per_convo_messages, # Check these are lists + messages_per_conversation=per_convo_messages, conversation_length=convos, ) @@ -253,21 +257,23 @@ async def fetch_all_helpers(self) -> List[Helper]: " H.identifier, H.total_messages, " " H.total_conversations, Hmp.amount, Hcl.time " "FROM Helper H " - "INNER JOIN Helper_messages_per Hmp" + "LEFT JOIN Helper_messages_per Hmp" " ON Hmp.helper_id = H.identifier " - "INNER JOIN Helper_convo_length Hcl " + "LEFT JOIN Helper_convo_length Hcl " " ON H.identifier = Hcl.helper_id " ) as cursor: helpers = [] helpers_raw = await cursor.fetchall() for val in helpers_raw: + per_convo_messages = [item[3] for item in val] + convos = [timedelta(seconds=s[4]) for s in val] helpers.append( Helper( identifier=val[0], total_messages=val[1], total_conversations=val[2], - messages_per_conversation=val[3], - conversation_length=val[4], + messages_per_conversation=per_convo_messages, + conversation_length=convos, ) ) return helpers diff --git a/conversations/manager.py b/conversations/manager.py index 03dbbd1..0c1c584 100644 --- a/conversations/manager.py +++ b/conversations/manager.py @@ -14,6 +14,7 @@ class Manager: + # TODO Let boosters see there own stats conversation_identifier = itertools.count().__next__ def __init__(self, datastore: DataStore): diff --git a/profile.prof b/profile.prof new file mode 100644 index 0000000..39451fe Binary files /dev/null and b/profile.prof differ