Skip to content

Commit

Permalink
Feat log new api (ideal-world#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
RWDai authored and 4t145 committed Nov 7, 2024
1 parent afcf91e commit 3ee65a0
Show file tree
Hide file tree
Showing 18 changed files with 494 additions and 39 deletions.
27 changes: 11 additions & 16 deletions backend/spi/spi-log/src/dto/log_item_dto.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bios_basic::{dto::BasicQueryCondInfo, enumeration::BasicQueryOpKind};
use bios_sdk_invoke::clients::spi_log_client::{StatsItemAddReq, StatsItemDeleteReq};
use serde::{Deserialize, Serialize};
use tardis::{
basic::field::TrimString,
Expand Down Expand Up @@ -154,22 +155,6 @@ pub struct LogConfigReq {
pub ref_field: String,
}

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

impl From<LogItemAddV2Req> for StatsItemAddReq {
fn from(value: LogItemAddV2Req) -> Self {
StatsItemAddReq {
Expand All @@ -184,3 +169,13 @@ impl From<LogItemAddV2Req> for StatsItemAddReq {
}
}
}

impl From<LogItemAddV2Req> for StatsItemDeleteReq {
fn from(value: LogItemAddV2Req) -> Self {
StatsItemDeleteReq {
idempotent_id: value.idempotent_id,
tag: value.tag,
key: value.key,
}
}
}
10 changes: 2 additions & 8 deletions backend/spi/spi-log/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{dto::log_item_dto::StatsItemAddReq, log_initializer::get_tardis_inst, serv};
use crate::{log_initializer::get_tardis_inst, serv};
use bios_sdk_invoke::clients::{
event_client::{
asteroid_mq::prelude::{EventAttribute, Subject},
get_topic, mq_error, ContextHandler, SPI_RPC_TOPIC,
},
event_client::{get_topic, mq_error, ContextHandler, SPI_RPC_TOPIC},
spi_log_client::LogItemAddV2Req,
};
use tardis::{
Expand All @@ -26,6 +23,3 @@ pub async fn handle_events() -> TardisResult<()> {

Ok(())
}
impl EventAttribute for StatsItemAddReq {
const SUBJECT: Subject = Subject::const_new("stats/add");
}
14 changes: 12 additions & 2 deletions backend/spi/spi-log/src/serv/pgv2/log_pg_item_serv.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{collections::HashMap, str::FromStr, vec};

use bios_sdk_invoke::clients::event_client::{get_topic, mq_error, EventAttributeExt as _, SPI_RPC_TOPIC};
use bios_sdk_invoke::clients::{
event_client::{get_topic, mq_error, EventAttributeExt as _, SPI_RPC_TOPIC},
spi_log_client::{StatsItemAddReq, StatsItemDeleteReq},
};
use tardis::{
basic::{dto::TardisContext, error::TardisError, result::TardisResult},
chrono::{DateTime, Utc},
Expand All @@ -22,7 +25,7 @@ use bios_basic::{
};

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

Expand Down Expand Up @@ -714,6 +717,13 @@ async fn push_to_eda(req: &LogItemAddV2Req, ref_fields: &Vec<String>, funs: &Tar
content.remove(ref_field);
}
}
if let Some(ref op) = req_clone.op {
if op.to_lowercase() == "delete" {
let stats_delete: StatsItemDeleteReq = req_clone.into();
topic.send_event(stats_delete.inject_context(funs, ctx).json()).map_err(mq_error).await?;
return Ok(());
}
}
let stats_add: StatsItemAddReq = req_clone.into();
topic.send_event(stats_add.inject_context(funs, ctx).json()).map_err(mq_error).await?;
}
Expand Down
1 change: 1 addition & 0 deletions backend/spi/spi-stats/src/api/ci.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod stats_ci_conf_api;
pub mod stats_ci_metric_api;
pub mod stats_ci_record_api;
pub mod stats_ci_sync_api;
33 changes: 31 additions & 2 deletions backend/spi/spi-stats/src/api/ci/stats_ci_conf_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use tardis::web::web_resp::{TardisApiResult, TardisPage, TardisResp, Void};

use crate::dto::stats_conf_dto::{
StatsConfDimAddReq, StatsConfDimInfoResp, StatsConfDimModifyReq, StatsConfFactAddReq, StatsConfFactColAddReq, StatsConfFactColInfoResp, StatsConfFactColModifyReq,
StatsConfFactInfoResp, StatsConfFactModifyReq,
StatsConfFactInfoResp, StatsConfFactModifyReq, StatsSyncDbConfigAddReq, StatsSyncDbConfigInfoResp, StatsSyncDbConfigModifyReq,
};
use crate::serv::{stats_conf_dim_serv, stats_conf_fact_col_serv, stats_conf_fact_serv};
use crate::serv::{stats_conf_dim_serv, stats_conf_fact_col_serv, stats_conf_fact_serv, stats_sync_serv};
use crate::stats_enumeration::StatsFactColKind;

#[derive(Clone)]
Expand Down Expand Up @@ -297,4 +297,33 @@ impl StatsCiConfApi {
stats_conf_fact_serv::create_inst(&fact_key.0, &funs, &ctx.0).await?;
TardisResp::ok(Void {})
}

/// Add Sync DateBase Config
///
/// 添加同步数据库配置
#[oai(path = "/sync/db", method = "post")]
async fn db_config_add(&self, add_req: Json<StatsSyncDbConfigAddReq>, ctx: TardisContextExtractor) -> TardisApiResult<Void> {
let funs = crate::get_tardis_inst();
stats_sync_serv::db_config_add(add_req.0, &funs, &ctx.0).await?;
TardisResp::ok(Void {})
}

/// Modify Sync DateBase Config
///
/// 修改同步数据库配置
#[oai(path = "/sync/db", method = "put")]
async fn db_config_modify(&self, modify_req: Json<StatsSyncDbConfigModifyReq>, ctx: TardisContextExtractor) -> TardisApiResult<Void> {
let funs = crate::get_tardis_inst();
stats_sync_serv::db_config_modify(modify_req.0, &funs, &ctx.0).await?;
TardisResp::ok(Void {})
}

/// List Sync DateBase Config
///
/// 查询同步数据库配置
#[oai(path = "/sync/db", method = "get")]
async fn db_config_list(&self, ctx: TardisContextExtractor) -> TardisApiResult<Vec<StatsSyncDbConfigInfoResp>> {
let funs = crate::get_tardis_inst();
TardisResp::ok(stats_sync_serv::db_config_list(&funs, &ctx.0).await?)
}
}
2 changes: 2 additions & 0 deletions backend/spi/spi-stats/src/api/ci/stats_ci_record_api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bios_basic::rbum::dto::rbum_cert_conf_dto::RbumCertConfAddReq;
use bios_basic::rbum::dto::rbum_cert_dto::RbumCertAddReq;
use tardis::chrono::{DateTime, Utc};
use tardis::serde_json::Value;
use tardis::web::context_extractor::TardisContextExtractor;
Expand Down
36 changes: 36 additions & 0 deletions backend/spi/spi-stats/src/api/ci/stats_ci_sync_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use tardis::web::{
context_extractor::TardisContextExtractor,
poem_openapi::{self, param::Path},
web_resp::{TardisApiResult, TardisResp, Void},
};

use crate::serv::stats_sync_serv;

#[derive(Clone)]
pub struct StatsCiSyncApi;

/// Interface Console Statistics Sync API
///
/// 统计同步接口
#[poem_openapi::OpenApi(prefix_path = "/ci", tag = "bios_basic::ApiTag::Interface")]
impl StatsCiSyncApi {
/// Sync Fact Record
///
/// 同步事实记录
#[oai(path = "/fact/:fact_key/sync", method = "put")]
async fn fact_record_sync(&self, fact_key: Path<String>, ctx: TardisContextExtractor) -> TardisApiResult<Void> {
let funs = crate::get_tardis_inst();
stats_sync_serv::fact_record_sync(&fact_key.0, &funs, &ctx.0).await?;
TardisResp::ok(Void {})
}

/// Sync Fact Column Record
///
/// 同步事实列记录
#[oai(path = "/fact/:fact_key/col/:col_key/sync", method = "put")]
async fn fact_col_record_sync(&self, fact_key: Path<String>, col_key: Path<String>, ctx: TardisContextExtractor) -> TardisApiResult<Void> {
let funs = crate::get_tardis_inst();
stats_sync_serv::fact_col_record_sync(&fact_key.0, &col_key.0, &funs, &ctx.0).await?;
TardisResp::ok(Void {})
}
}
80 changes: 80 additions & 0 deletions backend/spi/spi-stats/src/dto/stats_conf_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ pub struct StatsConfFactAddReq {
pub redirect_path: Option<String>,
/// default value is false
pub is_online: Option<bool>,
pub rel_cert_id: Option<String>,
pub sync_sql: Option<String>,
pub sync_cron: Option<String>,
pub sync_on: Option<bool>,
}

/// Modify Fact Configuration Request Object
Expand All @@ -186,6 +190,10 @@ pub struct StatsConfFactModifyReq {
pub remark: Option<String>,
pub redirect_path: Option<String>,
pub is_online: Option<bool>,
pub rel_cert_id: Option<String>,
pub sync_sql: Option<String>,
pub sync_cron: Option<String>,
pub sync_on: Option<bool>,
}

/// Fact Configuration Response Object
Expand Down Expand Up @@ -213,6 +221,10 @@ pub struct StatsConfFactInfoResp {
pub redirect_path: Option<String>,
pub create_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
pub rel_cert_id: Option<String>,
pub sync_sql: Option<String>,
pub sync_cron: Option<String>,
pub sync_on: Option<bool>,
}

/// Add Fact Column Configuration Request Object
Expand Down Expand Up @@ -295,6 +307,9 @@ pub struct StatsConfFactColAddReq {
pub rel_external_id: Option<String>,
pub dim_exclusive_rec: Option<bool>,
pub remark: Option<String>,
pub rel_field: Option<String>,
pub rel_sql: Option<String>,
pub rel_cert_id: Option<String>,
}

/// Modify Fact Column Configuration Request Object
Expand Down Expand Up @@ -373,6 +388,9 @@ pub struct StatsConfFactColModifyReq {
/// 用于扩展ext字段的事实列
pub rel_external_id: Option<String>,
pub remark: Option<String>,
pub rel_field: Option<String>,
pub rel_sql: Option<String>,
pub rel_cert_id: Option<String>,
}

/// Fact Column Configuration Response Object
Expand Down Expand Up @@ -462,4 +480,66 @@ pub struct StatsConfFactColInfoResp {
pub remark: Option<String>,
pub create_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
pub rel_field: Option<String>,
pub rel_sql: Option<String>,
pub rel_cert_id: Option<String>,
}

/// Add Sync DateBase Config Request Object
///
/// 添加同步数据库配置请求对象
#[derive(poem_openapi::Object, Serialize, Deserialize, Debug)]
pub struct StatsSyncDbConfigAddReq {
pub db_url: String,
pub db_user: String,
pub db_password: String,
pub max_connections: Option<u32>,
pub min_connections: Option<u32>,
}

/// Modify Sync DateBase Config Request Object
///
/// 修改同步数据库配置请求对象
#[derive(poem_openapi::Object, Serialize, Deserialize, Debug)]
pub struct StatsSyncDbConfigModifyReq {
pub id: String,
pub db_url: Option<String>,
pub db_user: Option<String>,
pub db_password: Option<String>,
pub max_connections: Option<u32>,
pub min_connections: Option<u32>,
}

/// Sync DateBase Config Response Object
///
/// 同步数据库配置响应对象
#[derive(poem_openapi::Object, sea_orm::FromQueryResult, Serialize, Deserialize, Debug)]
pub struct StatsSyncDbConfigInfoResp {
pub id: String,
pub db_url: String,
pub db_user: String,
pub max_connections: Option<u32>,
pub min_connections: Option<u32>,
}

/// Sync DateBase Config Response Object
///
/// 同步数据库配置响应对象
#[derive(poem_openapi::Object, sea_orm::FromQueryResult, Serialize, Deserialize, Debug)]
pub struct StatsSyncDbConfigInfoWithSkResp {
pub id: String,
pub db_url: String,
pub db_user: String,
pub db_password: String,
pub max_connections: Option<u32>,
pub min_connections: Option<u32>,
}

/// Sync DateBase Config Extension Object
///
/// 同步数据库配置扩展对象
#[derive(Serialize, Deserialize, Debug,Clone)]
pub struct StatsSyncDbConfigExt {
pub max_connections: Option<u32>,
pub min_connections: Option<u32>,
}
15 changes: 15 additions & 0 deletions backend/spi/spi-stats/src/dto/stats_record_dto.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bios_sdk_invoke::clients::spi_log_client::StatsItemAddReq;
use serde::{Deserialize, Serialize};
use tardis::{
chrono::{DateTime, Utc},
Expand Down Expand Up @@ -38,6 +39,20 @@ pub struct StatsFactRecordLoadReq {
/// 动态数据
pub ext: Option<Value>,
}

impl From<StatsItemAddReq> for StatsFactRecordLoadReq {
fn from(value: StatsItemAddReq) -> Self {
StatsFactRecordLoadReq {
own_paths: value.own_paths.unwrap_or_default(),
ct: value.ts.unwrap_or(Utc::now()),
idempotent_id: value.idempotent_id,
ignore_updates: None,
data: value.content,
ext: value.ext,
}
}
}

/// Load Fact Record Request Object
///
/// 事实记录加载请求对象
Expand Down
24 changes: 19 additions & 5 deletions backend/spi/spi-stats/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
use crate::{get_tardis_inst, serv};
use crate::{
get_tardis_inst,
serv::{self, stats_record_serv},
};
use bios_sdk_invoke::clients::{
event_client::{get_topic, mq_error, ContextHandler, SPI_RPC_TOPIC},
spi_log_client::LogItemAddV2Req,
spi_log_client::{LogItemAddV2Req, StatsItemAddReq, StatsItemDeleteReq},
};
use tardis::{
basic::{dto::TardisContext, result::TardisResult},
{log as tracing, log::instrument},
};

#[instrument]
async fn handle_add_event(req: LogItemAddV2Req, ctx: TardisContext) -> TardisResult<()> {
async fn handle_add_event(req: StatsItemAddReq, ctx: TardisContext) -> TardisResult<()> {
let funs = get_tardis_inst();
//TODO
if let Some(ref key) = req.key {
stats_record_serv::fact_record_load(&req.tag, &key.to_string(), req.clone().into(), &funs, &ctx).await?;
}
Ok(())
}
#[instrument]
async fn handle_delete_event(req: StatsItemDeleteReq, ctx: TardisContext) -> TardisResult<()> {
let funs = get_tardis_inst();
if let Some(ref key) = req.key {
stats_record_serv::fact_record_delete(&req.tag, &key.to_string(), &funs, &ctx).await?;
}
Ok(())
}

pub async fn handle_events() -> TardisResult<()> {
use bios_sdk_invoke::clients::event_client::asteroid_mq::prelude::*;
if let Some(topic) = get_topic(&SPI_RPC_TOPIC) {
topic.create_endpoint([Interest::new("stats/*")]).await.map_err(mq_error)?.create_event_loop().with_handler(ContextHandler(handle_add_event)).spawn();
topic.create_endpoint([Interest::new("stats/add")]).await.map_err(mq_error)?.create_event_loop().with_handler(ContextHandler(handle_add_event)).spawn();
topic.create_endpoint([Interest::new("stats/delete")]).await.map_err(mq_error)?.create_event_loop().with_handler(ContextHandler(handle_delete_event)).spawn();
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions backend/spi/spi-stats/src/serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod stats_conf_fact_col_serv;
pub mod stats_conf_fact_serv;
pub mod stats_metric_serv;
pub mod stats_record_serv;
pub mod stats_sync_serv;
1 change: 1 addition & 0 deletions backend/spi/spi-stats/src/serv/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod stats_pg_conf_fact_serv;
pub mod stats_pg_initializer;
pub mod stats_pg_metric_serv;
pub(crate) mod stats_pg_record_serv;
pub mod stats_pg_sync_serv;
Loading

0 comments on commit 3ee65a0

Please sign in to comment.