fast argument to only load cache

This commit is contained in:
klemek
2021-01-11 16:43:50 +01:00
parent ae9cc69ed2
commit a1d7354280
2 changed files with 99 additions and 75 deletions
+28 -6
View File
@@ -26,7 +26,11 @@ class GuildLogs:
return {id: self.channels[id].dict() for id in self.channels} return {id: self.channels[id].dict() for id in self.channels}
async def load( async def load(
self, progress: discord.Message, target_channels: List[discord.TextChannel] = [] self,
progress: discord.Message,
target_channels: List[discord.TextChannel] = [],
*,
fast: bool,
) -> Tuple[int, int]: ) -> Tuple[int, int]:
global current_analysis global current_analysis
if self.log_file in current_analysis: if self.log_file in current_analysis:
@@ -69,12 +73,31 @@ class GuildLogs:
logging.error(f"log {self.guild.id} > invalid JSON") logging.error(f"log {self.guild.id} > invalid JSON")
except IOError: except IOError:
logging.error(f"log {self.guild.id} > cannot read") logging.error(f"log {self.guild.id} > cannot read")
total_msg = 0
total_chan = 0
if fast:
if len(target_channels) == 0:
total_msg = sum(
[len(channel.messages) for channel in self.channels.values()]
)
total_chan = len(self.channels)
else:
target_channels_id = [channel.id for channel in target_channels]
total_msg = sum(
[
len(channel.messages)
for channel in self.channels.values()
if channel.id in target_channels_id
]
)
total_chan = len(target_channels)
else:
# load channels # load channels
t0 = datetime.now() t0 = datetime.now()
if len(target_channels) == 0: if len(target_channels) == 0:
target_channels = self.guild.text_channels target_channels = self.guild.text_channels
loading_new = 0 loading_new = 0
total_msg = 0
queried_msg = 0 queried_msg = 0
total_chan = 0 total_chan = 0
max_chan = len(target_channels) max_chan = len(target_channels)
@@ -93,9 +116,7 @@ class GuildLogs:
tmp_msg = total_msg + count tmp_msg = total_msg + count
warning_msg = "(this might take a while)" warning_msg = "(this might take a while)"
if len(target_channels) > 5 and loading_new > 5: if len(target_channels) > 5 and loading_new > 5:
warning_msg = ( warning_msg = "(most channels are new, this might take a looong while)"
"(most channels are new, this might take a looong while)"
)
elif loading_new > 0: elif loading_new > 0:
warning_msg = ( warning_msg = (
"(some channels are new, this might take a long while)" "(some channels are new, this might take a long while)"
@@ -145,7 +166,8 @@ class GuildLogs:
f"log {self.guild.id} > saved in {delta(t0):,}ms -> {real_total_msg / deltas(t0):,.3f} m/s" f"log {self.guild.id} > saved in {delta(t0):,}ms -> {real_total_msg / deltas(t0):,.3f} m/s"
) )
await code_message( await code_message(
progress, f"Analysing...\n{total_msg:,} messages in {total_chan:,} channels" progress,
f"Analysing...\n{total_msg:,} messages in {total_chan:,} channels",
) )
logging.info(f"log {self.guild.id} > TOTAL TIME: {delta(t00):,}ms") logging.info(f"log {self.guild.id} > TOTAL TIME: {delta(t00):,}ms")
current_analysis.remove(self.log_file) current_analysis.remove(self.log_file)
+4 -2
View File
@@ -44,7 +44,7 @@ class Scanner(ABC):
str_mentions = [member.mention for member in message.mentions] str_mentions = [member.mention for member in message.mentions]
for arg in args[1:]: for arg in args[1:]:
if ( if (
arg not in self.valid_args + ["me", "here"] arg not in self.valid_args + ["me", "here", "fast"]
and (not arg.isdigit() or not self.has_digit_args) and (not arg.isdigit() or not self.has_digit_args)
and arg not in str_channel_mentions and arg not in str_channel_mentions
and arg not in str_mentions and arg not in str_mentions
@@ -80,7 +80,9 @@ class Scanner(ABC):
# Start computing data # Start computing data
async with message.channel.typing(): async with message.channel.typing():
progress = await message.channel.send("```Starting analysis...```") progress = await message.channel.send("```Starting analysis...```")
total_msg, total_chan = await logs.load(progress, self.channels) total_msg, total_chan = await logs.load(
progress, self.channels, fast="fast" in args
)
if total_msg == -1: if total_msg == -1:
await message.channel.send( await message.channel.send(
f"{message.author.mention} An analysis is already running on this server, please be patient." f"{message.author.mention} An analysis is already running on this server, please be patient."