Ignore NaN values for influxdb#14347
Conversation
| # Infinity and NaN are not valid floats in InfluxDB | ||
| if key in json['fields']: | ||
| converted = json['fields'][key] | ||
| if math.isinf(converted) or math.isnan(converted): |
There was a problem hiding this comment.
Not sure if a problem here, but math.is... will raise ValueErrors if not passes floats (for example strings) which was not the previous behavior.
Looking at the code, this would probably only be triggered if an attribute is called something_str but still it might be good to have a check in place if there are other edge cases.
There was a problem hiding this comment.
It seems to be TypeError, not ValueError(?). I now added a check for that but I don't think it is actually possible to hit, so no test.
| RE_DECIMAL.sub('', new_value)) | ||
|
|
||
| # Infinity is not a valid float in InfluxDB | ||
| if (key, float("inf")) in json['fields'].items(): |
There was a problem hiding this comment.
Whoa, this line was pretty inefficient. If I understand correctly this would previously have stepped through the entire dictionary. It's not a big data structure but still...
There was a problem hiding this comment.
I thought it would be efficient because items() creates a view object, not a list.
There was a problem hiding this comment.
It still has to iterate through all items. New check is constant time 👍
There was a problem hiding this comment.
Hmm, I looked it up now and I still think the previous implementation was just as constant.
PEP 3106 says that (Python 3) .items() is "set-like" and specifies __contains__ with this pseudocode:
def __contains__(self, (key, value)):
return key in self.__d and self.__d[key] == valueThere was a problem hiding this comment.
Oh wow, thanks for the info. You learn something new every day :D
* Ignore NaN values for influxdb * Catch TypeError
* Ignore NaN values for influxdb * Catch TypeError
Description:
This extends the InfluxDB invalid-value filter to also handle "nan" values.
Related issue (if applicable): reported in forum
Checklist:
tox.If the code does not interact with devices: