fix list logic

This commit is contained in:
dal 2025-07-17 17:04:30 -06:00
parent cc2dd7fa61
commit 525a2c5e23
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
4 changed files with 55 additions and 39 deletions

View File

@ -128,11 +128,9 @@ pub async fn list_chats_handler(
);
}
// Order by updated date descending and apply pagination
// Order by updated date descending (no pagination yet)
query = query
.order_by(chats::updated_at.desc())
.offset(offset as i64)
.limit((request.page_size + 1) as i64);
.order_by(chats::updated_at.desc());
// Execute query and select required fields
let results: Vec<ChatWithUser> = query
@ -200,24 +198,19 @@ pub async fn list_chats_handler(
users::avatar_url.nullable(),
))
.order_by(chats::updated_at.desc())
.offset(offset as i64)
.limit((request.page_size + 1) as i64)
.load::<ChatWithUser>(&mut conn)
.await?
} else {
vec![]
};
// Check if there are more results and prepare pagination info
let has_more = results.len() > request.page_size as usize || workspace_shared_chats.len() > request.page_size as usize;
// Process all chats first
let mut all_items: Vec<ChatListItem> = Vec::new();
// Process directly-accessed chats
let mut items: Vec<ChatListItem> = results
.into_iter()
.filter(|chat| !chat.title.trim().is_empty()) // Filter out titles with only whitespace
.take(request.page_size as usize)
.map(|chat| {
ChatListItem {
for chat in results {
if !chat.title.trim().is_empty() {
all_items.push(ChatListItem {
id: chat.id.to_string(),
name: chat.title,
is_favorited: false, // TODO: Implement favorites feature
@ -232,15 +225,14 @@ pub async fn list_chats_handler(
latest_file_type: chat.most_recent_file_type,
latest_version_number: chat.most_recent_version_number,
is_shared: chat.created_by != user.id, // Mark as shared if the user is not the creator
}
})
.collect();
});
}
}
// Add workspace-shared chats (if we have room in the page)
let remaining_slots = request.page_size as usize - items.len();
for chat in workspace_shared_chats.into_iter().take(remaining_slots) {
// Add all workspace-shared chats
for chat in workspace_shared_chats {
if !chat.title.trim().is_empty() {
items.push(ChatListItem {
all_items.push(ChatListItem {
id: chat.id.to_string(),
name: chat.title,
is_favorited: false,
@ -259,12 +251,28 @@ pub async fn list_chats_handler(
}
}
// Sort all items by updated_at descending
all_items.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
// Apply pagination
let total_items = all_items.len();
let start_index = offset as usize;
let end_index = (start_index + request.page_size as usize).min(total_items);
let paginated_items: Vec<ChatListItem> = all_items.into_iter()
.skip(start_index)
.take(request.page_size as usize)
.collect();
// Check if there are more results
let has_more = end_index < total_items;
// Create pagination info
let _pagination = PaginationInfo {
has_more,
next_page: if has_more { Some(page + 1) } else { None },
total_items: items.len() as i32,
total_items: paginated_items.len() as i32,
};
Ok(items)
Ok(paginated_items)
}

View File

@ -93,8 +93,6 @@ async fn get_permissioned_collections(
)
.distinct()
.order((collections::updated_at.desc(), collections::id.asc()))
.offset(page * page_size)
.limit(page_size)
.into_boxed();
if let Some(filters) = req.filters {
@ -176,8 +174,6 @@ async fn get_permissioned_collections(
collections::organization_id,
))
.order((collections::updated_at.desc(), collections::id.asc()))
.offset(page * page_size)
.limit(page_size)
.load::<(
Uuid,
String,
@ -283,5 +279,14 @@ async fn get_permissioned_collections(
collections.push(collection);
}
Ok(collections)
// Sort all collections by updated_at descending
collections.sort_by(|a, b| b.last_edited.cmp(&a.last_edited));
// Apply pagination
let paginated_collections: Vec<ListCollectionsCollection> = collections.into_iter()
.skip((page * page_size) as usize)
.take(page_size as usize)
.collect();
Ok(paginated_collections)
}

View File

@ -254,14 +254,12 @@ pub async fn list_dashboard_handler(
}
}
// Sort dashboards by updated_at desc
// Sort dashboards by updated_at desc (already sorted by queries, but need to re-sort after combining)
dashboards.sort_by(|a, b| b.last_edited.cmp(&a.last_edited));
// Apply pagination after combining results
let start = request.page_token as usize * request.page_size as usize;
let end = start + request.page_size as usize;
let paginated_dashboards = dashboards.into_iter()
.skip(start)
.skip(offset as usize)
.take(request.page_size as usize)
.collect();

View File

@ -75,8 +75,6 @@ pub async fn list_metrics_handler(
))
.filter(metric_files::deleted_at.is_null())
.order((metric_files::updated_at.desc(), metric_files::id.asc()))
.offset(offset)
.limit(request.page_size)
.into_boxed();
// Add filters based on request parameters
@ -168,8 +166,6 @@ pub async fn list_metrics_handler(
users::avatar_url.nullable(),
))
.order((metric_files::updated_at.desc(), metric_files::id.asc()))
.offset(offset)
.limit(request.page_size)
.into_boxed();
// Apply filters to workspace-shared query
@ -201,7 +197,7 @@ pub async fn list_metrics_handler(
};
// Transform query results into BusterMetricListItem
let mut metrics: Vec<BusterMetricListItem> = metric_results
let mut all_metrics: Vec<BusterMetricListItem> = metric_results
.into_iter()
.map(
|(
@ -225,7 +221,7 @@ pub async fn list_metrics_handler(
// Add workspace-shared metrics
for (id, name, created_by, _created_at, updated_at, status, _workspace_sharing, created_by_name, created_by_email, created_by_avatar) in workspace_shared_results {
metrics.push(BusterMetricListItem {
all_metrics.push(BusterMetricListItem {
id,
name,
last_edited: updated_at,
@ -238,5 +234,14 @@ pub async fn list_metrics_handler(
});
}
Ok(metrics)
// Sort all metrics by last_edited descending
all_metrics.sort_by(|a, b| b.last_edited.cmp(&a.last_edited));
// Apply pagination
let paginated_metrics: Vec<BusterMetricListItem> = all_metrics.into_iter()
.skip(offset as usize)
.take(request.page_size as usize)
.collect();
Ok(paginated_metrics)
}