Commit Graph

91 Commits

Author SHA1 Message Date
Ville Skyttä
24b1e01d71 Update Ruff to 0.1.8, avoid linter/formatter conflicts (#106080)
* Disable Ruff rules that may conflict with the formatter

* Upgrade Ruff to 0.1.8

- https://github.com/astral-sh/ruff/releases/tag/v0.1.7
- https://github.com/astral-sh/ruff/releases/tag/v0.1.8

* Format with Ruff 0.1.8
2023-12-20 23:55:09 +01:00
J. Nick Koston
c6ed022cce Fix compiling missing statistics losing rows (#101616) 2023-10-08 19:43:00 +02:00
J. Nick Koston
c9a55c7f84 Cache the latest short term stat id for each metadata_id on each run (#100535) 2023-09-26 09:57:59 -05:00
Ville Skyttä
34b47a2597 Remove unnnecessary pylint configs from components [m-r]* (#98924) 2023-08-24 00:56:50 +02: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
bb4c03ce3c Avoid returning statistics columns that the metadata knows are impossible (#92095) 2023-04-30 20:46:26 +02: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
d0d4ab6056 Require a list of entity ids when fetching history (#90992) 2023-04-08 16:14:44 -10: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
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
ddcaa9d372 Break out statistics repairs into a auto_repairs modules (#90068)
* Break out statistics schema repairs into a repairs module

A future PR will add repairs for events, states, etc

* reorg

* reorg

* reorg

* reorg

* fixes

* fix patch targets

* name space rename
2023-03-21 21:08:06 -04:00
J. Nick Koston
d33a303a83 Fix statistics schema µs precision auto repair being ineffective (#89902)
If a user manually migrated their database to MySQL or PostgresSQL
and incorrectly created the timestamp columns as float we would
fail to correct them to double because when we migrated to use
timestamps for the columns I missed that we needed to change the
columns and types for µs precision
2023-03-20 00:06:37 -04:00
J. Nick Koston
e798c30b8b Fix statistics schema auto repair when there is bad data (#89903)
- If the user had previously duplicated data we could end up
  picking the next metadata_id and there could be stale rows
  in the database that have that metadata_id. This can only happen
  from bad manual migrations (which is what this is function
  is validating in the first place). To solve this we now insert
  data with a future date and look at the latest inserted row
  instead of the first.

Example
```
['stored_statistics',
  defaultdict(<class 'list'>,
              {'recorder.db_test_schema': [{'end': 948589200.0,
                                            'last_reset': None,
                                            'max': None,
                                            'mean': 2021.0,
                                            'min': None,
                                            'start': 948585600.0,
                                            'state': None,
                                            'sum': 394.5068},
                                          {'end': 1601946000.000001,
                                            'last_reset': 1601942400.000001,
                                            'max': 1.000000000000001,
                                            'mean': 1.000000000000001,
                                            'min': 1.000000000000001,
                                            'start': 1601942400.000001,
                                            'state': 1.000000000000001,
                                            'sum': 1.000000000000001}]})]
```
2023-03-20 00:06:23 -04:00
J. Nick Koston
c94b054d75 Retain history when renaming an entity_id (#89963)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
2023-03-19 17:33:21 -10:00
J. Nick Koston
5ffb233004 Avoid database executor job to fetch statistic metadata on cache hit (#89960)
* Avoid database executor job to fetch statistic metadata on cache hit

Since we will almost always have a cache hit fetching
statistic meta data we can avoid an executor job

* Avoid database executor job to fetch statistic metadata on cache hit

Since we will almost always have a cache hit fetching
statistic meta data we can avoid an executor job

* Avoid database executor job to fetch statistic metadata on cache hit

Since we will almost always have a cache hit fetching
statistic meta data we can avoid an executor job

* remove exception catch since the threading.excepthook will actually catch this in production

* fix a few missed ones

* threadsafe

* Update homeassistant/components/recorder/table_managers/statistics_meta.py

* coverage and optimistic caching
2023-03-19 22:01:16 -04:00
epenet
ab4a726e6c Add tmpdir to known fixtures in pylint (#89844) 2023-03-17 10:22:02 +01:00
J. Nick Koston
f6f3565796 Reduce latency to find stats metadata (#89824) 2023-03-16 19:00:02 -10:00
epenet
a0f725dfcb Add type hints to tests (#89497) 2023-03-10 12:06:50 +01:00
J. Nick Koston
3c70dd9b42 Make sql subqueries threadsafe (#89254)
* Make sql subqueries threadsafe

fixes #89224

* fix join outside of lambda

* move statement generation into a seperate function to make it easier to test

* add cache key tests

* no need to mock hass
2023-03-06 20:44:11 -05:00
J. Nick Koston
1bd9767d8c Handle InnoDB deadlocks during migration (#89073)
* Handle slow InnoDB rollback when encountering duplicates during migration

fixes #89069

* adjust

* fix mock

* tests

* return on success
2023-03-03 22:00:13 -05:00
epenet
185cd61cbd Add type hints to integration tests (recorder) (#88313) 2023-02-17 18:50:09 +01:00
Franck Nijhof
d3ed8de232 Update Ruff to v0.0.247 (#88210)
* Update Ruff to v0.0.247

* Fix new found violations
2023-02-15 16:46:03 -05:00
Erik Montnemery
4ca0a24f87 Increase test scope of MariaDB + PostgreSQL tests (#87019)
Co-authored-by: J. Nick Koston <nick@koston.org>
2023-02-13 15:58:13 -06:00
J. Nick Koston
c35661947a Optimize fetching statistics by avoiding timestamp conversions (#87845)
* Optimize fetching statistics by avoiding timestamp conversions

* remove accidential unrelated change

* fix test

* recreate so we handle timezone changes
2023-02-10 11:26:46 -05:00
Erik Montnemery
720f51657d Add CI job which runs recorder tests on PostgreSQL (#80614)
Co-authored-by: Franck Nijhof <git@frenck.dev>
Co-authored-by: J. Nick Koston <nick@koston.org>
2023-02-07 09:46:14 -06:00
epenet
4142f0d15d Add return type to tests without arguments (#87613)
* Add return type to tests without arguments

* Black

* Cancel fixture amends
2023-02-07 14:20:06 +01:00
epenet
58596d205c Ensure sensor and recorder converters align (#87138) 2023-02-02 10:36:03 +01:00
J. Nick Koston
c612a92cfb Use python defaults for comparing State, LazyState, and Event objects (#86856)
* Speed up comparing State and Event objects

Use default python implementation for State and Event __hash__ and __eq__

The default implementation compared based on the id() of the object
which is effectively what we want here anyways. These overrides are
left over from the days when these used to be attrs objects

By avoiding implementing these ourselves all of the equality checks
can happen in native code

* tweak

* adjust tests

* write out some more

* fix test to not compare objects

* more test fixes

* more test fixes

* correct stats tests

* fix more tests

* fix more tests

* update sensor recorder tests
2023-01-29 13:31:43 -05:00
Franck Nijhof
e4a78420b8 Enable Ruff PT013 (#86757)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2023-01-27 09:09:46 +01:00
epenet
30bf0634fe Add per-file-ignore to pylint (#86289) 2023-01-22 17:26:24 +01:00
Michael
72d3fa6d89 Replace the usage of unit constants by enumerations in Tests [o-r] (#85936) 2023-01-15 14:50:24 +01:00
Erik Montnemery
587381440f Fix repairing datetime precision for PostgreSQL (#83351) 2022-12-05 22:33:06 -05:00
Erik Montnemery
f869ce9d06 Validate common statistics DB schema errors on start (#79707)
* Validate common statistics db schema errors on start

* Fix test

* Add tests

* Adjust tests

* Disable statistics schema validation in tests

* Update after rebase
2022-11-29 10:16:08 +01:00
Erik Montnemery
a038314d8b Add display unit to WS recorder/get_statistics_metadata (#82870) 2022-11-28 13:46:57 -05:00
Erik Montnemery
607a0e7697 Reduce size of get_statistics_during_period WS API response (#82131) 2022-11-16 17:36:30 +01:00
Michael
8175dab7ab Add week period to recorder statistics api (#80784)
* add week period to get statistics api

* add test
2022-10-25 20:07:28 +02:00
Erik Montnemery
31a787558f Ensure recorder test fixture is setup before hass fixture (#80528)
* Ensure recorder test fixture is setup before hass fixture

* Adjust more tests
2022-10-19 07:58:47 +02:00
Erik Montnemery
2de273500e Remove state_unit_of_measurement from metadata DB table (#79370)
* Remove state_unit_of_measurement from metadata DB table

* Adjust test
2022-10-01 18:55:00 +02:00
Erik Montnemery
bc2dffabc4 Improve naming of units used in statistics (#79276) 2022-09-30 08:38:44 +02:00
Erik Montnemery
cd6697615f Validate units when importing statistics (#78891) 2022-09-21 12:08:53 -04:00
Erik Montnemery
dae00c70de Allow selecting display unit when fetching statistics (#78578) 2022-09-20 17:43:57 -04:00
Erik Montnemery
dd20a7ea62 Display statistics in the source's unit (#78031) 2022-09-15 12:01:24 -04:00
Erik Montnemery
008ac8d10d Improve statistics metadata WS API (#77209) 2022-08-31 11:30:45 +02:00
Erik Montnemery
f61edf0778 Fix updating of statistics metadata name (#77207)
* Fix updating of statistics metadata name

* Fix test

* Test renaming
2022-08-23 17:51:17 +02:00
Erik Montnemery
fd6ffef52f Support non-live database migration (#72433)
* Support non-live database migration

* Tweak startup order, add test

* Address review comments

* Fix typo

* Clarify comment about promoting dependencies

* Tweak

* Fix merge mistake

* Fix some tests

* Fix additional test

* Fix additional test

* Adjust tests

* Improve test coverage
2022-07-22 15:11:34 +02:00
Erik Montnemery
606d544157 Use recorder get_instance function to improve typing (#75567) 2022-07-22 11:58:26 +02:00
Thibault Cohen
1d7d2875e1 Add websocket command recorder/import_statistics (#73937)
* Expose ws_add_external_statistics in websocket API

* Refactor

* Add tests

* Improve test coverage

Co-authored-by: Thibault Cohen <titilambert@users.noreply.github.com>
Co-authored-by: Erik <erik@montnemery.com>
2022-07-21 12:36:49 +02:00
Erik Montnemery
5f2b4001f3 Separate recorder database schema from other classes (#72977)
* Separate recorder database schema from other classes

* fix logbook imports

* migrate new tests

* few more

* last one

* fix merge

Co-authored-by: J. Nick Koston <nick@koston.org>
2022-06-07 14:41:43 +02:00
J. Nick Koston
c66b000d34 Reduce branching in generated lambda_stmts (#73042) 2022-06-05 21:13:31 -07:00