Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add items count column to datasets list #276

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app-server/src/db/datasets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,40 @@ pub async fn get_dataset_by_name(

Ok(dataset)
}

pub async fn get_datasets_with_counts(
pool: &PgPool,
project_id: Uuid,
limit: i64,
offset: i64,
) -> Result<(Vec<(Dataset, i64)>, i64)> {
let datasets = sqlx::query_as::<_, (Dataset, i64)>(
"WITH dataset_counts AS (
SELECT d.id, COUNT(dp.id) as count
FROM datasets d
LEFT JOIN datapoints dp ON d.id = dp.dataset_id
WHERE d.project_id = $1
GROUP BY d.id
)
SELECT d.id, d.created_at, d.name, d.project_id, d.indexed_on, COALESCE(dc.count, 0)
FROM datasets d
LEFT JOIN dataset_counts dc ON d.id = dc.id
WHERE d.project_id = $1
ORDER BY d.created_at DESC
LIMIT $2 OFFSET $3",
)
.bind(project_id)
.bind(limit)
.bind(offset)
.fetch_all(pool)
.await?;

let total_count = sqlx::query_scalar::<_, i64>(
"SELECT COUNT(*) FROM datasets WHERE project_id = $1",
)
.bind(project_id)
.fetch_one(pool)
.await?;

Ok((datasets, total_count))
}
41 changes: 39 additions & 2 deletions app-server/src/routes/datasets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ async fn get_datapoints(
query_params: web::Query<PaginatedGetQueryParams>,
) -> ResponseResult {
let (_project_id, dataset_id) = path.into_inner();
let limit = query_params.page_size.unwrap_or(DEFAULT_PAGE_SIZE) as i64;
let offset = limit * (query_params.page_number) as i64;
let limit = query_params.page_size.unwrap_or(DEFAULT_PAGE_SIZE as i64);
let offset = limit * query_params.page_number as i64;
let datapoints = db::datapoints::get_datapoints(&db.pool, dataset_id, limit, offset).await?;
let total_entries = db::datapoints::count_datapoints(&db.pool, dataset_id).await?;

Expand All @@ -244,6 +244,43 @@ async fn get_datapoints(
Ok(HttpResponse::Ok().json(response))
}

#[get("projects/{project_id}/datasets")]
async fn get_datasets(
db: web::Data<DB>,
path: web::Path<Uuid>,
query_params: web::Query<PaginatedGetQueryParams>,
) -> ResponseResult {
let project_id = path.into_inner();
let limit = query_params.page_size.unwrap_or(DEFAULT_PAGE_SIZE as i64);
let offset = limit * query_params.page_number as i64;

let (datasets_with_counts, total_count) = db::datasets::get_datasets_with_counts(
&db.pool,
project_id,
limit,
offset,
).await?;

let datasets = datasets_with_counts
.into_iter()
.map(|(dataset, count)| serde_json::json!({
"id": dataset.id,
"createdAt": dataset.created_at,
"name": dataset.name,
"indexedOn": dataset.indexed_on,
"itemsCount": count
}))
.collect::<Vec<_>>();

let response = PaginatedResponse {
items: datasets,
total_count,
any_in_project: true,
};

Ok(HttpResponse::Ok().json(response))
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct IndexDatasetRequest {
Expand Down
5 changes: 5 additions & 0 deletions frontend/components/datasets/datasets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export default function Datasets() {
header: 'name',
size: 300
},
{
header: 'Items',
accessorKey: 'itemsCount',
size: 100,
},
{
header: 'Created at',
accessorKey: 'createdAt',
Expand Down
1 change: 1 addition & 0 deletions frontend/lib/dataset/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface Dataset {
createdAt?: string;
name: string;
indexedOn: string | null;
itemsCount: number;
}

export interface Datapoint {
Expand Down