Skip to content

Commit

Permalink
Merge pull request #29 from green-coding-solutions/fixes-NaN
Browse files Browse the repository at this point in the history
Removes NaN values
  • Loading branch information
ArneTR authored Sep 27, 2024
2 parents ea2f597 + db8d740 commit c5eff90
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 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,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
Expand All @@ -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)',
Expand Down

0 comments on commit c5eff90

Please sign in to comment.