From 3abd56819748fc7791696dd509aec6a769f24f68 Mon Sep 17 00:00:00 2001 From: ngc92 <7938269+ngc92@users.noreply.github.com> Date: Tue, 21 Jan 2025 19:27:15 +0200 Subject: [PATCH 1/2] split generate_report into smaller functions --- src/discord-cluster-manager/report.py | 128 +++++++++++++------------- 1 file changed, 66 insertions(+), 62 deletions(-) diff --git a/src/discord-cluster-manager/report.py b/src/discord-cluster-manager/report.py index c5a30bd..08239f5 100644 --- a/src/discord-cluster-manager/report.py +++ b/src/discord-cluster-manager/report.py @@ -1,7 +1,7 @@ import pprint import discord -from run_eval import FullResult +from run_eval import CompileResult, FullResult, RunResult def _limit_length(text: str, maxlen: int): @@ -41,84 +41,88 @@ async def _send_split_log(thread: discord.Thread, partial_message: str, header: return "" -async def generate_report(thread: discord.Thread, result: FullResult, has_tests: bool): # noqa: C901 +async def _generate_compile_report(thread: discord.Thread, comp: CompileResult): message = "" - if not result.success: - message += "# Failure\n" - message += result.error + if not comp.nvcc_found: + message += "# Compilation failed\nNVCC could not be found.\n" + message += "This indicates a bug in the runner configuration, _not in your code_.\n" + message += "Please notify the server admins of this problem" await thread.send(message) return - comp = result.compile - run = result.run + # ok, we found nvcc + message += "# Compilation failed\n" + message += "Command " + message += f"```bash\n>{_limit_length(comp.command, 1000)}```\n" + message += f"exited with code **{comp.exit_code}**." - message = "" - if comp is not None and not comp.success: - if not comp.nvcc_found: - message += "# Compilation failed\nNVCC could not be found.\n" - message += "This indicates a bug in the runner configuration, _not in your code_.\n" - message += "Please notify the server admins of this problem" - await thread.send(message) - return + message = await _send_split_log(thread, message, "Compiler stderr", comp.stderr.strip()) - # ok, we found nvcc - message += "# Compilation failed\n" - message += "Command " - message += f"```bash\n>{_limit_length(comp.command, 1000)}```\n" - message += f"exited with code **{comp.exit_code}**." + if len(comp.stdout.strip()) > 0: + message = await _send_split_log(thread, message, "Compiler stdout", comp.stdout.strip()) - message = await _send_split_log(thread, message, "Compiler stderr", comp.stderr.strip()) + if len(message) != 0: + await thread.send(message) - if len(comp.stdout.strip()) > 0: - message = await _send_split_log(thread, message, "Compiler stdout", comp.stdout.strip()) - if len(message) != 0: - await thread.send(message) +async def _generate_crash_report(thread: discord.Thread, run: RunResult): + message = "# Running failed\n" + message += "Command " + message += f"```bash\n{_limit_length(run.command, 1000)}```\n" + message += f"exited with error code **{run.exit_code}** after {run.duration:.2} seconds." - return + if len(run.stderr.strip()) > 0: + message = await _send_split_log(thread, message, "Program stderr", run.stderr.strip()) - if not run.success: - message += "# Running failed\n" - message += "Command " - message += f"```bash\n{_limit_length(run.command, 1000)}```\n" - message += f"exited with error code **{run.exit_code}** after {run.duration:.2} seconds." + if len(run.stdout.strip()) > 0: + message = await _send_split_log(thread, message, "Program stdout", run.stdout.strip()) + + if len(message) != 0: + await thread.send(message) - if len(run.stderr.strip()) > 0: - message = await _send_split_log(thread, message, "Program stderr", run.stderr.strip()) - if len(run.stdout.strip()) > 0: - message = await _send_split_log(thread, message, "Program stdout", run.stdout.strip()) +async def _generate_test_report(thread: discord.Thread, run: RunResult): + message = "# Testing failed\n" + message += "Command " + message += f"```bash\n{_limit_length(run.command, 1000)}```\n" + message += f"ran successfully in {run.duration:.2} seconds, but did not pass all tests.\n" - if len(message) != 0: - await thread.send(message) + if len(run.stderr.strip()) > 0: + message = await _send_split_log(thread, message, "Program stderr", run.stderr.strip()) + if len(run.stdout.strip()) > 0: + message = await _send_split_log(thread, message, "Program stdout", run.stdout.strip()) + + if len(message) != 0: + await thread.send(message) + + # TODO dedicated "error" entry in our results that gets + # populated by check_implementation + return + + +async def generate_report(thread: discord.Thread, result: FullResult, has_tests: bool): # noqa: C901 + if not result.success: + message = "# Failure\n" + message += result.error + await thread.send(message) return - if has_tests: - if not run.passed: - message += "# Testing failed\n" - message += "Command " - message += f"```bash\n{_limit_length(run.command, 1000)}```\n" - message += ( - f"ran successfully in {run.duration:.2} seconds, but did not pass all tests.\n" - ) - - if len(run.stderr.strip()) > 0: - message = await _send_split_log( - thread, message, "Program stderr", run.stderr.strip() - ) - - if len(run.stdout.strip()) > 0: - message = await _send_split_log( - thread, message, "Program stdout", run.stdout.strip() - ) - - if len(message) != 0: - await thread.send(message) - - # TODO dedicated "error" entry in our results that gets - # populated by check_implementation - return + comp = result.compile + run = result.run + + if comp is not None and not comp.success: + await _generate_compile_report(thread, comp) + return + + if not run.success: + await _generate_crash_report(thread, run) + return + + message = "" + if has_tests and not run.passed: + await _generate_test_report(thread, run) + return # OK, we were successful message += "# Success!\n" From b4e8c4f41669127960b30ff73153db59a6d5b808 Mon Sep 17 00:00:00 2001 From: ngc92 <7938269+ngc92@users.noreply.github.com> Date: Tue, 21 Jan 2025 19:27:20 +0200 Subject: [PATCH 2/2] fix --- src/discord-cluster-manager/report.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/discord-cluster-manager/report.py b/src/discord-cluster-manager/report.py index 08239f5..4864a04 100644 --- a/src/discord-cluster-manager/report.py +++ b/src/discord-cluster-manager/report.py @@ -34,7 +34,7 @@ async def _send_split_log(thread: discord.Thread, partial_message: str, header: # now, format the chunks for i, chunk in enumerate(chunks): - partial_message += f"\n\n## {header} ({i+1}/{len(chunks)}):\n" + partial_message = f"\n\n## {header} ({i+1}/{len(chunks)}):\n" partial_message += f"```\n{_limit_length(chunk, 1900)}```" await thread.send(partial_message)