Commit Graph

795 Commits (c522ea855d17f5072a6113852ce398a22800d333)

Author SHA1 Message Date
J. Nick Koston 809f394ff5
Bump sqlalchemy to 2.0.13 (#92936) 2023-05-12 04:37:16 +09:00
Erik Montnemery c1f716487c
Include data for entire periods in response to recorder/statistics_during_period (#92848)
* Include data for entire periods in response to recorder/statistics_during_period

* Break out function for calculating the end of the month
2023-05-11 11:28:33 +02:00
Erik Montnemery 4568207f9b
Support calculating changes between consecutive sum statistics (#92823)
* Support calculating changes between consecutive sum statistics

* Add support for unit conversion when calculating change

* Don't include sum in WS response unless requested

* Improve tests

* Break out calculating change to its own function

* Improve test coverage
2023-05-11 10:05:58 +02:00
J. Nick Koston ede1f08c51
Reduce size of migration transactions to accommodate slow/busy systems (#92312)
* Reduce size of migration transactions to accommodate slow/busy systems

related issue #91489

* handle overloaded RPIs better
2023-04-30 23:18:00 -04:00
J. Nick Koston 32f6dd4a82
Bump sqlalchemy to 2.0.12 (#92315)
changelog: https://docs.sqlalchemy.org/en/20/changelog/changelog_20.html#change-2.0.12
2023-04-30 23:11:29 -04:00
J. Nick Koston bb4c03ce3c
Avoid returning statistics columns that the metadata knows are impossible (#92095) 2023-04-30 20:46:26 +02:00
J. Nick Koston 1a82b353e0
Auto repair incorrect collation on MySQL schema (#92270)
* Auto repair incorrect collation on MySQL schema

As we do more union queries in 2023.5.x if there is a mismatch
between collations on tables, they will fail with an error
that is hard for the user to figure out how to fix

`Error executing query: (MySQLdb.OperationalError) (1271, "Illegal mix of collations for operation UNION")`

This was reported in the #beta channel and by PM from others
so the problem is not isolated to a single user

https://discord.com/channels/330944238910963714/427516175237382144/1100908739910963272

* test with ascii since older maraidb versions may not work otherwise

* Revert "test with ascii since older maraidb versions may not work otherwise"

This reverts commit 787fda1aefcd8418a28a8a8f430e7e7232218ef8.t

* older version need to check collation_server because the collation is not reflected if its the default
2023-04-29 21:17:09 -04:00
J. Nick Koston e136070718
Speed up purging the database (#92247) 2023-04-29 18:45:14 +02:00
J. Nick Koston e156d3132e
Ensure purge can cleanup old format detached states in the database (#92145) 2023-04-28 21:03:59 +02:00
J. Nick Koston 47b289b632
Bump sqlalchemy to 2.0.11 to fix a critical regression with postgresql (#92126) 2023-04-27 18:52:43 +02:00
J. Nick Koston e7e7990b60
Bump sqlalchemy to 2.0.10 (#91982) 2023-04-25 09:45:49 -05:00
J. Nick Koston 0b0c94ee52
Fix migration with negative event type cache (#91910)
* Fix migration with negative event type cache

fixes a regression with #91770

* Update homeassistant/components/recorder/table_managers/event_types.py
2023-04-23 20:56:17 -04:00
Marc Mueller 06520217bc
Update pylint to 2.17.2 (#91894) 2023-04-23 14:46:48 +02:00
J. Nick Koston 34b824a27b
Reduce number of columns when selecting attributes for history (#91717) 2023-04-22 14:21:08 +02:00
J. Nick Koston 6e628d2f06
Add a negative cache to the recorder event type manager (#91770) 2023-04-22 14:09:04 +02:00
J. Nick Koston b5fbbf8410
Speed up processing binary columns on SQLite/MariaDB/MySQL (#91818) 2023-04-22 13:55:11 +02:00
J. Nick Koston 95fcdc5684
Speed up sql queries where ORM rows are not needed (#91839)
* Speed up logbook and history queries where ORM rows are not needed

This avoids having sqlalchemy wrap Result in ChunkedIteratorResult
which has additional overhead we do not need for these cases

* more places

* anything that uses _sorted_statistics_to_dict does not need orm rows either
2023-04-21 23:28:07 -04:00
J. Nick Koston adc472862b
Fallback to generating a new ULID on migraiton if context is missing or invalid (#91704)
* Fallback to generating a new ULID on migraiton if context is missing or invalid

It was discovered that postgresql will do a full scan if
there is a low cardinality on the index because of missing
context ids. We will now generate a ULID for the timestamp
of the row if the context data is missing or invalid

fixes #91514

* tests

* tweak

* tweak

* preen
2023-04-19 21:56:07 -04:00
J. Nick Koston 408b2171ae
Handle long format context UUIDs during migration (#91657)
In https://github.com/home-assistant/core/issues/91514 is was discovered
these exist in older versions
2023-04-19 08:40:04 -04:00
J. Nick Koston 2ec1359063
Dynamically size recorder max backlog based on available memory (#90894)
Co-authored-by: Franck Nijhof <git@frenck.dev>
2023-04-18 12:35:49 +02:00
J. Nick Koston 2530031454
Use cache to lookup event type ids in logbook (#91576)
noticed we can speed this up while looking at
https://github.com/home-assistant/core/issues/91514

Note: this will not fix that issue as there is more going on there
2023-04-17 23:40:03 -04:00
J. Nick Koston fdc6cf3472
Continue recording events until final write (#91260) 2023-04-16 09:22:47 -10:00
J. Nick Koston 1379ad60c8
Ensure recorder always attempts clean shutdown if recorder thread raises (#91261)
* Ensure recorder run shutdown if the run loop raises

If anything goes wrong with the recorder we should
still try to shutdown cleanly

* tweak

* tests

* tests

* handle migraiton failure

* tweak comment

* naming

* order

* order

* order

* reword

* adjust test

* fixes

* threading

* failure case

* fix test

* have to wait for stop because the task blocks on thread join
2023-04-14 21:03:24 -04:00
J. Nick Koston e1a5ad069c
Improve performance of sums in the energy dashboard (#91342) 2023-04-13 11:52:38 -10:00
J. Nick Koston 4366f83ac8
Restore use of local timezone for MariaDB/MySQL in SQL integration (#91313)
* Use local timezone for recorder connection

The fix in #90335 had an unexpected side effect of
using UTC for the timezone since all recorder operations
use UTC. Since only sqlite much use the database executor
we can use a seperate connection pool which uses local time

This also ensures that the engines are disposed of
when Home Assistant is shutdown as previously we
did not cleanly disconnect

* coverage

* fix unclean shutdown in config flow

* tweaks
2023-04-12 20:24:55 -04:00
J. Nick Koston 9b2e9b8746
Update typing on recorder pool for sqlalchemy 2.0 (#91244) 2023-04-13 00:09:15 +02:00
J. Nick Koston e40a373c4b
Call sqlite pragma optimize during periodic cleanup task (#91245)
https://www.sqlite.org/pragma.html#pragma_optimize

> To achieve the best long-term query performance without the need to do a detailed engineering analysis of the application schema and SQL, it is recommended that applications run "PRAGMA optimize" (with no arguments) just before closing each database connection. Long-running applications might also benefit from setting a timer to run "PRAGMA optimize" every few hours.

> This pragma is usually a no-op or nearly so and is very fast.

Since we keep the recorder connection open for the entire time HA
is running we fall into the long-running application bucket
2023-04-11 22:39:19 -04:00
J. Nick Koston 4e6937d20f
Avoid multiple round trips to the database for history API calls (#91193)
* delete more code

* tweak

* tweak

* wrappers

* restore lost performance

* restore lost performance

* restore lost performance

* compact

* reduce

* fix refactor

* DRY

* tweak

* delete the start time state injector

* move away the legacy code

* tweak

* adjust

* adjust

* tweak

* ignore impossible

* fix a bug where the first start was changed to the start time when there was no previous history recorded before

* avoid the empty scan most cases

* postgresql

* fixes

* workaround for mariadb < 10.4

* remove unused

* remove unused

* adjust

* bail early

* tweak

* tweak

* fix more tests

* fix recorderrun being init in the future in the test

* run history tests on schema 30 as well

* Revert "run history tests on schema 30 as well"

This reverts commit d798b100ac.

* reduce

* cleanup

* tweak

* reduce

* prune

* adjust

* adjust

* adjust

* reverse later is faster because the index is in forward order and the data size we are reversing is much smaller even if we are in python code

* Revert "reverse later is faster because the index is in forward order and the data size we are reversing is much smaller even if we are in python code"

This reverts commit bf974e103e.

* fix test

* Revert "Revert "reverse later is faster because the index is in forward order and the data size we are reversing is much smaller even if we are in python code""

This reverts commit 119354499e.

* more coverage

* adjust

* fix for table order

* impossible for it to be missing

* remove some more legacy from the all states
2023-04-11 22:38:23 -04:00
J. Nick Koston 49079691d4
Reduce overhead of legacy database columns on new installs (#90246)
* Reduce overhead of legacy database columns on new installs

* Reduce overhead of legacy database columns on new installs

* Reduce overhead of legacy database columns on new installs

* Reduce overhead of legacy database columns on new installs

* not working as expected

* override the type compiler

* override the type compiler

* override the type compiler

* override the type compiler

* Apply suggestions from code review

* pgsql char1

* make entity filter test setup with old schema

* fix some more tests that were mutating state

* fix some more tests that were mutating state

* fix some more tests that were mutating state

* fix more dbstate mutations

* add shim for older tests

* split migration tests

* add coverage for purging legacy data

* tweak

* more fixes

* drop some legacy

* fix another test

* fix a few more

* add casts for postgresql in case someone deletes the schema changes table

* dry

* dry

* dry
2023-04-10 10:08:46 -04:00
J. Nick Koston a62ede78ca
Reduce space to store state_attributes and event_data for new MariaDB/MySQL databases (#91150)
Since MySQL and MariaDB support unsigned ints we can store the
hashes in 4 bytes instead of 8 bytes
2023-04-10 10:02:42 -04:00
J. Nick Koston bd0378a961
Bump sqlalchemy to 2.0.9 (#91151)
changes: https://docs.sqlalchemy.org/en/20/changelog/changelog_20.html#change-2.0.9
2023-04-10 10:01:49 -04:00
J. Nick Koston 69f751703b
Fix context_user_id round trip when calling to_native (#91098)
We do not actually use this in the history or logbook
APIs so nothing broke but there was a bug here for anyone
calling this directly

fixes #91090
2023-04-08 22:31:28 -04:00
J. Nick Koston d0d4ab6056
Require a list of entity ids when fetching history (#90992) 2023-04-08 16:14:44 -10:00
J. Nick Koston 398762fdd5
Resume entity id post migration after a restart (#90973)
* Restart entity id post migration after a restart

If the entity migration finished and Home Assistant was
restarted during the post migration it would never be resumed
which means the old index and space would never be recovered

* add migration resume test
2023-04-06 21:16:45 -04:00
J. Nick Koston 96c5e845e5
Guard against invalid ULIDs in contexts while recording events (#90889) 2023-04-06 10:34:54 +02:00
J. Nick Koston 19f71b3bb9
Fix entity_id migration query failing with MySQL 8.0.30 (#90895) 2023-04-06 10:34:13 +02:00
J. Nick Koston b4fec762bc
Switch to fnv-hash-fast from fnvhash (#90761)
* Switch to fnv-hash-fast from fnvhash

Replaces the pure python implemention with a fast cpp one
when available (with fallback to pure python)

changelog: https://github.com/bdraco/fnv-hash-fast/releases/tag/v0.3.1
source: https://github.com/bdraco/fnv-hash-fast/tree/main/src/fnv_hash_fast

* Apply suggestions from code review

* lint
2023-04-05 20:52:23 -04:00
J. Nick Koston a8f1d033a0
Add MariaDB deadlock retry wrapper to database timestamp column migrations (#90880)
Add deadlock retry wrapper to timestamp column migrations

fixes #90819
2023-04-05 20:46:56 -04:00
epenet c01b1eb013
Adjust async_track_time_interval name argument (#90838)
Adjust async_track_time_interval naming
2023-04-05 10:58:02 -04:00
J. Nick Koston b66a99fe8a
Bump sqlalchemy to 2.0.8 (#90756) 2023-04-04 12:47:54 +02:00
J. Nick Koston 4f1574b859
Prevent overly large event data from being stored in the database (#90747)
This is the same change as #87105 for events
2023-04-04 00:02:49 -04:00
J. Nick Koston 84292d4797
Cleanup some duplicate code in recorder statistics (#90549)
* Cleanup some duplicate code in recorder statistics

* more cleanup

* reduce

* reduce
2023-04-01 21:40:14 -04:00
J. Nick Koston a2efe2445a
Fix migration when encountering a NULL entity_id/event_type (#90542)
* Fix migration when encountering a NULL entity_id/event_type

reported in #beta on discord

* simplify
2023-03-30 20:54:13 -04:00
J. Nick Koston 6b0c98045e
Handle garbage in the context_id column during migration (#90544)
* Handle garbage in the context_id column during migration

* Update homeassistant/components/recorder/migration.py

* lint
2023-03-30 20:53:47 -04:00
J. Nick Koston 403dffc12d
Reduce cache key size for queries that only need single columns (#90430)
* Reduce cache key size for queries that only need single columns

These queries only cared about a single row but would select
the whole set of columns from the orm object

* wrap it
2023-03-28 23:28:24 -04:00
J. Nick Koston ce28bfe5b2
Remove unused types argument in statistics query generation (#90431)
* Remove unused types argument in statistics query generation

* update test
2023-03-28 23:01:11 -04:00
J. Nick Koston 0550b17d54
Rework recorder filters to avoid caching mistakes (#90419) 2023-03-28 22:51:46 +02:00
J. Nick Koston d21433b6af
Ensure filters are generated inside the lambda locks (#90418) 2023-03-28 20:50:10 +02:00
J. Nick Koston be5714e3fd
Use slots for recorder tasks to reduce memory (#90387) 2023-03-28 12:24:12 +02:00
J. Nick Koston 7bceedfc95
Bump sqlalchemy to 2.0.7 (#90256) 2023-03-25 07:05:35 -10:00