Skip to content

Commit

Permalink
Support instance_id query in the user server interface
Browse files Browse the repository at this point in the history
  • Loading branch information
iwannay committed Nov 24, 2024
1 parent c68db12 commit 51687ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
11 changes: 7 additions & 4 deletions openapi/src/api/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl InstanceApi {
user_info: Data<&logic::types::UserInfo>,

Query(ip): Query<Option<String>>,
Query(instance_id): Query<Option<String>>,
Query(instance_group_id): Query<Option<u64>>,
Query(tag_id): Query<Option<Vec<u64>>>,
Query(status): Query<Option<u8>>,
Expand Down Expand Up @@ -293,6 +294,7 @@ impl InstanceApi {
let query_result = svc
.instance
.query_admin_server(
instance_id.filter(|v| v != ""),
instance_group_id.filter(|&v| v != 0),
status,
ip.filter(|v| v != ""),
Expand All @@ -307,6 +309,7 @@ impl InstanceApi {
.instance
.query_user_server(
user_id,
instance_id.filter(|v| v != ""),
instance_group_id.filter(|&v| v != 0),
status,
ip.filter(|v| v != ""),
Expand Down Expand Up @@ -497,22 +500,22 @@ impl InstanceApi {
let (online_num, offline_num) = if can_manage_instance {
(
svc.instance
.query_admin_server(None, Some(1), None, 0, 1)
.query_admin_server(None, None, Some(1), None, 0, 1)
.await?
.1,
svc.instance
.query_admin_server(None, Some(0), None, 0, 1)
.query_admin_server(None, None, Some(0), None, 0, 1)
.await?
.1,
)
} else {
(
svc.instance
.query_user_server(user_info.user_id.clone(), None, Some(1), None, 0, 1)
.query_user_server(user_info.user_id.clone(), None, None, Some(1), None, 0, 1)
.await?
.1,
svc.instance
.query_user_server(user_info.user_id.clone(), None, Some(0), None, 0, 1)
.query_user_server(user_info.user_id.clone(), None, None, Some(0), None, 0, 1)
.await?
.1,
)
Expand Down
3 changes: 3 additions & 0 deletions openapi/src/api/manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl ManageApi {
user_info: Data<&logic::types::UserInfo>,

Query(ip): Query<Option<String>>,
Query(instance_id): Query<Option<String>>,
Query(instance_group_id): Query<Option<u64>>,
Query(tag_id): Query<Option<Vec<u64>>>,
Query(status): Query<Option<u8>>,
Expand Down Expand Up @@ -232,6 +233,7 @@ impl ManageApi {
let query_result = svc
.instance
.query_admin_server(
instance_id.filter(|v| v != ""),
instance_group_id.filter(|&v| v != 0),
status,
ip.filter(|v| v != ""),
Expand All @@ -246,6 +248,7 @@ impl ManageApi {
.instance
.query_user_server(
user_id,
instance_id.filter(|v| v != ""),
instance_group_id.filter(|&v| v != 0),
status,
ip.filter(|v| v != ""),
Expand Down
19 changes: 16 additions & 3 deletions openapi/src/logic/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ impl<'a> InstanceLogic<'a> {

pub async fn query_admin_server(
&self,
instance_id: Option<String>,
instance_group_id: Option<u64>,
status: Option<u8>,
ip: Option<String>,
Expand Down Expand Up @@ -411,6 +412,9 @@ impl<'a> InstanceLogic<'a> {
.apply_if(status, |query, v| {
query.filter(instance::Column::Status.eq(v))
})
.apply_if(instance_id, |query, v| {
query.filter(instance::Column::InstanceId.eq(v))
})
.apply_if(ip, |query, v| {
query.filter(instance::Column::Ip.contains(v))
})
Expand Down Expand Up @@ -532,6 +536,7 @@ impl<'a> InstanceLogic<'a> {
pub async fn query_user_server(
&self,
user_id: String,
instance_id: Option<String>,
instance_group_id: Option<u64>,
status: Option<u8>,
ip: Option<String>,
Expand Down Expand Up @@ -584,6 +589,9 @@ impl<'a> InstanceLogic<'a> {
.apply_if(status, |query, v| {
query.filter(instance::Column::Status.eq(v))
})
.apply_if(instance_id.clone(), |query, v| {
query.filter(instance::Column::InstanceId.eq(v))
})
.apply_if(instance_group_id, |query, v| {
query.filter(instance_group::Column::Id.eq(v))
})
Expand Down Expand Up @@ -630,6 +638,9 @@ impl<'a> InstanceLogic<'a> {
.apply_if(status, |query, v| {
query.filter(instance::Column::Status.eq(v))
})
.apply_if(instance_id, |query, v| {
query.filter(instance::Column::InstanceId.eq(v))
})
.as_query()
.clone(),
);
Expand Down Expand Up @@ -863,7 +874,7 @@ impl<'a> InstanceLogic<'a> {
.and(Expr::col((b, instance::Column::InstanceGroupId)).gt(0))
.into_condition()
})
.from(instance::Column::Id)
.from(instance::Column::InstanceId)
.to(instance_role::Column::InstanceId)
.into(),
)
Expand All @@ -889,7 +900,9 @@ impl<'a> InstanceLogic<'a> {
UserServer::find()
.select_only()
.column(instance::Column::Id)
.column(instance::Column::InstanceId)
.column(instance::Column::Ip)
.column(instance::Column::MacAddr)
.column(instance::Column::Namespace)
.column(instance::Column::Info)
.column(instance::Column::SysUser)
Expand All @@ -910,7 +923,7 @@ impl<'a> InstanceLogic<'a> {
.and(Expr::col((b, instance::Column::InstanceGroupId)).gt(0))
.into_condition()
})
.from(instance::Column::Id)
.from(instance::Column::InstanceId)
.to(user_server::Column::InstanceId)
.into(),
)
Expand All @@ -926,7 +939,7 @@ impl<'a> InstanceLogic<'a> {
query.filter(instance::Column::Namespace.eq(v))
})
.apply_if(instance_id.clone(), |query, v| {
query.filter(instance::Column::InstanceId.eq(v))
query.filter(user_server::Column::InstanceId.eq(v))
})
.as_query()
.clone(),
Expand Down

0 comments on commit 51687ce

Please sign in to comment.