Skip to content

Commit dacf37b

Browse files
authored
Merge pull request #385 from nadnein/fix/zero_div_error
Fix: zero division crashes when task realtime is 0
2 parents 7c6dee3 + d3d3936 commit dacf37b

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/main/nextflow/co2footprint/Metrics/Calculator.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class Calculator {
6161
if (values.isEmpty() || weights.isEmpty()) { return null }
6262

6363
Number norm = weights.sum() as Number
64+
if (norm == 0) { return null }
65+
6466
Number total = new BigDecimal(0)
6567
values.eachWithIndex { Object value, Integer index ->
6668
total += value * weights[index]

src/main/nextflow/co2footprint/Records/CiRecordCollector.groovy

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,6 @@ class CiRecordCollector {
124124
Long duration = start.until(end, ChronoUnit.MILLIS)
125125
List<LocalDateTime> timestamps = timeCIs.keySet().toList().sort()
126126

127-
/**
128-
* Return the weight of as the covered fraction of the total duration
129-
*/
130-
Closure<Double> getWeight = { LocalDateTime time1, LocalDateTime time2 ->
131-
(time1.until(time2, ChronoUnit.MILLIS)) / duration
132-
}
133-
134127
if (!timestamps) {
135128
String message = "No carbon intensity timestamps are available in timeCIs for traceRecord '${trace}' (${start}-${end})."
136129
log.error(message)
@@ -142,6 +135,14 @@ class CiRecordCollector {
142135
activeTimestamp = timestamps.min()
143136
}
144137

138+
if (duration == 0) {
139+
return timeCIs.get(activeTimestamp) as Double
140+
}
141+
142+
Closure<Double> getWeight = { LocalDateTime time1, LocalDateTime time2 ->
143+
(time1.until(time2, ChronoUnit.MILLIS)) / duration
144+
}
145+
145146
List<LocalDateTime> changesDuringRun = timestamps.findAll { LocalDateTime time -> time > start && time < end }
146147

147148
// Calculation of average carbon intensity

src/test/nextflow/co2footprint/Metrics/CalculatorTest.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ class CalculatorTest extends Specification {
1616
[null, 2] || [null, 2] || 2.0
1717
[null, null] || [null, null] || null
1818
[1.0, 1.0, 1.0] || [0.1, 0.5, 0.4] || 1.0
19+
[50.0, 80.0] || [0, 0] || null
20+
[50.0] || [0] || null
1921
}
2022
}

0 commit comments

Comments
 (0)