-
-
Notifications
You must be signed in to change notification settings - Fork 114
feat: add api to get information about diskspace usage of database. (jsonrpc method: get_storage_usage_report_string)
#7486
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
32d6553
feat: add api to get information about diskspace usage of database.
Simon-Laux 56cd771
fix that `largest_webxdc_data` result was ignored
Simon-Laux 6fb3b9a
fix allignment in the string report
Simon-Laux 2562354
use write! macro instead of building result string
Simon-Laux 6fe3e18
add hint that rust api users shouldn't rely on the content format of
Simon-Laux d7aba0a
remove storage usage from Context.get_info
Simon-Laux d55a5e4
dedicated method, not part of context
Simon-Laux 503ef27
remove another outdated comment
Simon-Laux cccab2b
remove check for dbstat
Simon-Laux 1677561
comment: safety -> security
Simon-Laux d466c6e
fix jsonrpc api
Simon-Laux 8f817c0
remove experimental warning
Simon-Laux ff45f58
rename api to storage
Simon-Laux 38c7d61
fill largest_tables in place
Simon-Laux ac6f12a
more space to storage renaming
Simon-Laux 6ec1733
use MsgId type for webxdc instance ids instead of usize
Simon-Laux 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| //! Module to collect and display Disk Space Usage of a Profile. | ||
| use crate::{context::Context, message::MsgId}; | ||
| use anyhow::Result; | ||
| use humansize::{BINARY, format_size}; | ||
|
|
||
| /// Storage Usage Report | ||
| /// Useful for debugging space usage problems in the deltachat database. | ||
| #[derive(Debug)] | ||
| pub struct StorageUsage { | ||
| /// Total database size, subtract this from the backup size to estimate size of all blobs | ||
| pub db_size: usize, | ||
| /// size and row count of the 10 biggest tables | ||
| pub largest_tables: Vec<(String, usize, Option<usize>)>, | ||
| /// count and total size of status updates | ||
| /// for the 10 webxdc apps with the most size usage in status updates | ||
| pub largest_webxdc_data: Vec<(MsgId, usize, usize)>, | ||
| } | ||
|
|
||
| impl std::fmt::Display for StorageUsage { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| writeln!(f, "Storage Usage:")?; | ||
| let human_db_size = format_size(self.db_size, BINARY); | ||
| writeln!(f, "[Database Size]: {human_db_size}")?; | ||
| writeln!(f, "[Largest Tables]:")?; | ||
| for (name, size, row_count) in &self.largest_tables { | ||
| let human_table_size = format_size(*size, BINARY); | ||
| writeln!( | ||
| f, | ||
| " {name:<20} {human_table_size:>10}, {row_count:>6} rows", | ||
| name = format!("{name}:"), | ||
| row_count = row_count.map(|c| c.to_string()).unwrap_or("?".to_owned()) | ||
| )?; | ||
| } | ||
| writeln!(f, "[Webxdc With Biggest Status Update Space Usage]:")?; | ||
| for (msg_id, size, update_count) in &self.largest_webxdc_data { | ||
| let human_size = format_size(*size, BINARY); | ||
| writeln!( | ||
| f, | ||
| " {msg_id:<8} {human_size:>10} across {update_count:>5} updates", | ||
| msg_id = format!("{msg_id}:") | ||
| )?; | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Get storage usage information for the Context's database | ||
| pub async fn get_storage_usage(ctx: &Context) -> Result<StorageUsage> { | ||
| let page_size: usize = ctx | ||
| .sql | ||
| .query_get_value("PRAGMA page_size", ()) | ||
| .await? | ||
| .unwrap_or_default(); | ||
| let page_count: usize = ctx | ||
| .sql | ||
| .query_get_value("PRAGMA page_count", ()) | ||
| .await? | ||
| .unwrap_or_default(); | ||
|
|
||
| let mut largest_tables = ctx | ||
| .sql | ||
| .query_map_vec( | ||
| "SELECT name, | ||
| SUM(pgsize) AS size | ||
| FROM dbstat | ||
| WHERE name IN (SELECT name FROM sqlite_master WHERE type='table') | ||
| GROUP BY name ORDER BY size DESC LIMIT 10", | ||
| (), | ||
| |row| { | ||
| let name: String = row.get(0)?; | ||
| let size: usize = row.get(1)?; | ||
| Ok((name, size, None)) | ||
| }, | ||
| ) | ||
| .await?; | ||
|
|
||
| for row in &mut largest_tables { | ||
| let name = &row.0; | ||
| let row_count: Result<Option<usize>> = ctx | ||
| .sql | ||
| // SECURITY: the table name comes from the db, not from the user | ||
| .query_get_value(&format!("SELECT COUNT(*) FROM {name}"), ()) | ||
| .await; | ||
| row.2 = row_count.unwrap_or_default(); | ||
| } | ||
|
|
||
| let largest_webxdc_data = ctx | ||
| .sql | ||
| .query_map_vec( | ||
| "SELECT msg_id, SUM(length(update_item)) as size, COUNT(*) as update_count | ||
| FROM msgs_status_updates | ||
| GROUP BY msg_id ORDER BY size DESC LIMIT 10", | ||
| (), | ||
| |row| { | ||
| let msg_id: MsgId = row.get(0)?; | ||
| let size: usize = row.get(1)?; | ||
| let count: usize = row.get(2)?; | ||
|
|
||
| Ok((msg_id, size, count)) | ||
| }, | ||
| ) | ||
| .await?; | ||
|
|
||
| Ok(StorageUsage { | ||
| db_size: page_size * page_count, | ||
| largest_tables, | ||
| largest_webxdc_data, | ||
| }) | ||
| } | ||
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.