feat(data-grid): add multi-format copy extractors
This commit is contained in:
1 parent
166027600f
commit
0b7a886cfd
60 files changed
+4615
-1766
No files matched your search
@@ -21,8 +21,40 @@ use state::WebState;
|
||||
use tokio::sync::RwLock;
|
||||
use tower_http::compression::predicate::{DefaultPredicate, NotForContentType, Predicate};
|
||||
use tower_http::compression::CompressionLayer;
|
||||
use utoipa::OpenApi;
|
||||
|
||||
const XLSX_CONTENT_TYPE: &str = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
const DATA_GRID_EXTRACTOR_BODY_LIMIT_BYTES: usize = 96 * 1024 * 1024;
|
||||
|
||||
#[derive(OpenApi)]
|
||||
#[openapi(
|
||||
info(title = "DBX Data Grid Extractor API", description = "HTTP contract for data-grid clipboard extraction."),
|
||||
paths(routes::query::extract_data_grid_selection),
|
||||
tags((name = "data-grid", description = "Data grid extraction and clipboard formats"))
|
||||
)]
|
||||
struct ApiDoc;
|
||||
|
||||
async fn openapi_json() -> axum::Json<utoipa::openapi::OpenApi> {
|
||||
axum::Json(ApiDoc::openapi())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod data_grid_extractor_openapi_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn extractor_openapi_contains_the_versioned_request_and_error_responses() {
|
||||
let document = serde_json::to_value(ApiDoc::openapi()).expect("serialize extractor OpenAPI document");
|
||||
let operation = &document["paths"]["/api/query/extract-data-grid-selection"]["post"];
|
||||
|
||||
assert_eq!(operation["requestBody"]["required"], true);
|
||||
assert!(operation["responses"].get("200").is_some());
|
||||
assert!(operation["responses"].get("400").is_some());
|
||||
assert!(operation["responses"].get("413").is_some());
|
||||
assert!(operation["responses"].get("422").is_some());
|
||||
assert!(operation["responses"].get("500").is_some());
|
||||
}
|
||||
}
|
||||
|
||||
fn web_compression_predicate() -> impl Predicate {
|
||||
// XLSX exports are already compressed ZIP archives, so gzip would only add CPU overhead.
|
||||
@@ -377,6 +409,12 @@ async fn main() {
|
||||
.route("/query/build-single-column-alter-sql", post(routes::query::build_single_column_alter_sql))
|
||||
.route("/query/analyze-editability", post(routes::query::analyze_editable_query_editability))
|
||||
.route("/query/prepare-data-grid-save", post(routes::query::prepare_data_grid_save))
|
||||
.route("/query/data-grid-extractor-openapi.json", get(openapi_json))
|
||||
.route(
|
||||
"/query/extract-data-grid-selection",
|
||||
post(routes::query::extract_data_grid_selection)
|
||||
.layer(DefaultBodyLimit::max(DATA_GRID_EXTRACTOR_BODY_LIMIT_BYTES)),
|
||||
)
|
||||
.route(
|
||||
"/query/build-data-grid-copy-update-statements",
|
||||
post(routes::query::build_data_grid_copy_update_statements),
|
||||
|
||||
@@ -1540,8 +1540,8 @@ mod tests {
|
||||
password_hash: RwLock::new(None),
|
||||
sessions: RwLock::new(HashSet::new()),
|
||||
sse_channels: RwLock::new(HashMap::new()),
|
||||
sql_file_executions: RwLock::new(HashMap::new()),
|
||||
table_import_channels: RwLock::new(HashMap::new()),
|
||||
sql_file_executions: RwLock::new(HashMap::new()),
|
||||
nacos_imports: RwLock::new(HashMap::new()),
|
||||
login_rate_limit: Mutex::new(LoginRateLimit { fail_count: 0, locked_until: None }),
|
||||
export_files: RwLock::new(HashMap::new()),
|
||||
|
||||
@@ -250,6 +250,12 @@ pub struct PrepareDataGridSaveRequest {
|
||||
pub options: dbx_core::data_grid_sql::DataGridSaveStatementOptions,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, utoipa::ToSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ExtractDataGridSelectionRequest {
|
||||
pub request: dbx_core::data_grid_extractors::DataGridExtractRequest,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BuildDataGridCopyUpdateStatementsRequest {
|
||||
@@ -757,6 +763,38 @@ pub async fn prepare_data_grid_save(
|
||||
Json(dbx_core::data_grid_sql::prepare_data_grid_save(req.options))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/query/extract-data-grid-selection",
|
||||
request_body = ExtractDataGridSelectionRequest,
|
||||
responses(
|
||||
(status = 200, description = "Selection extracted successfully", body = dbx_core::data_grid_extractors::DataGridExtractResult),
|
||||
(status = 400, description = "Invalid selection or extractor configuration", body = dbx_core::data_grid_extractors::DataGridExtractError),
|
||||
(status = 413, description = "Request body exceeds the extractor upload limit"),
|
||||
(status = 422, description = "Request body does not match the extractor contract"),
|
||||
(status = 500, description = "Extractor worker failed", body = dbx_core::data_grid_extractors::DataGridExtractError)
|
||||
),
|
||||
tag = "data-grid"
|
||||
)]
|
||||
pub async fn extract_data_grid_selection(
|
||||
Json(req): Json<ExtractDataGridSelectionRequest>,
|
||||
) -> Result<
|
||||
Json<dbx_core::data_grid_extractors::DataGridExtractResult>,
|
||||
(axum::http::StatusCode, Json<dbx_core::data_grid_extractors::DataGridExtractError>),
|
||||
> {
|
||||
tokio::task::spawn_blocking(move || dbx_core::data_grid_extractors::extract_data_grid_selection(req.request))
|
||||
.await
|
||||
.map_err(|error| {
|
||||
let error = dbx_core::data_grid_extractors::DataGridExtractError::new(
|
||||
dbx_core::data_grid_extractors::DataGridExtractErrorCode::ExecutionFailed,
|
||||
format!("Data grid extractor worker failed: {error}"),
|
||||
);
|
||||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, Json(error))
|
||||
})?
|
||||
.map(Json)
|
||||
.map_err(|error| (axum::http::StatusCode::BAD_REQUEST, Json(error)))
|
||||
}
|
||||
|
||||
pub async fn build_data_grid_copy_update_statements(
|
||||
Json(req): Json<BuildDataGridCopyUpdateStatementsRequest>,
|
||||
) -> Json<Vec<String>> {
|
||||
|
||||
Reference in new issue
Block a user