Skip to content

Commit

Permalink
iam:fix org stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljl committed Jun 26, 2024
1 parent a1b3980 commit 46b7030
Show file tree
Hide file tree
Showing 15 changed files with 401 additions and 34 deletions.
37 changes: 23 additions & 14 deletions backend/supports/iam/src/basic/serv/clients/iam_kv_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,13 @@ impl IamKvClient {
.await
}

pub async fn async_add_or_modify_key_name(
key: String,
name: String,
_funs: &TardisFunsInst,
ctx: &TardisContext,
) -> TardisResult<()> {
pub async fn async_add_or_modify_key_name(tag: String, key: String, name: String, _funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
let ctx_clone = ctx.clone();
ctx.add_async_task(Box::new(|| {
Box::pin(async move {
let task_handle = tokio::spawn(async move {
let funs = iam_constants::get_tardis_inst();
let _ = Self::add_or_modify_key_name(&key, &name, &funs, &ctx_clone).await;
let _ = Self::add_or_modify_key_name(&tag, &key, &name, &funs, &ctx_clone).await;
});
task_handle.await.unwrap();
Ok(())
Expand All @@ -69,6 +64,21 @@ impl IamKvClient {
.await
}

pub async fn async_delete_key_name(tag: String, key: String, _funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
let ctx_clone = ctx.clone();
ctx.add_async_task(Box::new(|| {
Box::pin(async move {
let task_handle = tokio::spawn(async move {
let funs = iam_constants::get_tardis_inst();
let _ = Self::delete_key_name(&tag, &key, &funs, &ctx_clone).await;
});
task_handle.await.unwrap();
Ok(())
})
}))
.await
}

pub async fn add_or_modify_item(
key: &str,
value: &Value,
Expand All @@ -80,13 +90,12 @@ impl IamKvClient {
SpiKvClient::add_or_modify_item(key, value, info, scope_level.map(|kind| kind.to_int()), funs, ctx).await
}

pub async fn add_or_modify_key_name(
key: &str,
name: &str,
funs: &TardisFunsInst,
ctx: &TardisContext,
) -> TardisResult<()> {
SpiKvClient::add_or_modify_key_name(key, name, funs, ctx).await
pub async fn add_or_modify_key_name(tag: &str, key: &str, name: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
SpiKvClient::add_or_modify_key_name(&format!("{}:{}", tag, key), name, funs, ctx).await
}

pub async fn delete_key_name(tag: &str, key: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
SpiKvClient::delete_item(&format!("__k_n__:{}:{}", tag, key), funs, ctx).await
}

pub async fn delete_item(key: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
Expand Down
132 changes: 132 additions & 0 deletions backend/supports/iam/src/basic/serv/clients/iam_stats_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use bios_basic::rbum::{
dto::rbum_filer_dto::{RbumBasicFilterReq, RbumSetCateFilterReq, RbumSetFilterReq, RbumSetItemFilterReq},
serv::{
rbum_crud_serv::RbumCrudOperation,
rbum_set_serv::{RbumSetCateServ, RbumSetItemServ, RbumSetServ},
},
};
use bios_sdk_invoke::{clients::spi_stats_client::SpiStatsClient, dto::stats_record_dto::StatsFactRecordLoadReq};
use itertools::Itertools;

use tardis::{
basic::{dto::TardisContext, result::TardisResult},
serde_json::json,
tokio, TardisFunsInst,
};

use crate::{
iam_config::{IamBasicConfigApi, IamConfig},
iam_constants,
iam_enumeration::IamSetKind,
};

use super::iam_kv_client::IamKvClient;
pub struct IamStatsClient;

impl IamStatsClient {
pub async fn async_org_fact_record_load(org_cate_id: String, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
let ctx_clone = ctx.clone();
let org_cate = RbumSetCateServ::get_rbum(
&org_cate_id,
&RbumSetCateFilterReq {
basic: RbumBasicFilterReq {
with_sub_own_paths: true,
own_paths: Some("".to_owned()),
..Default::default()
},
..Default::default()
},
funs,
ctx,
)
.await?;
let set = RbumSetServ::get_rbum(
&org_cate.rel_rbum_set_id,
&RbumSetFilterReq {
basic: RbumBasicFilterReq {
with_sub_own_paths: true,
own_paths: Some("".to_owned()),
..Default::default()
},
..Default::default()
},
funs,
ctx,
)
.await?;
// 只允许组织 Set 继续执行
if set.kind != IamSetKind::Org.to_string() {
return Ok(());
}
let account_ids = RbumSetItemServ::find_rbums(
&RbumSetItemFilterReq {
basic: RbumBasicFilterReq {
with_sub_own_paths: true,
own_paths: Some("".to_owned()),
..Default::default()
},
rel_rbum_item_disabled: Some(false),
// rel_rbum_set_id: Some(org_set_id),
rel_rbum_set_cate_ids: Some(vec![org_cate_id.clone()]),
rel_rbum_item_kind_ids: Some(vec![funs.iam_basic_kind_account_id()]),
..Default::default()
},
None,
None,
funs,
&ctx_clone,
)
.await?
.into_iter()
.map(|resp| resp.rel_rbum_item_id)
.collect();
ctx.add_async_task(Box::new(|| {
Box::pin(async move {
let task_handle = tokio::spawn(async move {
let funs = iam_constants::get_tardis_inst();
let _ = Self::org_fact_record_load(org_cate_id.clone(), account_ids, &funs, &ctx_clone).await;
let _ = IamKvClient::async_add_or_modify_key_name(funs.conf::<IamConfig>().spi.kv_orgs_prefix.clone(), org_cate_id, org_cate.name, &funs, &ctx_clone).await;
});
task_handle.await.unwrap();
Ok(())
})
}))
.await
}

pub async fn org_fact_record_load(org_id: String, account_id: Vec<String>, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
let add_req = StatsFactRecordLoadReq {
own_paths: ctx.own_paths.clone(),
ct: tardis::chrono::Utc::now(),
data: json!({
"org_id": org_id,
"account_ids": account_id,
"account_num": account_id.len(),
}),
ext: None,
};
SpiStatsClient::fact_record_load(&funs.conf::<IamConfig>().spi.kv_orgs_prefix.clone(), &org_id, add_req, funs, ctx).await?;
Ok(())
}

pub async fn async_org_fact_record_remove(org_id: String, _funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
let ctx_clone = ctx.clone();
ctx.add_async_task(Box::new(|| {
Box::pin(async move {
let task_handle = tokio::spawn(async move {
let funs = iam_constants::get_tardis_inst();
let _ = Self::org_fact_record_remove(org_id.clone(), &funs, &ctx_clone).await;
let _ = IamKvClient::async_delete_key_name(funs.conf::<IamConfig>().spi.kv_orgs_prefix.clone(), org_id, &funs, &ctx_clone).await;
});
task_handle.await.unwrap();
Ok(())
})
}))
.await
}

pub async fn org_fact_record_remove(org_id: String, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
SpiStatsClient::fact_record_delete(&funs.conf::<IamConfig>().spi.kv_orgs_prefix.clone(), &org_id, funs, ctx).await?;
Ok(())
}
}
7 changes: 3 additions & 4 deletions backend/supports/iam/src/basic/serv/iam_role_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl RbumItemCrudOperation<iam_role::ActiveModel, IamRoleAddReq, IamRoleModifyRe
ctx,
)
.await;
IamKvClient::async_add_or_modify_key_name(id.to_string(), role.name.clone(), funs, ctx).await?;
IamKvClient::async_add_or_modify_key_name("iam_role".to_string(), id.to_string(), role.name.clone(), funs, ctx).await?;

Ok(())
}
Expand Down Expand Up @@ -220,7 +220,7 @@ impl RbumItemCrudOperation<iam_role::ActiveModel, IamRoleAddReq, IamRoleModifyRe
if !op_describe.is_empty() {
let _ = IamLogClient::add_ctx_task(LogParamTag::IamRole, Some(id.to_string()), op_describe, Some(op_kind), ctx).await;
}
IamKvClient::async_add_or_modify_key_name(id.to_string(), role.name.clone(), funs, ctx).await?;
IamKvClient::async_add_or_modify_key_name("iam_role".to_string(),id.to_string(), role.name.clone(), funs, ctx).await?;

Ok(())
}
Expand Down Expand Up @@ -325,8 +325,7 @@ impl RbumItemCrudOperation<iam_role::ActiveModel, IamRoleAddReq, IamRoleModifyRe
ctx,
)
.await;
IamKvClient::async_delete_item(id.to_string(), funs, ctx).await?;

IamKvClient::async_delete_key_name("iam_role".to_string(), id.to_string(), funs, ctx).await?;
Ok(())
}

Expand Down
24 changes: 10 additions & 14 deletions backend/supports/iam/src/basic/serv/iam_set_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::iam_enumeration::{IamRelKind, IamSetCateKind, IamSetKind};

use super::clients::iam_log_client::{IamLogClient, LogParamTag};
use super::clients::iam_search_client::IamSearchClient;
use super::clients::iam_stats_client::IamStatsClient;
use super::iam_account_serv::IamAccountServ;
use super::iam_rel_serv::IamRelServ;

Expand Down Expand Up @@ -193,13 +194,7 @@ impl IamSetServ {
)
.await?;
} else if kind == IamSetKind::Org.to_string() && result.is_ok() {
SpiKvClient::add_or_modify_key_name(
&format!("{}:{}", funs.conf::<IamConfig>().spi.kv_orgs_prefix.clone(), result.clone().unwrap()),
&add_req.name,
funs,
ctx,
)
.await?;
IamStatsClient::async_org_fact_record_load(result.clone().unwrap(), &funs, &ctx).await?;
}
if result.is_ok() {
kind.make_ascii_lowercase();
Expand Down Expand Up @@ -269,13 +264,7 @@ impl IamSetServ {
)
.await?;
} else if kind == IamSetKind::Org.to_string() && result.is_ok() {
SpiKvClient::add_or_modify_key_name(
&format!("{}:{}", funs.conf::<IamConfig>().spi.kv_orgs_prefix.clone(), &set_cate_id),
&set_cate_item.name.clone(),
funs,
ctx,
)
.await?;
IamStatsClient::async_org_fact_record_load(set_cate_id.to_owned(), &funs, &ctx).await?;
}
kind.make_ascii_lowercase();
match kind.as_str() {
Expand Down Expand Up @@ -340,6 +329,9 @@ impl IamSetServ {

if result.is_ok() {
let mut kind = item.kind;
if kind == IamSetKind::Org.to_string() {
IamStatsClient::async_org_fact_record_remove(set_cate_id.to_owned(), &funs, &ctx).await?;
}
kind.make_ascii_lowercase();
let (op_describe, tag, op_kind) = match kind.as_str() {
"org" => ("删除部门".to_string(), Some(LogParamTag::IamOrg), Some("Delete".to_string())),
Expand Down Expand Up @@ -621,6 +613,7 @@ impl IamSetServ {
)
.await;
let _ = IamSearchClient::async_add_or_modify_account_search(&add_req.rel_rbum_item_id, Box::new(true), "", funs, ctx).await;
IamStatsClient::async_org_fact_record_load(set_cate_id.clone(), &funs, &ctx).await?;
}

result
Expand All @@ -647,6 +640,9 @@ impl IamSetServ {
let result = RbumSetItemServ::delete_rbum(set_item_id, funs, ctx).await;

if result.is_ok() && item.rel_rbum_item_kind_id == funs.iam_basic_kind_account_id() {
if let Some(cate_id) = item.rel_rbum_set_cate_id.clone() {
IamStatsClient::async_org_fact_record_load(cate_id, &funs, &ctx).await?;
}
if let Ok(account) = IamAccountServ::get_item(item.rel_rbum_item_id.clone().as_str(), &IamAccountFilterReq::default(), funs, ctx).await {
let _ = IamLogClient::add_ctx_task(
LogParamTag::IamOrg,
Expand Down
1 change: 1 addition & 0 deletions backend/supports/iam/src/console_common/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod iam_cc_app_api;
pub mod iam_cc_app_set_api;
pub mod iam_cc_config_api;
pub mod iam_cc_org_api;
pub mod iam_cc_org_task_api;
pub mod iam_cc_res_api;
pub mod iam_cc_role_api;
pub mod iam_cc_system_api;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::basic::serv::iam_set_serv::IamSetServ;
use crate::iam_constants;
use bios_basic::helper::request_helper::try_set_real_ip_from_req_to_ctx;
use bios_basic::rbum::dto::rbum_filer_dto::RbumSetTreeFilterReq;
use bios_basic::rbum::dto::rbum_set_dto::{RbumSetTreeCateNodeResp, RbumSetTreeCateResp, RbumSetTreeResp};
use bios_basic::rbum::dto::rbum_set_dto::{RbumSetTreeCateNodeResp, RbumSetTreeResp};
use bios_basic::rbum::rbum_enumeration::RbumSetCateLevelQueryKind;
use tardis::web::context_extractor::TardisContextExtractor;
use tardis::web::poem::Request;
Expand Down
28 changes: 28 additions & 0 deletions backend/supports/iam/src/console_common/api/iam_cc_org_task_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use bios_basic::{helper::request_helper::try_set_real_ip_from_req_to_ctx, process::task_processor::TaskProcessor};
use tardis::web::{
context_extractor::TardisContextExtractor,
poem::Request,
poem_openapi,
web_resp::{TardisApiResult, TardisResp},
};

use crate::{console_common::serv::iam_cc_org_task_serv::IamCcOrgTaskServ, iam_constants};

#[derive(Clone, Default)]
pub struct IamCcOrgTaskApi;

/// Common Console Org task API
#[poem_openapi::OpenApi(prefix_path = "/cc/org/task", tag = "bios_basic::ApiTag::Common")]
impl IamCcOrgTaskApi {
#[oai(path = "/", method = "get")]
async fn execute_org_task(&self, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Option<String>> {
try_set_real_ip_from_req_to_ctx(request, &ctx.0).await?;
let funs = iam_constants::get_tardis_inst();
IamCcOrgTaskServ::execute_org_task(&funs, &ctx.0).await?;
if let Some(task_id) = TaskProcessor::get_task_id_with_ctx(&ctx.0).await? {
TardisResp::accepted(Some(task_id))
} else {
TardisResp::ok(None)
}
}
}
1 change: 1 addition & 0 deletions backend/supports/iam/src/console_common/serv.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod iam_cc_account_task_serv;
pub mod iam_cc_org_task_serv;
pub mod iam_cc_role_task_serv;
Loading

0 comments on commit 46b7030

Please sign in to comment.