Skip to content

Ignore NaN values for influxdb#14347

Merged
balloob merged 2 commits intohome-assistant:devfrom
amelchio:influxdb-nan
May 9, 2018
Merged

Ignore NaN values for influxdb#14347
balloob merged 2 commits intohome-assistant:devfrom
amelchio:influxdb-nan

Conversation

@amelchio
Copy link
Copy Markdown
Contributor

@amelchio amelchio commented May 8, 2018

Description:

This extends the InfluxDB invalid-value filter to also handle "nan" values.

Related issue (if applicable): reported in forum

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@amelchio amelchio added this to the 0.69 milestone May 8, 2018
Comment thread homeassistant/components/influxdb.py Outdated
# 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):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be efficient because items() creates a view object, not a list.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still has to iterate through all items. New check is constant time 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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] == value

Copy link
Copy Markdown
Member

@OttoWinter OttoWinter May 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, thanks for the info. You learn something new every day :D

@balloob balloob merged commit d43e6a2 into home-assistant:dev May 9, 2018
balloob pushed a commit that referenced this pull request May 9, 2018
* Ignore NaN values for influxdb

* Catch TypeError
@balloob balloob mentioned this pull request May 11, 2018
girlpunk pushed a commit to girlpunk/home-assistant that referenced this pull request Sep 4, 2018
* Ignore NaN values for influxdb

* Catch TypeError
@home-assistant home-assistant locked and limited conversation to collaborators Sep 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants