Skip to content

Commit

Permalink
Push fixes to sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
Skelmis committed Jun 27, 2021
1 parent fb19f99 commit 1785d2c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
9 changes: 8 additions & 1 deletion cogs/stats.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import cProfile
import pstats
import timeit

import discord
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions conversations/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Binary file modified conversations/datastore/sqlite/datastore.db
Binary file not shown.
36 changes: 21 additions & 15 deletions conversations/datastore/sqlite/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
)

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions conversations/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


class Manager:
# TODO Let boosters see there own stats
conversation_identifier = itertools.count().__next__

def __init__(self, datastore: DataStore):
Expand Down
Binary file added profile.prof
Binary file not shown.

0 comments on commit 1785d2c

Please sign in to comment.