From 0fe248af7245dd4ce7f9fe76d15ee767b30f3a0f Mon Sep 17 00:00:00 2001 From: dal Date: Thu, 17 Apr 2025 11:30:46 -0600 Subject: [PATCH 1/2] list on logs ordered and now have cached file info --- .../handlers/src/chats/list_chats_handler.rs | 4 ++-- .../handlers/src/chats/post_chat_handler.rs | 2 +- .../handlers/src/logs/list_logs_handler.rs | 22 ++++++++++++++----- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/api/libs/handlers/src/chats/list_chats_handler.rs b/api/libs/handlers/src/chats/list_chats_handler.rs index 26b9756e2..afbafd8f1 100644 --- a/api/libs/handlers/src/chats/list_chats_handler.rs +++ b/api/libs/handlers/src/chats/list_chats_handler.rs @@ -115,9 +115,9 @@ pub async fn list_chats_handler( ); } - // Order by creation date descending and apply pagination + // Order by updated date descending and apply pagination query = query - .order_by(chats::created_at.desc()) + .order_by(chats::updated_at.desc()) .offset(offset as i64) .limit((request.page_size + 1) as i64); diff --git a/api/libs/handlers/src/chats/post_chat_handler.rs b/api/libs/handlers/src/chats/post_chat_handler.rs index 58d118286..09e3d05dd 100644 --- a/api/libs/handlers/src/chats/post_chat_handler.rs +++ b/api/libs/handlers/src/chats/post_chat_handler.rs @@ -2879,7 +2879,7 @@ fn generate_file_response_values(filtered_files: &[CompletedFileInfo]) -> Vec, pub last_edited: String, + pub most_recent_file_id: Option, + pub most_recent_file_type: Option, + pub most_recent_version_number: Option, } #[derive(Debug, Serialize, Deserialize)] @@ -47,6 +50,9 @@ struct ChatWithUser { pub created_at: DateTime, pub updated_at: DateTime, pub created_by: Uuid, + pub most_recent_file_id: Option, + pub most_recent_file_type: Option, + pub most_recent_version_number: Option, // User fields pub user_name: Option, pub user_attributes: Value, @@ -62,7 +68,7 @@ struct ChatWithUser { pub async fn list_logs_handler( request: ListLogsRequest, organization_id: Uuid, -) -> Result, anyhow::Error> { +) -> Result { use database::schema::{chats, users}; let mut conn = get_pg_pool().get().await?; @@ -78,9 +84,9 @@ pub async fn list_logs_handler( let page = request.page.unwrap_or(1); let offset = (page - 1) * request.page_size; - // Order by creation date descending and apply pagination + // Order by updated date descending and apply pagination query = query - .order_by(chats::created_at.desc()) + .order_by(chats::updated_at.desc()) .offset(offset as i64) .limit((request.page_size + 1) as i64); @@ -92,6 +98,9 @@ pub async fn list_logs_handler( chats::created_at, chats::updated_at, chats::created_by, + chats::most_recent_file_id, + chats::most_recent_file_type, + chats::most_recent_version_number, users::name.nullable(), users::attributes, )) @@ -120,16 +129,19 @@ pub async fn list_logs_handler( created_by_name: chat.user_name.unwrap_or_else(|| "Unknown".to_string()), created_by_avatar, last_edited: chat.updated_at.to_rfc3339(), + most_recent_file_id: chat.most_recent_file_id.map(|id| id.to_string()), + most_recent_file_type: chat.most_recent_file_type, + most_recent_version_number: chat.most_recent_version_number, } }) .collect(); // Create pagination info - let _pagination = PaginationInfo { + let pagination = PaginationInfo { has_more, next_page: if has_more { Some(page + 1) } else { None }, total_items: items.len() as i32, }; - Ok(items) + Ok(ListLogsResponse { items, pagination }) } From df1eda84d38e919274eb127c244d2a5990c0b64b Mon Sep 17 00:00:00 2001 From: dal Date: Thu, 17 Apr 2025 11:38:21 -0600 Subject: [PATCH 2/2] list logs fix move dataset security --- api/.cursor/rules/libs.mdc | 4 ++-- api/Cargo.toml | 1 + api/documentation/libs.mdc | 4 ++-- api/libs/dataset_security/Cargo.toml | 20 +++++++++++++++++++ .../dataset_security/src/lib.rs} | 13 +++++++++--- api/server/Cargo.toml | 8 +++++++- .../src/routes/rest/routes/logs/list_logs.rs | 2 +- .../src/routes/rest/routes/sql/run_sql.rs | 5 +++-- 8 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 api/libs/dataset_security/Cargo.toml rename api/{server/src/utils/security/dataset_security.rs => libs/dataset_security/src/lib.rs} (92%) diff --git a/api/.cursor/rules/libs.mdc b/api/.cursor/rules/libs.mdc index 1947e0147..7b46723f9 100644 --- a/api/.cursor/rules/libs.mdc +++ b/api/.cursor/rules/libs.mdc @@ -72,7 +72,7 @@ Example `lib.rs`: //! This library provides... // Re-export common workspace types if needed -pub use common_types::{Result, Error}; +pub use anyhow::{Result, Error}; pub mod models; pub mod utils; @@ -91,7 +91,7 @@ pub use models::{ImportantType, AnotherType}; Example `errors.rs`: ```rust use thiserror::Error; -use common_types::Error as WorkspaceError; +use anyhow::Error as WorkspaceError; #[derive(Error, Debug)] pub enum Error { diff --git a/api/Cargo.toml b/api/Cargo.toml index 199fc0aff..2c1570d9e 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -9,6 +9,7 @@ members = [ "libs/sharing", "libs/sql_analyzer", "libs/search", + "libs/dataset_security", ] resolver = "2" diff --git a/api/documentation/libs.mdc b/api/documentation/libs.mdc index 1947e0147..7b46723f9 100644 --- a/api/documentation/libs.mdc +++ b/api/documentation/libs.mdc @@ -72,7 +72,7 @@ Example `lib.rs`: //! This library provides... // Re-export common workspace types if needed -pub use common_types::{Result, Error}; +pub use anyhow::{Result, Error}; pub mod models; pub mod utils; @@ -91,7 +91,7 @@ pub use models::{ImportantType, AnotherType}; Example `errors.rs`: ```rust use thiserror::Error; -use common_types::Error as WorkspaceError; +use anyhow::Error as WorkspaceError; #[derive(Error, Debug)] pub enum Error { diff --git a/api/libs/dataset_security/Cargo.toml b/api/libs/dataset_security/Cargo.toml new file mode 100644 index 000000000..553ff4042 --- /dev/null +++ b/api/libs/dataset_security/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "dataset_security" +version = "0.1.0" +edition = "2021" + +[dependencies] +# Workspace dependencies +anyhow = { workspace = true } +diesel = { workspace = true } +diesel-async = { workspace = true } +uuid = { workspace = true } +tracing = { workspace = true } + +# Internal workspace dependencies +database = { path = "../database" } + +# Development dependencies +[dev-dependencies] +tokio = { workspace = true } +# Add other workspace dev dependencies as needed \ No newline at end of file diff --git a/api/server/src/utils/security/dataset_security.rs b/api/libs/dataset_security/src/lib.rs similarity index 92% rename from api/server/src/utils/security/dataset_security.rs rename to api/libs/dataset_security/src/lib.rs index 4df5abf40..273849901 100644 --- a/api/server/src/utils/security/dataset_security.rs +++ b/api/libs/dataset_security/src/lib.rs @@ -1,3 +1,5 @@ +//! Library for handling dataset security and permissions. + use anyhow::{anyhow, Result}; use diesel::{BoolExpressionMethods, ExpressionMethods, JoinOnDsl, QueryDsl}; use diesel_async::RunQueryDsl; @@ -7,8 +9,11 @@ use database::{ pool::{get_pg_pool, PgPool}, models::Dataset, schema::{ - datasets, datasets_to_permission_groups, permission_groups, - permission_groups_to_identities, teams_to_users, + datasets, + datasets_to_permission_groups, + permission_groups, + permission_groups_to_identities, + teams_to_users, }, }; @@ -23,6 +28,8 @@ pub async fn get_permissioned_datasets( Err(e) => return Err(anyhow!("Unable to get connection from pool: {}", e)), }; + // TODO: Add logic to check if user is admin, if so, return all datasets + let datasets = match datasets::table .select(datasets::all_columns) .inner_join( @@ -106,4 +113,4 @@ pub async fn has_dataset_access(user_id: &Uuid, dataset_id: &Uuid) -> Result Ok(ApiResponse::JsonData(response)), + Ok(response) => Ok(ApiResponse::JsonData(response.items)), Err(e) => { tracing::error!("Error listing logs: {}", e); Err((StatusCode::INTERNAL_SERVER_ERROR, "Failed to list logs")) diff --git a/api/server/src/routes/rest/routes/sql/run_sql.rs b/api/server/src/routes/rest/routes/sql/run_sql.rs index 97b44d6ae..da5977185 100644 --- a/api/server/src/routes/rest/routes/sql/run_sql.rs +++ b/api/server/src/routes/rest/routes/sql/run_sql.rs @@ -4,7 +4,6 @@ use diesel::{BoolExpressionMethods, ExpressionMethods, JoinOnDsl, QueryDsl}; use indexmap::IndexMap; use query_engine::data_source_query_routes::query_engine::query_engine; use query_engine::data_types::DataType; -use rayon::iter::ParallelIterator; use reqwest::StatusCode; use uuid::Uuid; @@ -18,9 +17,11 @@ use database::{ types::DataMetadata, }; -use crate::{routes::rest::ApiResponse, utils::dataset_security::has_dataset_access}; +use dataset_security::has_dataset_access; use middleware::AuthenticatedUser; +use crate::routes::rest::ApiResponse; + const MAX_UNIQUE_VALUES: usize = 100; #[derive(Serialize, Deserialize, Debug, Clone)]