Skip to content
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

[jdbc] Return QuantityTypes for number items with dimension #9426

Merged
merged 1 commit into from
Jan 3, 2021

Conversation

cweitkamp
Copy link
Contributor

@cweitkamp cweitkamp commented Dec 19, 2020

  • Return QuantityTypes for number items with dimension

This implementation uses the same approach like introduces for RRD4J in #8866 and #8938. But I am seeing some issue with Items of type Number:Dimensionless.

In general it would be possible to extend the database structure by adding a column for the unit for number items. This will need a little bit more effort.

@mhilbush May I ask you to chime in and help testing?

Related to openhab/openhab-core#1954

Signed-off-by: Christoph Weitkamp github@christophweitkamp.de


Depends on #9445

@cweitkamp cweitkamp added enhancement An enhancement or new feature for an existing add-on additional testing preferred The change works for the pull request author. A test from someone else is preferred though. work in progress A PR that is not yet ready to be merged labels Dec 19, 2020
@cweitkamp cweitkamp requested a review from a team as a code owner December 19, 2020 10:27
@mhilbush
Copy link
Contributor

@mhilbush May I ask you to chime in and help testing?

Yes, certainly. This is for post 3.0, correct?

@cweitkamp
Copy link
Contributor Author

Yes, certainly. This is for post 3.0, correct?

Thank you. Yes, my plan is to introduce this feature after OH 3 release.

@cweitkamp cweitkamp force-pushed the feature-jdbc-quantitytype branch 2 times, most recently from cb034ea to 31072f0 Compare December 21, 2020 13:22
@cweitkamp
Copy link
Contributor Author

But I am seeing some issue with Items of type Number:Dimensionless.

I added a check which excludes all value having base unit ONE from being converted before storing them. This results in nice values again.

@cweitkamp cweitkamp added the awaiting other PR Depends on another PR label Dec 22, 2020
@cweitkamp cweitkamp force-pushed the feature-jdbc-quantitytype branch from 31072f0 to 5906dc5 Compare December 23, 2020 11:25
@cweitkamp cweitkamp removed awaiting other PR Depends on another PR work in progress A PR that is not yet ready to be merged labels Dec 29, 2020
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
@cweitkamp cweitkamp force-pushed the feature-jdbc-quantitytype branch from 5906dc5 to df7b4e4 Compare December 29, 2020 13:01
@cweitkamp cweitkamp changed the title [WIP][jdbc] Return QuantityTypes for number items with dimension [jdbc] Return QuantityTypes for number items with dimension Dec 29, 2020
@cweitkamp
Copy link
Contributor Author

I am running this version for nearly two weeks now and it seems to be stable. Feel free to do some additional testing or start review. Thanks.

@mhilbush
Copy link
Contributor

One quick comment... If we're upgrading the mysql jdbc driver version to 8.0.13, this log message should be changed, too.

/~https://github.com/openhab/openhab-addons/blob/main/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcConfiguration.java#L306

@mhilbush
Copy link
Contributor

And, unrelated to your PR, this line could be made debug.

@cweitkamp
Copy link
Contributor Author

If we're upgrading the mysql jdbc driver version to 8.0.13, this log message should be changed, too.

Not sure if we have to do it, because the message shows information about the minimum required version. I thought about that too when I increased the MariaDB connector version but decided to not change it.

@mhilbush
Copy link
Contributor

Not sure if we have to do it, because the message shows information about the minimum required version.

But when I installed 5.1.37, it didn't work. Then I looked in the pom and saw the 8.0.13 dependency. So I installed 8.0.13 and it started to work. I'll try again to see what it's complaining about.

@mhilbush
Copy link
Contributor

Maybe I missing something, but none of the 5.1.* or 6.0.* versions I've tried will work for me. Only the 8.0.x versions work.

The older versions all result in this.

2020-12-29 11:28:58.203 [DEBUG] [b.persistence.jdbc.internal.JdbcPersistenceService] - JDBC::activate: persistence service activated
2020-12-29 11:28:58.203 [DEBUG] [b.persistence.jdbc.internal.JdbcPersistenceService] - JDBC::updateConfig
2020-12-29 11:28:58.203 [DEBUG] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::JdbcConfiguration
2020-12-29 11:28:58.204 [DEBUG] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::updateConfig: configuration size = 11
2020-12-29 11:28:58.204 [DEBUG] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::updateConfig: user=XXXXX
2020-12-29 11:28:58.204 [DEBUG] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::updateConfig: password exists? true
2020-12-29 11:28:58.204 [DEBUG] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::updateConfig: url=jdbc:mysql://XXXXXXXXX:3306/openhab3?serverTimezone=America/New_York
2020-12-29 11:28:58.204 [DEBUG] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::updateConfig: found serviceName = 'mysql'
2020-12-29 11:28:58.204 [DEBUG] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::updateConfig: Init Data Access Object Class: 'org.openhab.persistence.jdbc.db.JdbcMysqlDAO'
2020-12-29 11:28:58.204 [DEBUG] [org.openhab.persistence.jdbc.db.JdbcBaseDAO       ] - JDBC::initSqlTypes: Initialize the type array
2020-12-29 11:28:58.205 [DEBUG] [org.openhab.persistence.jdbc.db.JdbcBaseDAO       ] - JDBC::initSqlQueries: 'JdbcMysqlDAO'
2020-12-29 11:28:58.205 [DEBUG] [org.openhab.persistence.jdbc.db.JdbcMysqlDAO      ] - JDBC::initSqlTypes: Initialize the type array
2020-12-29 11:28:58.205 [DEBUG] [org.openhab.persistence.jdbc.db.JdbcMysqlDAO      ] - JDBC::initSqlQueries: 'JdbcMysqlDAO'
2020-12-29 11:28:58.205 [DEBUG] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::updateConfig: dBDAO ClassName=org.openhab.persistence.jdbc.db.JdbcMysqlDAO
2020-12-29 11:28:58.205 [DEBUG] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::updateConfig: tableUseRealItemNames=true
2020-12-29 11:28:58.205 [DEBUG] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::updateConfig: enableLogTime false
2020-12-29 11:28:58.206 [ERROR] [penhab.persistence.jdbc.internal.JdbcConfiguration] - JDBC::updateConfig: could NOT load JDBC-driverClassName or JDBC-dataSourceClassName. ClassNotFoundException: 'com.mysql.jdbc.Driver cannot be found by org.openhab.persistence.jdbc_3.1.0.202012291431'
2020-12-29 11:28:58.206 [WARN ] [penhab.persistence.jdbc.internal.JdbcConfiguration] -

	!!!
	To avoid this error, place an appropriate JDBC driver file for serviceName 'mysql' in addons directory.
	Copy missing JDBC-Driver-jar to your openHab/addons Folder.
	!!!
	DOWNLOAD:
	MySQL:     version >= 5.1.36 from             https://mvnrepository.com/artifact/mysql/mysql-connector-java

I realize this is unrelated to your changes, so let me see if I can get it sorted out.

@cweitkamp
Copy link
Contributor Author

tbh I don't know which version is missing for MySQL and when it has been changed to the used one. Maybe you can find an answer on the old openhab1-addons repository.

@mhilbush
Copy link
Contributor

mhilbush commented Dec 29, 2020

In openhab1-addons, I saw where the mysql persistence service was updated to use the 8.0.13 driver back in November 2018, and the mysql version in the jdbc persistence service was updated to use 8.0 in December 2018. But the jdbc service's README still says it was tested with the 5.1.39 driver version. I dunno. In any event, 8.0.13 works so that's what I'm using.

Question on averageSince and deltaSince. I've never used those before, and I see they currently return a DecimalType. As compared to maximumSince and minimumSince, both of which return an item (which is correctly representaed as a QuantityType). Do you know why averageSince and deltaSince are treated differently from maximumSince and minimumSince?

Even the documentation is confusing, as it doesn't say anything about the return type of averageSince and deltaSince.

jdbc1

Further down in the README here, it shows an example using temperature where it actually includes the units in answer. 🤔

@cweitkamp
Copy link
Contributor Author

I agree, the docs might be confusing in the example.

Beside others minimumSince and maximumSince return a HistoricItem which is kind of wrapper for the historic state to transport the timestamp as well.

averageSince and deltaSince are performing an internal calculation (in OHC - see PersistenceExtensions for more details). Thus all historic values are converted to DecimalType before doing the math. This is not the best solution when dealing with UoM values. I already filed openhab/openhab-core#1954 for it and started testing / working on it but as a requirement to solve it all - or nearly all - persistence add-ons have to be capable of returning QuantityType values. This now is possible.

@mhilbush
Copy link
Contributor

Ok, that's what I figured. And changing it will be a breaking change, right? Since existing rules depend on those functions always returning a DecimalType.

Other than that bit of confusion on my part, everything I've seen looks good.

@cweitkamp
Copy link
Contributor Author

And changing it will be a breaking change, right?

Changing the return value is a breaking change.

@mhilbush
Copy link
Contributor

I ran into another issue, but I don't think it's related to your changes because it also happens when I use an item defined as just a plain Number.

The functions varianceSince and deviationSince (which is derived from varianceSince) are returning null.

I know this call works, because it's also used to get averageSince and that function is working correctly.

But here, does it need to be reinitialized (or can it just use result to do the iteration)?

Or, is there something else I'm missing?

@mhilbush
Copy link
Contributor

I added the line it = result.iterator(); above line 400 in PersistenceExtensions.java. The functions varianceSince and deviationSince produced results instead of returning null. If you think that's the correct way to fix it, I can submit a PR for core.

@cweitkamp
Copy link
Contributor Author

Thanks for the analysis. What makes me curious is that my unit test in PersistenceExtensionsTest are working. Might they be wrong too?

@mhilbush
Copy link
Contributor

Hmm. That's really weird.

Am I right that by the time it gets to line 400, the iterator it is at the end of the iteration as a result of calling internalAverageSince?

@mhilbush
Copy link
Contributor

Doesn't there need to be an @Test in front of public void testVarianceSince for it to execute the test?

@cweitkamp
Copy link
Contributor Author

cweitkamp commented Dec 30, 2020

🤦 Guess what ... The methods testVarianceSince() and testDeviationSince() are not annotated by @Test and thus not executed.

// EDIT: You figured it a second before me. And your suggestion is working. 👍 PR would be appreciated. Thanks.

@mhilbush
Copy link
Contributor

Sure. Will do.

Shall I also add the annotations to the tests?

@cweitkamp
Copy link
Contributor Author

Of course. This will validate that you are right. 😉

@mhilbush
Copy link
Contributor

openhab/openhab-core#2036

Tests now run and succeed.

@cweitkamp cweitkamp removed the additional testing preferred The change works for the pull request author. A test from someone else is preferred though. label Jan 3, 2021
@cweitkamp
Copy link
Contributor Author

@openhab/add-ons-maintainers Ready for review. Thanks.

@fwolter fwolter merged commit 9394dc4 into openhab:main Jan 3, 2021
@fwolter fwolter added this to the 3.1 milestone Jan 3, 2021
@cweitkamp cweitkamp deleted the feature-jdbc-quantitytype branch January 3, 2021 16:45
thinkingstone pushed a commit to thinkingstone/openhab-addons that referenced this pull request Nov 7, 2021
…9426)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
marcfischerboschio pushed a commit to bosch-io/openhab-addons that referenced this pull request May 5, 2022
…9426)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature for an existing add-on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants