Add apply_filter attribute to recorder.purge service#45826
Conversation
|
Out of time to fully run though this. It going to make it more difficult for someone to fix #44958 I might need to bite the bullet on this and get a MSSQL server setup so I can make the change to fix that before we merge this since it doesn't look like we have a MSSQL dev stepping up to do the work in that issue. |
|
I've made the changes to fix MSSQL. Hopefully it will be a small change to make the change sets compatible https://github.com/home-assistant/core/pull/46678/files |
I'll check later how to best update my PR. Not sure if that will be a small change just yet. |
a6028c9 to
1079b19
Compare
|
Did you get a chance to test this with MySQL? |
1079b19 to
e1c1ad2
Compare
It took me a moment to set it up, but it works as intended. I also tested it with |
|
Looks good 👍 I want to get another set of 👀 on #46678 and then they should both be good to merge |
This PR is compatible with with both, the current implementation and #46678. So it should be possible to merge it earlier. I would even suggest to do it that way. The other one is a bit easier to rebase. Should be just adding one indent for the change in |
|
Merging this one first makes sense as the other one will be easier to fix conflicts. I've asked for additional reviews. |
* Always delete states, even if event_id is None * Remove subsets * Set old_state_ids to Null before removing states
|
@bdraco I've rebased and updated this PR. The first two commits include everything from before, including the tests. In the last commit (3961b07), I've updated the logic which should now be much cleaner and easier to understand, in part thanks to your refactoring in #46678. The PR is ready for review now. |
|
Looks good. I'll do some testing with it as soon as I have some free cycles |
|
Sorry to come late - feel free to ignore - but would a I had a similar experience to OP when I tried out an integration that I decided not to go with and then needed to get rid of the unwanted entities. In this use-case, the original proposal would mean editing the recorder yaml, restarting and running |
|
@PeteBa That might be an idea, although I would imagine that it might get abused in the end. Just as an example, say a user as a default retention period of 7 days but decides to use service calls to remove just specific entities earlier, maybe even with repacking each time. That's additional wear on their storage medium, an SD card in the worst case, and system slow downs. Using the IMO this feature isn't intended for regular use. It works as you would expect but I would only recommend it if you really need it. @bdraco What do you think? |
|
@cdce8p , thanks for the response ! and I agree with the "only in emergency" usage. Also, with the comment that the Either way - and more importantly ! - the functionality in the PR is great and would have saved me a ton of grief a few weeks ago as I was trying to figure out SQL 8) |
I agree, it should almost be trivial with core/homeassistant/components/recorder/purge.py Lines 178 to 179 in 3961b07 The only thing left would be to decide how to best wire it up. It might make sense to create a new service for it. Maybe it could take a list of entities or an entity filter as argument? That way a user could still use the glob patterns if they want. |
|
Tested and working as expected |
Proposed change
This PR adds the
apply_filterattribute to therecorder.purgeservice. The attribute indicates that the purge service should remove all filtered states and events even if they occurred betweennowandnow - purge_days. This feature is intended to be used by a user only, so there is no change to the daily purge.Rational / Use case
I recently noticed that my production database had quadrupled in size after I missed excluding a few entity_ids. Since I use a Raspberry PI with an SD card, this caused my whole system to crash. After updating my config, I ran a manual purge, to try to reduce the db size. I noticed then, that the filter is only applied when a state or event is being added. I could have used
keep_days: 0or removed the database completely but I just wanted to remove the falsely added entries. I had to remove them manually.With this change, a user can just run
recorder.purgewithrepack: trueandapply_filter: trueto remove the entries without loosing their entire history or knowing SQL.Implementation details
apply_filter: truehas to be set in a manual service call torecorder.purge. As described earlier, the daily purge doesn't change.statesshould be removed, I useddistinct(States.entity_id).all()and check for each entity if it should be filtered. For events I useddistinct(Events.event_type).all().nowandnow - purge_daysare considered. Everything before then will be removed by the existing logic.event_id. (state_changedevents)state_changedevents)Type of change
Example entry for
configuration.yaml:Additional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: