mirror of https://github.com/buster-so/buster.git
dashboard handler
This commit is contained in:
parent
c40c6ef626
commit
63490d60f0
|
@ -9,14 +9,14 @@ use futures::future::{try_join_all, join_all};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde_yaml;
|
use serde_yaml;
|
||||||
|
|
||||||
use crate::files::dashboard_files::types::{
|
|
||||||
BusterDashboard, BusterDashboardResponse, DashboardConfig, DashboardRow, DashboardRowItem,
|
|
||||||
};
|
|
||||||
use crate::metrics::{get_metric_handler, BusterMetric};
|
use crate::metrics::{get_metric_handler, BusterMetric};
|
||||||
use database::enums::{AssetPermissionRole, Verification};
|
use database::enums::{AssetPermissionRole, Verification};
|
||||||
use database::pool::get_pg_pool;
|
use database::pool::get_pg_pool;
|
||||||
use database::schema::dashboard_files;
|
use database::schema::dashboard_files;
|
||||||
|
|
||||||
|
use super::{BusterDashboard, BusterDashboardResponse, DashboardConfig, DashboardRow, DashboardRowItem};
|
||||||
|
|
||||||
#[derive(Queryable, Selectable)]
|
#[derive(Queryable, Selectable)]
|
||||||
#[diesel(table_name = dashboard_files)]
|
#[diesel(table_name = dashboard_files)]
|
||||||
struct QueryableDashboardFile {
|
struct QueryableDashboardFile {
|
|
@ -0,0 +1,112 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use diesel::{
|
||||||
|
BoolExpressionMethods, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl,
|
||||||
|
Queryable, Selectable,
|
||||||
|
};
|
||||||
|
use diesel_async::RunQueryDsl;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
|
||||||
|
use database::{
|
||||||
|
enums::{AssetPermissionRole, AssetType, IdentityType, Verification},
|
||||||
|
pool::get_pg_pool,
|
||||||
|
schema::{asset_permissions, dashboard_files, users},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{BusterDashboardListItem, DashboardMember};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct DashboardsListRequest {
|
||||||
|
/// The page number to fetch
|
||||||
|
pub page_token: i64,
|
||||||
|
/// Number of items per page
|
||||||
|
pub page_size: i64,
|
||||||
|
/// Filter for dashboards shared with the current user
|
||||||
|
pub shared_with_me: Option<bool>,
|
||||||
|
/// Filter for dashboards owned by the current user
|
||||||
|
pub only_my_dashboards: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Selectable)]
|
||||||
|
#[diesel(table_name = dashboard_files)]
|
||||||
|
struct QueryableDashboardFile {
|
||||||
|
id: Uuid,
|
||||||
|
name: String,
|
||||||
|
created_by: Uuid,
|
||||||
|
created_at: DateTime<Utc>,
|
||||||
|
updated_at: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_dashboard_handler(
|
||||||
|
user_id: &Uuid,
|
||||||
|
request: DashboardsListRequest,
|
||||||
|
) -> Result<Vec<BusterDashboardListItem>> {
|
||||||
|
let mut conn = match get_pg_pool().get().await {
|
||||||
|
Ok(conn) => conn,
|
||||||
|
Err(e) => return Err(anyhow!("Failed to get database connection: {}", e)),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Calculate offset from page_token
|
||||||
|
let offset = request.page_token * request.page_size;
|
||||||
|
|
||||||
|
// Build the base query
|
||||||
|
let mut dashboard_statement = dashboard_files::table
|
||||||
|
.select((
|
||||||
|
dashboard_files::id,
|
||||||
|
dashboard_files::name,
|
||||||
|
dashboard_files::created_by,
|
||||||
|
dashboard_files::created_at,
|
||||||
|
dashboard_files::updated_at,
|
||||||
|
))
|
||||||
|
.filter(dashboard_files::deleted_at.is_null())
|
||||||
|
.distinct()
|
||||||
|
.order((dashboard_files::updated_at.desc(), dashboard_files::id.asc()))
|
||||||
|
.offset(offset)
|
||||||
|
.limit(request.page_size)
|
||||||
|
.into_boxed();
|
||||||
|
|
||||||
|
// Execute the query
|
||||||
|
let dashboard_results = match dashboard_statement
|
||||||
|
.load::<(
|
||||||
|
Uuid,
|
||||||
|
String,
|
||||||
|
Uuid,
|
||||||
|
DateTime<Utc>,
|
||||||
|
DateTime<Utc>,
|
||||||
|
)>(&mut conn)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(results) => results,
|
||||||
|
Err(e) => return Err(anyhow!("Error getting dashboard results: {}", e)),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Transform query results into BusterDashboardListItem
|
||||||
|
let dashboards = dashboard_results
|
||||||
|
.into_iter()
|
||||||
|
.map(
|
||||||
|
|(id, name, created_by, created_at, updated_at)| {
|
||||||
|
let owner = DashboardMember {
|
||||||
|
id: created_by,
|
||||||
|
name: "Unknown".to_string(),
|
||||||
|
avatar_url: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
BusterDashboardListItem {
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
created_at,
|
||||||
|
last_edited: updated_at,
|
||||||
|
owner,
|
||||||
|
members: vec![],
|
||||||
|
status: Verification::Verified, // Default status, can be updated if needed
|
||||||
|
is_shared: false,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(dashboards)
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
mod get_dashboard_handler;
|
||||||
|
mod list_dashboard_handler;
|
||||||
|
mod types;
|
||||||
|
|
||||||
|
pub use get_dashboard_handler::*;
|
||||||
|
pub use list_dashboard_handler::*;
|
||||||
|
pub use types::*;
|
|
@ -9,9 +9,9 @@ use crate::metrics::types::BusterMetric;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct BusterDashboardListItem {
|
pub struct BusterDashboardListItem {
|
||||||
pub created_at: String,
|
pub created_at: DateTime<Utc>,
|
||||||
pub id: String,
|
pub id: Uuid,
|
||||||
pub last_edited: String,
|
pub last_edited: DateTime<Utc>,
|
||||||
pub members: Vec<DashboardMember>,
|
pub members: Vec<DashboardMember>,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub owner: DashboardMember,
|
pub owner: DashboardMember,
|
||||||
|
@ -22,7 +22,7 @@ pub struct BusterDashboardListItem {
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct DashboardMember {
|
pub struct DashboardMember {
|
||||||
pub avatar_url: Option<String>,
|
pub avatar_url: Option<String>,
|
||||||
pub id: String,
|
pub id: Uuid,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
pub mod get_dashboard_handler;
|
|
||||||
|
|
||||||
pub use get_dashboard_handler::*;
|
|
|
@ -1,5 +0,0 @@
|
||||||
mod types;
|
|
||||||
mod helpers;
|
|
||||||
|
|
||||||
pub use types::*;
|
|
||||||
pub use helpers::*;
|
|
|
@ -1,3 +0,0 @@
|
||||||
pub mod dashboard_files;
|
|
||||||
|
|
||||||
pub use dashboard_files::*;
|
|
|
@ -1,7 +1,7 @@
|
||||||
pub mod chats;
|
pub mod chats;
|
||||||
pub mod collections;
|
pub mod collections;
|
||||||
|
pub mod dashboards;
|
||||||
pub mod favorites;
|
pub mod favorites;
|
||||||
pub mod files;
|
|
||||||
pub mod logs;
|
pub mod logs;
|
||||||
pub mod messages;
|
pub mod messages;
|
||||||
pub mod metrics;
|
pub mod metrics;
|
||||||
|
|
|
@ -2,10 +2,9 @@ use crate::routes::rest::ApiResponse;
|
||||||
use axum::extract::Path;
|
use axum::extract::Path;
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::Extension;
|
use axum::Extension;
|
||||||
use handlers::files::dashboard_files::get_dashboard_handler::get_dashboard_handler;
|
use handlers::dashboards::{get_dashboard_handler, BusterDashboardResponse};
|
||||||
use handlers::files::dashboard_files::BusterDashboardResponse;
|
|
||||||
use uuid::Uuid;
|
|
||||||
use middleware::AuthenticatedUser;
|
use middleware::AuthenticatedUser;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub async fn get_dashboard_rest_handler(
|
pub async fn get_dashboard_rest_handler(
|
||||||
Extension(user): Extension<AuthenticatedUser>,
|
Extension(user): Extension<AuthenticatedUser>,
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
use crate::routes::rest::ApiResponse;
|
||||||
|
use axum::extract::Query;
|
||||||
|
use axum::http::StatusCode;
|
||||||
|
use axum::Extension;
|
||||||
|
use handlers::dashboards::{list_dashboard_handler, DashboardsListRequest, BusterDashboardListItem};
|
||||||
|
use middleware::AuthenticatedUser;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct ListDashboardsQuery {
|
||||||
|
page_token: Option<i64>,
|
||||||
|
page_size: Option<i64>,
|
||||||
|
shared_with_me: Option<bool>,
|
||||||
|
only_my_dashboards: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_dashboard_rest_handler(
|
||||||
|
Extension(user): Extension<AuthenticatedUser>,
|
||||||
|
Query(query): Query<ListDashboardsQuery>,
|
||||||
|
) -> Result<ApiResponse<Vec<BusterDashboardListItem>>, (StatusCode, &'static str)> {
|
||||||
|
let request = DashboardsListRequest {
|
||||||
|
page_token: query.page_token.unwrap_or(0),
|
||||||
|
page_size: query.page_size.unwrap_or(25),
|
||||||
|
shared_with_me: query.shared_with_me,
|
||||||
|
only_my_dashboards: query.only_my_dashboards,
|
||||||
|
};
|
||||||
|
|
||||||
|
let dashboards = match list_dashboard_handler(&user.id, request).await {
|
||||||
|
Ok(dashboards) => dashboards,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Error listing dashboards: {}", e);
|
||||||
|
return Err((StatusCode::INTERNAL_SERVER_ERROR, "Failed to list dashboards"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(ApiResponse::JsonData(dashboards))
|
||||||
|
}
|
|
@ -5,8 +5,10 @@ use axum::{
|
||||||
|
|
||||||
// Placeholder modules that you'll need to create
|
// Placeholder modules that you'll need to create
|
||||||
mod get_dashboard;
|
mod get_dashboard;
|
||||||
|
mod list_dashboards;
|
||||||
|
|
||||||
pub fn router() -> Router {
|
pub fn router() -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/:id", get(get_dashboard::get_dashboard_rest_handler))
|
.route("/:id", get(get_dashboard::get_dashboard_rest_handler))
|
||||||
|
.route("/", get(list_dashboards::list_dashboard_rest_handler))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue