Skip to content

Commit e5a8718

Browse files
authored
Merging of statistics must cope with execution time stats being unavailable (#1381)
Closes #1380.
1 parent 2f02555 commit e5a8718

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

cms/grading/steps/stats.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,40 @@ def merge_execution_stats(
103103
if first_stats is None:
104104
return second_stats.copy()
105105

106+
Stat = typing.TypeVar('Stat', int, float)
107+
108+
def safe_sum(x: Stat | None, y: Stat | None) -> Stat | None:
109+
if x is None:
110+
return y
111+
elif y is None:
112+
return x
113+
else:
114+
return x + y
115+
116+
def safe_max(x: Stat | None, y: Stat | None) -> Stat | None:
117+
if x is None:
118+
return y
119+
elif y is None:
120+
return x
121+
else:
122+
return max(x, y)
123+
106124
ret = first_stats.copy()
107-
ret["execution_time"] += second_stats["execution_time"]
125+
ret["execution_time"] = safe_sum(ret["execution_time"],
126+
second_stats["execution_time"])
108127

109128
if concurrent:
110-
ret["execution_wall_clock_time"] = max(
129+
ret["execution_wall_clock_time"] = safe_max(
111130
ret["execution_wall_clock_time"],
112131
second_stats["execution_wall_clock_time"])
113-
ret["execution_memory"] += second_stats["execution_memory"]
132+
ret["execution_memory"] = safe_sum(ret["execution_memory"],
133+
second_stats["execution_memory"])
114134
else:
115-
ret["execution_wall_clock_time"] += \
116-
second_stats["execution_wall_clock_time"]
117-
ret["execution_memory"] = max(ret["execution_memory"],
118-
second_stats["execution_memory"])
135+
ret["execution_wall_clock_time"] = safe_sum(
136+
ret["execution_wall_clock_time"],
137+
second_stats["execution_wall_clock_time"])
138+
ret["execution_memory"] = safe_max(ret["execution_memory"],
139+
second_stats["execution_memory"])
119140

120141
if first_stats["exit_status"] == Sandbox.EXIT_OK:
121142
ret["exit_status"] = second_stats["exit_status"]

0 commit comments

Comments
 (0)