-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ljl
committed
Jun 26, 2024
1 parent
a1b3980
commit 46b7030
Showing
15 changed files
with
401 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
backend/supports/iam/src/basic/serv/clients/iam_stats_client.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
backend/supports/iam/src/console_common/api/iam_cc_org_task_api.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.