Skip to content

Commit

Permalink
Bugfix: Wrong unit for ugCO2 badges
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneTR committed Aug 8, 2023
1 parent bf4c191 commit 2c2c71e
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions api/api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,23 @@

def rescale_energy_value(value, unit):
# We only expect values to be mJ for energy!
if unit in ['mJ', 'ug'] or unit.startswith('ugCO2e/'):
unit_type = unit[1:]

energy_rescaled = [value, unit]
if unit != 'mJ' and not unit.startswith('ugCO2e/'):
raise RuntimeError('Unexpected unit occured for energy rescaling: ', unit)

# pylint: disable=multiple-statements
if value > 1_000_000_000: energy_rescaled = [value/(10**12), f"G{unit_type}"]
elif value > 1_000_000_000: energy_rescaled = [value/(10**9), f"M{unit_type}"]
elif value > 1_000_000: energy_rescaled = [value/(10**6), f"k{unit_type}"]
elif value > 1_000: energy_rescaled = [value/(10**3), f"m{unit_type}"]
elif value < 0.001: energy_rescaled = [value*(10**3), f"n{unit_type}"]
unit_type = unit[1:]

else:
raise RuntimeError('Unexpected unit occured for energy rescaling: ', unit)
if unit.startswith('ugCO2e'): # bring also to mg
value = value / (10**3)
unit = f"m{unit_type}"

# pylint: disable=multiple-statements
if value > 1_000_000_000: return [value/(10**12), f"G{unit_type}"]
if value > 1_000_000_000: return [value/(10**9), f"M{unit_type}"]
if value > 1_000_000: return [value/(10**6), f"k{unit_type}"]
if value > 1_000: return [value/(10**3), f"{unit_type}"]
if value < 0.001: return [value*(10**3), f"n{unit_type}"]

return energy_rescaled
return [value, unit] # default, no change

def is_valid_uuid(val):
try:
Expand Down

0 comments on commit 2c2c71e

Please sign in to comment.