feat: health API — add build_timestamp + detailed resource status list

- build.rs: BUILD_TIMESTAMP from build time via `date -u`
- GET /health: now returns build_timestamp
- GET /health/detailed: returns build_timestamp + resources block
  (cpu_used/cpu_idle/memory/gpu usage)
This commit is contained in:
Accusys
2026-05-14 14:59:30 +08:00
parent 2c4e32f14a
commit 6927415c41
2 changed files with 39 additions and 0 deletions

View File

@@ -9,6 +9,15 @@ fn main() {
.map(|s| s.trim().to_string())
.unwrap_or_else(|| "unknown".to_string());
let timestamp = std::process::Command::new("date")
.args(["-u", "+%Y-%m-%dT%H:%M:%SZ"])
.output()
.ok()
.and_then(|o| String::from_utf8(o.stdout).ok())
.map(|s| s.trim().to_string())
.unwrap_or_else(|| "unknown".to_string());
println!("cargo:rustc-env=BUILD_VERSION={}", version);
println!("cargo:rustc-env=BUILD_GIT_HASH={}", git_hash);
println!("cargo:rustc-env=BUILD_TIMESTAMP={}", timestamp);
}

View File

@@ -16,6 +16,7 @@ use crate::core::cache::{keys, MongoCache, RedisCache};
use crate::core::config::REDIS_KEY_PREFIX;
use crate::core::db::schema;
use crate::core::db::{Database, PostgresDb, QdrantDb, RedisClient, VideoRecord, VideoStatus};
use crate::worker::resources::SystemResources;
use crate::core::text::tokenizer::tokenize_chinese_text;
use crate::{Embedder, FileManager};
@@ -73,6 +74,7 @@ struct HealthResponse {
status: String,
version: String,
build_git_hash: String,
build_timestamp: String,
uptime_ms: u64,
}
@@ -371,8 +373,22 @@ struct DetailedHealthResponse {
status: String,
version: String,
build_git_hash: String,
build_timestamp: String,
uptime_ms: u64,
services: ServiceHealth,
resources: ResourceStatus,
}
#[derive(Debug, Serialize)]
struct ResourceStatus {
cpu_used_percent: f64,
cpu_idle_percent: f64,
memory_available_mb: u64,
memory_total_mb: u64,
memory_used_percent: f64,
gpu_available: bool,
gpu_utilization: Option<f64>,
gpu_memory_used_pct: Option<f64>,
}
#[derive(Debug, Serialize)]
@@ -411,6 +427,7 @@ async fn health(State(state): State<AppState>) -> Json<HealthResponse> {
status: status.to_string(),
version: env!("BUILD_VERSION").to_string(),
build_git_hash: env!("BUILD_GIT_HASH").to_string(),
build_timestamp: env!("BUILD_TIMESTAMP").to_string(),
uptime_ms: get_uptime_ms(),
})
}
@@ -431,10 +448,13 @@ async fn health_detailed(State(state): State<AppState>) -> Json<DetailedHealthRe
"degraded"
};
let sys = SystemResources::check();
Json(DetailedHealthResponse {
status: overall_status.to_string(),
version: env!("BUILD_VERSION").to_string(),
build_git_hash: env!("BUILD_GIT_HASH").to_string(),
build_timestamp: env!("BUILD_TIMESTAMP").to_string(),
uptime_ms: get_uptime_ms(),
services: ServiceHealth {
postgres,
@@ -442,6 +462,16 @@ async fn health_detailed(State(state): State<AppState>) -> Json<DetailedHealthRe
qdrant,
mongodb,
},
resources: ResourceStatus {
cpu_used_percent: sys.cpu_used_percent,
cpu_idle_percent: sys.cpu_idle_percent,
memory_available_mb: sys.memory_available_mb,
memory_total_mb: sys.memory_total_mb,
memory_used_percent: sys.memory_used_percent,
gpu_available: sys.gpu_available,
gpu_utilization: sys.gpu_utilization,
gpu_memory_used_pct: sys.gpu_memory_used_pct,
},
})
}