Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/nextflow/co2footprint/Metrics/Calculator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class Calculator {
if (values.isEmpty() || weights.isEmpty()) { return null }

Number norm = weights.sum() as Number
if (norm == 0) { return null }

Number total = new BigDecimal(0)
values.eachWithIndex { Object value, Integer index ->
total += value * weights[index]
Expand Down
15 changes: 8 additions & 7 deletions src/main/nextflow/co2footprint/Records/CiRecordCollector.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,6 @@ class CiRecordCollector {
Long duration = start.until(end, ChronoUnit.MILLIS)
List<LocalDateTime> timestamps = timeCIs.keySet().toList().sort()

/**
* Return the weight of as the covered fraction of the total duration
*/
Closure<Double> getWeight = { LocalDateTime time1, LocalDateTime time2 ->
(time1.until(time2, ChronoUnit.MILLIS)) / duration
}

if (!timestamps) {
String message = "No carbon intensity timestamps are available in timeCIs for traceRecord '${trace}' (${start}-${end})."
log.error(message)
Expand All @@ -142,6 +135,14 @@ class CiRecordCollector {
activeTimestamp = timestamps.min()
}

if (duration == 0) {
return timeCIs.get(activeTimestamp) as Double
}

Closure<Double> getWeight = { LocalDateTime time1, LocalDateTime time2 ->
(time1.until(time2, ChronoUnit.MILLIS)) / duration
}

List<LocalDateTime> changesDuringRun = timestamps.findAll { LocalDateTime time -> time > start && time < end }

// Calculation of average carbon intensity
Expand Down
2 changes: 2 additions & 0 deletions src/test/nextflow/co2footprint/Metrics/CalculatorTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ class CalculatorTest extends Specification {
[null, 2] || [null, 2] || 2.0
[null, null] || [null, null] || null
[1.0, 1.0, 1.0] || [0.1, 0.5, 0.4] || 1.0
[50.0, 80.0] || [0, 0] || null
[50.0] || [0] || null
}
}
Loading