concurrent fast analysis

This commit is contained in:
Klemek
2021-04-07 14:55:54 +02:00
parent f97682f46a
commit 91ae6ed383
3 changed files with 50 additions and 32 deletions
+1 -1
View File
@@ -46,7 +46,7 @@
* %words - rank words by their usage
* arguments:
* <n> - top <n> words, default is 10
* %cancel - cancel current analysis
* %cancel - cancel current analysis (not launched with fast)
* Common arguments:
* @member/me: filter for one or more member
+47 -29
View File
@@ -53,12 +53,13 @@ class GuildLogs:
self.guild = guild
self.log_file = os.path.join(LOG_DIR, f"{guild.id}.logz")
self.channels = {}
self.locked = False
def dict(self) -> dict:
return {id: self.channels[id].dict() for id in self.channels}
def check_cancelled(self) -> bool:
return self.log_file not in current_analysis
return self.locked and self.log_file not in current_analysis
async def load(
self,
@@ -68,12 +69,15 @@ class GuildLogs:
fast: bool,
fresh: bool,
) -> Tuple[int, int]:
current_analysis_lock.acquire()
if self.log_file in current_analysis:
self.locked = False
if not fast:
self.locked = True
current_analysis_lock.acquire()
if self.log_file in current_analysis:
current_analysis_lock.release()
return ALREADY_RUNNING, 0
current_analysis.append(self.log_file)
current_analysis_lock.release()
return ALREADY_RUNNING, 0
current_analysis.append(self.log_file)
current_analysis_lock.release()
t00 = datetime.now()
# read logs
if not os.path.exists(LOG_DIR):
@@ -122,31 +126,43 @@ class GuildLogs:
else:
fast = False
if len(target_channels) == 0:
target_channels = (
self.channels.values() if fast else self.guild.text_channels
)
elif fast:
# select already loaded channels only
target_channels_tmp = [
channel for channel in target_channels if channel.id in self.channels
]
if len(target_channels_tmp) == 0:
fast = False
else:
target_channels = target_channels_tmp
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)
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:
if not self.locked:
self.locked = True
current_analysis_lock.acquire()
if self.log_file in current_analysis:
current_analysis_lock.release()
return ALREADY_RUNNING, 0
current_analysis.append(self.log_file)
current_analysis_lock.release()
# load channels
t0 = datetime.now()
if len(target_channels) == 0:
target_channels = (
self.guild.text_channels if not fast else self.channels.keys()
)
loading_new = 0
queried_msg = 0
total_chan = 0
@@ -247,9 +263,10 @@ class GuildLogs:
f"Analysing...\n{total_msg:,} messages in {total_chan:,} channels",
)
logging.info(f"log {self.guild.id} > TOTAL TIME: {delta(t00):,}ms")
current_analysis_lock.acquire()
current_analysis.remove(self.log_file)
current_analysis_lock.release()
if self.locked:
current_analysis_lock.acquire()
current_analysis.remove(self.log_file)
current_analysis_lock.release()
return total_msg, total_chan
@staticmethod
@@ -262,5 +279,6 @@ class GuildLogs:
else:
current_analysis_lock.release()
await message.channel.send(
f"No analysis are currently running on this server", reference=message
f"No cancellable analysis are currently running on this server",
reference=message,
)
+2 -2
View File
@@ -42,8 +42,8 @@ bot.log_calls = True
bot.register_command(
"(cancel|stop)",
GuildLogs.cancel,
"cancel: stop current analysis",
"```\n" + "%cancel: Stop current analysis\n" + "```",
"cancel: stop current analysis (not launched with fast)",
"```\n" + "%cancel: Stop current analysis (not launched with fast)\n" + "```",
)
bot.register_command(
"last",