Skip to content

Commit

Permalink
spi-stats:temporary TODO add delete by ownership.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljl committed Oct 26, 2023
1 parent 7d31938 commit 9de0883
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
17 changes: 16 additions & 1 deletion spi/spi-stats/src/api/ci/stats_ci_record_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl StatsCiRecordApi {
TardisResp::ok(Void {})
}

/// 删除事实记录
/// Delete Fact Record
#[oai(path = "/fact/:fact_key/:record_key", method = "delete")]
async fn fact_record_delete(&self, fact_key: Path<String>, record_key: Path<String>, ctx: TardisContextExtractor) -> TardisApiResult<Void> {
Expand All @@ -67,6 +68,7 @@ impl StatsCiRecordApi {
TardisResp::ok(Void {})
}

/// 删除事实记录
/// Load Fact Records
#[oai(path = "/fact/:fact_key/batch/load", method = "put")]
async fn fact_records_load(&self, fact_key: Path<String>, add_req: Json<Vec<StatsFactRecordsLoadReq>>, ctx: TardisContextExtractor) -> TardisApiResult<Void> {
Expand All @@ -75,6 +77,7 @@ impl StatsCiRecordApi {
TardisResp::ok(Void {})
}

/// 删除事实记录
/// Delete Fact Records
#[oai(path = "/fact/:fact_key/batch/remove", method = "put")]
async fn fact_records_delete(&self, fact_key: Path<String>, delete_req: Json<Vec<String>>, ctx: TardisContextExtractor) -> TardisApiResult<Void> {
Expand All @@ -83,6 +86,17 @@ impl StatsCiRecordApi {
TardisResp::ok(Void {})
}

/// 删除事实记录
/// 注意:此操作在逻辑上删除了所有权的实际记录。
/// Delete Fact Records
/// Note: This action logically removes the factual record of ownership.
#[oai(path = "/fact/:fact_key/ownership/remove", method = "put")]
async fn fact_records_logic_delete_by_ownership(&self, fact_key: Path<String>, own_paths: Query<String>, ctx: TardisContextExtractor) -> TardisApiResult<Void> {
let funs = crate::get_tardis_inst();
stats_record_serv::fact_records_logic_delete_by_ownership(&fact_key.0, &own_paths.0, &funs, &ctx.0).await?;
TardisResp::ok(Void {})
}

/// Delete Fact Records
#[oai(path = "/fact/:fact_key/dim/:dim_conf_key/batch/remove", method = "put")]
async fn fact_records_delete_by_dim_key(
Expand All @@ -97,8 +111,9 @@ impl StatsCiRecordApi {
TardisResp::ok(Void {})
}

/// 清空事实记录
/// 注意:此操作将物理删除所有事实记录,且无法恢复,请谨慎使用!
/// Clean Fact Records
///
/// Note:This operation will physically delete all fact records and cannot be recovered, please use caution!
#[oai(path = "/fact/:fact_key/batch/clean", method = "delete")]
async fn fact_records_clean(&self, fact_key: Path<String>, before_ct: Query<Option<DateTime<Utc>>>, ctx: TardisContextExtractor) -> TardisApiResult<Void> {
Expand Down
2 changes: 2 additions & 0 deletions spi/spi-stats/src/serv/pg/stats_pg_conf_fact_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ async fn create_inst_table(
}
}
sql.push("ct timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP".to_string());
// TODO DELETE
// sql.push("is_delete boolean".to_string());
index.push(("ct".to_string(), "btree"));
index.push(("date(timezone('UTC', ct))".to_string(), "btree"));
index.push(("date_part('hour',timezone('UTC', ct))".to_string(), "btree"));
Expand Down
2 changes: 2 additions & 0 deletions spi/spi-stats/src/serv/pg/stats_pg_metric_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ pub async fn query_metrics(query_req: &StatsQueryMetricsReq, funs: &TardisFunsIn
// package limit
let query_limit = if let Some(limit) = &query_req.limit { format!("LIMIT {limit}") } else { "".to_string() };
let ignore_group_agg = !(!sql_part_groups.is_empty() && query_req.group_agg.unwrap_or(false));
// TODO DELETE: AND fact.is_delete = TRUE
let final_sql = format!(
r#"SELECT {sql_part_outer_selects}{}
FROM (
Expand All @@ -484,6 +485,7 @@ pub async fn query_metrics(query_req: &StatsQueryMetricsReq, funs: &TardisFunsIn
LEFT JOIN {fact_inst_del_table_name} del ON del.key = fact.key AND del.ct >= $2 AND del.ct <= $3
WHERE
fact.own_paths LIKE $1
AND del.key IS NULL
AND fact.ct >= $2 AND fact.ct <= $3
ORDER BY {}fact.ct DESC
Expand Down
23 changes: 23 additions & 0 deletions spi/spi-stats/src/serv/pg/stats_pg_record_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,29 @@ pub(crate) async fn fact_records_delete(fact_conf_key: &str, fact_record_delete_
Ok(())
}


pub(crate) async fn fact_records_logic_delete_by_ownership(fact_conf_key: &str, own_paths: &str, funs: &TardisFunsInst, ctx: &TardisContext, inst: &SpiBsInst) -> TardisResult<()> {
let bs_inst = inst.inst::<TardisRelDBClient>();
let (mut conn, _) = common_pg::init_conn(bs_inst).await?;
conn.begin().await?;
if !stats_pg_conf_fact_serv::online(fact_conf_key, &conn, ctx).await? {
return Err(funs.err().conflict("fact_record", "delete_set", "The fact config not online.", "409-spi-stats-fact-conf-not-online"));
}

let table_name = package_table_name(&format!("stats_inst_fact_{fact_conf_key}"), ctx);
conn.execute_one(
&format!(
r#"UPDATE {table_name} SET is_delete = TRUE
WHERE own_paths = $1
"#,
),
vec![Value::from(own_paths)],
)
.await?;
conn.commit().await?;
Ok(())
}

pub(crate) async fn fact_records_delete_by_dim_key(
fact_conf_key: &str,
dim_conf_key: &str,
Expand Down
3 changes: 2 additions & 1 deletion spi/spi-stats/src/serv/stats_record_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ spi_dispatch_service! {
fact_record_delete(fact_conf_key: &str, fact_record_key: &str) -> TardisResult<()>;
fact_records_load(fact_conf_key: &str, add_req_set: Vec<StatsFactRecordsLoadReq>) -> TardisResult<()>;
fact_records_delete(fact_conf_key: &str, fact_record_delete_keys: &[String]) -> TardisResult<()>;
fact_records_delete_by_dim_key(fact_conf_key: &str, dim_conf_key: &str,dim_record_key: Option<serde_json::Value>) -> TardisResult<()>;
fact_records_logic_delete_by_ownership(fact_conf_key: &str, dim_conf_key: &str) -> TardisResult<()>;
fact_records_delete_by_dim_key(fact_conf_key: &str, own_paths: &str,dim_record_key: Option<serde_json::Value>) -> TardisResult<()>;
fact_records_clean(fact_conf_key: &str, before_ct: Option<DateTime<Utc>>) -> TardisResult<()>;
dim_record_add(dim_conf_key: String, add_req: StatsDimRecordAddReq) -> TardisResult<()>;
dim_record_paginate(
Expand Down

0 comments on commit 9de0883

Please sign in to comment.