diff --git a/power_logger.py b/power_logger.py index 5c18fcd..d9b7f49 100755 --- a/power_logger.py +++ b/power_logger.py @@ -22,6 +22,7 @@ import threading import logging import select +import math from datetime import timezone from pathlib import Path @@ -260,6 +261,22 @@ def find_top_processes(data: list, elapsed_ns:int): 'cputime_ns': ((p['cputime_ms_per_s'] * 1_000_000) / 1_000_000_000) * elapsed_ns, } +class RemoveNaNEncoder(json.JSONEncoder): + def encode(self, obj): + def remove_nan(o): + if isinstance(o, dict): + return {remove_nan(k): remove_nan(v) for k, v in o.items() + if not ((isinstance(k, float) and math.isnan(k)) or + (isinstance(v, float) and math.isnan(v)))} + elif isinstance(o, list): + return [remove_nan(v) for v in o + if not (isinstance(v, float) and math.isnan(v))] + else: + return o + cleaned_obj = remove_nan(obj) + return super(RemoveNaNEncoder, self).encode(cleaned_obj) + + def parse_powermetrics_output(output: str): global stats @@ -278,7 +295,7 @@ def parse_powermetrics_output(output: str): logging.error(f"XML Error:\n{data}") raise exc - compressed_data = zlib.compress(str(json.dumps(data)).encode()) + compressed_data = zlib.compress(str(json.dumps(data, cls=RemoveNaNEncoder)).encode()) compressed_data_str = base64.b64encode(compressed_data).decode() c.execute('INSERT INTO measurements (time, data, uploaded) VALUES (?, ?, 0)',