| 197 | return r.json() |
| 198 | |
| 199 | def add_emission(self, carbon_emission: dict): |
| 200 | assert self.experiment_id is not None |
| 201 | if self.run_id is None: |
| 202 | logger.warning( |
| 203 | "ApiClient.add_emission() need a run_id : the initial call may " |
| 204 | + "have failed. Retrying..." |
| 205 | ) |
| 206 | self._create_run(self.experiment_id) |
| 207 | if self.run_id is None: |
| 208 | logger.error( |
| 209 | "ApiClient.add_emission still no run_id, aborting for this time !" |
| 210 | ) |
| 211 | return False |
| 212 | if carbon_emission["duration"] < 1: |
| 213 | logger.warning( |
| 214 | "ApiClient : emissions not sent because of a duration smaller than 1." |
| 215 | ) |
| 216 | return False |
| 217 | emission = EmissionCreate( |
| 218 | timestamp=get_datetime_with_timezone(), |
| 219 | run_id=self.run_id, |
| 220 | duration=int(carbon_emission["duration"]), |
| 221 | emissions_sum=carbon_emission["emissions"], |
| 222 | emissions_rate=carbon_emission["emissions_rate"], |
| 223 | cpu_power=carbon_emission["cpu_power"], |
| 224 | gpu_power=carbon_emission["gpu_power"], |
| 225 | ram_power=carbon_emission["ram_power"], |
| 226 | cpu_energy=carbon_emission["cpu_energy"], |
| 227 | gpu_energy=carbon_emission["gpu_energy"], |
| 228 | ram_energy=carbon_emission["ram_energy"], |
| 229 | energy_consumed=carbon_emission["energy_consumed"], |
| 230 | cpu_utilization_percent=carbon_emission.get("cpu_utilization_percent"), |
| 231 | gpu_utilization_percent=carbon_emission.get("gpu_utilization_percent"), |
| 232 | ram_utilization_percent=carbon_emission.get("ram_utilization_percent"), |
| 233 | wue=carbon_emission.get("wue", 0), |
| 234 | ) |
| 235 | try: |
| 236 | payload = dataclasses.asdict(emission) |
| 237 | url = self.url + "/emissions" |
| 238 | headers = self._get_headers() |
| 239 | r = requests.post(url=url, json=payload, timeout=2, headers=headers) |
| 240 | if r.status_code != 201: |
| 241 | self._log_error(url, payload, r) |
| 242 | return False |
| 243 | logger.debug(f"ApiClient - Successful upload emission {payload} to {url}") |
| 244 | except Exception as e: |
| 245 | logger.error(e, exc_info=True) |
| 246 | return False |
| 247 | return True |
| 248 | |
| 249 | def _create_run(self, experiment_id: str): |
| 250 | """ |