44from typing import List
55
66import consts
7- import discord
87from run_eval import CompileResult , EvalResult , FullResult , RunResult , SystemInfo
9- from utils import format_time
10-
11-
12- def _limit_length (text : str , maxlen : int ):
13- if len (text ) > maxlen :
14- return text [: maxlen - 6 ] + " [...]"
15- else :
16- return text
17-
18-
19- async def _send_split_log (thread : discord .Thread , partial_message : str , header : str , log : str ):
20- if len (partial_message ) + len (log ) + len (header ) < 1900 :
21- partial_message += f"\n \n ## { header } :\n "
22- partial_message += f"```\n { log } ```"
23- return partial_message
24- else :
25- # send previous chunk
26- if len (partial_message ) > 0 :
27- await thread .send (partial_message )
28- lines = log .splitlines ()
29- chunks = []
30- partial_message = ""
31- for line in lines :
32- if len (partial_message ) + len (line ) < 1900 :
33- partial_message += line + "\n "
34- else :
35- if partial_message != "" :
36- chunks .append (partial_message )
37- partial_message = line
38-
39- if partial_message != "" :
40- chunks .append (partial_message )
41-
42- # now, format the chunks
43- for i , chunk in enumerate (chunks ):
44- partial_message = f"\n \n ## { header } ({ i + 1 } /{ len (chunks )} ):\n "
45- partial_message += f"```\n { _limit_length (chunk , 1900 )} ```"
46- await thread .send (partial_message )
47-
48- return ""
8+ from utils import format_time , limit_length
499
5010
5111@dataclasses .dataclass
@@ -95,7 +55,7 @@ def _generate_compile_report(reporter: "RunResultReport", comp: CompileResult):
9555 # ok, we found nvcc
9656 message += "# Compilation failed\n "
9757 message += "Command "
98- message += f"```bash\n >{ _limit_length (comp .command , 1000 )} ```\n "
58+ message += f"```bash\n >{ limit_length (comp .command , 1000 )} ```\n "
9959 message += f"exited with code **{ comp .exit_code } **."
10060 reporter .add_text (message )
10161
@@ -108,7 +68,7 @@ def _generate_compile_report(reporter: "RunResultReport", comp: CompileResult):
10868def _generate_crash_report (reporter : "RunResultReport" , run : RunResult ):
10969 message = "# Running failed\n "
11070 message += "Command "
111- message += f"```bash\n { _limit_length (run .command , 1000 )} ```\n "
71+ message += f"```bash\n { limit_length (run .command , 1000 )} ```\n "
11272 if run .exit_code == consts .ExitCode .TIMEOUT_EXPIRED :
11373 message += f"**timed out** after { float (run .duration ):.2f} seconds."
11474 else :
@@ -127,7 +87,7 @@ def _generate_crash_report(reporter: "RunResultReport", run: RunResult):
12787def _generate_test_report (reporter : "RunResultReport" , run : RunResult ):
12888 message = "# Testing failed\n "
12989 message += "Command "
130- message += f"```bash\n { _limit_length (run .command , 1000 )} ```\n "
90+ message += f"```bash\n { limit_length (run .command , 1000 )} ```\n "
13191 message += f"ran successfully in { run .duration :.2f} seconds, but did not pass all tests.\n "
13292 reporter .add_text (message )
13393
@@ -392,7 +352,7 @@ def generate_report(result: FullResult) -> RunResultReport: # noqa: C901
392352 # OK, we were successful
393353 message = "# Success!\n "
394354 message += "Command "
395- message += f"```bash\n { _limit_length (run .command , 1000 )} ```\n "
355+ message += f"```bash\n { limit_length (run .command , 1000 )} ```\n "
396356 message += f"ran successfully in { run .duration :.2f} seconds.\n "
397357 report .add_text (message )
398358
@@ -407,34 +367,6 @@ def generate_report(result: FullResult) -> RunResultReport: # noqa: C901
407367 return report
408368
409369
410- class MultiProgressReporter :
411- def __init__ (self , interaction : discord .Interaction , header : str ):
412- self .header = header
413- self .runs = []
414- self .interaction = interaction
415-
416- async def show (self ):
417- await self ._update_message ()
418-
419- def add_run (self , title : str ) -> "RunProgressReporter" :
420- rpr = RunProgressReporterDiscord (self , self .interaction , title )
421- self .runs .append (rpr )
422- return rpr
423-
424- def make_message (self ):
425- formatted_runs = []
426- for run in self .runs :
427- formatted_runs .append (run .get_message ())
428-
429- return str .join ("\n \n " , [f"# { self .header } " ] + formatted_runs )
430-
431- async def _update_message (self ):
432- if self .interaction is None :
433- return
434-
435- await self .interaction .edit_original_response (content = self .make_message (), view = None )
436-
437-
438370class RunProgressReporter :
439371 def __init__ (self , title : str ):
440372 # short report
@@ -465,57 +397,3 @@ async def display_report(self, title: str, report: RunResultReport):
465397
466398 async def _update_message (self ):
467399 raise NotImplementedError ()
468-
469-
470- class RunProgressReporterDiscord (RunProgressReporter ):
471- def __init__ (
472- self ,
473- root : MultiProgressReporter ,
474- interaction : discord .Interaction ,
475- title : str ,
476- ):
477- super ().__init__ (title = title )
478- self .root = root
479- self .interaction = interaction
480-
481- async def _update_message (self ):
482- await self .root ._update_message ()
483-
484- async def display_report (self , title : str , report : RunResultReport ):
485- thread = await self .interaction .channel .create_thread (
486- name = title ,
487- type = discord .ChannelType .private_thread ,
488- auto_archive_duration = 1440 ,
489- )
490- await thread .add_user (self .interaction .user )
491- message = ""
492- for part in report .data :
493- if isinstance (part , Text ):
494- if len (message ) + len (part .text ) > 1900 :
495- await thread .send (message )
496- message = ""
497- message += part .text
498- elif isinstance (part , Log ):
499- message = await _send_split_log (thread , message , part .header , part .content )
500-
501- if len (message ) > 0 :
502- await thread .send (message )
503-
504- await self .push (f"See results at { thread .jump_url } " )
505-
506-
507- class RunProgressReporterAPI (RunProgressReporter ):
508- def __init__ (self , title : str ):
509- super ().__init__ (title = title )
510- self .long_report = ""
511-
512- async def _update_message (self ):
513- pass
514-
515- async def display_report (self , title : str , report : RunResultReport ):
516- for part in report .data :
517- if isinstance (part , Text ):
518- self .long_report += part .text
519- elif isinstance (part , Log ):
520- self .long_report += f"\n \n ## { part .header } :\n "
521- self .long_report += f"```\n { part .content } ```"
0 commit comments