Compare commits

...

3 Commits

Author SHA1 Message Date
B. Petersen
e00d2402cf add number of files and bytes to get_info() 2020-11-24 20:58:24 +01:00
B. Petersen
4d5872632b show database size as bytes in dc_get_info() 2020-11-24 18:16:26 +01:00
B. Petersen
078d1837b1 change label of 'database_dir' to more correct 'database' (it shows the file, not the directory) 2020-11-24 17:52:15 +01:00
2 changed files with 34 additions and 5 deletions

View File

@@ -12,7 +12,7 @@ use crate::chat::*;
use crate::config::Config;
use crate::constants::*;
use crate::contact::*;
use crate::dc_tools::duration_to_str;
use crate::dc_tools::{dc_get_dirbytes, dc_get_filebytes, duration_to_str};
use crate::error::*;
use crate::events::{Event, EventEmitter, EventType, Events};
use crate::key::{DcKey, SignedPublicKey};
@@ -262,6 +262,9 @@ impl Context {
******************************************************************************/
pub async fn get_info(&self) -> BTreeMap<&'static str, String> {
let blobdir = self.get_blobdir();
let (blobdir_files, blobdir_bytes) = dc_get_dirbytes(self, blobdir).await;
let unset = "0";
let l = LoginParam::from_database(self, "").await;
let l2 = LoginParam::from_database(self, "configured_").await;
@@ -323,10 +326,16 @@ impl Context {
res.insert("number_of_chat_messages", real_msgs.to_string());
res.insert("messages_in_contact_requests", deaddrop_msgs.to_string());
res.insert("number_of_contacts", contacts.to_string());
res.insert("database_dir", self.get_dbfile().display().to_string());
res.insert("database", self.get_dbfile().display().to_string());
res.insert("database_version", dbversion.to_string());
res.insert(
"database_bytes",
dc_get_filebytes(self, self.get_dbfile()).await.to_string(),
);
res.insert("journal_mode", journal_mode);
res.insert("blobdir", self.get_blobdir().display().to_string());
res.insert("blobdir", blobdir.display().to_string());
res.insert("blobdir_files", blobdir_files.to_string());
res.insert("blobdir_bytes", blobdir_bytes.to_string());
res.insert("display_name", displayname.unwrap_or_else(|| unset.into()));
res.insert(
"selfavatar",
@@ -595,14 +604,16 @@ mod tests {
let t = TestContext::new().await;
let info = t.ctx.get_info().await;
assert!(info.get("database_dir").is_some());
assert!(info.get("database").is_some());
assert!(info.get("database_bytes").unwrap().parse::<u64>().unwrap() > 1000);
}
#[test]
fn test_get_info_no_context() {
let info = get_info();
assert!(info.get("deltachat_core_version").is_some());
assert!(info.get("database_dir").is_none());
assert!(info.get("database").is_none());
assert!(info.get("database_bytes").is_none());
assert_eq!(info.get("level").unwrap(), "awesome");
}
}

View File

@@ -339,6 +339,24 @@ pub(crate) async fn dc_get_filebytes(context: &Context, path: impl AsRef<Path>)
}
}
pub(crate) async fn dc_get_dirbytes(context: &Context, path: impl AsRef<Path>) -> (usize, u64) {
let path_abs = dc_get_abs_path(context, &path);
let mut files: usize = 0;
let mut bytes: u64 = 0;
if let Ok(mut read_dir) = async_std::fs::read_dir(path_abs).await {
while let Some(entry) = read_dir.next().await {
if let Ok(entry) = entry {
files += 1;
bytes += match entry.metadata().await {
Ok(meta) => meta.len(),
Err(_err) => 0,
}
}
}
}
(files, bytes)
}
pub(crate) async fn dc_delete_file(context: &Context, path: impl AsRef<Path>) -> bool {
let path_abs = dc_get_abs_path(context, &path);
if !path_abs.exists().await {