From 9de08837709d3026c61137ae84f40c53d8c3e7f4 Mon Sep 17 00:00:00 2001 From: ljl <17743125563@163.com> Date: Thu, 26 Oct 2023 10:44:26 +0800 Subject: [PATCH] spi-stats:temporary TODO add delete by ownership. --- .../src/api/ci/stats_ci_record_api.rs | 17 +++++++++++++- .../src/serv/pg/stats_pg_conf_fact_serv.rs | 2 ++ .../src/serv/pg/stats_pg_metric_serv.rs | 2 ++ .../src/serv/pg/stats_pg_record_serv.rs | 23 +++++++++++++++++++ spi/spi-stats/src/serv/stats_record_serv.rs | 3 ++- 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/spi/spi-stats/src/api/ci/stats_ci_record_api.rs b/spi/spi-stats/src/api/ci/stats_ci_record_api.rs index 344a5c2fd..6e88397d5 100644 --- a/spi/spi-stats/src/api/ci/stats_ci_record_api.rs +++ b/spi/spi-stats/src/api/ci/stats_ci_record_api.rs @@ -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, record_key: Path, ctx: TardisContextExtractor) -> TardisApiResult { @@ -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, add_req: Json>, ctx: TardisContextExtractor) -> TardisApiResult { @@ -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, delete_req: Json>, ctx: TardisContextExtractor) -> TardisApiResult { @@ -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, own_paths: Query, ctx: TardisContextExtractor) -> TardisApiResult { + 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( @@ -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, before_ct: Query>>, ctx: TardisContextExtractor) -> TardisApiResult { diff --git a/spi/spi-stats/src/serv/pg/stats_pg_conf_fact_serv.rs b/spi/spi-stats/src/serv/pg/stats_pg_conf_fact_serv.rs index 611f90f85..8359ce005 100644 --- a/spi/spi-stats/src/serv/pg/stats_pg_conf_fact_serv.rs +++ b/spi/spi-stats/src/serv/pg/stats_pg_conf_fact_serv.rs @@ -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")); diff --git a/spi/spi-stats/src/serv/pg/stats_pg_metric_serv.rs b/spi/spi-stats/src/serv/pg/stats_pg_metric_serv.rs index 630d7fe6f..378b200ca 100644 --- a/spi/spi-stats/src/serv/pg/stats_pg_metric_serv.rs +++ b/spi/spi-stats/src/serv/pg/stats_pg_metric_serv.rs @@ -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 ( @@ -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 diff --git a/spi/spi-stats/src/serv/pg/stats_pg_record_serv.rs b/spi/spi-stats/src/serv/pg/stats_pg_record_serv.rs index 65c71f229..4bada6bbf 100644 --- a/spi/spi-stats/src/serv/pg/stats_pg_record_serv.rs +++ b/spi/spi-stats/src/serv/pg/stats_pg_record_serv.rs @@ -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::(); + 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, diff --git a/spi/spi-stats/src/serv/stats_record_serv.rs b/spi/spi-stats/src/serv/stats_record_serv.rs index fc170d37b..837f1bad0 100644 --- a/spi/spi-stats/src/serv/stats_record_serv.rs +++ b/spi/spi-stats/src/serv/stats_record_serv.rs @@ -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) -> 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) -> 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) -> TardisResult<()>; fact_records_clean(fact_conf_key: &str, before_ct: Option>) -> TardisResult<()>; dim_record_add(dim_conf_key: String, add_req: StatsDimRecordAddReq) -> TardisResult<()>; dim_record_paginate(