hopefully made generating logs faster

This commit is contained in:
brandons209 2020-07-20 02:19:54 -04:00
parent 7cca507f33
commit c77bf071d6

View file

@ -380,7 +380,7 @@ class ActivityLogger(commands.Cog):
return return
guild = ctx.guild guild = ctx.guild
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*.log")), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*.log"))
# remove audit log entries # remove audit log entries
log_files = [log for log in log_files if "guild" not in log] log_files = [log for log in log_files if "guild" not in log]
@ -513,22 +513,16 @@ class ActivityLogger(commands.Cog):
plt.yticks(fontsize=fontsize) plt.yticks(fontsize=fontsize)
plt.grid(True) plt.grid(True)
plt.legend(bbox_to_anchor=(1.00, 1.0), loc='upper left', prop={"size": 30}) plt.legend(bbox_to_anchor=(1.00, 1.0), loc="upper left", prop={"size": 30})
fig.tight_layout() fig.tight_layout()
fig.savefig(save_path, dpi=fig.dpi) fig.savefig(save_path, dpi=fig.dpi)
plt.close() plt.close()
with open(table_save_path, "w") as f: df.to_csv(table_save_path, index=False)
# need to set pandas options so that full data is printed to string
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', -1)
f.write(str(df))
with open(save_path, "rb") as f, open(table_save_path, "r") as t: with open(save_path, "rb") as f, open(table_save_path, "r") as t:
files = (discord.File(f, filename="graph.png"), discord.File(t, filename="graph_data.txt")) files = (discord.File(f, filename="graph.png"), discord.File(t, filename="graph_data.csv"))
await ctx.send(files=files) await ctx.send(files=files)
os.remove(save_path) os.remove(save_path)
@ -549,32 +543,26 @@ class ActivityLogger(commands.Cog):
else: else:
messages = [] messages = []
# runs in descending order, with most recent log file first parsed_logs = []
log_files.sort(reverse=True)
for log in log_files: for log in log_files:
if split_channels: if split_channels:
channel_id = int(log.split("_")[-1].strip(".log")) channel_id = int(log.split("_")[-1].strip(".log"))
if channel_id not in messages: if channel_id not in messages:
messages[channel_id] = [] messages[channel_id] = []
with open(log, "r") as f: with open(log, "r") as f:
for line in reversed(list(f)): lines = f.readlines()
# time interval check:
try: # shouldnt happen, but just in case
current_time = parse_time_naive(line[:19])
except:
continue
if start and start < current_time:
continue
if end_time > current_time:
break
if split_channels: # binary search to find where the cutoff for messages is
messages[channel_id].append(line) index = bisect_left(lines, end_time.strftime(TIMESTAMP_FORMAT))
else:
messages.append(line) lines = lines[index:]
# don't break if end_time > current_time in a log, so that getting lines.reverse()
# logs for a user from an entire guild works, as different channels if split_channels:
# need to be checked. this doesn't save that much time when used messages[channel_id].extend(lines)
# on a specific channel else:
messages.extend(lines)
# reverse messages to get correct order # reverse messages to get correct order
if split_channels: if split_channels:
@ -645,7 +633,7 @@ class ActivityLogger(commands.Cog):
return return
guild = ctx.guild guild = ctx.guild
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*{}*.log".format(channel.id))), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*{}*.log".format(channel.id)))
if interval: if interval:
end_time = datetime.utcnow() - interval end_time = datetime.utcnow() - interval
@ -680,7 +668,7 @@ class ActivityLogger(commands.Cog):
return return
guild = ctx.guild guild = ctx.guild
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*{}*.log".format(channel.id))), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*{}*.log".format(channel.id)))
await self.log_sender(ctx, log_files, end, start=start) await self.log_sender(ctx, log_files, end, start=start)
@ -720,7 +708,7 @@ class ActivityLogger(commands.Cog):
return return
guild = ctx.guild guild = ctx.guild
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*.log")), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*.log"))
log_files = [log for log in log_files if "guild" not in log] log_files = [log for log in log_files if "guild" not in log]
if interval: if interval:
@ -756,7 +744,7 @@ class ActivityLogger(commands.Cog):
return return
guild = ctx.guild guild = ctx.guild
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*.log")), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*.log"))
log_files = [log for log in log_files if "guild" not in log] log_files = [log for log in log_files if "guild" not in log]
await self.log_sender(ctx, log_files, end, start=start, user=user) await self.log_sender(ctx, log_files, end, start=start, user=user)
@ -799,7 +787,7 @@ class ActivityLogger(commands.Cog):
return return
guild = ctx.guild guild = ctx.guild
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*guild*.log")), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*guild*.log"))
if interval: if interval:
end_time = datetime.utcnow() - interval end_time = datetime.utcnow() - interval
@ -837,8 +825,7 @@ class ActivityLogger(commands.Cog):
return return
guild = ctx.guild guild = ctx.guild
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*guild*.log")), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*guild*.log"))
await self.log_sender(ctx, log_files, end, start=start) await self.log_sender(ctx, log_files, end, start=start)
@logs_audit.group(name="user") @logs_audit.group(name="user")
@ -879,7 +866,7 @@ class ActivityLogger(commands.Cog):
return return
guild = ctx.guild guild = ctx.guild
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*guild*.log")), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*guild*.log"))
if interval: if interval:
end_time = datetime.utcnow() - interval end_time = datetime.utcnow() - interval
@ -917,7 +904,7 @@ class ActivityLogger(commands.Cog):
return return
guild = ctx.guild guild = ctx.guild
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*guild*.log")), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*guild*.log"))
await self.log_sender(ctx, log_files, end, start=start, user=user) await self.log_sender(ctx, log_files, end, start=start, user=user)
@ -954,7 +941,7 @@ class ActivityLogger(commands.Cog):
if not channel: if not channel:
await ctx.send("Invalid channel!") await ctx.send("Invalid channel!")
return return
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*{}*.log".format(channel.id))), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*{}*.log".format(channel.id)))
if interval: if interval:
end_time = datetime.utcnow() - interval end_time = datetime.utcnow() - interval
@ -993,7 +980,7 @@ class ActivityLogger(commands.Cog):
if not channel: if not channel:
await ctx.send("Invalid channel!") await ctx.send("Invalid channel!")
return return
log_files = sorted(glob.glob(os.path.join(PATH, str(guild.id), "*{}*.log".format(channel.id))), reverse=True) log_files = glob.glob(os.path.join(PATH, str(guild.id), "*{}*.log".format(channel.id)))
await self.log_sender(ctx, log_files, end, start=start) await self.log_sender(ctx, log_files, end, start=start)