-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
WIP Purge old data from sqlite db on hass start-up #1681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c89cd6a
fd48fc5
d5ca97b
bf3b77e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,8 @@ | |
| import queue | ||
| import sqlite3 | ||
| import threading | ||
| from datetime import date, datetime | ||
| from datetime import date, datetime, timedelta | ||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.util.dt as dt_util | ||
| from homeassistant.const import ( | ||
|
|
@@ -30,6 +31,14 @@ | |
| RETURN_LASTROWID = "lastrowid" | ||
| RETURN_ONE_ROW = "one_row" | ||
|
|
||
| CONF_PURGE_DAYS = "purge_days" | ||
| CONFIG_SCHEMA = vol.Schema({ | ||
| DOMAIN: vol.All(dict, { | ||
| CONF_PURGE_DAYS: int | ||
| }) | ||
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
|
|
||
| _INSTANCE = None | ||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -102,14 +111,14 @@ def setup(hass, config): | |
| """Setup the recorder.""" | ||
| # pylint: disable=global-statement | ||
| global _INSTANCE | ||
|
|
||
| _INSTANCE = Recorder(hass) | ||
| purge_days = config.get(DOMAIN, {}).get(CONF_PURGE_DAYS) | ||
| _INSTANCE = Recorder(hass, purge_days=purge_days) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| class RecorderRun(object): | ||
| """Representation of arecorder run.""" | ||
| """Representation of a recorder run.""" | ||
|
|
||
| def __init__(self, row=None): | ||
| """Initialize the recorder run.""" | ||
|
|
@@ -169,11 +178,12 @@ class Recorder(threading.Thread): | |
| """A threaded recorder class.""" | ||
|
|
||
| # pylint: disable=too-many-instance-attributes | ||
| def __init__(self, hass): | ||
| def __init__(self, hass, purge_days): | ||
| """Initialize the recorder.""" | ||
| threading.Thread.__init__(self) | ||
|
|
||
| self.hass = hass | ||
| self.purge_days = purge_days | ||
| self.conn = None | ||
| self.queue = queue.Queue() | ||
| self.quit_object = object() | ||
|
|
@@ -194,6 +204,7 @@ def run(self): | |
| """Start processing events to save.""" | ||
| self._setup_connection() | ||
| self._setup_run() | ||
| self._purge_old_data() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would make sense to schedule this some time after launch so it won't slow down the launch of Home Assistant. from homeassistant.helpers.event import track_point_in_utc_time
if self.purge_days is not None:
track_point_in_utc_time(self.hass, lambda now: self._purge_old_data(),
dt_util.utcnow() + timedelta(minutes=5))
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this will break your tests, you can just call |
||
|
|
||
| while True: | ||
| event = self.queue.get() | ||
|
|
@@ -475,6 +486,32 @@ def _close_run(self): | |
| "UPDATE recorder_runs SET end=? WHERE start=?", | ||
| (dt_util.utcnow(), self.recording_start)) | ||
|
|
||
| def _purge_old_data(self): | ||
| """Purge events and states older than purge_days ago.""" | ||
| if not self.purge_days or self.purge_days < 1: | ||
| _LOGGER.debug("purge_days set to %s, will not purge any old data.", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for this if you never call it when purge_days is not None, like i suggested above. |
||
| self.purge_days) | ||
| return | ||
|
|
||
| purge_before = dt_util.utcnow() - timedelta(days=self.purge_days) | ||
|
|
||
| _LOGGER.info("Purging events created before %s", purge_before) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would merge this with the query below and only print it to |
||
| deleted_rows = self.query( | ||
| sql_query="DELETE FROM events WHERE created < ?;", | ||
| data=(int(purge_before.timestamp()),), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to just put in |
||
| return_value=RETURN_ROWCOUNT) | ||
| _LOGGER.debug("Deleted %s events", deleted_rows) | ||
|
|
||
| _LOGGER.info("Purging states created before %s", purge_before) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
| deleted_rows = self.query( | ||
| sql_query="DELETE FROM states WHERE created < ?;", | ||
| data=(int(purge_before.timestamp()),), | ||
| return_value=RETURN_ROWCOUNT) | ||
| _LOGGER.debug("Deleted %s states", deleted_rows) | ||
|
|
||
| # Execute sqlite vacuum command to free up space on disk | ||
| self.query("VACUUM;") | ||
|
|
||
|
|
||
| def _adapt_datetime(datetimestamp): | ||
| """Turn a datetime into an integer for in the DB.""" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's optional, please mark it so. It also should be at least 1: