Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bring back log v1 api #858

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/gateways/spacegate-plugins/src/plugin/audit_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use std::str::FromStr;
use std::time::Instant;

use bios_sdk_invoke::clients::spi_log_client::{self, LogItemAddReq};
use bios_sdk_invoke::clients::spi_log_client::{self, LogItemAddV2Req};
use bios_sdk_invoke::invoke_config::InvokeConfig;
use bios_sdk_invoke::invoke_enumeration::InvokeModuleKind;
use bios_sdk_invoke::invoke_initializer;
Expand Down Expand Up @@ -208,8 +208,8 @@ impl AuditLogPlugin {
let tag = self.tag.clone();
if !self.log_url.is_empty() && !self.spi_app_id.is_empty() {
tokio::task::spawn(async move {
match spi_log_client::SpiLogClient::add(
LogItemAddReq {
match spi_log_client::SpiLogClient::addv2(
LogItemAddV2Req {
tag,
content: TardisFuns::json.obj_to_json(&content).unwrap_or_default(),
kind: None,
Expand Down
6 changes: 3 additions & 3 deletions backend/middlewares/flow/src/serv/clients/flow_log_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bios_sdk_invoke::clients::{
iam_client::IamClient,
spi_log_client::{LogItemAddReq, SpiLogClient},
spi_log_client::{LogItemAddV2Req, SpiLogClient},
};
use serde::Serialize;

Expand Down Expand Up @@ -96,7 +96,7 @@ impl FlowLogClient {
.await?
.owner_name;

let req = LogItemAddReq {
let req = LogItemAddV2Req {
tag: tag.to_string(),
content: TardisFuns::json.obj_to_json(&content).expect("req_msg not a valid json value"),
kind,
Expand All @@ -112,7 +112,7 @@ impl FlowLogClient {
owner_name,
push: push,
};
SpiLogClient::add(req, funs, ctx).await?;
SpiLogClient::addv2(req, funs, ctx).await?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{sync::Arc, task::ready, time::Duration};

use bios_sdk_invoke::clients::spi_log_client::{LogItemAddReq, LogItemFindReq, SpiLogClient};
use bios_sdk_invoke::clients::spi_log_client::{LogItemAddReq, LogItemAddV2Req, LogItemFindReq, SpiLogClient};
use tardis::{
basic::dto::TardisContext,
chrono::Utc,
Expand Down Expand Up @@ -109,8 +109,8 @@ impl EventComponent for SpiLog {
let ctx = self.ctx.clone();
let code = code.to_string();
let _handle = tokio::spawn(async move {
let result = SpiLogClient::add(
LogItemAddReq {
let result = SpiLogClient::addv2(
LogItemAddV2Req {
tag: TASK_TAG.to_string(),
content: tardis::serde_json::Value::Null,
ext: Some(ext),
Expand Down
25 changes: 24 additions & 1 deletion backend/spi/spi-log/src/api/ci/log_ci_item_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ use tardis::web::poem_openapi::param::Path;
use tardis::web::poem_openapi::payload::Json;
use tardis::web::web_resp::{TardisApiResult, TardisPage, TardisResp, Void};

use crate::dto::log_item_dto::{LogConfigReq, LogItemAddReq, LogItemFindReq, LogItemFindResp};
use crate::dto::log_item_dto::{LogConfigReq, LogItemAddReq, LogItemAddV2Req, LogItemFindReq, LogItemFindResp};
use crate::serv::log_item_serv;
use tardis::serde_json::Value;

#[derive(Clone)]
pub struct LogCiItemApi;

#[derive(Clone)]
pub struct LogCiItemApiV2;

/// Interface Console Log API
#[poem_openapi::OpenApi(prefix_path = "/ci/item", tag = "bios_basic::ApiTag::Interface")]
impl LogCiItemApi {
Expand All @@ -29,6 +32,26 @@ impl LogCiItemApi {
let resp = log_item_serv::find(&mut find_req.0, &funs, &ctx.0).await?;
TardisResp::ok(resp)
}
}

/// Interface Console Log API V2
#[poem_openapi::OpenApi(prefix_path = "/ci/v2/item", tag = "bios_basic::ApiTag::Interface")]
impl LogCiItemApiV2 {
/// Add Item
#[oai(path = "/", method = "post")]
async fn add(&self, mut add_req: Json<LogItemAddV2Req>, ctx: TardisContextExtractor) -> TardisApiResult<String> {
let funs = crate::get_tardis_inst();
let id = log_item_serv::addv2(&mut add_req.0, &funs, &ctx.0).await?;
TardisResp::ok(id)
}

/// Find Items
#[oai(path = "/find", method = "put")]
async fn find(&self, mut find_req: Json<LogItemFindReq>, ctx: TardisContextExtractor) -> TardisApiResult<TardisPage<LogItemFindResp>> {
let funs = crate::get_tardis_inst();
let resp = log_item_serv::findv2(&mut find_req.0, &funs, &ctx.0).await?;
TardisResp::ok(resp)
}

/// Modify Item ext by key
#[oai(path = "/modify/:tag/:key/ext", method = "post")]
Expand Down
51 changes: 46 additions & 5 deletions backend/spi/spi-log/src/dto/log_item_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,49 @@ use tardis::{
web::poem_openapi,
};

#[derive(poem_openapi::Object, Serialize, Deserialize, Clone, Debug)]
#[derive(poem_openapi::Object, Serialize, Deserialize, Debug)]
pub struct LogItemAddReq {
#[oai(validator(pattern = r"^[a-z0-9_]+$"))]
pub tag: String,
// #[oai(validator(min_length = "2"))]
pub content: String,
#[oai(validator(min_length = "2"))]
pub kind: Option<TrimString>,
pub ext: Option<Value>,
#[oai(validator(min_length = "2"))]
pub key: Option<TrimString>,
#[oai(validator(min_length = "2"))]
pub op: Option<String>,
#[oai(validator(min_length = "2"))]
pub rel_key: Option<TrimString>,
#[oai(validator(min_length = "2"))]
pub id: Option<String>,
pub ts: Option<DateTime<Utc>>,
#[oai(validator(min_length = "2"))]
pub owner: Option<String>,
pub own_paths: Option<String>,
}

impl From<bios_sdk_invoke::clients::spi_log_client::LogItemAddReq> for LogItemAddReq {
fn from(value: bios_sdk_invoke::clients::spi_log_client::LogItemAddReq) -> Self {
Self {
tag: value.tag,
content: value.content,
kind: value.kind.map(Into::into),
ext: value.ext,
key: value.key.map(Into::into),
op: value.op,
rel_key: value.rel_key.map(Into::into),
id: value.id,
ts: value.ts,
owner: value.owner,
own_paths: value.own_paths,
}
}
}

#[derive(poem_openapi::Object, Serialize, Deserialize, Clone, Debug)]
pub struct LogItemAddV2Req {
#[oai(validator(pattern = r"^[a-z0-9_]+$"))]
pub tag: String,
pub content: Value,
Expand All @@ -32,8 +73,8 @@ pub struct LogItemAddReq {
pub push: bool,
pub msg: Option<String>,
}
impl From<bios_sdk_invoke::clients::spi_log_client::LogItemAddReq> for LogItemAddReq {
fn from(value: bios_sdk_invoke::clients::spi_log_client::LogItemAddReq) -> Self {
impl From<bios_sdk_invoke::clients::spi_log_client::LogItemAddV2Req> for LogItemAddV2Req {
fn from(value: bios_sdk_invoke::clients::spi_log_client::LogItemAddV2Req) -> Self {
Self {
tag: value.tag,
content: value.content,
Expand Down Expand Up @@ -129,8 +170,8 @@ pub struct StatsItemAddReq {
pub own_paths: Option<String>,
}

impl From<LogItemAddReq> for StatsItemAddReq {
fn from(value: LogItemAddReq) -> Self {
impl From<LogItemAddV2Req> for StatsItemAddReq {
fn from(value: LogItemAddV2Req) -> Self {
StatsItemAddReq {
idempotent_id: value.idempotent_id,
tag: value.tag,
Expand Down
6 changes: 3 additions & 3 deletions backend/spi/spi-log/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use bios_sdk_invoke::clients::{
asteroid_mq::prelude::{EventAttribute, Subject},
get_topic, mq_error, ContextHandler, SPI_RPC_TOPIC,
},
spi_log_client::LogItemAddReq,
spi_log_client::LogItemAddV2Req,
};
use tardis::{
basic::{dto::TardisContext, result::TardisResult},
{log as tracing, log::instrument},
};

#[instrument]
async fn handle_add_event(req: LogItemAddReq, ctx: TardisContext) -> TardisResult<()> {
async fn handle_add_event(req: LogItemAddV2Req, ctx: TardisContext) -> TardisResult<()> {
let funs = get_tardis_inst();
serv::log_item_serv::add(&mut req.into(), &funs, &ctx).await?;
serv::log_item_serv::addv2(&mut req.into(), &funs, &ctx).await?;
Ok(())
}

Expand Down
1 change: 0 additions & 1 deletion backend/spi/spi-log/src/serv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod log_item_serv;
#[deprecated]
pub mod pg;
pub mod pgv2;
4 changes: 3 additions & 1 deletion backend/spi/spi-log/src/serv/log_item_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bios_basic::spi::spi_constants;
use bios_basic::spi::spi_funs::SpiBsInstExtractor;
use bios_basic::spi_dispatch_service;

use crate::dto::log_item_dto::{LogConfigReq, LogItemAddReq, LogItemFindReq, LogItemFindResp};
use crate::dto::log_item_dto::{LogConfigReq, LogItemAddReq, LogItemAddV2Req, LogItemFindReq, LogItemFindResp};
use crate::log_initializer;
use tardis::web::web_resp::TardisPage;

Expand All @@ -24,6 +24,8 @@ spi_dispatch_service! {
@method: {
add(add_req: &mut LogItemAddReq) -> TardisResult<String>;
find(find_req: &mut LogItemFindReq) -> TardisResult<TardisPage<LogItemFindResp>>;
addv2(add_req: &mut LogItemAddV2Req) -> TardisResult<String>;
findv2(find_req: &mut LogItemFindReq) -> TardisResult<TardisPage<LogItemFindResp>>;
modify_ext(tag: &str,key: &str, ext: &mut Value) -> TardisResult<()>;
add_config(config: &mut LogConfigReq) -> TardisResult<()>;
delete_config(config: &mut LogConfigReq) -> TardisResult<()>;
Expand Down
26 changes: 17 additions & 9 deletions backend/spi/spi-log/src/serv/pg/log_pg_item_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ use tardis::{

use bios_basic::{dto::BasicQueryCondInfo, enumeration::BasicQueryOpKind, helper::db_helper, spi::spi_funs::SpiBsInst};

use crate::dto::log_item_dto::{AdvBasicQueryCondInfo, LogConfigReq, LogItemAddReq, LogItemFindReq, LogItemFindResp};
use crate::dto::log_item_dto::{AdvBasicQueryCondInfo, LogConfigReq, LogItemAddReq, LogItemAddV2Req, LogItemFindReq, LogItemFindResp};

use super::log_pg_initializer;

pub async fn add(add_req: &mut LogItemAddReq, _funs: &TardisFunsInst, ctx: &TardisContext, inst: &SpiBsInst) -> TardisResult<String> {
let id = add_req.idempotent_id.clone().unwrap_or(TardisFuns::field.nanoid());
let id = add_req.id.clone().unwrap_or(TardisFuns::field.nanoid());
let mut params = vec![
Value::from(id.clone()),
Value::from(add_req.kind.as_ref().unwrap_or(&"".into()).to_string()),
Value::from(add_req.key.as_ref().unwrap_or(&"".into()).to_string()),
Value::from(add_req.op.as_ref().unwrap_or(&"".to_string()).as_str()),
Value::from(TardisFuns::json.json_to_string(add_req.content.clone())?.as_str()),
Value::from(add_req.content.clone()),
Value::from(add_req.owner.as_ref().unwrap_or(&"".to_string()).as_str()),
Value::from(add_req.own_paths.as_ref().unwrap_or(&"".to_string()).as_str()),
Value::from(if let Some(ext) = &add_req.ext {
Expand Down Expand Up @@ -509,14 +509,22 @@ ORDER BY ts DESC
})
}

pub async fn modify_ext(_tag: &str, _key: &str, _ext: &mut JsonValue, _funs: &TardisFunsInst, _ctx: &TardisContext, _inst: &SpiBsInst) -> TardisResult<()> {
Ok(())
pub async fn addv2(add_req: &mut LogItemAddV2Req, funs: &TardisFunsInst, ctx: &TardisContext, inst: &SpiBsInst) -> TardisResult<String> {
Err(funs.err().bad_request("item", "add", "Add v2 is not supported", "400-spi-log-add-v2-not-supported"))
}

pub async fn add_config(_req: &LogConfigReq, _funs: &TardisFunsInst, _ctx: &TardisContext, _inst: &SpiBsInst) -> TardisResult<()> {
Ok(())
pub async fn findv2(find_req: &mut LogItemFindReq, funs: &TardisFunsInst, ctx: &TardisContext, inst: &SpiBsInst) -> TardisResult<TardisPage<LogItemFindResp>> {
Err(funs.err().bad_request("item", "find", "Find v2 is not supported", "400-spi-log-find-v2-not-supported"))
}

pub async fn delete_config(_config: &mut LogConfigReq, _funs: &TardisFunsInst, _ctx: &TardisContext, _inst: &SpiBsInst) -> TardisResult<()> {
Ok(())
pub async fn modify_ext(_tag: &str, _key: &str, _ext: &mut JsonValue, funs: &TardisFunsInst, _ctx: &TardisContext, _inst: &SpiBsInst) -> TardisResult<()> {
Err(funs.err().bad_request("item", "modify_ext", "Modify ext is not supported", "400-spi-log-modify-ext-not-supported"))
}

pub async fn add_config(_req: &LogConfigReq, funs: &TardisFunsInst, _ctx: &TardisContext, _inst: &SpiBsInst) -> TardisResult<()> {
Err(funs.err().bad_request("item", "add_config", "Add config is not supported", "400-spi-log-add-config-not-supported"))
}

pub async fn delete_config(_config: &mut LogConfigReq, funs: &TardisFunsInst, _ctx: &TardisContext, _inst: &SpiBsInst) -> TardisResult<()> {
Err(funs.err().bad_request("item", "delete_config", "Delete config is not supported", "400-spi-log-delete-config-not-supported"))
}
13 changes: 10 additions & 3 deletions backend/spi/spi-log/src/serv/pgv2/log_pg_item_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ use bios_basic::{
};

use crate::{
dto::log_item_dto::{AdvBasicQueryCondInfo, LogConfigReq, LogItemAddReq, LogItemFindReq, LogItemFindResp, StatsItemAddReq},
dto::log_item_dto::{AdvBasicQueryCondInfo, LogConfigReq, LogItemAddReq, LogItemAddV2Req, LogItemFindReq, LogItemFindResp, StatsItemAddReq},
log_constants::{CONFIG_TABLE_NAME, LOG_REF_FLAG, TABLE_LOG_FLAG_V2},
};

use super::log_pg_initializer;

pub async fn add(add_req: &mut LogItemAddReq, funs: &TardisFunsInst, ctx: &TardisContext, inst: &SpiBsInst) -> TardisResult<String> {
crate::serv::pg::log_pg_item_serv::add(add_req, funs, ctx, inst).await
}
pub async fn find(find_req: &mut LogItemFindReq, funs: &TardisFunsInst, ctx: &TardisContext, inst: &SpiBsInst) -> TardisResult<TardisPage<LogItemFindResp>> {
crate::serv::pg::log_pg_item_serv::find(find_req, funs, ctx, inst).await
}

pub async fn addv2(add_req: &mut LogItemAddV2Req, funs: &TardisFunsInst, ctx: &TardisContext, inst: &SpiBsInst) -> TardisResult<String> {
let id = add_req.idempotent_id.clone().unwrap_or(TardisFuns::field.nanoid());

let bs_inst = inst.inst::<TardisRelDBClient>();
Expand Down Expand Up @@ -144,7 +151,7 @@ fn parse_ref_ts_key(ref_key: &str) -> TardisResult<(DateTime<Utc>, String)> {
))
}

pub async fn find(find_req: &mut LogItemFindReq, funs: &TardisFunsInst, ctx: &TardisContext, inst: &SpiBsInst) -> TardisResult<TardisPage<LogItemFindResp>> {
pub async fn findv2(find_req: &mut LogItemFindReq, funs: &TardisFunsInst, ctx: &TardisContext, inst: &SpiBsInst) -> TardisResult<TardisPage<LogItemFindResp>> {
let mut where_fragments: Vec<String> = Vec::new();
let mut sql_vals: Vec<Value> = vec![];

Expand Down Expand Up @@ -699,7 +706,7 @@ async fn get_ref_fields_by_table_name(conn: &TardisRelDBlConnection, schema_name
Ok(ref_fields)
}

async fn push_to_eda(req: &LogItemAddReq, ref_fields: &Vec<String>, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
async fn push_to_eda(req: &LogItemAddV2Req, ref_fields: &Vec<String>, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
if let Some(topic) = get_topic(&SPI_RPC_TOPIC) {
let mut req_clone = req.clone();
for ref_field in ref_fields {
Expand Down
4 changes: 2 additions & 2 deletions backend/spi/spi-stats/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::{get_tardis_inst, serv};
use bios_sdk_invoke::clients::{
event_client::{get_topic, mq_error, ContextHandler, SPI_RPC_TOPIC},
spi_log_client::LogItemAddReq,
spi_log_client::LogItemAddV2Req,
};
use tardis::{
basic::{dto::TardisContext, result::TardisResult},
{log as tracing, log::instrument},
};

#[instrument]
async fn handle_add_event(req: LogItemAddReq, ctx: TardisContext) -> TardisResult<()> {
async fn handle_add_event(req: LogItemAddV2Req, ctx: TardisContext) -> TardisResult<()> {
let funs = get_tardis_inst();
//TODO
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions backend/supports/iam/src/basic/serv/clients/iam_log_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bios_basic::{
serv::{rbum_crud_serv::RbumCrudOperation, rbum_item_serv::RbumItemCrudOperation, rbum_set_serv::RbumSetCateServ},
},
};
use bios_sdk_invoke::clients::spi_log_client::{LogItemAddReq, SpiLogClient};
use bios_sdk_invoke::clients::spi_log_client::{LogItemAddV2Req, SpiLogClient};
use serde::Serialize;

use tardis::{
Expand Down Expand Up @@ -135,7 +135,7 @@ impl IamLogClient {
let own_paths = if ctx.own_paths.len() < 2 { None } else { Some(ctx.own_paths.clone()) };
let owner = if ctx.owner.len() < 2 { None } else { Some(ctx.owner.clone()) };

let add_req = LogItemAddReq {
let add_req = LogItemAddV2Req {
tag,
content: TardisFuns::json.obj_to_json(&content).expect("req_msg not a valid json value"),
kind,
Expand All @@ -151,7 +151,7 @@ impl IamLogClient {
owner_name: None,
push: false,
};
SpiLogClient::add(add_req, funs, ctx).await?;
SpiLogClient::addv2(add_req, funs, ctx).await?;
Ok(())
}

Expand Down
Loading
Loading