feat: refactor and multiworkspace rows

This commit is contained in:
Zack Fu Zi Xiang 2024-03-14 11:46:32 +08:00
parent 13ce89d27e
commit b68b897165
No known key found for this signature in database
6 changed files with 86 additions and 34 deletions

View File

@ -1,5 +1,6 @@
use database_entity::dto::AFWorkspace;
use serde::Deserialize;
use super::entities::{JsonResponse, UserUsageLimit};
pub async fn get_user_workspace_count(
auth_header: &str,
@ -36,13 +37,22 @@ pub async fn get_user_workspace_limit(
Ok(c as u32)
}
#[derive(Debug, Deserialize)]
pub struct JsonResponse<T> {
pub code: u16,
pub data: T,
}
pub async fn get_workspace_limit(
auth_header: &str,
appflowy_cloud_base_url: &str,
) -> Result<u32, reqwest::Error> {
let http_client = reqwest::Client::new();
let resp = http_client
.get(format!("{}/api/user/limit", appflowy_cloud_base_url))
.header("Authorization", format!("Bearer {}", auth_header))
.send()
.await?;
#[derive(Deserialize)]
pub struct UserUsageLimit {
pub workspace_count: Option<i64>,
let res = resp.json::<JsonResponse<UserUsageLimit>>().await?;
let b = res.data;
let c = b.workspace_count.unwrap_or({
tracing::warn!("workspace_count is None, returning 0");
0
});
Ok(c as u32)
}

View File

@ -0,0 +1,22 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize)]
pub struct JsonResponse<T> {
pub code: u16,
pub data: T,
}
#[derive(Deserialize)]
pub struct UserUsageLimit {
pub workspace_count: Option<i64>,
}
#[derive(Serialize)]
pub struct WorkspaceUsage {
pub name: String,
pub member_count: i64,
pub member_limit: i64,
pub total_doc_size: String,
pub total_blob_size: String,
pub total_blob_limit: String,
}

View File

@ -0,0 +1,2 @@
pub mod api;
pub mod entities;

View File

@ -1,6 +1,8 @@
use askama::Template;
use gotrue_entity::{dto::User, sso::SSOProvider};
use crate::ext::entities::WorkspaceUsage;
#[derive(Template)]
#[template(path = "components/user_usage.html")]
pub struct UserUsage {
@ -8,15 +10,11 @@ pub struct UserUsage {
pub workspace_limit: u32,
}
// ./../templates/components/workspace_usage.html
#[derive(Template)]
#[template(path = "components/workspace_usage.html")]
pub struct WorkspaceUsage<'a> {
pub name: &'a str,
pub member_count: u32,
pub member_limit: u32,
pub total_doc_size: &'a str,
pub total_blob_size: &'a str,
pub total_blob_limit: &'a str,
pub struct WorkspaceUsageList {
pub workspace_usages: Vec<WorkspaceUsage>,
}
#[derive(Template)]

View File

@ -1,5 +1,6 @@
use crate::error::WebAppError;
use crate::ext::{get_user_workspace_count, get_user_workspace_limit};
use crate::ext::api::{get_user_workspace_count, get_user_workspace_limit};
use crate::ext::entities::WorkspaceUsage;
use crate::session::UserSession;
use askama::Template;
use axum::extract::{Path, State};
@ -122,15 +123,30 @@ pub async fn user_usage_handler(
})
}
pub async fn workspace_usage_handler(session: UserSession) -> Result<Html<String>, WebAppError> {
render_template(templates::WorkspaceUsage {
name: "test",
member_count: 6,
member_limit: 7,
total_doc_size: &human_bytes(987654),
total_blob_size: &human_bytes(9876543),
total_blob_limit: &human_bytes(98765432),
})
pub async fn workspace_usage_handler(
State(app_state): State<AppState>,
session: UserSession,
) -> Result<Html<String>, WebAppError> {
let workspace_usages = vec![
WorkspaceUsage {
name: "test".to_owned(),
member_count: 6,
member_limit: 7,
total_doc_size: human_bytes(987654),
total_blob_size: human_bytes(9876543),
total_blob_limit: human_bytes(98765432),
},
WorkspaceUsage {
name: "test".to_owned(),
member_count: 6,
member_limit: 7,
total_doc_size: human_bytes(987654),
total_blob_size: human_bytes(9876543),
total_blob_limit: human_bytes(98765432),
},
];
render_template(templates::WorkspaceUsageList { workspace_usages })
}
pub async fn admin_users_create_handler() -> Result<Html<String>, WebAppError> {

View File

@ -10,13 +10,17 @@
<th>Object Storage Limit</th>
</tr>
</thead>
<tr>
<td> {{ name|escape }} </td>
<td> {{ member_count|escape }} </td>
<td> {{ member_limit|escape }} </td>
<td> {{ total_doc_size|escape }} </td>
<td> {{ total_blob_size|escape }} </td>
<td> {{ total_blob_limit|escape }} </td>
</tr>
{% for workspace_usage in workspace_usages %}
<tr>
<td> {{ workspace_usage.name|escape }} </td>
<td> {{ workspace_usage.member_count|escape }} </td>
<td> {{ workspace_usage.member_limit|escape }} </td>
<td> {{ workspace_usage.total_doc_size|escape }} </td>
<td> {{ workspace_usage.total_blob_size|escape }} </td>
<td> {{ workspace_usage.total_blob_limit|escape }} </td>
</tr>
{% endfor %}
</table>
</div>