WIP Purge old data from sqlite db on hass start-up#1681
Conversation
|
pylint question: Should I add a comment to ignore this, or is there a recommendation for what to do here? I added self.config to the Recorder class to get the purge_days config item. |
There was a problem hiding this comment.
The recorder should not be aware of a config existing. Instead, let's pass down the parameter for how many days to purge (or None if not to purge at all)
|
Yes, you're allowed to ignore that pylint warning. This will require tests to be written. |
|
@justyns are you still working on adding test cases? Need a hand with that? |
|
Thanks @infamy . I started adding tests for this the other day, but ran into a couple issues:
I'm planning on trying to finish this this weekend, but I don't really have anything worth keeping at the moment if anyone wanted to jump in on the testing. edit: ignore the first issue, I was thinking about it wrong. |
Issue home-assistant#1337 When purge_days is set under the history component, recorder.py will delete all events and states that are older than purge_days days ago. Currently, this is only done once at start up. A vacuum command is also run to free up the disk space sqlite would still use after deleting records.
CONF_PURGE_DAYS so it can be changed easier later. Use 'recorder' domain instead of 'history' domain. Pass purge_days config directly into Recorder object instead of passing the config object around.
|
@infamy or @balloob, do these tests look okay? It tests whether |
|
@justyns you have a bunch of lint errors in the test. (take a look at the Tavis CI output) they should be easy fixes. As for the test cases those make sense to me. The only thing I would add, is a base feature to have it do the clean up every day at midnight, instead of just at startup. You would need to add the import And create the event to be called Or something like that. That would allow HA to do the clean up nightly. (only create the event handle if it is enabled of course) |
| _LOGGER.info("Purging events created before %s", purge_before) | ||
| deleted_rows = self.query( | ||
| sql_query="DELETE FROM events WHERE created < ?;", | ||
| data=(int(purge_before.timestamp()),), |
There was a problem hiding this comment.
You should be able to just put in purge_before and it will be translated.
|
The tests look great 👍 Added some comments. |
Description:
Each time HASS starts, the recorder component will run _purge_old_data() before going into the event loop. _purge_old_data() will delete records from the
eventsandstatestables older than X days. After deleting those records,VACUUMis run on the database to free up disk space taken by the deleted records.This also adds a
purge_daysconfig option to the history component. By default it is set to-1which means no old records will be deleted.Let me know if
purge_daysshould be changed to something else. I consideredpurge_afterand a few other similar names, but figured purge_days would be obvious that it's expecting days.Related issue (if applicable): #1337
Example entry for
configuration.yaml(if applicable):Checklist:
If code communicates with devices:
toxrun successfully. Your PR cannot be merged unless tests passREQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.If the code does not interact with devices:
toxrun successfully. Your PR cannot be merged unless tests pass