-
-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Add Estimated Database Size to the recorder system health #71463
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
homeassistant/components/recorder/system_health/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """Provide info to system health.""" | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from typing import Any | ||
|
|
||
| from sqlalchemy.orm.session import Session | ||
| from yarl import URL | ||
|
|
||
| from homeassistant.components import system_health | ||
| from homeassistant.core import HomeAssistant, callback | ||
|
|
||
| from .. import get_instance | ||
| from ..const import DIALECT_MYSQL, DIALECT_POSTGRESQL, DIALECT_SQLITE | ||
| from .mysql import db_size_bytes as mysql_db_size_bytes | ||
| from .postgresql import db_size_bytes as postgresql_db_size_bytes | ||
| from .sqlite import db_size_bytes as sqlite_db_size_bytes | ||
|
|
||
| DIALECT_TO_GET_SIZE = { | ||
| DIALECT_SQLITE: sqlite_db_size_bytes, | ||
| DIALECT_MYSQL: mysql_db_size_bytes, | ||
| DIALECT_POSTGRESQL: postgresql_db_size_bytes, | ||
| } | ||
|
|
||
|
|
||
| @callback | ||
| def async_register( | ||
| hass: HomeAssistant, register: system_health.SystemHealthRegistration | ||
| ) -> None: | ||
| """Register system health callbacks.""" | ||
| register.async_register_info(system_health_info) | ||
|
|
||
|
|
||
| def _get_estimated_db_size( | ||
| session_maker: Callable[[], Session], dialect: str, database_name: str | ||
| ) -> str | None: | ||
| """Get the estimated size of the database.""" | ||
| if (get_size := DIALECT_TO_GET_SIZE.get(dialect)) and ( | ||
| db_bytes := get_size(session_maker(), database_name) | ||
| ): | ||
| return f"{db_bytes/1024/1024:.2f} MiB" | ||
| return None | ||
|
|
||
|
|
||
| async def system_health_info(hass: HomeAssistant) -> dict[str, Any]: | ||
| """Get info for the info page.""" | ||
| instance = get_instance(hass) | ||
| run_history = instance.run_history | ||
| database_name = URL(instance.db_url).path.lstrip("/") | ||
| estimated_db_size = await instance.async_add_executor_job( | ||
| _get_estimated_db_size, instance.get_session, instance.dialect, database_name | ||
| ) | ||
| health_data = { | ||
| "oldest_recorder_run": run_history.first.start, | ||
| "current_recorder_run": run_history.current.start, | ||
| } | ||
| if estimated_db_size: | ||
| health_data["estimated_db_size"] = estimated_db_size | ||
| return health_data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| """Provide info to system health for mysql.""" | ||
| from __future__ import annotations | ||
|
|
||
| from sqlalchemy import text | ||
| from sqlalchemy.orm.session import Session | ||
|
|
||
|
|
||
| def db_size_bytes(session: Session, database_name: str) -> float: | ||
| """Get the mysql database size.""" | ||
| return float( | ||
| session.execute( | ||
| text( | ||
| "SELECT ROUND(SUM(DATA_LENGTH + INDEX_LENGTH), 2) " | ||
| "FROM information_schema.TABLES WHERE " | ||
| "TABLE_SCHEMA=:database_name" | ||
| ), | ||
| {"database_name": database_name}, | ||
| ).first()[0] | ||
| ) |
15 changes: 15 additions & 0 deletions
15
homeassistant/components/recorder/system_health/postgresql.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| """Provide info to system health for postgresql.""" | ||
| from __future__ import annotations | ||
|
|
||
| from sqlalchemy import text | ||
| from sqlalchemy.orm.session import Session | ||
|
|
||
|
|
||
| def db_size_bytes(session: Session, database_name: str) -> float: | ||
| """Get the mysql database size.""" | ||
| return float( | ||
| session.execute( | ||
| text("select pg_database_size(:database_name);"), | ||
| {"database_name": database_name}, | ||
| ).first()[0] | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| """Provide info to system health for sqlite.""" | ||
| from __future__ import annotations | ||
|
|
||
| from sqlalchemy import text | ||
| from sqlalchemy.orm.session import Session | ||
|
|
||
|
|
||
| def db_size_bytes(session: Session, database_name: str) -> float: | ||
| """Get the mysql database size.""" | ||
| return float( | ||
| session.execute( | ||
| text( | ||
| "SELECT page_count * page_size as size " | ||
| "FROM pragma_page_count(), pragma_page_size();" | ||
| ) | ||
| ).first()[0] | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.