8000 Update InfluxDB to handle datetime objects and multiple decimal points by philhawthorne · Pull Request #8080 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Update InfluxDB to handle datetime objects and multiple decimal points #8080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 20, 2017

Conversation

philhawthorne
Copy link
Contributor

Description:

Updates the InfluxDB regex to ignore datetime objects being coverted
into float values.

Adds tests to the component to ensure datetime objects are corectly
handled.

Related issue (if applicable): fixes #8042

Checklist:

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

Updates the InfluxDB regex to ignore datetime objects being coverted
into float values.

Adds tests to the component to ensure datetime objects are corectly
handled.
@mention-bot
Copy link

@philhawthorne, thanks for your PR! By analyzing the history of the files in this pull request, we identified @fabaff, @titilambert and @kk7ds to be potential reviewers.

@@ -164,7 +167,8 @@ def test_event_listener(self, mock_client):
'battery_level': 99.0,
'temperature_str': '20c',
'temperature': 20.0,
'last_seen_str': 'Last seen 23 minutes ago'
'last_seen_str': 'Last seen 23 minutes ago',
'updated_at_str': '2017-01-01 00:00:00'

Choose a reason for hiding this comment

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

trailing whitespace

@@ -165,7 +167,8 @@ def influx_event_listener(event):
except (ValueError, TypeError):
new_key = "{}_str".format(key)
json_body[0]['fields'][new_key] = str(value)
if non_digit_tail.match(json_body[0]['fields'][new_key]):
if non_digit_tail.match(json_body[0]['fields'][new_key]) and not isinstance(

Choose a reason for hiding this comment

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

line too long (96 > 79 characters)

Fixes errors from Hound bot
@@ -165,7 +167,9 @@ def influx_event_listener(event):
except (ValueError, TypeError):
new_key = "{}_str".format(key)
json_body[0]['fields'][new_key] = str(value)
if non_digit_tail.match(json_body[0]['fields'][new_key]):
if non_digit_tail.match(
json_body[0]['fields'][new_key]) and not isinstance(

Choose a reason for hiding this comment

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

line too long (80 > 79 characters)

Changes the way InfluxDB handles values such as 1.2.3.4 to be 1.234 so
it stores in InfluxDB as a valid float value
json_body[0]['fields'][key] = float(
non_decimal.sub('', value))
if non_digit_tail.match(
json_body[0]['fields'][new_key]) and not isinstance(

Choose a reason for hiding this comment

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

line too long (80 > 79 characters)

json_body[0]['fields'][key] = float(
non_decimal.sub('', value))
if non_digit_tail.match(
json_body[0]['fields'][new_key]) and not isinstance(

Choose a reason for hiding this comment

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

line too long (80 > 79 characters)

Reduce the size of a line for the linter
@philhawthorne philhawthorne changed the title Update InfluxDB to handle datetime objects Update InfluxDB to handle datetime objects and multiple decimal points Jun 18, 2017
@azogue
Copy link
Member
azogue commented Jun 18, 2017

I can say this is working with the issue I've opened before (#8086)
Sorry, this patch doesn't work with #8086, now the traceback is:

2017-06-18 12:01:42 ERROR (Recorder) [homeassistant.core] Error doing job: Future exception was never retrieved
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/myinfluxdb.py", line 170, in influx_event_listener
    json_body[0]['fields'][key] = float(value)
ValueError: could not convert string to float: '8.5,8.1,9.6,8.9,9.6,9.3,3.7'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/myinfluxdb.py", line 179, in influx_event_listener
    non_decimal.sub('', value))
ValueError: could not convert string to float: '8.58.19.68.99.69.33.7'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/homeassistant/.pyenv/versions/3.5.2/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/myinfluxdb.py", line 182, in influx_event_listener
    re.sub(r'^([^.]*\.)(.*)$', decreplace, value))
ValueError: could not convert string to float: '8.5,81,96,89,96,93,37'

I'm thinking... what need is there for conversion if the string is already being stored?
In many cases, this 'forced' conversion will be generating nonsense data, right?

Would not it be better, simply, to give up conversion in such cases?

except (ValueError, TypeError):
    pass

@philhawthorne
Copy link
Contributor Author

Hey @azogue

So the original intention for this was to catch any components that are sending numeric data, but as strings. For example, "45%" instead of "45". When this happens, you can't use the string value in Grafana or other graph suites, as the data stored in InfluxDB isn't a numerical value.

This change detects values such as "45%", strips out the "%" character and leaves two values, "45" and a _str of "45%". This way, both values are stored in InfluxDB and can be used.< 8000 /p>

The alternative is each time a new platform comes along, if the developer doesn't return a numerical value for say the battery level, we'll then have to go back and update that component to return the correct data.

@azogue
Copy link
Member
azogue commented Jun 18, 2017

hi @philhawthorne,

I understand the initial intention, and it seems phenomenal to me, but, for the cases of more complex strings with numbers, perhaps the best alternative is to avoid the parsing, right?

For cases of 'number + unit' I think the change is great.

json_body[0]['fields'][key] = float(
non_decimal.sub('', value))
except (ValueError, TypeError):
json_body[0]['fields'][key] = float(
Copy link
Member

Choose a reason for hiding this comment

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

@philhawthorne, sorry if I was not understood, I was referring to this except clause, to simply pass if a ValueError is raised.

If we get an error trying to convert a variable to a float, let's ignore
it completely
@philhawthorne
Copy link
Contributor Author

Thanks @azogue

That makes more sense. Makes it more simple, and handles both traces that were reported in the issue. I've updated that as you suggested.

Cheers!

Copy link
Member
@azogue azogue left a comment

Choose a reason for hiding this comment

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

Fixes #8086. Looks good to me, and I think it should be included in 0.47.1

@mezz64
Copy link
Contributor
mezz64 commented Jun 18, 2017

Just wanted to confirm that this change fixes the original issue I reported in #8042.

Copy link
Member
@pvizeli pvizeli left a comment

Choose a reason for hiding this comment

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

Makes the Regex's used by InfluxDB constants so they don't need to be
compiled each time
@philhawthorne
Copy link
Contributor Author

Thanks @pvizeli changed that

@pvizeli
Copy link
Member
pvizeli commented Jun 19, 2017

I think instead to create this complex things we should use match regex: ^\w*\d+\.?\d+\w*$. So we are sure that we only match things that we can also filter later.

@pvizeli pvizeli modified the milestones: 0.47, 0.47.1 Jun 19, 2017
@pvizeli
Copy link
Member
pvizeli commented Jun 19, 2017

I update the regex for better detection

'last_seen': 23.0,
'updated_at_str': '2017-01-01 00:00:00',
'updated_at': 20170101000000,
'multi_periods_str': '0.120.240.2023873' },

Choose a reason for hiding this comment

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

line too long (86 > 79 characters)
whitespace before '}'

@philhawthorne
Copy link
Contributor Author

Thanks @pvizeli !

@pvizeli pvizeli merged commit 06b051c into home-assistant:dev Jun 20, 2017
@pvizeli
Copy link
Member
pvizeli commented Jun 20, 2017

Good teamwork!

balloob pushed a commit that referenced this pull request Jun 21, 2017
#8080)

* Update InfluxDB to handle datetime objects

Updates the InfluxDB regex to ignore datetime objects being coverted
into float values.

Adds tests to the component to ensure datetime objects are corectly
handled.

* Fix Hound errors

Fixes errors from Hound bot

* Update InfluxDB to handle multiple decimal points

Changes the way InfluxDB handles values such as 1.2.3.4 to be 1.234 so
it stores in InfluxDB as a valid float value

* Fix lint issues

Reduce the size of a line for the linter

* Update InfluxDB to pass on unknown variable

If we get an error trying to convert a variable to a float, let's ignore
it completely

* Make InfluxDB Regex constants

Makes the Regex's used by InfluxDB constants so they don't need to be
compiled each time

* cleanup

* fix lint

* Update regex

* fix tests

* Fix JSON body missing new line character

* fix exceptions
@balloob
Copy link
Member
balloob commented Jun 21, 2017

Cherry-picked for 0.47.1

@balloob balloob mentioned this pull request Jun 21, 2017
@balloob balloob mentioned this pull request Jul 1, 2017
dethpickle pushed a commit to dethpickle/home-assistant that referenced this pull request Aug 18, 2017
home-assistant#8080)

* Update InfluxDB to handle datetime objects

Updates the InfluxDB regex to ignore datetime objects being coverted
into float values.

Adds tests to the component to ensure datetime objects are corectly
handled.

* Fix Hound errors

Fixes errors from Hound bot

* Update InfluxDB to handle multiple decimal points

Changes the way InfluxDB handles values such as 1.2.3.4 to be 1.234 so
it stores in InfluxDB as a valid float value

* Fix lint issues

Reduce the size of a line for the linter

* Update InfluxDB to pass on unknown variable

If we get an error trying to convert a variable to a float, let's ignore
it completely

* Make InfluxDB Regex constants

Makes the Regex's used by InfluxDB constants so they don't need to be
compiled each time

* cleanup

* fix lint

* Update regex

* fix tests

* Fix JSON body missing new line character

* fix exceptions
@home-assistant home-assistant locked and limited conversation to collaborators Oct 20, 2017
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.

InfluxDB datetime to float TypeError after #7879
9 participants
0