Skip to content

Commit

Permalink
feat(controller): update deploys filter form
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxiaohei committed May 31, 2024
1 parent bce6830 commit 1aa446b
Show file tree
Hide file tree
Showing 13 changed files with 474 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ rust-embed = { version = "8.4.0", features = [
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
serde_yaml = "0.9.33"
strum = { version = "0.26.2", features = ["derive"] }
time = { version = "0.3.36", features = ["local-offset"] }
tokio = { version = "1.38.0", features = ["full"] }
tower-http = { version = "0.5.2", features = [
Expand Down
1 change: 1 addition & 0 deletions crates/core-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ tokio = { workspace = true }
toml = "0.8.13"
tracing = { workspace = true }
walkdir = { workspace = true }
strum = { workspace = true }
152 changes: 150 additions & 2 deletions crates/core-service/src/vars/admin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Result;
use land_dao::models::deployment;
use land_dao::projects;
use land_dao::deployment::{DeployStatus, DeploymentStatus};
use land_dao::models::{deployment, deployment_task};
use land_dao::{projects, user, worker};
use serde::Serialize;
use strum::IntoEnumIterator;

/// DashboardVars is the vars for admin dashboard page
#[derive(Serialize)]
Expand Down Expand Up @@ -38,6 +40,9 @@ pub struct DeployVars {
pub status: String,
pub created_at: String,
pub updated_at: String,
pub user_name: String,
pub user_email: String,
pub user_oauth: String,
}

impl DeployVars {
Expand All @@ -48,6 +53,16 @@ impl DeployVars {
project_ids.push(dp.project_id);
}
let projects = projects::list_by_ids(project_ids).await?;

let mut user_ids = vec![];
for dp in &dps {
user_ids.push(dp.user_id);
}
// unique user_ids
user_ids.sort();
user_ids.dedup();
let users = land_dao::user::list_infos(user_ids).await?;

for dp in dps {
let mut v = DeployVars {
id: dp.id,
Expand All @@ -60,13 +75,146 @@ impl DeployVars {
status: dp.status,
created_at: dp.created_at.to_string(),
updated_at: dp.updated_at.to_string(),
user_name: "".to_string(),
user_email: "".to_string(),
user_oauth: "".to_string(),
};
if let Some(project) = projects.get(&dp.project_id) {
v.language.clone_from(&project.language);
v.created_by.clone_from(&project.created_by);
}
if let Some(user) = users.get(&dp.user_id) {
v.user_name.clone_from(&user.nick_name);
v.user_email.clone_from(&user.email);
v.user_oauth.clone_from(&user.origin_provider);
}
vars.push(v);
}
Ok(vars)
}

pub async fn from_model(dp: deployment::Model) -> Result<DeployVars> {
let project = projects::get_by_id(dp.project_id, None).await?;
let user = user::get_info_by_id(dp.user_id, None).await?;
let mut v = DeployVars {
id: dp.id,
domain: dp.domain,
language: "".to_string(),
created_by: "".to_string(),
size: dp.storage_size,
deploy_status: dp.deploy_status,
deploy_message: dp.deploy_message,
status: dp.status,
created_at: dp.created_at.to_string(),
updated_at: dp.updated_at.to_string(),
user_name: "".to_string(),
user_email: "".to_string(),
user_oauth: "".to_string(),
};
if let Some(project) = project {
v.language.clone_from(&project.language);
v.created_by.clone_from(&project.created_by);
}
if let Some(user) = user {
v.user_name.clone_from(&user.nick_name);
v.user_email.clone_from(&user.email);
v.user_oauth.clone_from(&user.origin_provider);
}
Ok(v)
}
}

#[derive(Serialize)]
pub struct DeployDetailVars {
pub id: i32,
pub deploy_id: i32,
pub worker_id: i32,
pub worker_ip: String,
pub deploy_status: String,
pub deploy_message: String,
pub created_at: String,
pub updated_at: String,
}

impl DeployDetailVars {
pub async fn from_models(tasks: Vec<deployment_task::Model>) -> Result<Vec<DeployDetailVars>> {
let mut vars = vec![];
let mut worker_ids = vec![];
for task in &tasks {
worker_ids.push(task.worker_id);
}
worker_ids.sort();
worker_ids.dedup();

let workers = worker::list_by_ids(worker_ids).await?;
for task in tasks {
vars.push(DeployDetailVars {
id: task.id,
deploy_id: task.deployment_id,
worker_id: task.worker_id,
worker_ip: "".to_string(),
deploy_status: task.deploy_status,
deploy_message: task.deploy_message,
created_at: task.created_at.to_string(),
updated_at: task.updated_at.to_string(),
});
}
if !workers.is_empty() {
for v in vars.iter_mut() {
if let Some(worker) = workers.get(&v.worker_id) {
v.worker_ip.clone_from(&worker.ip);
}
}
}
Ok(vars)
}
}

#[derive(Serialize, Debug)]
pub struct DeployStatusVars {
pub value: String,
pub is_selected: bool,
}

impl DeployStatusVars {
pub fn new_list(selected: &str) -> Vec<DeployStatusVars> {
let mut vars = vec![];
vars.push(DeployStatusVars {
value: "all".to_string(),
is_selected: selected.is_empty(),
});
for status in DeployStatus::iter() {
vars.push(DeployStatusVars {
value: status.to_string(),
is_selected: status.to_string() == selected,
});
}
vars
}
}

#[derive(Serialize, Debug)]
pub struct DeployCommonStatusVars {
pub value: String,
pub label: String,
pub is_selected: bool,
}

impl DeployCommonStatusVars {
pub fn new_list(selected: &str) -> Vec<DeployCommonStatusVars> {
let mut vars = vec![];
vars.push(DeployCommonStatusVars {
value: "".to_string(),
label: "Available".to_string(),
is_selected: selected.is_empty(),
});
for status in DeploymentStatus::iter() {
vars.push(DeployCommonStatusVars {
value: status.to_string(),
label: status.to_string(),
is_selected: status.to_string() == selected,
});
}
vars
}
}
2 changes: 1 addition & 1 deletion crates/dao/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
sha2 = "0.10.8"
strum = { version = "0.26.2", features = ["derive"] }
strum = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
uuid = { workspace = true }
54 changes: 45 additions & 9 deletions crates/dao/src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Default for Spec {
}
}

#[derive(strum::Display)]
#[derive(strum::Display, PartialEq, strum::EnumString, strum::EnumIter)]
#[strum(serialize_all = "lowercase")]
pub enum DeployStatus {
Waiting,
Expand All @@ -41,7 +41,7 @@ pub enum DeployStatus {
Failed,
}

#[derive(strum::Display)]
#[derive(strum::Display, PartialEq, strum::EnumString, strum::EnumIter)]
#[strum(serialize_all = "lowercase")]
pub enum DeploymentStatus {
Active,
Expand All @@ -62,6 +62,13 @@ pub async fn get_last_by_project(project_id: i32) -> Result<Option<deployment::M
Ok(dp)
}

/// get_by_id gets a deployment by id
pub async fn get_by_id(id: i32) -> Result<Option<deployment::Model>> {
let db = DB.get().unwrap();
let dp = deployment::Entity::find_by_id(id).one(db).await?;
Ok(dp)
}

/// create a deployment
pub async fn create(
user_id: i32,
Expand Down Expand Up @@ -147,17 +154,36 @@ pub async fn list_by_deploy_status(status: DeployStatus) -> Result<Vec<deploymen
/// list_by_status_paginate gets deployments by status with pagination
pub async fn list_by_status_paginate(
status: Vec<DeploymentStatus>,
deploy_status: Vec<DeployStatus>,
current: u64,
page_size: u64,
domain: Option<String>,
) -> Result<(Vec<deployment::Model>, ItemsAndPagesNumber)> {
let mut args = vec![];
for s in status {
args.push(s.to_string());
}
let db = DB.get().unwrap();
let pager = deployment::Entity::find()
.filter(deployment::Column::Status.is_in(args))
.order_by_desc(deployment::Column::Id)
let mut select = deployment::Entity::find();

let mut args1 = vec![];
if !status.is_empty() {
for s in status {
args1.push(s.to_string());
}
select = select.filter(deployment::Column::Status.is_in(args1));
}

let mut args2 = vec![];
if !deploy_status.is_empty() {
for s in deploy_status {
args2.push(s.to_string());
}
select = select.filter(deployment::Column::DeployStatus.is_in(args2));
}

if let Some(d) = domain {
select = select.filter(deployment::Column::Domain.contains(d));
}

let pager = select
.order_by_desc(deployment::Column::UpdatedAt)
.paginate(db, page_size);
let dps = pager.fetch_page(current - 1).await?;
let pages = pager.num_items_and_pages().await?;
Expand Down Expand Up @@ -287,6 +313,16 @@ pub async fn list_tasks_by_taskid(task_id: String) -> Result<Vec<deployment_task
Ok(tasks)
}

/// list_tasks_by_deploy_id gets all tasks by deploy_id
pub async fn list_tasks_by_deploy_id(deploy_id: i32) -> Result<Vec<deployment_task::Model>> {
let db = DB.get().unwrap();
let tasks = deployment_task::Entity::find()
.filter(deployment_task::Column::DeploymentId.eq(deploy_id))
.all(db)
.await?;
Ok(tasks)
}

/// create_task creates a task
pub async fn create_task(
worker_id: i32,
Expand Down
16 changes: 16 additions & 0 deletions crates/dao/src/worker.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use crate::{db::DB, models::worker, now_time};
use anyhow::Result;
use sea_orm::{
Expand Down Expand Up @@ -32,6 +34,20 @@ pub async fn list_all() -> Result<Vec<worker::Model>> {
Ok(workers)
}

/// list_by_ids returns workers by ids
pub async fn list_by_ids(ids: Vec<i32>) -> Result<HashMap<i32, worker::Model>> {
let db = DB.get().unwrap();
let workers = worker::Entity::find()
.filter(worker::Column::Id.is_in(ids))
.all(db)
.await?;
let mut map = HashMap::new();
for w in workers {
map.insert(w.id, w);
}
Ok(map)
}

/// update_online updates worker status
pub async fn update_online(
ip: String,
Expand Down
Loading

0 comments on commit 1aa446b

Please sign in to comment.