Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

21.5 KB
Raw Blame
use std::sync::Arc;

use axum::extract::{Multipart, Path, State};
use axum::response::sse::{Event, Sse};
use axum::Json;
use dbx_core::agent_manager::{
    AgentDriverInfo, AgentState, DriverStoreUsage, JavaRuntimeConfig, JavaRuntimeMode, DEFAULT_JRE_KEY,
};
use dbx_core::agent_service::{
    batch_cancellation_key, build_agent_list, cancel_agent_batch_upgrade, cancel_agent_driver_install,
    clear_agent_download_cache, fetch_registry, fetch_registry_from_claimed, import_agent_driver,
    import_agents_from_package as import_agents_from_package_core, inspect_offline_package,
    install_agent_driver_claimed, install_cancellation_key, invalidate_registry_cache, reinstall_agent_jre,
    uninstall_agent_driver, uninstall_agent_jre, upgrade_all_agent_drivers_claimed, AgentProgressEvent,
    OfflineImportPlan,
};
use dbx_core::driver_runtime::DriverRuntimeSummary;
use futures::Stream;
use serde::{Deserialize, Serialize};
use tokio::io::AsyncWriteExt;
use tokio::sync::broadcast;

use crate::error::AppError;
use crate::state::WebState;

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentTypeRequest {
    pub db_type: String,
    pub operation_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JreRequest {
    pub jre_key: Option<String>,
    pub operation_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentOperationRequest {
    pub operation_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentUpdateBlockersRequest {
    pub db_types: Vec<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct AgentUpdateBlocker {
    pub db_type: String,
    pub label: String,
    pub connections: Vec<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JavaRuntimeRequest {
    pub config: JavaRuntimeConfig,
}

pub async fn list_installed_agents_local(
    State(state): State<Arc<WebState>>,
) -> Result<Json<Vec<AgentDriverInfo>>, AppError> {
    Ok(Json(build_agent_list(&state.app.agent_manager, None)))
}

pub async fn list_installed_agents(State(state): State<Arc<WebState>>) -> Result<Json<Vec<AgentDriverInfo>>, AppError> {
    let registry = fetch_registry().await.ok();
    Ok(Json(build_agent_list(&state.app.agent_manager, registry.as_ref())))
}

pub async fn is_agent_installed(
    State(state): State<Arc<WebState>>,
    Path(db_type): Path<String>,
) -> Result<Json<bool>, AppError> {
    Ok(Json(state.app.agent_manager.is_driver_installed(&db_type)))
}

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 clear_driver_download_cache(
    State(state): State<Arc<WebState>>,
) -> Result<Json<serde_json::Value>, AppError> {
    clear_agent_download_cache(&state.app.agent_manager).map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn get_driver_runtime_summary(
    State(state): State<Arc<WebState>>,
) -> Result<Json<DriverRuntimeSummary>, AppError> {
    Ok(Json(dbx_core::driver_runtime::collect_driver_runtime_summary(&state.app).await))
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DriverRuntimeRequest {
    pub runtime_id: String,
}

pub async fn stop_driver_runtime(
    State(state): State<Arc<WebState>>,
    headers: axum::http::HeaderMap,
    Json(req): Json<DriverRuntimeRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let key = dbx_core::driver_runtime::runtime_agent_key(&req.runtime_id)
        .ok_or_else(|| AppError::from("Only agent runtimes can be stopped".to_owned()))?;
    let permit = super::pl_debug::claim_mutation(
        &state,
        &headers,
        dbx_core::query::pl_debug::PlDebugMutationScope::Driver(key.to_owned()),
    )
    .await?;
    permit.after_debug_cleanup(dbx_core::driver_runtime::stop_driver_runtime(&state.app, &req.runtime_id)).await?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn restart_driver_runtime(
    State(state): State<Arc<WebState>>,
    headers: axum::http::HeaderMap,
    Json(req): Json<DriverRuntimeRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    if req.runtime_id.starts_with("agent-runtime:") {
        let key = dbx_core::driver_runtime::runtime_agent_key(&req.runtime_id)
            .ok_or_else(|| AppError::from("Only agent runtimes can be restarted".to_owned()))?;
        let permit = super::pl_debug::claim_mutation(
            &state,
            &headers,
            dbx_core::query::pl_debug::PlDebugMutationScope::Driver(key.to_owned()),
        )
        .await?;
        permit
            .after_debug_cleanup(dbx_core::driver_runtime::restart_driver_runtime(&state.app, &req.runtime_id))
            .await?;
    } else {
        dbx_core::driver_runtime::restart_driver_runtime(&state.app, &req.runtime_id).await?;
    }
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn install_agent(
    State(state): State<Arc<WebState>>,
    Json(req): Json<AgentTypeRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    // Resolve the operation id first, then register the cancellation token under
    // it BEFORE any awaitable setup (blocker check, lock wait, registry fetch)
    // so a cancel fired while the modal is showing is observed by this exact
    // install instead of being silently lost or crossing into a second
    // same-driver install.
    let operation_id = req.operation_id.clone().unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
    let cancellation =
        state.app.agent_manager.begin_install_cancellation(&install_cancellation_key(&operation_id)).await;
    let result = async {
        ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&req.db_type))
            .await
            .map_err(AppError::from)?;
        let tx = progress_sender(&state, "global").await;
        install_agent_driver_claimed(
            &state.app.agent_manager,
            &req.db_type,
            |event| send_progress_event(&tx, event.with_operation_id(&operation_id)),
            &cancellation,
        )
        .await
        .map_err(AppError::from)?;
        Ok(Json(serde_json::json!({ "ok": true })))
    }
    .await;
    state.app.agent_manager.finish_install_cancellation(&install_cancellation_key(&operation_id), &cancellation).await;
    result
}

pub async fn upgrade_all_agents(
    State(state): State<Arc<WebState>>,
    Json(req): Json<AgentOperationRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    // Resolve the batch operation id first, then register the batch token under
    // it BEFORE the registry fetch + blocker check so a cancel fired while the
    // batch is still setting up aborts it.
    let operation_id = req.operation_id.clone().unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
    let cancellation = state.app.agent_manager.begin_install_cancellation(&batch_cancellation_key(&operation_id)).await;
    let result = async {
        // The batch token is registered above; race the pre-blocker registry
        // fetch against it so a cancel fired during batch setup aborts promptly
        // instead of waiting out the 10s client timeout.
        let registry = fetch_registry_from_claimed(dbx_core::DownloadSource::Official, &[cancellation.as_ref()])
            .await
            .map_err(AppError::from)?;
        let agents = build_agent_list(&state.app.agent_manager, Some(&registry));
        let updatable: Vec<String> =
            agents.iter().filter(|agent| agent.update_available).map(|agent| agent.db_type.clone()).collect();
        ensure_no_agent_update_blockers(&state.app, &updatable).await.map_err(AppError::from)?;
        let tx = progress_sender(&state, "global").await;
        let result = upgrade_all_agent_drivers_claimed(
            &state.app.agent_manager,
            |event| send_progress_event(&tx, event.with_operation_id(&operation_id)),
            &cancellation,
            &operation_id,
        )
        .await
        .map_err(AppError::from)?;
        Ok(Json(serde_json::to_value(result).map_err(|err| AppError::from(err.to_string()))?))
    }
    .await;
    state.app.agent_manager.finish_install_cancellation(&batch_cancellation_key(&operation_id), &cancellation).await;
    result
}

pub async fn cancel_install(
    State(state): State<Arc<WebState>>,
    Json(req): Json<AgentTypeRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    cancel_agent_driver_install(&state.app.agent_manager, &req.db_type, req.operation_id.as_deref())
        .await
        .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn cancel_upgrade_all(
    State(state): State<Arc<WebState>>,
    Json(req): Json<AgentOperationRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    cancel_agent_batch_upgrade(&state.app.agent_manager, req.operation_id.as_deref()).await.map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn check_agent_update_blockers(
    State(state): State<Arc<WebState>>,
    Json(req): Json<AgentUpdateBlockersRequest>,
) -> Result<Json<Vec<AgentUpdateBlocker>>, AppError> {
    Ok(Json(agent_update_blockers(&state.app, &req.db_types).await))
}

pub async fn uninstall_agent(
    State(state): State<Arc<WebState>>,
    Json(req): Json<AgentTypeRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&req.db_type)).await.map_err(AppError::from)?;
    uninstall_agent_driver(&state.app.agent_manager, &req.db_type).await.map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn get_agent_java_runtime_config(
    State(state): State<Arc<WebState>>,
) -> Result<Json<JavaRuntimeConfig>, AppError> {
    Ok(Json(state.app.agent_manager.load_state().java_runtime))
}

pub async fn set_agent_java_runtime_config(
    State(state): State<Arc<WebState>>,
    headers: axum::http::HeaderMap,
    Json(req): Json<JavaRuntimeRequest>,
) -> Result<Json<JavaRuntimeConfig>, AppError> {
    let am = &state.app.agent_manager;
    let mut config = req.config;
    if config.mode == JavaRuntimeMode::Custom || config.mode == JavaRuntimeMode::System {
        let candidate_state = AgentState { java_runtime: config.clone(), ..am.load_state() };
        let resolved = am.resolve_java_runtime(&candidate_state, DEFAULT_JRE_KEY).map_err(AppError::from)?;
        if config.mode == JavaRuntimeMode::Custom {
            config.custom_java_path = Some(resolved.to_string_lossy().to_string());
        }
    }
    if config.mode != JavaRuntimeMode::Custom {
        config.custom_java_path = None;
    }

    let permit =
        super::pl_debug::claim_mutation(&state, &headers, dbx_core::query::pl_debug::PlDebugMutationScope::AllRuntimes)
            .await?;
    permit
        .after_debug_cleanup(async {
            am.mutate_state(|local_state| local_state.java_runtime = config.clone())?;
            am.stop_daemons().await;
            Ok(())
        })
        .await?;
    Ok(Json(config))
}

pub async fn invalidate_agent_registry_cache() -> Result<Json<serde_json::Value>, AppError> {
    invalidate_registry_cache().await;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn import_agents_from_zip(
    State(state): State<Arc<WebState>>,
    mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, AppError> {
    let tmp_dir = state.data_dir.join("tmp");
    std::fs::create_dir_all(&tmp_dir).map_err(|err| AppError::from(err.to_string()))?;

    let mut operation_id = uuid::Uuid::new_v4().to_string();
    while let Some(field) = multipart.next_field().await.map_err(|err| AppError::from(err.to_string()))? {
        if field.name() == Some("operationId") {
            let candidate = field.text().await.map_err(|err| AppError::from(err.to_string()))?;
            if !candidate.is_empty() {
                operation_id = candidate;
            }
            continue;
        }
        if field.name() != Some("file") {
            continue;
        }

        let file_name = field.file_name().unwrap_or("offline-drivers.zip").to_string();
        let extension = offline_package_extension(&file_name)
            .ok_or_else(|| AppError::from("Offline driver package must be a .zip or .tar.zst file".to_string()))?;
        let package_path = tmp_dir.join(format!("agent-offline-{}.{}", uuid::Uuid::new_v4(), extension));
        let tx = progress_sender(&state, "global").await;
        let result = async {
            let mut upload =
                tokio::fs::File::create(&package_path).await.map_err(|err| AppError::from(err.to_string()))?;
            let mut field = field;
            while let Some(chunk) = field.chunk().await.map_err(|err| AppError::from(err.to_string()))? {
                upload.write_all(&chunk).await.map_err(|err| AppError::from(err.to_string()))?;
            }
            upload.flush().await.map_err(|err| AppError::from(err.to_string()))?;
            drop(upload);

            let plan = inspect_offline_package(&package_path).map_err(AppError::from)?;
            ensure_no_offline_import_blockers(&state.app, &plan).await.map_err(AppError::from)?;
            let import_result = import_agents_from_package_core(&state.app.agent_manager, &package_path, |event| {
                send_progress_event(&tx, event.with_operation_id(&operation_id))
            })
            .await
            .map_err(AppError::from)?;
            let plugins_root = state.app.plugins.root_dir().to_path_buf();
            // Blocking on purpose: the embedded plugin install scans and waits
            // for processes holding the JDBC plugin directory (#12004); the
            // blocking pool absorbs it.
            let package_path = package_path.clone();
            let import = tokio::task::spawn_blocking(move || {
                dbx_core::jdbc::import_offline_jdbc_payload(&plugins_root, &package_path)
            })
            .await
            .map_err(|err| AppError::internal(err.to_string()))?;
            import.map_err(AppError::from)?;
            Ok::<_, AppError>(import_result)
        }
        .await;
        let _ = std::fs::remove_file(&package_path);

        let result = result?;
        send_progress_event(&tx, AgentProgressEvent::step("done").with_operation_id(&operation_id));
        return Ok(Json(serde_json::json!({
            "count": result.drivers_installed.len() as u32,
            "jreCount": result.jre_installed.len(),
            "failures": result.failures,
        })));
    }

    Err(AppError::from("No file uploaded".to_string()))
}

pub async fn import_agent_driver_file(
    State(state): State<Arc<WebState>>,
    mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, AppError> {
    let mut db_type: Option<String> = None;
    let mut driver_data: Option<Vec<u8>> = None;
    let mut driver_name = String::new();

    while let Ok(Some(field)) = multipart.next_field().await {
        let name = field.name().unwrap_or("").to_string();
        if name == "dbType" {
            db_type = Some(field.text().await.map_err(|e| AppError::from(e.to_string()))?);
        } else if name == "file" {
            driver_name = field.file_name().unwrap_or("agent").to_string();
            driver_data = Some(field.bytes().await.map_err(|e| AppError::from(e.to_string()))?.to_vec());
        }
    }

    let db_type = db_type.ok_or_else(|| AppError::from("Missing dbType field".to_string()))?;
    let data = driver_data.ok_or_else(|| AppError::from("No file uploaded".to_string()))?;

    let temp_dir = state.app.plugins.root_dir().join("agent_upload_tmp");
    std::fs::create_dir_all(&temp_dir).map_err(|e| AppError::from(e.to_string()))?;
    let suffix = std::path::Path::new(&driver_name)
        .extension()
        .and_then(|extension| extension.to_str())
        .map(|extension| format!(".{extension}"))
        .unwrap_or_default();
    let tmp_path = temp_dir.join(format!("agent-{}{}", uuid::Uuid::new_v4(), suffix));
    std::fs::write(&tmp_path, &data).map_err(|e| AppError::from(e.to_string()))?;

    let result = async {
        ensure_no_agent_update_blockers(&state.app, std::slice::from_ref(&db_type)).await.map_err(AppError::from)?;
        import_agent_driver(&state.app.agent_manager, &db_type, &tmp_path).await.map_err(AppError::from)
    }
    .await;
    let _ = std::fs::remove_file(&tmp_path);
    result?;
    Ok(Json(serde_json::json!({ "success": true })))
}

pub async fn reinstall_jre(
    State(state): State<Arc<WebState>>,
    Json(req): Json<JreRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let operation_id = req.operation_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
    let tx = progress_sender(&state, "global").await;
    reinstall_agent_jre(&state.app.agent_manager, req.jre_key.as_deref().unwrap_or(DEFAULT_JRE_KEY), |event| {
        send_progress_event(&tx, event.with_operation_id(&operation_id));
    })
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn uninstall_jre(
    State(state): State<Arc<WebState>>,
    Json(req): Json<JreRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let key = req.jre_key.as_deref().unwrap_or(DEFAULT_JRE_KEY);
    uninstall_agent_jre(&state.app.agent_manager, key).await.map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn agent_progress(
    State(state): State<Arc<WebState>>,
    Path(operation_id): Path<String>,
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, AppError> {
    let tx = progress_sender(&state, &operation_id).await;
    Ok(crate::sse::sse_from_channel(tx.subscribe()))
}

async fn progress_sender(state: &WebState, operation_id: &str) -> broadcast::Sender<String> {
    let mut channels = state.sse_channels.write().await;
    channels
        .entry(format!("agent-install-progress:{operation_id}"))
        .or_insert_with(|| {
            let (tx, _) = broadcast::channel::<String>(256);
            tx
        })
        .clone()
}

fn send_progress_event(tx: &broadcast::Sender<String>, event: AgentProgressEvent) {
    if let Ok(payload) = serde_json::to_string(&event) {
        let _ = tx.send(payload);
    }
}

async fn ensure_no_agent_update_blockers(
    state: &dbx_core::connection::AppState,
    db_types: &[String],
) -> Result<(), String> {
    let blockers = update_blockers_from_connections(state.prepare_agent_driver_updates(db_types).await, db_types);
    if blockers.is_empty() {
        return Ok(());
    }
    let labels = blockers
        .into_iter()
        .flat_map(|blocker| if blocker.connections.is_empty() { vec![blocker.label] } else { blocker.connections })
        .collect::<Vec<_>>()
        .join(", ");
    Err(format!("Close these database connections before updating drivers: {labels}"))
}

async fn agent_update_blockers(state: &dbx_core::connection::AppState, db_types: &[String]) -> Vec<AgentUpdateBlocker> {
    update_blockers_from_connections(state.active_agent_connection_driver_connections().await, db_types)
}

fn update_blockers_from_connections(
    active_connections: std::collections::HashMap<String, Vec<String>>,
    db_types: &[String],
) -> Vec<AgentUpdateBlocker> {
    let candidate_keys: std::collections::HashSet<&str> = db_types.iter().map(String::as_str).collect();
    if candidate_keys.is_empty() {
        return Vec::new();
    }
    let mut blockers = active_connections
        .into_iter()
        .filter(|(key, _)| candidate_keys.contains(key.as_str()))
        .map(|(db_type, connections)| AgentUpdateBlocker {
            label: dbx_core::agent_catalog::label_for_key(&db_type).unwrap_or(&db_type).to_string(),
            db_type,
            connections,
        })
        .collect::<Vec<_>>();
    blockers.sort_by(|left, right| left.label.cmp(&right.label));
    blockers
}

fn offline_package_extension(file_name: &str) -> Option<&'static str> {
    let lower_name = file_name.to_ascii_lowercase();
    if lower_name.ends_with(".tar.zst") {
        Some("tar.zst")
    } else if lower_name.ends_with(".zip") {
        Some("zip")
    } else {
        None
    }
}

async fn ensure_no_offline_import_blockers(
    state: &dbx_core::connection::AppState,
    plan: &OfflineImportPlan,
) -> Result<(), String> {
    let mut driver_keys = plan.driver_keys.clone();
    if plan.includes_jre {
        // Replacing a managed JRE affects every running Java Agent, so include
        // all active runtimes in the same connection-aware update preflight.
        driver_keys.extend(state.agent_manager.active_daemon_keys().await);
        driver_keys.extend(state.active_agent_connection_driver_keys().await);
        driver_keys.sort();
        driver_keys.dedup();
    }
    ensure_no_agent_update_blockers(state, &driver_keys).await
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn update_blockers_include_active_duckdb_connections() {
        let active_connections = std::collections::HashMap::from([
            ("duckdb".to_string(), vec!["本地分析".to_string()]),
            ("oracle".to_string(), vec!["生产 Oracle".to_string()]),
        ]);
        let blockers = update_blockers_from_connections(active_connections, &["duckdb".to_string()]);

        assert_eq!(blockers.len(), 1);
        assert_eq!(blockers[0].db_type, "duckdb");
        assert_eq!(blockers[0].label, "DuckDB");
        assert_eq!(blockers[0].connections, vec!["本地分析"]);
    }

    #[test]
    fn offline_package_extension_accepts_zip_and_tar_zstd() {
        assert_eq!(offline_package_extension("dbx-agents.zip"), Some("zip"));
        assert_eq!(offline_package_extension("dbx-agent-duckdb.TAR.ZST"), Some("tar.zst"));
        assert_eq!(offline_package_extension("dbx-agent-duckdb.zst"), None);
    }
}