Skip to content

Commit

Permalink
Removes NaN values
Browse files Browse the repository at this point in the history
  • Loading branch information
ribalba committed Sep 25, 2024
1 parent ea2f597 commit f7d293a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion power_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import threading
import logging
import select
import math

from datetime import timezone
from pathlib import Path
Expand Down Expand Up @@ -260,6 +261,19 @@ 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 {k: remove_nan(v) for k, v in o.items()
if not (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
Expand All @@ -278,7 +292,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)',
Expand Down

0 comments on commit f7d293a

Please sign in to comment.