Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement first outline of real time CI retrieval from Electricity maps #96

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nextflow.co2footprint

import groovy.json.JsonSlurper
import groovy.transform.PackageScope
import groovy.util.logging.Slf4j

Expand Down Expand Up @@ -37,6 +38,7 @@ class CO2FootprintConfig {
private Boolean ignoreCpuModel = false
private Double powerdrawCpuDefault = 12.0
private String customCpuTdpFile = null
private String EMToken = null

// Retrieve CI value from file containing CI values for different locations
protected Double retrieveCi(String location) {
Expand All @@ -59,6 +61,31 @@ class CO2FootprintConfig {
return localCi
}

protected Double getRealTimeCI(String location, EMToken) {
// TODO Use location in API curl
// TODO How to resolve the location!
if (EMToken) {
def API_response = ['bash', '-c', "curl -H \"auth-token: $EMToken\" https://api.electricitymap.org/v3/carbon-intensity/latest?zone=DE"].execute()
API_response.waitFor()
Double realTimeCi
try {
// response will result in empty string if offline
def EM_json_result = (Map) new JsonSlurper().parseText(API_response.text)
realTimeCi = EM_json_result.carbonIntensity as Double
}
catch (Exception e) {
// If offline use tabular data
log.warn "Could not retrieve real time data from https://app.electricitymaps.com/map/24h, check if you have internet access. The nf-co2footprint report will include an average of your location."
realTimeCi = retrieveCi(location)
}
return realTimeCi
}
else{
log.warn "Could not retrieve real time data from https://app.electricitymaps.com/map/24h, to access real time data please register and use the access token within the configuration of the plugin @EMToken. The nf-co2footprint report will include an average of your location."
return retrieveCi(location)
}
}

// Load user provided file containing custom TDP values for different CPU models
protected void loadCustomCpuTdpData(Map<String, Double> data, String customCpuTdpFile) {
new File(customCpuTdpFile).withReader(){ reader ->
Expand Down Expand Up @@ -95,7 +122,7 @@ class CO2FootprintConfig {
ci = config.ci
}
if (config.location) {
ci = retrieveCi(config.location)
ci = getRealTimeCI(config.location, config.EMToken)
location = config.location
}
if (config.pue) {
Expand Down Expand Up @@ -125,6 +152,7 @@ class CO2FootprintConfig {
Double getPowerdrawMem() { powerdrawMem }
Double getPowerdrawCpuDefault() { powerdrawCpuDefault }
String getCustomCpuTdpFile() { customCpuTdpFile }
String getEMToken() { EMToken }

// Different functions to collect options for reporting, grouped by purpose
SortedMap<String, Object> collectInputFileOptions() {
Expand Down
Loading