Skip to content

Commit 3e6a59c

Browse files
committed
send profile results as attached files
1 parent 0e51cf5 commit 3e6a59c

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

src/kernelbot/discord_reporter.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import discord
2-
from discord_utils import _send_split_log
2+
from discord_utils import _send_split_log, _send_file
33

44
from libkernelbot.report import (
5+
File,
56
Link,
67
Log,
78
MultiProgressReporter,
@@ -70,6 +71,11 @@ async def display_report(self, title: str, report: RunResultReport):
7071
message += part.text
7172
elif isinstance(part, Log):
7273
message = await _send_split_log(thread, message, part.header, part.content)
74+
elif isinstance(part, File):
75+
if len(message) > 0:
76+
await thread.send(message)
77+
await _send_file(thread, part.message, part.name, part.content)
78+
message = ""
7379
elif isinstance(part, Link):
7480
if len(message) > 0:
7581
await thread.send(message)

src/kernelbot/discord_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import logging
3+
from io import BytesIO
34

45
import discord
56

@@ -136,3 +137,7 @@ async def _send_split_log(thread: discord.Thread, partial_message: str, header:
136137
await thread.send(partial_message)
137138

138139
return ""
140+
141+
142+
async def _send_file(thread: discord.Thread, message: str, name: str, file: bytes):
143+
await thread.send(message, file=discord.File(BytesIO(file), filename=name))

src/libkernelbot/launchers/github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async def run_submission( # noqa: C901
143143
# Update profile artifact to the actual download URL.
144144
# For the GitHub launcher the profile_artifact currently just contains
145145
# the name of the artifact.
146-
if profile_res is not None:
146+
if profile_res is not None and "profile-data" in index:
147147
profile_res.download_url = index["profile-data"].public_download_url
148148

149149
res = EvalResult(

src/libkernelbot/report.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,19 @@ class Link:
4343
url: str
4444

4545

46+
@dataclasses.dataclass
47+
class File:
48+
"""
49+
Link represents a file that gets attached to the report.
50+
"""
51+
name: str
52+
message: str
53+
content: bytes
54+
55+
4656
class RunResultReport:
4757
def __init__(self, data=None):
48-
self.data: List[Text | Log | Link] = data or []
58+
self.data: List[Text | Log | Link | File] = data or []
4959

5060
def add_text(self, section: str):
5161
self.data.append(Text(section))
@@ -56,6 +66,9 @@ def add_log(self, header: str, log: str):
5666
def add_link(self, title: str, text: str, url: str):
5767
self.data.append(Link(title, text, url))
5868

69+
def add_file(self, name: str, message: str, content: bytes):
70+
self.data.append(File(name, message, content))
71+
5972
def __repr__(self):
6073
return f"RunResultReport(data={self.data})"
6174

@@ -335,18 +348,28 @@ def generate_report(result: FullResult) -> RunResultReport: # noqa: C901
335348
if _handle_crash_report(report, prof_run):
336349
return report
337350

338-
report.add_log(
339-
"Profiling",
340-
make_profile_log(prof_run.run),
341-
)
342-
343-
if prof_run.profile is not None and prof_run.profile.download_url is not None:
344-
report.add_link(
345-
f"{prof_run.profile.profiler} profiling output",
346-
"Download from GitHub",
347-
prof_run.profile.download_url,
351+
if prof_run.profile.trace is not None:
352+
report.add_log(
353+
"Profiling",
354+
make_profile_log(prof_run.run),
348355
)
349356

357+
if prof_run.profile.download_url is not None:
358+
report.add_link(
359+
f"{prof_run.profile.profiler} profiling output",
360+
"Download from GitHub",
361+
prof_run.profile.download_url,
362+
)
363+
364+
for prof_run in profile_runs:
365+
if prof_run.profile is not None:
366+
if prof_run.profile.trace is not None:
367+
report.add_file(
368+
"profile.zip",
369+
make_profile_log(prof_run.run),
370+
base64.b64decode(prof_run.profile.trace),
371+
)
372+
350373
if "leaderboard" in runs:
351374
bench_run = runs["leaderboard"]
352375
if _handle_crash_report(report, bench_run):

src/libkernelbot/run_eval.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,14 @@ def _create_files(files: Optional[dict[str, str]]):
128128

129129

130130
def _directory_to_zip_bytes(directory_path) -> str:
131-
"""Create a zip archive and return as bas64 encoded bytes."""
132-
with tempfile.NamedTemporaryFile() as archive_path:
133-
shutil.make_archive(archive_path.name, 'zip', directory_path)
134-
data = archive_path.read()
131+
"""Create a zip archive and return as base64 encoded bytes."""
132+
with tempfile.TemporaryDirectory() as temp_dir:
133+
archive_path = os.path.join(temp_dir, 'archive')
134+
shutil.make_archive(archive_path, 'zip', directory_path)
135+
136+
with open(archive_path + '.zip', 'rb') as f:
137+
data = f.read()
138+
135139
return base64.b64encode(data).decode('utf-8')
136140

137141

0 commit comments

Comments
 (0)