feat: 添加定时任务以获取总数据量2
This commit is contained in:
parent
e92fee98d7
commit
a750a630df
|
|
@ -315,6 +315,17 @@ version = "2.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
|
||||
|
||||
[[package]]
|
||||
name = "cron"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f8c3e73077b4b4a6ab1ea5047c37c57aee77657bc8ecd6f29b0af082d0b0c07"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"nom",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.15"
|
||||
|
|
@ -815,6 +826,7 @@ dependencies = [
|
|||
"serde_with",
|
||||
"sqlx",
|
||||
"tokio",
|
||||
"tokio-cron-scheduler",
|
||||
"tracing",
|
||||
"tracing-appender",
|
||||
"tracing-subscriber",
|
||||
|
|
@ -1415,6 +1427,17 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
|
||||
|
||||
[[package]]
|
||||
name = "num-derive"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.46"
|
||||
|
|
@ -2784,6 +2807,21 @@ dependencies = [
|
|||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-cron-scheduler"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4c2e3a88f827f597799cf70a6f673074e62f3fc5ba5993b2873345c618a29af"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"cron",
|
||||
"num-derive",
|
||||
"num-traits",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.5.0"
|
||||
|
|
@ -3109,6 +3147,17 @@ version = "1.0.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee48d38b119b0cd71fe4141b30f5ba9c7c5d9f4e7a3a8b4a674e4b6ef789976f"
|
||||
dependencies = [
|
||||
"getrandom 0.3.3",
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.1"
|
||||
|
|
|
|||
23
src/main.rs
23
src/main.rs
|
|
@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize, Serializer};
|
|||
use serde_json::json;
|
||||
use sqlx::{FromRow, PgPool};
|
||||
use std::env;
|
||||
use std::sync::atomic::{AtomicI64, Ordering};
|
||||
use dotenv::dotenv;
|
||||
use tracing::level_filters::LevelFilter;
|
||||
use tracing_subscriber::{
|
||||
|
|
@ -19,7 +20,7 @@ use salvo::cors::Cors;
|
|||
use salvo::http::Method;
|
||||
|
||||
static POSTGRES: OnceCell<PgPool> = OnceCell::new();
|
||||
|
||||
static TOTAL_COUNT: AtomicI64 = AtomicI64::new(0);
|
||||
|
||||
pub fn format_time_8<S>(time: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
|
|
@ -82,10 +83,16 @@ async fn get_mplogx(req: &mut Request, res: &mut Response) {
|
|||
.unwrap_or(0)
|
||||
},
|
||||
None => {
|
||||
// 使用缓存的TOTAL_COUNT值,如果为0则执行查询
|
||||
let cached_count = TOTAL_COUNT.load(Ordering::SeqCst);
|
||||
if cached_count == 0 {
|
||||
sqlx::query_scalar(&count_sql)
|
||||
.fetch_one(get_postgres())
|
||||
.await
|
||||
.unwrap_or(0)
|
||||
} else {
|
||||
cached_count
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -140,7 +147,19 @@ async fn analyze_chunks() {
|
|||
"#;
|
||||
|
||||
match sqlx::query(sql).execute(get_postgres()).await {
|
||||
Ok(_) => tracing::info!("Successfully analyzed TimescaleDB chunks"),
|
||||
Ok(_) => {
|
||||
tracing::info!("Successfully analyzed TimescaleDB chunks");
|
||||
// 更新总行数
|
||||
if let Ok(count) = sqlx::query_scalar::<_, i64>("SELECT approximate_row_count('mplogx');")
|
||||
.fetch_one(get_postgres())
|
||||
.await
|
||||
{
|
||||
TOTAL_COUNT.store(count, Ordering::SeqCst);
|
||||
tracing::info!("Updated total_count to {}", count);
|
||||
} else {
|
||||
tracing::error!("Failed to update total_count");
|
||||
}
|
||||
},
|
||||
Err(e) => tracing::error!("Failed to analyze TimescaleDB chunks: {}", e),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue