feat(driver-store): add storage usage breakdown
This commit is contained in:
1 parent
b2324aea29
commit
d7dc4c959a
9 files changed
+231
-25
No files matched your search
@@ -195,6 +195,23 @@ pub struct AgentDriverInfo {
|
||||
pub jre_installed: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DriverStoreUsageItem {
|
||||
pub id: String,
|
||||
pub bytes: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DriverStoreUsage {
|
||||
pub total_bytes: u64,
|
||||
pub jre_bytes: u64,
|
||||
pub agent_driver_bytes: u64,
|
||||
pub jdbc_plugin_bytes: u64,
|
||||
pub jdbc_driver_bytes: u64,
|
||||
pub jres: Vec<DriverStoreUsageItem>,
|
||||
pub agent_drivers: Vec<DriverStoreUsageItem>,
|
||||
}
|
||||
|
||||
pub struct AgentManager {
|
||||
base_dir: PathBuf,
|
||||
app_version: String,
|
||||
@@ -281,6 +298,68 @@ impl AgentManager {
|
||||
self.driver_jar_path(db_type).exists()
|
||||
}
|
||||
|
||||
pub fn collect_driver_store_usage(&self, plugin_root: &Path) -> DriverStoreUsage {
|
||||
let mut jres = Vec::new();
|
||||
let mut jre_bytes = 0u64;
|
||||
if let Ok(entries) = std::fs::read_dir(&self.base_dir) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if !path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
let Some(name) = path.file_name().and_then(|v| v.to_str()) else {
|
||||
continue;
|
||||
};
|
||||
if !name.starts_with("jre-") {
|
||||
continue;
|
||||
}
|
||||
let key = name.trim_start_matches("jre-").to_string();
|
||||
let bytes = path_size_bytes(&path);
|
||||
jre_bytes = jre_bytes.saturating_add(bytes);
|
||||
jres.push(DriverStoreUsageItem { id: key, bytes });
|
||||
}
|
||||
}
|
||||
jres.sort_by(|left, right| left.id.cmp(&right.id));
|
||||
|
||||
let mut agent_drivers = Vec::new();
|
||||
let mut agent_driver_bytes = 0u64;
|
||||
let drivers_root = self.base_dir.join("drivers");
|
||||
if let Ok(entries) = std::fs::read_dir(&drivers_root) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if !path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
let Some(id) = path.file_name().and_then(|v| v.to_str()) else {
|
||||
continue;
|
||||
};
|
||||
let bytes = path_size_bytes(&path);
|
||||
agent_driver_bytes = agent_driver_bytes.saturating_add(bytes);
|
||||
agent_drivers.push(DriverStoreUsageItem { id: id.to_string(), bytes });
|
||||
}
|
||||
}
|
||||
agent_drivers.sort_by(|left, right| left.id.cmp(&right.id));
|
||||
|
||||
let jdbc_root = plugin_root.join("jdbc");
|
||||
let jdbc_driver_root = jdbc_root.join("drivers");
|
||||
let jdbc_driver_bytes = path_size_bytes(&jdbc_driver_root);
|
||||
let jdbc_total_bytes = path_size_bytes(&jdbc_root);
|
||||
let jdbc_plugin_bytes = jdbc_total_bytes.saturating_sub(jdbc_driver_bytes);
|
||||
|
||||
DriverStoreUsage {
|
||||
total_bytes: jre_bytes
|
||||
.saturating_add(agent_driver_bytes)
|
||||
.saturating_add(jdbc_plugin_bytes)
|
||||
.saturating_add(jdbc_driver_bytes),
|
||||
jre_bytes,
|
||||
agent_driver_bytes,
|
||||
jdbc_plugin_bytes,
|
||||
jdbc_driver_bytes,
|
||||
jres,
|
||||
agent_drivers,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve_java_runtime(&self, state: &AgentState, jre_key: &str) -> Result<PathBuf, String> {
|
||||
match state.java_runtime.mode {
|
||||
JavaRuntimeMode::Managed => {
|
||||
@@ -429,6 +508,27 @@ impl AgentManager {
|
||||
}
|
||||
}
|
||||
|
||||
fn path_size_bytes(path: &Path) -> u64 {
|
||||
if let Ok(meta) = std::fs::symlink_metadata(path) {
|
||||
if meta.is_file() {
|
||||
return meta.len();
|
||||
}
|
||||
if !meta.is_dir() {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut total = 0u64;
|
||||
if let Ok(entries) = std::fs::read_dir(path) {
|
||||
for entry in entries.flatten() {
|
||||
total = total.saturating_add(path_size_bytes(&entry.path()));
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
|
||||
fn java_executable_name() -> &'static str {
|
||||
if cfg!(windows) {
|
||||
"java.exe"
|
||||
|
||||
@@ -89,6 +89,7 @@ async fn main() {
|
||||
// Agent drivers
|
||||
.route("/agents/installed-local", get(routes::agents::list_installed_agents_local))
|
||||
.route("/agents/installed", get(routes::agents::list_installed_agents))
|
||||
.route("/agents/storage-usage", get(routes::agents::get_driver_store_usage))
|
||||
.route("/agents/install", post(routes::agents::install_agent))
|
||||
.route("/agents/upgrade-all", post(routes::agents::upgrade_all_agents))
|
||||
.route("/agents/uninstall", post(routes::agents::uninstall_agent))
|
||||
|
||||
@@ -4,8 +4,8 @@ use axum::extract::{Multipart, Path, State};
|
||||
use axum::response::sse::{Event, Sse};
|
||||
use axum::Json;
|
||||
use dbx_core::agent_manager::{
|
||||
AgentDriverInfo, AgentManager, AgentRegistry, AgentState, InstalledDriver, JavaRuntimeConfig, JavaRuntimeMode,
|
||||
DEFAULT_JRE_KEY,
|
||||
AgentDriverInfo, AgentManager, AgentRegistry, AgentState, DriverStoreUsage, InstalledDriver, JavaRuntimeConfig,
|
||||
JavaRuntimeMode, DEFAULT_JRE_KEY,
|
||||
};
|
||||
use dbx_core::agent_service::{
|
||||
build_agent_list, download_temp_path, fetch_registry, find_local_agent_jar, github_url_to_r2_path,
|
||||
@@ -48,6 +48,10 @@ pub async fn list_installed_agents(State(state): State<Arc<WebState>>) -> Result
|
||||
Ok(Json(build_agent_list(&state.app.agent_manager, registry.as_ref())))
|
||||
}
|
||||
|
||||
pub async fn get_driver_store_usage(State(state): State<Arc<WebState>>) -> Result<Json<DriverStoreUsage>, AppError> {
|
||||
Ok(Json(state.app.agent_manager.collect_driver_store_usage(state.app.plugins.root_dir())))
|
||||
}
|
||||
|
||||
pub async fn install_agent(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<AgentTypeRequest>,
|
||||
|
||||
Reference in new issue
Block a user