Commit Graph

61 Commits

Author SHA1 Message Date
Erik Montnemery 426f73b1f4
Add State.last_reported (#113511)
* Add State.last_reported

* Update tests

* Update test snapshots

* Call state_reported listeners when firing state_changed event

* Add tests
2024-03-20 21:05:07 +01:00
Erik Montnemery d31124d5d4
Avoid creating unneeded Context and Event objects when firing events (#113798)
* Avoid creating unneeded Context and Event objects when firing events

* Add test

---------

Co-authored-by: J. Nick Koston <nick@koston.org>
2024-03-20 09:40:06 +01:00
J. Nick Koston 06f356a038
Avoid expensive db queries at startup to check if data is migrated (#113773) 2024-03-19 15:27:40 -10:00
Marc Mueller a033574ee2
Add empty line after module docstring [r] (#112703) 2024-03-08 15:05:07 +01:00
J. Nick Koston 15384f4661
Remove unused entity_sources argument from shared_attrs_bytes_from_event (#108210) 2024-01-17 08:45:04 -05:00
J. Nick Koston 3d595fff13
Avoid duplicate timestamp conversions for websocket api and recorder (#108144)
* Avoid duplicate timestamp conversions for websocket api and recorder

We convert the time from datetime to timestamps one per
open websocket connection and the recorder for every
state update. Only do the conversion once since its
~30% of the cost of building the state diff

* more

* two more

* two more in live history
2024-01-16 14:05:01 +01:00
Aarni Koskela 706add4a57
Switch formatting from black to ruff-format (#102893)
Co-authored-by: Franck Nijhof <git@frenck.dev>
2023-11-27 14:38:59 +01:00
J. Nick Koston 268425b5e3
Recover from previously failed statistics migrations (#101781)
* Handle statistics columns being unmigrated from previous downgrades

If the user downgraded HA from 2023.3.x to an older version without
restoring the database and they upgrade again with the same database
they will have unmigrated statistics columns since we only migrate them
once.

As its expensive to check, we do not want to check every time
at startup, so we will only do this one more time since the
risk that someone will downgrade to an older version is very
low at this point.

* add guard to sqlite to prevent re-migrate

* test

* move test to insert with old schema

* use helper

* normalize timestamps

* remove

* add check

* add fallback migration

* add fallback migration

* commit

* remove useless logging

* remove useless logging

* do the other columns at the same time

* coverage

* dry

* comment

* Update tests/components/recorder/test_migration_from_schema_32.py
2023-10-22 23:34:43 -04:00
Erik Montnemery 0dc21504f5
Remove support for excluding attributes in recorder platforms (#100679) 2023-09-24 14:45:06 +02:00
Erik Montnemery fbcc5318c5
Move attributes to be excluded from recording to entity classes (#100239)
Co-authored-by: J. Nick Koston <nick@koston.org>
2023-09-20 18:09:12 +02:00
Erik Montnemery 51576b7214
Improve typing of entity.entity_sources (#99407)
* Improve typing of entity.entity_sources

* Calculate entity info source when generating WS response

* Adjust typing

* Update tests
2023-09-12 20:41:26 +02:00
Ville Skyttä 34b47a2597
Remove unnnecessary pylint configs from components [m-r]* (#98924) 2023-08-24 00:56:50 +02:00
Marc Mueller 77f38e33e5
Import names from typing instead of typing_extensions [3.11] (#97065) 2023-07-22 17:03:44 -05:00
J. Nick Koston e7e7990b60
Bump sqlalchemy to 2.0.10 (#91982) 2023-04-25 09:45:49 -05: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 b5fbbf8410
Speed up processing binary columns on SQLite/MariaDB/MySQL (#91818) 2023-04-22 13:55:11 +02: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 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 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 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 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 4ebce9746d
Add schema auto repairs for states tables (#90083) 2023-03-22 10:05:23 -10:00
J. Nick Koston bf63e6cbd4
Set unique on StatesMeta and EventTypes database tables (#89971)
Set unique on StatesMeta and EventTypes

These should have been marked unique originally to prevent
collision bugs from going unnoticed. These have not been
to beta yet so this is not a breaking change
2023-03-19 22:30:01 -04:00
J. Nick Koston b1f64de6ce
Remove the old ix_states_event_id index if its no longer being used (#89901)
* Remove the old ix_states_event_id index if its no longer being used

* cover it

* fixes

* fixup

* Update homeassistant/components/recorder/tasks.py
2023-03-17 20:27:33 -04:00
J. Nick Koston b906d67c1e
Fix filtered purge not removing newer events (#89721) 2023-03-14 16:33:19 -10:00
J. Nick Koston dbc0890ce8
Add index to event_type and entity_id (#89676) 2023-03-14 10:09:21 +01:00
J. Nick Koston d1ee303e85
Drop duplicated indices from recorder database schema (#89613)
Drop duplicated indices from schema

https://docs.percona.com/percona-toolkit/pt-duplicate-key-checker.html

```
% pt-duplicate-key-checker --databases fresh

ALTER TABLE `fresh`.`events` DROP INDEX `ix_events_event_type_id`;

ALTER TABLE `fresh`.`states` DROP INDEX `ix_states_metadata_id`;

ALTER TABLE `fresh`.`statistics` DROP INDEX `ix_statistics_metadata_id`;

ALTER TABLE `fresh`.`statistics_short_term` DROP INDEX `ix_statistics_short_term_metadata_id`;

```
2023-03-13 09:24:57 +01:00
J. Nick Koston 459ea048ba
Fix old indices never being removed with PostgreSQL (#89599) 2023-03-12 14:07:05 -10:00
J. Nick Koston c41f91be89
Deduplicate entity_id in the states table (#89557) 2023-03-12 10:01:58 -10:00
J. Nick Koston 8bd43760b6
Deduplicate event_types in the events table (#89465)
* Deduplicate event_types in the events table

* Deduplicate event_types in the events table

* more fixes

* adjust

* adjust

* fix product

* fix tests

* adjust

* migrate

* migrate

* migrate

* more test fixes

* more test fixes

* fix

* migration test

* adjust

* speed up

* fix index

* fix more tests

* handle db failure

* preload

* tweak

* adjust

* fix stale docs strings, remove dead code

* refactor

* fix slow tests

* coverage

* self join to resolve query performance

* fix typo

* no need for quiet

* no need to drop index already dropped

* remove index that will never be used

* drop index sooner as we no longer use it

* Revert "remove index that will never be used"

This reverts commit 461aad2c52.

* typo
2023-03-11 14:54:55 -05:00
J. Nick Koston 52cea16f74
Remove unused code in RecorderRuns.entity_ids (#89526) 2023-03-11 12:46:12 +01:00
J. Nick Koston 170a13302c
Reduce overhead to store context ids in the database (#88942) 2023-03-08 14:51:45 -10:00
J. Nick Koston 1759f58fc1
Ensure new MariaDB/MySQL tables are created using the InnoDB engine (#88468)
Ensure new tables are created using InnoDB

InnoDB is the only supported engine to use with MariaDB
or MySQL as we currently have large keys in the states
table that will not work with MyIASM. Other storage
engines including Aria will likely work fine, but they
are not officially supported.
2023-02-20 08:58:59 -05:00
J. Nick Koston c2b770bcb9
Load pending state attributes and event data ids at startup (#88444)
* Load pending state attributes and event data ids at startup

Since we queue all events to be processed after startup
we can have a thundering herd of queries to prime the
LRUs of event data and state attributes ids. Since we
know we are about to process a chunk of events we can
fetch all the ids in two queries

* lru

* fix hang

* Fix recorder LRU being destroyed if event session is reopened

We would clear the LRU in _close_event_session but
it would never get replaced with an LRU again so
it would leak memory if the event session is reopened

* Fix recorder LRU being destroyed if event session is reopened

We would clear the LRU in _close_event_session but
it would never get replaced with an LRU again so
it would leak memory if the event session is reopened

* cleanup
2023-02-19 21:26:38 -05:00
J. Nick Koston 9a6bcc2b63
Fix incorrect created and created_ts columns in statistics database schema (#88462)
* Remove default from created statistics schema

We were still inserting created times because even though
None was passed when creating the object explictly, the
default would still be used

* adjust column

* preserve original pre sql alc 2.0 behavior
2023-02-19 21:00:49 -05:00
Erik Montnemery 1eb20affa1
Update use of sqlalchemy with_variant (#88395) 2023-02-18 09:18:01 -06:00
Erik Montnemery 83e5bf7ae8
Use entity_sources to determine integration in recorder platforms (#88382) 2023-02-18 07:21:41 -06:00
J. Nick Koston 289bab6f87
Make recorder platform attribute exclude integration aware (#88357) 2023-02-18 10:08:59 +01:00
J. Nick Koston 5fe8829cf6
Speed up compiling hourly statistics (#88225) 2023-02-16 12:55:25 -06:00
epenet ba23816a0c
Inverse json import logic (#88099)
* Fix helpers and util

* Adjust components

* Move back errors

* Add report

* mypy

* mypy

* Assert deprecation messages

* Move test_json_loads_object

* Adjust tests

* Fix rebase

* Adjust pylint plugin

* Fix plugin

* Adjust references

* Adjust backup tests
2023-02-16 11:37:57 +01:00
J. Nick Koston 484027023c
Fix new stats rows creating data in the old created column (#88013) 2023-02-13 21:53:37 -06:00
J. Nick Koston abf0c87e40
Migrate statistics to use timestamp columns (#87321) 2023-02-09 12:24:19 -06:00
Erik Montnemery 94519de8dd
Upgrade SQLAlchemy to 2.0.2 (#86436)
Co-authored-by: J. Nick Koston <nick@koston.org>
2023-02-08 08:17:32 -06:00
epenet a202588fd2
Add return type to json_loads (#85672)
* Add JSON type definitions

* Sample use

* Keep mutable for a follo-up PR (avoid dead code)

* Use list/dict

* Remove JsonObjectType

* Remove reference to Union

* Cleanup

* Improve rest

* Rename json_dict => json_data

* Add docstring

* Add type hint to json_loads

* Add cast

* Move type alias to json helpers

* Cleanup

* Create and use json_loads_object

* Make error more explicit and add tests

* Use JsonObjectType in conversation

* Remove quotes
2023-02-07 17:21:55 +01:00
Marc Mueller 342b406dc0
Add Self typing (1) [mypy 1.0] (#87598) 2023-02-06 22:29:47 -06:00
J. Nick Koston 389fc515a1
Prevent overly large attributes from being stored in the database (#87105) 2023-02-02 11:49:33 -06:00
Erik Montnemery 53c5f02ca2
Remove some dead code from recorder (#86697) 2023-01-30 18:37:48 +01:00
J. Nick Koston 4e9bd09d39
Fix old indices not being removed in schema migration leading to slow MySQL queries (#86917)
fixes #83787
2023-01-29 21:33:23 -05:00
Erik Montnemery fea30c1ce9
Terminate strings at NUL when recording states and events (#86687) 2023-01-26 11:11:03 +01:00