feat: file blob usage
This commit is contained in:
parent
3041812d23
commit
d7cd101c44
|
|
@ -1,6 +1,9 @@
|
||||||
use database_entity::dto::AFWorkspace;
|
use database_entity::dto::AFWorkspace;
|
||||||
|
|
||||||
use super::entities::{JsonResponse, UserUsageLimit, WorkspaceMember, WorkspaceUsage};
|
use super::entities::{
|
||||||
|
JsonResponse, UserUsageLimit, WorkspaceBlobUsage, WorkspaceMember, WorkspaceUsage,
|
||||||
|
WorkspaceUsageLimit,
|
||||||
|
};
|
||||||
|
|
||||||
pub async fn get_user_workspace_count(auth_header: &str, appflowy_cloud_base_url: &str) -> u32 {
|
pub async fn get_user_workspace_count(auth_header: &str, appflowy_cloud_base_url: &str) -> u32 {
|
||||||
let workspaces = get_user_workspaces(auth_header, appflowy_cloud_base_url).await;
|
let workspaces = get_user_workspaces(auth_header, appflowy_cloud_base_url).await;
|
||||||
|
|
@ -72,15 +75,32 @@ pub async fn get_user_workspace_usages(
|
||||||
for user_workspace in user_workspaces {
|
for user_workspace in user_workspaces {
|
||||||
let workspace_id = user_workspace.workspace_id.to_string();
|
let workspace_id = user_workspace.workspace_id.to_string();
|
||||||
let members =
|
let members =
|
||||||
get_user_workspace_members(&workspace_id, auth_header, appflowy_cloud_gateway_base_url).await;
|
get_user_workspace_members(&workspace_id, auth_header, appflowy_cloud_base_url).await;
|
||||||
|
let workspace_limits =
|
||||||
|
get_user_workspace_limits(&workspace_id, auth_header, appflowy_cloud_gateway_base_url).await;
|
||||||
|
let blob_usage =
|
||||||
|
get_user_workspace_blob_usage(&workspace_id, auth_header, appflowy_cloud_base_url)
|
||||||
|
.await
|
||||||
|
.map(|u| u.consumed_capacity)
|
||||||
|
.unwrap_or_else(|err| {
|
||||||
|
tracing::error!("Error getting user workspace blob usage: {:?}", err);
|
||||||
|
0
|
||||||
|
});
|
||||||
|
let (member_limit, total_blob_limit) = match workspace_limits {
|
||||||
|
Some(limit) => (
|
||||||
|
limit.member_count.to_string(),
|
||||||
|
human_bytes::human_bytes(limit.total_blob_size as f64),
|
||||||
|
),
|
||||||
|
None => ("N/A".to_string(), "N/A".to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
workspace_usages.push(WorkspaceUsage {
|
workspace_usages.push(WorkspaceUsage {
|
||||||
name: user_workspace.workspace_name,
|
name: user_workspace.workspace_name,
|
||||||
member_count: members.len(),
|
member_count: members.len(),
|
||||||
member_limit: 98798, // todo
|
member_limit,
|
||||||
total_doc_size: human_bytes::human_bytes(987654), // todo
|
total_doc_size: "WIP".to_string(),
|
||||||
total_blob_size: human_bytes::human_bytes(9876543), // todo
|
total_blob_size: human_bytes::human_bytes(blob_usage as f64),
|
||||||
total_blob_limit: human_bytes::human_bytes(98765432), // todo
|
total_blob_limit,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,3 +138,54 @@ async fn get_user_workspace_members(
|
||||||
};
|
};
|
||||||
res.data
|
res.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_user_workspace_limits(
|
||||||
|
workspace_id: &str,
|
||||||
|
auth_header: &str,
|
||||||
|
appflowy_cloud_gateway_base_url: &str,
|
||||||
|
) -> Option<WorkspaceUsageLimit> {
|
||||||
|
let http_client = reqwest::Client::new();
|
||||||
|
let resp = match http_client
|
||||||
|
.get(format!(
|
||||||
|
"{}/api/workspace/{}/limit",
|
||||||
|
appflowy_cloud_gateway_base_url, workspace_id
|
||||||
|
))
|
||||||
|
.header("Authorization", format!("Bearer {}", auth_header))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(resp) => resp,
|
||||||
|
Err(err) => {
|
||||||
|
tracing::error!("Error getting user workspace members: {:?}", err);
|
||||||
|
return None;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = match resp.json::<JsonResponse<WorkspaceUsageLimit>>().await {
|
||||||
|
Ok(res) => res,
|
||||||
|
Err(err) => {
|
||||||
|
tracing::error!("Error parsing user workspace limit: {:?}", err);
|
||||||
|
return None;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
Some(res.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_user_workspace_blob_usage(
|
||||||
|
workspace_id: &str,
|
||||||
|
auth_header: &str,
|
||||||
|
appflowy_cloud_gateway_base_url: &str,
|
||||||
|
) -> Result<WorkspaceBlobUsage, reqwest::Error> {
|
||||||
|
let http_client = reqwest::Client::new();
|
||||||
|
let resp = http_client
|
||||||
|
.get(format!(
|
||||||
|
"{}/api/file_storage/{}/usage",
|
||||||
|
appflowy_cloud_gateway_base_url, workspace_id
|
||||||
|
))
|
||||||
|
.header("Authorization", format!("Bearer {}", auth_header))
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let res = resp.json::<JsonResponse<WorkspaceBlobUsage>>().await?;
|
||||||
|
Ok(res.data)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ pub struct UserUsageLimit {
|
||||||
pub struct WorkspaceUsage {
|
pub struct WorkspaceUsage {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub member_count: usize,
|
pub member_count: usize,
|
||||||
pub member_limit: i64,
|
pub member_limit: String,
|
||||||
pub total_doc_size: String,
|
pub total_doc_size: String,
|
||||||
pub total_blob_size: String,
|
pub total_blob_size: String,
|
||||||
pub total_blob_limit: String,
|
pub total_blob_limit: String,
|
||||||
|
|
@ -27,3 +27,15 @@ pub struct WorkspaceMember {
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub role: String,
|
pub role: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct WorkspaceUsageLimit {
|
||||||
|
pub total_blob_size: i64,
|
||||||
|
pub single_blob_size: i64,
|
||||||
|
pub member_count: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub struct WorkspaceBlobUsage {
|
||||||
|
pub consumed_capacity: u64,
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@ pub async fn user_usage_handler(
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map(|limit| limit.to_string())
|
.map(|limit| limit.to_string())
|
||||||
.unwrap_or("".to_owned());
|
.unwrap_or("N/A".to_owned());
|
||||||
|
|
||||||
render_template(templates::UserUsage {
|
render_template(templates::UserUsage {
|
||||||
workspace_count,
|
workspace_count,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue