tweak quota display (#2616)

* resultify get_connectivity_html()

* tweak quota titles, avoid confusing 'quota' wording

* show bars for quota usage

* skip secondady 'Storage' title, see comment for reasoning
This commit is contained in:
bjoern
2021-08-22 12:46:46 +02:00
committed by GitHub
parent 3440daca1a
commit d0c97bce4c
3 changed files with 65 additions and 22 deletions

View File

@@ -275,7 +275,15 @@ pub unsafe extern "C" fn dc_get_connectivity_html(
return "".strdup();
}
let ctx = &*context;
block_on(async move { ctx.get_connectivity_html().await.strdup() })
block_on(async move {
match ctx.get_connectivity_html().await {
Ok(html) => html.strdup(),
Err(err) => {
error!(ctx, "Failed to get connectivity html: {}", err);
"".strdup()
}
}
})
}
#[no_mangle]

View File

@@ -514,9 +514,15 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
let file = dirs::home_dir()
.unwrap_or_default()
.join("connectivity.html");
let html = context.get_connectivity_html().await;
fs::write(&file, html)?;
println!("Report written to: {:#?}", file);
match context.get_connectivity_html().await {
Ok(html) => {
fs::write(&file, html)?;
println!("Report written to: {:#?}", file);
}
Err(err) => {
bail!("Failed to get connectivity html: {}", err);
}
}
}
"maybenetwork" => {
context.maybe_network().await;

View File

@@ -8,8 +8,9 @@ use crate::events::EventType;
use crate::quota::{
QUOTA_ERROR_THRESHOLD_PERCENTAGE, QUOTA_MAX_AGE_SECONDS, QUOTA_WARN_THRESHOLD_PERCENTAGE,
};
use crate::{config::Config, scheduler::Scheduler};
use crate::{config::Config, dc_tools, scheduler::Scheduler};
use crate::{context::Context, log::LogExt};
use anyhow::{anyhow, Result};
use humansize::{file_size_opts, FileSize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumProperty, PartialOrd, Ord)]
@@ -297,7 +298,7 @@ impl Context {
///
/// This comes as an HTML from the core so that we can easily improve it
/// and the improvement instantly reaches all UIs.
pub async fn get_connectivity_html(&self) -> String {
pub async fn get_connectivity_html(&self) -> Result<String> {
let mut ret = r#"<!DOCTYPE html>
<html>
<head>
@@ -308,13 +309,29 @@ impl Context {
list-style-type: none;
padding-left: 1em;
}
.dot {
height: 0.9em; width: 0.9em;
border: 1px solid #888;
border-radius: 50%;
display: inline-block;
position: relative; left: -0.1em; top: 0.1em;
}
.bar {
width: 90%;
border: 1px solid #888;
border-radius: .5em;
margin-top: .2em;
margin-bottom: 1em;
position: relative; left: -0.2em;
}
.progress {
min-width:1.8em;
height: 1em;
border-radius: .45em;
color: white;
text-align: center;
padding-bottom: 2px;
}
.red {
background-color: #f33b2d;
}
@@ -358,8 +375,7 @@ impl Context {
smtp.state.connectivity.clone(),
),
Scheduler::Stopped => {
ret += "Not started</body></html>\n";
return ret;
return Err(anyhow!("Not started"));
}
};
drop(lock);
@@ -408,7 +424,14 @@ impl Context {
ret += &*escaper::encode_minimal(&detailed.to_string_smtp(self));
ret += "</li></ul>";
ret += "<h3>Quota</h3><ul>";
let domain = dc_tools::EmailAddress::new(
&self
.get_config(Config::ConfiguredAddr)
.await?
.unwrap_or_default(),
)?
.domain;
ret += &format!("<h3>Storage on {}</h3><ul>", domain);
let quota = self.quota.read().await;
if let Some(quota) = &*quota {
match &quota.recent {
@@ -419,15 +442,6 @@ impl Context {
for resource in resources {
ret += "<li>";
let usage_percent = resource.get_usage_percentage();
if usage_percent >= QUOTA_ERROR_THRESHOLD_PERCENTAGE {
ret += "<span class=\"red dot\"></span> ";
} else if usage_percent >= QUOTA_WARN_THRESHOLD_PERCENTAGE {
ret += "<span class=\"yellow dot\"></span> ";
} else {
ret += "<span class=\"green dot\"></span> ";
}
// root name is empty eg. for gmail and redundant eg. for riseup.
// therefore, use it only if there are really several roots.
if roots_cnt > 1 && !root_name.is_empty() {
@@ -454,16 +468,31 @@ impl Context {
)
}
Storage => {
// do not use a special title needed for "Storage":
// - it is usually shown directly under the "Storage" headline
// - by the units "1 MB of 10 MB used" there is some difference to eg. "Messages: 1 of 10 used"
// - the string is not longer than the other strings that way (minus title, plus units) -
// additional linebreaks on small displays are unlikely therefore
// - most times, this is the only item anyway
let usage = (resource.usage * 1024)
.file_size(file_size_opts::BINARY)
.unwrap_or_default();
let limit = (resource.limit * 1024)
.file_size(file_size_opts::BINARY)
.unwrap_or_default();
format!("<b>Storage:</b> {} of {} used", usage, limit)
format!("{} of {} used", usage, limit)
}
};
ret += &format!(" ({}%)", usage_percent);
let percent = resource.get_usage_percentage();
let color = if percent >= QUOTA_ERROR_THRESHOLD_PERCENTAGE {
"red"
} else if percent >= QUOTA_WARN_THRESHOLD_PERCENTAGE {
"yellow"
} else {
"green"
};
ret += &format!("<div class=\"bar\"><div class=\"progress {}\" style=\"width: {}%\">{}%</div></div>", color, percent, percent);
ret += "</li>";
}
@@ -484,7 +513,7 @@ impl Context {
ret += "</ul>";
ret += "</body></html>\n";
ret
Ok(ret)
}
pub async fn all_work_done(&self) -> bool {