Skip to content

Commit

Permalink
flow: fix bug (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzIsGod1019 authored Jul 4, 2024
1 parent b5d4e71 commit db807ff
Show file tree
Hide file tree
Showing 22 changed files with 943 additions and 384 deletions.
46 changes: 37 additions & 9 deletions backend/middlewares/flow/src/api/ca/flow_ca_model_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use tardis::{
};

use crate::{
dto::flow_model_dto::{FlowModelAggResp, FlowModelAssociativeOperationKind, FlowModelCopyOrReferenceReq},
dto::flow_model_dto::{FlowModelAggResp, FlowModelAssociativeOperationKind, FlowModelCopyOrReferenceReq, FlowModelSingleCopyOrReferenceReq},
flow_constants,
serv::flow_model_serv::FlowModelServ,
serv::{flow_inst_serv::FlowInstServ, flow_model_serv::FlowModelServ},
};

#[derive(Clone)]
Expand All @@ -35,24 +35,52 @@ impl FlowCaModelApi {
) -> TardisApiResult<HashMap<String, FlowModelAggResp>> {
let mut funs = flow_constants::get_tardis_inst();
funs.begin().await?;
let _orginal_models = FlowModelServ::clean_rel_models(None, None, None, &funs, &ctx.0).await?;
let mut result = HashMap::new();
let orginal_models = FlowModelServ::find_rel_models(None, true, &funs, &ctx.0).await?;
let mock_ctx = match req.0.op {
FlowModelAssociativeOperationKind::Copy => ctx.0.clone(),
FlowModelAssociativeOperationKind::Reference => TardisContext {
own_paths: rbum_scope_helper::get_path_item(RbumScopeLevelKind::L1.to_int(), &ctx.0.own_paths).unwrap_or_default(),
..ctx.0.clone()
},
};
for (tag, rel_model_id) in req.0.rel_model_ids {
let orginal_model_id = orginal_models.get(&tag).map(|orginal_model| orginal_model.id.clone());
result.insert(
rel_model_id.clone(),
FlowModelServ::copy_or_reference_model(orginal_model_id, &rel_model_id, Some(ctx.0.own_paths.clone()), &req.0.op, Some(false), &funs, &mock_ctx).await?,
);
for (_, rel_model_id) in req.0.rel_model_ids {
let new_model = FlowModelServ::copy_or_reference_model(&rel_model_id, Some(ctx.0.own_paths.clone()), &req.0.op, Some(false), &funs, &mock_ctx).await?;
FlowInstServ::batch_update_when_switch_model(None, &new_model.tag, &new_model.id, new_model.states.clone(), &new_model.init_state_id, &funs, &ctx.0).await?;

result.insert(rel_model_id.clone(), new_model);
}

funs.commit().await?;
ctx.0.execute_task().await?;
TardisResp::ok(result)
}

/// Creating or referencing single model
///
/// 创建或引用单个模型
#[oai(path = "/copy_or_reference_single_model", method = "post")]
async fn copy_or_reference_single_model(
&self,
req: Json<FlowModelSingleCopyOrReferenceReq>,
ctx: TardisContextExtractor,
_request: &Request,
) -> TardisApiResult<FlowModelAggResp> {
let mut funs = flow_constants::get_tardis_inst();
funs.begin().await?;
let _orginal_models = FlowModelServ::clean_rel_models(None, None, Some(vec![req.0.tag.clone()]), &funs, &ctx.0).await?;
let mock_ctx = match req.0.op {
FlowModelAssociativeOperationKind::Copy => ctx.0.clone(),
FlowModelAssociativeOperationKind::Reference => TardisContext {
own_paths: rbum_scope_helper::get_path_item(RbumScopeLevelKind::L1.to_int(), &ctx.0.own_paths).unwrap_or_default(),
..ctx.0.clone()
},
};
let new_model = FlowModelServ::copy_or_reference_model(&req.0.rel_model_id, Some(ctx.0.own_paths.clone()), &req.0.op, Some(false), &funs, &mock_ctx).await?;
FlowInstServ::batch_update_when_switch_model(None, &new_model.tag, &new_model.id, new_model.states.clone(), &new_model.init_state_id, &funs, &ctx.0).await?;

funs.commit().await?;
ctx.0.execute_task().await?;
TardisResp::ok(new_model)
}
}
82 changes: 12 additions & 70 deletions backend/middlewares/flow/src/api/cc/flow_cc_model_api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::collections::HashMap;

use bios_basic::rbum::dto::rbum_filer_dto::{RbumBasicFilterReq, RbumItemRelFilterReq};
use bios_basic::rbum::rbum_enumeration::RbumRelFromKind;
use bios_basic::rbum::dto::rbum_filer_dto::RbumBasicFilterReq;
use bios_basic::rbum::serv::rbum_item_serv::RbumItemCrudOperation;
use itertools::Itertools;
use tardis::basic::dto::TardisContext;
use tardis::futures::future::join_all;
use tardis::web::context_extractor::TardisContextExtractor;
use tardis::web::poem::Request;
use tardis::web::poem_openapi;
Expand All @@ -21,7 +18,6 @@ use crate::dto::flow_state_dto::FlowStateRelModelModifyReq;
use crate::dto::flow_transition_dto::{FlowTransitionModifyReq, FlowTransitionSortStatesReq};
use crate::flow_constants;
use crate::serv::flow_model_serv::FlowModelServ;
use crate::serv::flow_rel_serv::{FlowRelKind, FlowRelServ};
#[derive(Clone)]
pub struct FlowCcModelApi;

Expand Down Expand Up @@ -83,70 +79,7 @@ impl FlowCcModelApi {
_request: &Request,
) -> TardisApiResult<Vec<FlowModelSummaryResp>> {
let funs = flow_constants::get_tardis_inst();
let mut result = vec![];
let mut not_bind_template_models = join_all(
FlowModelServ::find_items(
&FlowModelFilterReq {
basic: RbumBasicFilterReq {
ignore_scope: true,
with_sub_own_paths: false,
..Default::default()
},
tags: Some(vec![tag.0.clone()]),
template: template.0,
..Default::default()
},
Some(true),
None,
&funs,
&ctx.0,
)
.await?
.into_iter()
.map(|model| async move {
let funs = flow_constants::get_tardis_inst();
let global_ctx: TardisContext = TardisContext::default();
if FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowModelTemplate, &model.id, None, None, &funs, &global_ctx).await.unwrap().is_empty() {
Some(model)
} else {
None
}
}),
)
.await
.into_iter()
.flatten()
.collect_vec();
result.append(&mut not_bind_template_models);
if let Some(rel_template_id) = rel_template_id.0 {
let mut rel_template_models = FlowModelServ::find_items(
&FlowModelFilterReq {
basic: RbumBasicFilterReq {
ignore_scope: true,
with_sub_own_paths: false,
..Default::default()
},
tags: Some(vec![tag.0.clone()]),
template: template.0,
rel: Some(RbumItemRelFilterReq {
optional: false,
rel_by_from: true,
tag: Some(FlowRelKind::FlowModelTemplate.to_string()),
from_rbum_kind: Some(RbumRelFromKind::Item),
rel_item_id: Some(rel_template_id),
..Default::default()
}),
..Default::default()
},
Some(true),
None,
&funs,
&ctx.0,
)
.await?;
result.append(&mut rel_template_models);
}
TardisResp::ok(result)
TardisResp::ok(FlowModelServ::find_models_by_rel_template_id(tag.0, template.0, rel_template_id.0, &funs, &ctx.0).await?)
}

/// Find Models
Expand Down Expand Up @@ -414,7 +347,16 @@ impl FlowCcModelApi {
async fn modify_rel_state(&self, flow_model_id: Path<String>, req: Json<FlowStateRelModelModifyReq>, ctx: TardisContextExtractor, _request: &Request) -> TardisApiResult<Void> {
let mut funs = flow_constants::get_tardis_inst();
funs.begin().await?;
FlowModelServ::modify_rel_state_ext(&flow_model_id.0, &req.0, &funs, &ctx.0).await?;
FlowModelServ::modify_model(
&flow_model_id.0,
&mut FlowModelModifyReq {
modify_states: Some(vec![req.0]),
..Default::default()
},
&funs,
&ctx.0,
)
.await?;
funs.commit().await?;
ctx.0.execute_task().await?;
TardisResp::ok(Void {})
Expand Down
19 changes: 19 additions & 0 deletions backend/middlewares/flow/src/api/ci/flow_ci_inst_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,23 @@ impl FlowCiInstApi {
});
TardisResp::ok(Void {})
}

///Script: update tag information for current instance
///
/// 数据修复脚本:更新当前实例的tag信息
#[oai(path = "/reflesh_inst_tag", method = "post")]
async fn reflesh_inst_tag(&self) -> TardisApiResult<Void> {
let funs = flow_constants::get_tardis_inst();
tokio::spawn(async move {
match FlowInstServ::reflesh_inst_tag(&funs).await {
Ok(_) => {
log::trace!("[Flow.Inst] add log success")
}
Err(e) => {
log::warn!("[Flow.Inst] failed to add log:{e}")
}
}
});
TardisResp::ok(Void {})
}
}
76 changes: 58 additions & 18 deletions backend/middlewares/flow/src/api/ci/flow_ci_model_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::dto::flow_model_dto::{
FlowModelFilterReq, FlowModelFindRelStateResp,
};
use crate::flow_constants;
use crate::serv::flow_inst_serv::FlowInstServ;
use crate::serv::flow_model_serv::FlowModelServ;
use crate::serv::flow_rel_serv::{FlowRelKind, FlowRelServ};
use bios_basic::rbum::dto::rbum_filer_dto::RbumBasicFilterReq;
Expand All @@ -14,6 +15,7 @@ use bios_basic::rbum::serv::rbum_item_serv::RbumItemCrudOperation;
use itertools::Itertools;
use std::iter::Iterator;
use tardis::basic::dto::TardisContext;
use tardis::log::warn;
use tardis::web::context_extractor::TardisContextExtractor;
use tardis::web::poem::Request;
use tardis::web::poem_openapi;
Expand Down Expand Up @@ -105,7 +107,7 @@ impl FlowCiModelApi {

/// Creating or referencing models
///
/// 创建或引用模型(rel_model_id:关联模型ID, op:关联模型操作类型(复制或者引用),is_create_copy:是否创建副本(当op为复制时需指定,默认不需要))
///
#[oai(path = "/copy_or_reference_model", method = "post")]
async fn copy_or_reference_model(
&self,
Expand All @@ -116,6 +118,8 @@ impl FlowCiModelApi {
let mut funs = flow_constants::get_tardis_inst();
check_without_owner_and_unsafe_fill_ctx(request, &funs, &mut ctx.0)?;
funs.begin().await?;
warn!("ci copy_or_reference_model req: {:?}", req.0);
let orginal_models = FlowModelServ::clean_rel_models(None, None, None, &funs, &ctx.0).await?;
// find rel models
let rel_model_ids = FlowRelServ::find_to_simple_rels(
&FlowRelKind::FlowModelTemplate,
Expand All @@ -130,12 +134,8 @@ impl FlowCiModelApi {
.map(|rel| rel.rel_id)
.collect_vec();
let mut result = HashMap::new();
let mut orginal_models = HashMap::new();
let mut mock_ctx = ctx.0.clone();
if rbum_scope_helper::get_scope_level_by_context(&ctx.0)? == RbumScopeLevelKind::L1 {
orginal_models = FlowModelServ::find_rel_models(req.0.rel_template_id.clone(), true, &funs, &ctx.0).await?;
} else if rbum_scope_helper::get_scope_level_by_context(&ctx.0)? == RbumScopeLevelKind::L2 {
orginal_models = FlowModelServ::find_rel_models(None, true, &funs, &ctx.0).await?;
if rbum_scope_helper::get_scope_level_by_context(&ctx.0)? == RbumScopeLevelKind::L2 {
mock_ctx = match req.0.op {
FlowModelAssociativeOperationKind::Copy => ctx.0.clone(),
FlowModelAssociativeOperationKind::Reference => TardisContext {
Expand All @@ -144,14 +144,23 @@ impl FlowCiModelApi {
},
};
}

for rel_model_id in rel_model_ids {
let tag = FlowModelServ::get_item_detail_aggs(&rel_model_id, false, &funs, &mock_ctx).await?.tag;
let orginal_model_id = orginal_models.get(&tag).map(|orginal_model| orginal_model.id.clone());
result.insert(
rel_model_id.clone(),
FlowModelServ::copy_or_reference_model(orginal_model_id, &rel_model_id, Some(ctx.0.own_paths.clone()), &req.0.op, Some(false), &funs, &mock_ctx).await?.id,
);
let new_model = FlowModelServ::copy_or_reference_model(&rel_model_id, Some(ctx.0.own_paths.clone()), &req.0.op, Some(false), &funs, &mock_ctx).await?;
FlowInstServ::batch_update_when_switch_model(
if rbum_scope_helper::get_scope_level_by_context(&ctx.0)? == RbumScopeLevelKind::L2 {
None
} else {
orginal_models.get(&new_model.tag).map(|model| model.id.clone())
},
&new_model.tag,
&new_model.id,
new_model.states.clone(),
&new_model.init_state_id,
&funs,
&ctx.0,
)
.await?;
result.insert(rel_model_id.clone(), new_model.id.clone());
}
funs.commit().await?;
ctx.0.execute_task().await?;
Expand Down Expand Up @@ -195,8 +204,7 @@ impl FlowCiModelApi {
)
.await?
{
let added_model =
FlowModelServ::copy_or_reference_model(None, &from_model.rel_model_id, None, &FlowModelAssociativeOperationKind::Copy, Some(true), &funs, &ctx.0).await?;
let added_model = FlowModelServ::copy_or_reference_model(&from_model.rel_model_id, None, &FlowModelAssociativeOperationKind::Copy, Some(true), &funs, &ctx.0).await?;
FlowRelServ::add_simple_rel(
&FlowRelKind::FlowModelTemplate,
&added_model.id,
Expand Down Expand Up @@ -240,11 +248,43 @@ impl FlowCiModelApi {
async fn exist_rel_by_template_ids(&self, req: Json<FlowModelExistRelByTemplateIdsReq>, mut ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Vec<String>> {
let funs = flow_constants::get_tardis_inst();
check_without_owner_and_unsafe_fill_ctx(request, &funs, &mut ctx.0)?;
warn!("ci exist_rel_by_template_ids req: {:?}", req.0);
let support_tags = req.0.support_tags;
let mut result = vec![];
for rel_template_id in req.0.rel_template_ids {
if !FlowRelServ::find_to_simple_rels(&FlowRelKind::FlowModelTemplate, &rel_template_id, None, None, &funs, &ctx.0).await?.is_empty() {
result.push(rel_template_id.clone());
for (rel_template_id, current_tags) in req.0.rel_tag_by_template_ids {
// 当前模板tag和需要支持的tag取交集,得到当前模板tag中需要检查的tag列表
let tags = current_tags.into_iter().filter(|current_tag| support_tags.contains(current_tag)).collect_vec();
if !tags.is_empty() {
// 当前模板关联的模型所支持的tag
let rel_model_tags = FlowModelServ::find_items(
&FlowModelFilterReq {
basic: RbumBasicFilterReq {
ids: Some(
FlowRelServ::find_to_simple_rels(&FlowRelKind::FlowModelTemplate, &rel_template_id, None, None, &funs, &ctx.0)
.await?
.into_iter()
.map(|rel| rel.rel_id)
.collect_vec(),
),
..Default::default()
},
..Default::default()
},
None,
None,
&funs,
&ctx.0,
)
.await?
.into_iter()
.map(|model| model.tag.clone())
.collect_vec();
// 如果出现了当前模板tag中需要检查的tag没有被当前模板关联,则说明当前关联模板不是可用状态
if !tags.into_iter().filter(|tag| !rel_model_tags.contains(tag)).collect_vec().is_empty() {
continue;
}
}
result.push(rel_template_id.clone());
}

TardisResp::ok(result)
Expand Down
25 changes: 24 additions & 1 deletion backend/middlewares/flow/src/api/ci/flow_ci_state_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use bios_basic::rbum::dto::rbum_filer_dto::RbumBasicFilterReq;
use bios_basic::rbum::helper::rbum_scope_helper::{self, check_without_owner_and_unsafe_fill_ctx};
use bios_basic::rbum::rbum_enumeration::RbumScopeLevelKind;
use bios_basic::rbum::serv::rbum_item_serv::RbumItemCrudOperation;
use tardis::basic::dto::TardisContext;
use tardis::log;
use tardis::tokio;
use tardis::web::context_extractor::TardisContextExtractor;
use tardis::web::poem::Request;
use tardis::web::poem_openapi;
use tardis::web::poem_openapi::param::Query;
use tardis::web::poem_openapi::payload::Json;
use tardis::web::web_resp::{TardisApiResult, TardisPage, TardisResp};
use tardis::web::web_resp::{TardisApiResult, TardisPage, TardisResp, Void};

use crate::dto::flow_state_dto::{FlowStateCountGroupByStateReq, FlowStateCountGroupByStateResp, FlowStateFilterReq, FlowStateKind, FlowStateSummaryResp, FlowSysStateKind};
use crate::flow_constants;
Expand Down Expand Up @@ -103,4 +106,24 @@ impl FlowCiStateApi {
ctx.0.execute_task().await?;
TardisResp::ok(result)
}

///Script: merge global states with the same name
///
/// 脚本:合并相同名称的全局状态
#[oai(path = "/merge_state_by_name", method = "post")]
async fn merge_state_by_name(&self) -> TardisApiResult<Void> {
let funs = flow_constants::get_tardis_inst();
let global_ctx = TardisContext::default();
tokio::spawn(async move {
match FlowStateServ::merge_state_by_name(&funs, &global_ctx).await {
Ok(_) => {
log::trace!("[Flow.Inst] add log success")
}
Err(e) => {
log::warn!("[Flow.Inst] failed to add log:{e}")
}
}
});
TardisResp::ok(Void {})
}
}
Loading

0 comments on commit db807ff

Please sign in to comment.