Skip to content

Commit

Permalink
Merge branch 'main' into split
Browse files Browse the repository at this point in the history
  • Loading branch information
ljl committed Nov 29, 2023
2 parents 6c39df2 + 30c3461 commit 8efde75
Show file tree
Hide file tree
Showing 27 changed files with 329 additions and 188 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ tardis = { version = "=0.1.0-rc.3" }
# tardis = { path = "../tardis/tardis" }
# tardis = { git = "https://github.com/ideal-world/tardis.git", rev = "9424e16" }
#spacegate
spacegate-kernel = { git = "https://github.com/ideal-world/spacegate.git", rev = "0f03a1b",features = [
spacegate-kernel = { git = "https://github.com/ideal-world/spacegate.git", rev = "40e7bf8d0b",features = [
"ws",
"cache",
"k8s",
Expand Down
3 changes: 3 additions & 0 deletions basic/src/rbum/helper/rbum_scope_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ pub fn check_scope(record_own_paths: &str, record_scope_level: Option<i16>, filt
return false;
}
if let Some(record_scope_level) = record_scope_level {
if record_scope_level == 0 {
return true;
}
if let Some(p1) = get_pre_paths(1, filter_own_paths) {
if record_scope_level == 1 {
return record_own_paths.is_empty() || record_own_paths.contains(&p1);
Expand Down
1 change: 1 addition & 0 deletions middleware/flow/src/dto/flow_external_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct FlowExternalReq {
pub original_sys_state: Option<FlowSysStateKind>,
pub owner_paths: String,
pub obj_ids: Vec<String>,
pub notify: Option<bool>,
pub params: Vec<FlowExternalParams>,
}

Expand Down
2 changes: 2 additions & 0 deletions middleware/flow/src/dto/flow_inst_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub struct FlowInstDetailResp {
pub current_state_id: String,
pub current_state_name: Option<String>,
pub current_state_color: Option<String>,
pub current_state_kind: Option<FlowSysStateKind>,
pub current_state_ext: Option<String>,

pub current_assigned: Option<String>,
pub current_vars: Option<HashMap<String, Value>>,
Expand Down
1 change: 1 addition & 0 deletions middleware/flow/src/dto/flow_model_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub struct FlowModelFilterReq {
pub rel_template_id: Option<String>,
pub template: Option<bool>,
pub own_paths: Option<Vec<String>>,
pub specified_state_ids: Option<Vec<String>>,
}

impl RbumItemFilterFetcher for FlowModelFilterReq {
Expand Down
58 changes: 2 additions & 56 deletions middleware/flow/src/flow_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@ use bios_basic::rbum::{
};
use bios_sdk_invoke::invoke_initializer;

use itertools::Itertools;
use serde_json::Value;
use tardis::{
basic::{dto::TardisContext, field::TrimString, result::TardisResult},
db::{
reldb_client::TardisActiveModel,
sea_orm::{
self,
sea_query::{Query, Table},
},
},
db::{reldb_client::TardisActiveModel, sea_orm::sea_query::Table},
log::info,
web::web_server::TardisWebServer,
TardisFuns, TardisFunsInst,
Expand All @@ -32,9 +24,7 @@ use crate::{
dto::{
flow_model_dto::FlowModelFilterReq,
flow_state_dto::FlowSysStateKind,
flow_transition_dto::{
FlowTransitionActionByVarChangeInfoChangedKind, FlowTransitionActionChangeInfo, FlowTransitionActionChangeKind, FlowTransitionDoubleCheckInfo, FlowTransitionInitInfo,
},
flow_transition_dto::{FlowTransitionDoubleCheckInfo, FlowTransitionInitInfo},
},
flow_config::{BasicInfo, FlowBasicInfoManager, FlowConfig},
flow_constants,
Expand Down Expand Up @@ -77,7 +67,6 @@ pub async fn init_db(mut funs: TardisFunsInst) -> TardisResult<()> {
funs.begin().await?;
if check_initialized(&funs, &ctx).await? {
init_basic_info(&funs).await?;
// self::modify_post_actions(&funs, &ctx).await?;
} else {
let db_kind = TardisFuns::reldb().backend();
let compatible_type = TardisFuns::reldb().compatible_type();
Expand Down Expand Up @@ -125,49 +114,6 @@ async fn init_basic_info<'a>(funs: &TardisFunsInst) -> TardisResult<()> {
Ok(())
}

// @TODO temporary
pub async fn modify_post_actions(funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
#[derive(sea_orm::FromQueryResult)]
pub struct FlowTransactionPostAction {
id: String,
action_by_post_changes: Value,
}
let transactions = funs
.db()
.find_dtos::<FlowTransactionPostAction>(
Query::select()
.columns([
(flow_transition::Entity, flow_transition::Column::Id),
(flow_transition::Entity, flow_transition::Column::ActionByPostChanges),
])
.from(flow_transition::Entity),
)
.await?
.into_iter()
.filter(|res| !TardisFuns::json.json_to_obj::<Vec<FlowTransitionActionChangeInfo>>(res.action_by_post_changes.clone()).unwrap_or_default().is_empty())
.collect_vec();
for transaction in transactions {
let mut post_changes = TardisFuns::json.json_to_obj::<Vec<FlowTransitionActionChangeInfo>>(transaction.action_by_post_changes.clone()).unwrap_or_default();
for post_change in post_changes.iter_mut() {
if post_change.changed_kind.is_none() && post_change.kind == FlowTransitionActionChangeKind::Var {
if post_change.changed_val.is_some() {
post_change.changed_kind = Some(FlowTransitionActionByVarChangeInfoChangedKind::ChangeContent);
} else {
post_change.changed_kind = Some(FlowTransitionActionByVarChangeInfoChangedKind::Clean);
}
}
}
let flow_transition = flow_transition::ActiveModel {
id: sea_orm::ActiveValue::Set(transaction.id.clone()),
action_by_post_changes: sea_orm::ActiveValue::Set(TardisFuns::json.obj_to_json(&post_changes)?),
..Default::default()
};
funs.db().update_one(flow_transition, ctx).await?;
}

Ok(())
}

pub async fn init_rbum_data(funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
let kind_state_id = add_kind(flow_constants::RBUM_KIND_STATE_CODE, flow_constants::RBUM_EXT_TABLE_STATE, funs, ctx).await?;
let kind_model_id = add_kind(flow_constants::RBUM_KIND_MODEL_CODE, flow_constants::RBUM_EXT_TABLE_MODEL, funs, ctx).await?;
Expand Down
3 changes: 2 additions & 1 deletion middleware/flow/src/serv/flow_config_serv.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bios_basic::rbum::rbum_enumeration::RbumScopeLevelKind;
use bios_sdk_invoke::clients::spi_kv_client::{KvItemSummaryResp, SpiKvClient};
use tardis::{
basic::{dto::TardisContext, result::TardisResult},
Expand All @@ -12,7 +13,7 @@ pub struct FlowConfigServ;
impl FlowConfigServ {
pub async fn modify_config(modify_req: &Vec<FlowConfigModifyReq>, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
for req in modify_req {
SpiKvClient::add_or_modify_item(&format!("{}:config:{}", flow_constants::DOMAIN_CODE, req.code.clone()), &req.value, None, funs, ctx).await?;
SpiKvClient::add_or_modify_item(&format!("{}:config:{}", flow_constants::DOMAIN_CODE, req.code.clone()), &req.value, None, Some(RbumScopeLevelKind::Root.to_int()), funs, ctx).await?;
}
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions middleware/flow/src/serv/flow_external_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl FlowExternalServ {
target_sys_state: Option<FlowSysStateKind>,
original_state: Option<String>,
original_sys_state: Option<FlowSysStateKind>,
is_notify: bool,
params: Vec<FlowExternalParams>,
ctx: &TardisContext,
funs: &TardisFunsInst,
Expand Down Expand Up @@ -110,6 +111,7 @@ impl FlowExternalServ {
target_sys_state,
original_state,
original_sys_state,
notify: Some(is_notify),
params,
..Default::default()
};
Expand Down Expand Up @@ -138,6 +140,7 @@ impl FlowExternalServ {
target_sys_state: FlowSysStateKind,
original_state: String,
original_sys_state: FlowSysStateKind,
is_notify: bool,
ctx: &TardisContext,
funs: &TardisFunsInst,
) -> TardisResult<FlowExternalNotifyChangesResp> {
Expand All @@ -156,6 +159,7 @@ impl FlowExternalServ {
target_sys_state: Some(target_sys_state),
original_state: Some(original_state),
original_sys_state: Some(original_sys_state),
notify: Some(is_notify),
..Default::default()
};
debug!("do_notify_changes body: {:?}", body);
Expand Down Expand Up @@ -215,11 +219,7 @@ impl FlowExternalServ {
curr_bus_obj_id: "".to_string(),
owner_paths: own_paths.to_string(),
obj_ids: rel_business_obj_ids,
target_state: None,
target_sys_state: None,
original_state: None,
original_sys_state: None,
params: vec![],
..Default::default()
};
debug!("do_query_field body: {:?}", body);
let resp: FlowExternalResp<FlowExternalQueryFieldResp> = funs
Expand Down
82 changes: 42 additions & 40 deletions middleware/flow/src/serv/flow_inst_serv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use async_recursion::async_recursion;
use bios_basic::{
Expand Down Expand Up @@ -308,6 +308,8 @@ impl FlowInstServ {
pub current_state_id: String,
pub current_state_name: Option<String>,
pub current_state_color: Option<String>,
pub current_state_kind: Option<FlowSysStateKind>,
pub current_state_ext: Option<String>,

pub current_assigned: Option<String>,
pub current_vars: Option<Value>,
Expand All @@ -330,6 +332,7 @@ impl FlowInstServ {
let rel_state_table = Alias::new("rel_state");
let flow_state_table = Alias::new("flow_state");
let rel_model_table = Alias::new("rel_model");
let rbum_rel_table = Alias::new("rbum_rel");
let mut query = Query::select();
query
.columns([
Expand All @@ -351,6 +354,8 @@ impl FlowInstServ {
])
.expr_as(Expr::col((rel_state_table.clone(), NAME_FIELD.clone())).if_null(""), Alias::new("current_state_name"))
.expr_as(Expr::col((flow_state_table.clone(), Alias::new("color"))).if_null(""), Alias::new("current_state_color"))
.expr_as(Expr::col((flow_state_table.clone(), Alias::new("sys_state"))).if_null(""), Alias::new("current_state_kind"))
.expr_as(Expr::col((rbum_rel_table.clone(), Alias::new("ext"))).if_null(""), Alias::new("current_state_ext"))
.expr_as(Expr::col((rel_model_table.clone(), NAME_FIELD.clone())).if_null(""), Alias::new("rel_flow_model_name"))
.from(flow_inst::Entity)
.join_as(
Expand All @@ -377,6 +382,15 @@ impl FlowInstServ {
.add(Expr::col((rel_model_table.clone(), REL_KIND_ID_FIELD.clone())).eq(FlowModelServ::get_rbum_kind_id().unwrap()))
.add(Expr::col((rel_model_table.clone(), REL_DOMAIN_ID_FIELD.clone())).eq(FlowModelServ::get_rbum_domain_id().unwrap())),
)
.join_as(
JoinType::LeftJoin,
rbum_rel_table.clone(),
rbum_rel_table.clone(),
Cond::all()
.add(Expr::col((rbum_rel_table.clone(), Alias::new("to_rbum_item_id"))).equals((flow_inst::Entity, flow_inst::Column::CurrentStateId)))
.add(Expr::col((rbum_rel_table.clone(), Alias::new("from_rbum_id"))).equals((flow_inst::Entity, flow_inst::Column::RelFlowModelId)))
.add(Expr::col((rbum_rel_table.clone(), Alias::new("tag"))).eq("FlowModelState".to_string())),
)
.and_where(Expr::col((flow_inst::Entity, flow_inst::Column::Id)).is_in(flow_inst_ids))
.and_where(Expr::col((flow_inst::Entity, flow_inst::Column::OwnPaths)).like(format!("{}%", ctx.own_paths)));

Expand All @@ -399,6 +413,8 @@ impl FlowInstServ {
current_state_id: inst.current_state_id,
current_state_name: inst.current_state_name,
current_state_color: inst.current_state_color,
current_state_kind: inst.current_state_kind,
current_state_ext: inst.current_state_ext,
current_assigned: inst.current_assigned,
current_vars: inst.current_vars.map(|current_vars| TardisFuns::json.json_to_obj(current_vars).unwrap()),
rel_business_obj_id: inst.rel_business_obj_id,
Expand Down Expand Up @@ -518,11 +534,12 @@ impl FlowInstServ {
let flow_models = FlowModelServ::find_detail_items(
&FlowModelFilterReq {
basic: RbumBasicFilterReq {
ids: Some(flow_insts.iter().map(|inst| inst.rel_flow_model_id.to_string()).collect_vec()),
ids: Some(flow_insts.iter().map(|inst| inst.rel_flow_model_id.to_string()).collect::<HashSet<_>>().into_iter().collect()),
with_sub_own_paths: true,
own_paths: Some("".to_string()),
..Default::default()
},
specified_state_ids: Some(flow_insts.iter().map(|inst| inst.current_state_id.clone()).collect::<HashSet<_>>().into_iter().collect()),
..Default::default()
},
None,
Expand Down Expand Up @@ -662,15 +679,9 @@ impl FlowInstServ {
if next_flow_transition.is_none() {
return Err(funs.err().not_found("flow_inst", "transfer", "no transferable state", "404-flow-inst-transfer-state-not-found"));
}
if FlowModelServ::check_post_action_ring(
flow_model.transitions().into_iter().find(|trans| trans.id == transfer_req.flow_transition_id).unwrap(),
(false, vec![]),
funs,
ctx,
)
.await?
.0
{
let model_transition = flow_model.transitions();
let next_transition_detail = model_transition.iter().find(|trans| trans.id == transfer_req.flow_transition_id).unwrap().to_owned();
if FlowModelServ::check_post_action_ring(next_transition_detail.clone(), (false, vec![]), funs, ctx).await?.0 {
return Err(funs.err().not_found("flow_inst", "transfer", "this post action exist endless loop", "500-flow-transition-endless-loop"));
}

Expand Down Expand Up @@ -724,6 +735,7 @@ impl FlowInstServ {
Some(next_flow_state.sys_state.clone()),
Some(prev_flow_state.name.clone()),
Some(prev_flow_state.sys_state.clone()),
next_transition_detail.is_notify,
params,
ctx,
funs,
Expand Down Expand Up @@ -776,10 +788,9 @@ impl FlowInstServ {
// get updated instance detail
let flow_inst_detail = Self::get(flow_inst_id, funs, ctx).await?;

let model_transition = flow_model.transitions();
Self::do_request_webhook(
from_transition_id.and_then(|id: String| model_transition.iter().find(|model_transition| model_transition.id == id)),
model_transition.iter().find(|model_transition| model_transition.id == next_flow_transition.next_flow_transition_id),
Some(&next_transition_detail),
)
.await?;

Expand All @@ -793,6 +804,7 @@ impl FlowInstServ {
next_flow_state.sys_state,
prev_flow_state.name.clone(),
prev_flow_state.sys_state,
next_transition_detail.is_notify,
ctx,
funs,
)
Expand All @@ -802,7 +814,16 @@ impl FlowInstServ {
let post_changes =
model_transition.into_iter().find(|model_transition| model_transition.id == next_flow_transition.next_flow_transition_id).unwrap_or_default().action_by_post_changes();
if !post_changes.is_empty() {
Self::do_post_change(&flow_inst_detail, &flow_model, post_changes, updated_instance_list, ctx, funs).await?;
Self::do_post_change(
&flow_inst_detail,
&flow_model,
post_changes,
updated_instance_list,
next_transition_detail.is_notify,
ctx,
funs,
)
.await?;
}
let next_flow_transitions = Self::do_find_next_transitions(&flow_inst_detail, &flow_model, None, &None, skip_filter, funs, ctx).await?.next_flow_transitions;

Expand Down Expand Up @@ -833,6 +854,7 @@ impl FlowInstServ {
current_model: &FlowModelDetailResp,
post_changes: Vec<FlowTransitionActionChangeInfo>,
updated_instance_list: &mut Vec<String>,
is_notify: bool,
ctx: &TardisContext,
funs: &TardisFunsInst,
) -> TardisResult<()> {
Expand Down Expand Up @@ -867,6 +889,7 @@ impl FlowInstServ {
None,
None,
None,
is_notify,
vec![FlowExternalParams {
rel_kind: None,
rel_tag: None,
Expand All @@ -891,6 +914,7 @@ impl FlowInstServ {
None,
None,
None,
is_notify,
vec![FlowExternalParams {
rel_kind: None,
rel_tag: None,
Expand Down Expand Up @@ -1107,7 +1131,7 @@ impl FlowInstServ {
spec_flow_transition_id: Option<String>,
req_vars: &Option<HashMap<String, Value>>,
skip_filter: bool,
funs: &TardisFunsInst,
_funs: &TardisFunsInst,
ctx: &TardisContext,
) -> TardisResult<FlowInstFindStateAndTransitionsResp> {
let flow_model_transitions = flow_model.transitions();
Expand Down Expand Up @@ -1188,36 +1212,14 @@ impl FlowInstServ {
double_check: model_transition.double_check(),
})
.collect_vec();
let current_flow_state = FlowStateServ::find_one_item(
&FlowStateFilterReq {
basic: RbumBasicFilterReq {
ids: Some(vec![flow_inst.current_state_id.to_string()]),
with_sub_own_paths: true,
own_paths: Some("".to_string()),
..Default::default()
},
..Default::default()
},
funs,
ctx,
)
.await?
.ok_or_else(|| funs.err().not_found("flow_inst", "do_find_next_transitions", "flow state is not found", "404-flow-state-not-found"))?;

let state_and_next_transitions = FlowInstFindStateAndTransitionsResp {
flow_inst_id: flow_inst.id.to_string(),
finish_time: flow_inst.finish_time,
current_flow_state_name: flow_inst.current_state_name.as_ref().unwrap_or(&"".to_string()).to_string(),
current_flow_state_kind: current_flow_state.sys_state.clone(),
current_flow_state_color: current_flow_state.color.clone(),
current_flow_state_ext: TardisFuns::json.str_to_obj::<FlowStateRelModelExt>(
&FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowModelState, &flow_inst.rel_flow_model_id, None, None, funs, ctx)
.await?
.into_iter()
.find(|rel| current_flow_state.id == rel.rel_id)
.ok_or_else(|| funs.err().not_found("flow_inst", "do_find_next_transitions", "flow state is not found", "404-flow-state-not-found"))?
.ext,
)?,
current_flow_state_color: flow_inst.current_state_color.as_ref().unwrap_or(&"".to_string()).to_string(),
current_flow_state_kind: flow_inst.current_state_kind.as_ref().unwrap_or(&FlowSysStateKind::Start).clone(),
current_flow_state_ext: TardisFuns::json.str_to_obj::<FlowStateRelModelExt>(&flow_inst.current_state_ext.clone().unwrap_or_default())?,
next_flow_transitions: next_transitions,
};
Ok(state_and_next_transitions)
Expand Down
Loading

0 comments on commit 8efde75

Please sign in to comment.