Add Estimated Database Size to the recorder system health (#71463)

This commit is contained in:
J. Nick Koston 2022-05-07 23:02:54 -05:00 committed by GitHub
parent 49d13b9981
commit a8aa0e1cca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 200 additions and 67 deletions

View file

@ -25,7 +25,7 @@ from typing_extensions import Concatenate, ParamSpec
from homeassistant.core import HomeAssistant
import homeassistant.util.dt as dt_util
from .const import DATA_INSTANCE, SQLITE_URL_PREFIX
from .const import DATA_INSTANCE, SQLITE_URL_PREFIX, SupportedDialect
from .models import (
ALL_TABLES,
TABLE_RECORDER_RUNS,
@ -353,7 +353,7 @@ def setup_connection_for_dialect(
# Returns False if the the connection needs to be setup
# on the next connection, returns True if the connection
# never needs to be setup again.
if dialect_name == "sqlite":
if dialect_name == SupportedDialect.SQLITE:
if first_connection:
old_isolation = dbapi_connection.isolation_level
dbapi_connection.isolation_level = None
@ -381,7 +381,7 @@ def setup_connection_for_dialect(
# enable support for foreign keys
execute_on_connection(dbapi_connection, "PRAGMA foreign_keys=ON")
elif dialect_name == "mysql":
elif dialect_name == SupportedDialect.MYSQL:
execute_on_connection(dbapi_connection, "SET session wait_timeout=28800")
if first_connection:
result = query_on_connection(dbapi_connection, "SELECT VERSION()")
@ -408,7 +408,7 @@ def setup_connection_for_dialect(
version or version_string, "MySQL", MIN_VERSION_MYSQL
)
elif dialect_name == "postgresql":
elif dialect_name == SupportedDialect.POSTGRESQL:
if first_connection:
# server_version_num was added in 2006
result = query_on_connection(dbapi_connection, "SHOW server_version")
@ -455,7 +455,7 @@ def retryable_database_job(
except OperationalError as err:
assert instance.engine is not None
if (
instance.engine.dialect.name == "mysql"
instance.engine.dialect.name == SupportedDialect.MYSQL
and err.orig.args[0] in RETRYABLE_MYSQL_ERRORS
):
_LOGGER.info(
@ -481,7 +481,7 @@ def periodic_db_cleanups(instance: Recorder) -> None:
These cleanups will happen nightly or after any purge.
"""
assert instance.engine is not None
if instance.engine.dialect.name == "sqlite":
if instance.engine.dialect.name == SupportedDialect.SQLITE:
# Execute sqlite to create a wal checkpoint and free up disk space
_LOGGER.debug("WAL checkpoint")
with instance.engine.connect() as connection: