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

quit thread on close event #30

Merged
merged 1 commit into from
Mar 16, 2024
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
74 changes: 47 additions & 27 deletions src/core/cpuCO2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from utils.io import safe_read_config

# Read YAML file
config = safe_read_config('config.yml')
config = safe_read_config("config.yml")
config_appearence = config["Appearance"]


def cpu_co2(country_code = None) -> None:
def cpu_co2(country_code=None) -> None:
"""Function to run the core of the app: compare CPU usage with CO2 emissions"""

# Use manual country code if set, otherwise use geolocalizaion
Expand All @@ -19,51 +19,66 @@ def cpu_co2(country_code = None) -> None:
else:
# Get location
geolocation = api_calls.get_location()
lon = geolocation['longitude']
lat = geolocation['latitude']
lon = geolocation["longitude"]
lat = geolocation["latitude"]

# Retrieve data from Co2Signal API
co2data = api_calls.get_request_co2signal(lon, lat)
country_code = co2data['countryCode']
country_code = co2data["countryCode"]

try:
# Extract relevant measures
print('\nRetrieving Co2Signal data...')
carbon_intensity = co2data['data']['carbonIntensity']
carbon_intensity_unit = co2data['units']['carbonIntensity']
fossil_percentage = round(co2data['data']['fossilFuelPercentage'],2)
print("- Selected Country: {0} \n- Carbon Intensity: {1} ({2}) \n- Percentage of fossil fuel: {3}%".format(country_code, carbon_intensity, carbon_intensity_unit, fossil_percentage))
print("\nRetrieving Co2Signal data...")
carbon_intensity = co2data["data"]["carbonIntensity"]
carbon_intensity_unit = co2data["units"]["carbonIntensity"]
fossil_percentage = round(co2data["data"]["fossilFuelPercentage"], 2)
print(
"- Selected Country: {0} \n- Carbon Intensity: {1} ({2}) \n- Percentage of fossil fuel: {3}%".format(
country_code, carbon_intensity, carbon_intensity_unit, fossil_percentage
)
)

# Retrieve cpu info
print('\nRetrieving CPU data...')
print("\nRetrieving CPU data...")
cpu_retrieval_time = config["Functional"]["cpu_retrieval_time"]
# print(config_file.Functional.cpu_retrieval_time)
cpu_info = cpu.get_cpu_info()
print("- Current CPU: {0}".format(cpu_info))

# Launch plot
app = QApplication(sys.argv)
qwidget_params = '''
qwidget_params = """
QWidget {{
font-size: {int}px;
}}
'''.format(int=config_appearence['Window']["window_font_size"])
""".format(
int=config_appearence["Window"]["window_font_size"]
)
app.setStyleSheet(qwidget_params)
myApp = PlotWindowApp(cpu.get_cpu_tdp(cpu_info), carbon_intensity, cpu_retrieval_time, get_interval_emissions, config_appearence)
myApp = PlotWindowApp(
cpu.get_cpu_tdp(cpu_info),
carbon_intensity,
cpu_retrieval_time,
get_interval_emissions,
config_appearence,
)
myApp.show()
print('\nPlot window in execution...')
print("\nPlot window in execution...")
try:
sys.exit(app.exec_())
except SystemExit:
print('Closing Window...')
print("Goodbye!")

except KeyError:
print('Sorry, your country is not present in the Electricity Map ' +
'database, it is therefore not possible to retrieve CO2 emissions data.')
print(
"Sorry, your country is not present in the Electricity Map "
+ "database, it is therefore not possible to retrieve CO2 emissions data."
)


def combine_cpu_CO2(cpu_usage: float, usage_time: float,
cpu_tdp: float, co2_intensity: int) -> float:
def combine_cpu_CO2(
cpu_usage: float, usage_time: float, cpu_tdp: float, co2_intensity: int
) -> float:
"""
Combine CPU usage, CPU TDP and CO2 intensity to compute CO2 consumption.

Expand All @@ -80,19 +95,22 @@ def combine_cpu_CO2(cpu_usage: float, usage_time: float,
"""

# transfrom Watt in kWatt
cpu_tdp_KW = cpu_tdp/1000
cpu_tdp_KW = cpu_tdp / 1000

# transfrom kW in kWh and take a certain proportion of it
used_KWh = cpu_usage * cpu_tdp_KW / 100 * (usage_time/3600)
used_KWh = cpu_usage * cpu_tdp_KW / 100 * (usage_time / 3600)

# get the g of Co2
co2_emissions = round(used_KWh * co2_intensity, config["Functional"]["co2_emissions_precision"])
co2_emissions = round(
used_KWh * co2_intensity, config["Functional"]["co2_emissions_precision"]
)

return co2_emissions


def get_interval_emissions(cpu_tdp: float, co2_intensity: int,
time_frequency: int) -> float:
def get_interval_emissions(
cpu_tdp: float, co2_intensity: int, time_frequency: int
) -> float:
"""
From a given cpu_tdp and co2_intensity return the co2 emissions computing the cpu_usage at current time.

Expand All @@ -107,9 +125,11 @@ def get_interval_emissions(cpu_tdp: float, co2_intensity: int,
"""

# Retrieve cpu usage
cpu_usage = cpu.get_cpu_usage(time_frequency-config["Functional"]["cpu_retrieval_time_decrement"])
cpu_usage = cpu.get_cpu_usage(
time_frequency - config["Functional"]["cpu_retrieval_time_decrement"]
)

# Combine Co2 with CPU usage
co2_emissions = combine_cpu_CO2(cpu_usage, time_frequency, cpu_tdp, co2_intensity)

return co2_emissions
return co2_emissions
Loading
Loading