Skip to content
Closed
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
22 changes: 20 additions & 2 deletions codecarbon/core/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# from httpx import AsyncClient
import dataclasses
import json
from datetime import timedelta, tzinfo
from datetime import datetime, timedelta, tzinfo

import requests

Expand All @@ -33,6 +33,22 @@ def get_datetime_with_timezone():
return str(arrow.now().isoformat())


def _measurement_timestamp(carbon_emission: dict) -> str:
"""
Offset-aware ISO timestamp of *when the measurement was taken*, taken from
EmissionsData.timestamp. Falls back to now for hand-built payloads that
carry no usable timestamp.
"""
try:
return (
datetime.fromisoformat(carbon_emission["timestamp"])
.astimezone()
.isoformat()
)
except (KeyError, TypeError, ValueError):
return get_datetime_with_timezone()


class ApiClient: # (AsyncClient)
"""
This class call the Code Carbon API
Expand Down Expand Up @@ -195,7 +211,7 @@ def add_emission(self, carbon_emission: dict):
)
return False
emission = EmissionCreate(
timestamp=get_datetime_with_timezone(),
timestamp=_measurement_timestamp(carbon_emission),
run_id=self.run_id,
duration=int(carbon_emission["duration"]),
emissions_sum=carbon_emission["emissions"],
Expand Down Expand Up @@ -237,6 +253,8 @@ def _create_run(self, experiment_id: str):
return None
try:
run = RunCreate(
# "now" is correct here: a run's timestamp is its creation time,
# unlike an emission's, which is its measurement time.
timestamp=get_datetime_with_timezone(),
experiment_id=experiment_id,
os=self.conf.get("os"),
Expand Down
40 changes: 40 additions & 0 deletions tests/test_api_call.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dataclasses
import unittest
from datetime import datetime
from uuid import uuid4

import requests
Expand Down Expand Up @@ -261,6 +262,45 @@ def test_add_emission_skips_short_duration(self):
)
)

def test_add_emission_keeps_measurement_timestamp(self):
"""The row must carry when it was measured, not when it was sent."""
payload = {
"duration": 10,
"emissions": 1.0,
"emissions_rate": 1.0,
"cpu_power": 1.0,
"gpu_power": 0.0,
"ram_power": 0.5,
"cpu_energy": 0.1,
"gpu_energy": 0.0,
"ram_energy": 0.1,
"energy_consumed": 0.2,
}
with requests_mock.Mocker() as m:
m.post("http://test.com/emissions", status_code=201)
api = ApiClient(
endpoint_url="http://test.com",
experiment_id="exp-1",
conf=conf,
create_run_automatically=False,
)
api.run_id = "run-1"

# naive timestamp, as produced by EmissionsData
assert api.add_emission({**payload, "timestamp": "2020-01-01T00:00:00"})
sent = datetime.fromisoformat(m.last_request.json()["timestamp"])
self.assertEqual(
sent.replace(tzinfo=None).isoformat(), "2020-01-01T00:00:00"
)
self.assertIsNotNone(sent.tzinfo)

# missing / unparseable timestamps fall back to now
for bad in ({}, {"timestamp": None}, {"timestamp": "222"}):
assert api.add_emission({**payload, **bad})
sent = datetime.fromisoformat(m.last_request.json()["timestamp"])
self.assertIsNotNone(sent.tzinfo)
self.assertGreater(sent.year, 2020)

def test_add_emission_raises_on_unsuccessful_post(self):
with requests_mock.Mocker() as m:
m.post("http://test.com/emissions", text="bad", status_code=500)
Expand Down
Loading