From 656eaebb1451c2f0b16a8c38bb279bbd552ec675 Mon Sep 17 00:00:00 2001 From: Ayush Ojha Date: Mon, 16 Nov 2026 14:18:18 -0800 Subject: [PATCH 1/2] feat: dashboards v2 (insight/text tiles, filters, templates) and public share links - dashboards + dashboard_tiles tables; /api/v2/dashboards CRUD, list with search/tags/pinned, one home dashboard per project, soft delete - tiles: saved-insight or markdown text, grid layout, add/update/delete, bulk layout save, move between dashboards, duplicate (optionally copying insights), at most 100 tiles - dashboard filters (date range incl. relative dates + extra properties) applied to every insight tile; per-tile cache keyed by insight version and filters; without filters tiles share the insight's own cache; a failing tile reports its error without failing the dashboard; person-property tiles respect the identity module - built-in templates (web analytics, product analytics) at /api/v2/dashboard-templates, created via {"template": "..."} - share links can target a dashboard or a saved insight; new public GET /api/shared/{token} (password via x-share-password, constant-time check, lockout after 10 failures); password hashes no longer returned - legacy /api/v1/dashboards (custom dashboards used by the SDK) unchanged Co-Authored-By: Claude Opus 5.5 --- crates/pulse-server/src/main.rs | 38 + crates/pulse-server/src/routes/dashboards.rs | 307 +++++++ .../pulse-server/src/routes/features_ext.rs | 18 + crates/pulse-server/src/routes/mod.rs | 1 + .../pulse-server/src/services/dashboards.rs | 815 ++++++++++++++++++ crates/pulse-server/src/services/mod.rs | 1 + crates/pulse-server/src/services/sharing.rs | 34 +- migrations/032_dashboards_v2.down.sql | 4 + migrations/032_dashboards_v2.up.sql | 40 + 9 files changed, 1248 insertions(+), 10 deletions(-) create mode 100644 crates/pulse-server/src/routes/dashboards.rs create mode 100644 crates/pulse-server/src/services/dashboards.rs create mode 100644 migrations/032_dashboards_v2.down.sql create mode 100644 migrations/032_dashboards_v2.up.sql diff --git a/crates/pulse-server/src/main.rs b/crates/pulse-server/src/main.rs index e7c3bce..0dd9bc1 100644 --- a/crates/pulse-server/src/main.rs +++ b/crates/pulse-server/src/main.rs @@ -231,6 +231,43 @@ async fn main() -> anyhow::Result<()> { "/api/v1/saved-insights/{id}/versions/{version}/restore", axum::routing::post(routes::saved_insights::restore), ) + // Dashboards v2 (v1 paths remain the legacy custom dashboards used by the SDK) + .route( + "/api/v2/dashboards", + get(routes::dashboards::list).post(routes::dashboards::create), + ) + .route( + "/api/v2/dashboard-templates", + get(routes::dashboards::templates), + ) + .route( + "/api/v2/dashboards/{id}", + get(routes::dashboards::get) + .patch(routes::dashboards::update) + .delete(routes::dashboards::delete), + ) + .route("/api/v2/dashboards/{id}/run", get(routes::dashboards::run)) + .route( + "/api/v2/dashboards/{id}/duplicate", + axum::routing::post(routes::dashboards::duplicate), + ) + .route( + "/api/v2/dashboards/{id}/layouts", + axum::routing::put(routes::dashboards::update_layouts), + ) + .route( + "/api/v2/dashboards/{id}/tiles", + axum::routing::post(routes::dashboards::add_tile), + ) + .route( + "/api/v2/dashboards/{id}/tiles/{tile_id}", + axum::routing::patch(routes::dashboards::update_tile) + .delete(routes::dashboards::delete_tile), + ) + .route( + "/api/v2/dashboards/{id}/tiles/{tile_id}/move", + axum::routing::post(routes::dashboards::move_tile), + ) .route( "/api/v1/annotations", get(routes::saved_insights::list_annotations) @@ -1117,6 +1154,7 @@ async fn main() -> anyhow::Result<()> { "/api/embed/bi/{token}", get(routes::features_ext::resolve_bi_embed), ) + .route("/api/shared/{token}", get(routes::dashboards::shared)) .route("/api/docs", get(routes::docs::serve_docs)) .route("/docs", get(routes::docs::redirect_docs)); diff --git a/crates/pulse-server/src/routes/dashboards.rs b/crates/pulse-server/src/routes/dashboards.rs new file mode 100644 index 0000000..13ae674 --- /dev/null +++ b/crates/pulse-server/src/routes/dashboards.rs @@ -0,0 +1,307 @@ +use axum::extract::{Path, Query}; +use axum::http::HeaderMap; +use axum::response::IntoResponse; +use axum::{Extension, Json}; +use chrono::{Duration, Utc}; +use redis::AsyncCommands; +use serde::Deserialize; +use serde_json::json; +use uuid::Uuid; + +use crate::error::{AppError, AppResult}; +use crate::middleware::auth::AuthenticatedProject; +use crate::services; +use crate::services::dashboards::{ + self, DashboardInput, DashboardListQuery, DashboardPatch, LayoutItem, TileInput, TilePatch, +}; +use crate::services::insights::saved::{self, ParsedQuery}; +use crate::state::SharedState; + +const MODULE: &str = "dashboards"; +const IDENTITY_MODULE: &str = "identity"; +/// Wrong share passwords allowed per token before a cool-down. +const SHARE_MAX_FAILURES: i64 = 10; +const SHARE_LOCKOUT_SECS: i64 = 15 * 60; + +async fn can_read(state: &SharedState, auth: &AuthenticatedProject) -> AppResult<()> { + auth.require_scope("query")?; + services::modules::require_module_read(state, auth.project_id, MODULE, &auth.allowed_modules) + .await +} + +async fn can_write(state: &SharedState, auth: &AuthenticatedProject) -> AppResult<()> { + auth.require_scope("query")?; + services::modules::require_module_write(state, auth.project_id, MODULE, &auth.allowed_modules) + .await +} + +// GET /api/v2/dashboards +pub async fn list( + Extension(state): Extension, + auth: AuthenticatedProject, + Query(f): Query, +) -> AppResult { + can_read(&state, &auth).await?; + Ok(Json( + json!({ "data": dashboards::list(&state.db, auth.project_id, &f).await? }), + )) +} + +// POST /api/v2/dashboards +pub async fn create( + Extension(state): Extension, + auth: AuthenticatedProject, + Json(input): Json, +) -> AppResult { + can_write(&state, &auth).await?; + Ok(Json( + dashboards::create(&state.db, auth.project_id, &input).await?, + )) +} + +// GET /api/v2/dashboard-templates +pub async fn templates( + Extension(state): Extension, + auth: AuthenticatedProject, +) -> AppResult { + can_read(&state, &auth).await?; + let list: Vec<_> = dashboards::templates() + .into_iter() + .map(|t| { + json!({ + "id": t.id, "name": t.name, "description": t.description, + "tiles": t.tiles.iter().map(|i| json!({"name": i.name, "kind": i.kind})).collect::>(), + }) + }) + .collect(); + Ok(Json(json!({ "data": list }))) +} + +// GET /api/v2/dashboards/{id} +pub async fn get( + Extension(state): Extension, + auth: AuthenticatedProject, + Path(id): Path, +) -> AppResult { + can_read(&state, &auth).await?; + let d = dashboards::get(&state.db, auth.project_id, id).await?; + let tiles = dashboards::tiles(&state.db, id).await?; + Ok(Json(json!({ "dashboard": d, "tiles": tiles }))) +} + +// PATCH /api/v2/dashboards/{id} +pub async fn update( + Extension(state): Extension, + auth: AuthenticatedProject, + Path(id): Path, + Json(p): Json, +) -> AppResult { + can_write(&state, &auth).await?; + Ok(Json( + dashboards::update(&state.db, auth.project_id, id, &p).await?, + )) +} + +// DELETE /api/v2/dashboards/{id} +pub async fn delete( + Extension(state): Extension, + auth: AuthenticatedProject, + Path(id): Path, +) -> AppResult { + can_write(&state, &auth).await?; + dashboards::delete(&state.db, auth.project_id, id).await?; + Ok(Json(json!({ "ok": true }))) +} + +#[derive(Debug, Deserialize, Default)] +pub struct DuplicateParams { + #[serde(default)] + pub duplicate_insights: bool, +} + +// POST /api/v2/dashboards/{id}/duplicate +pub async fn duplicate( + Extension(state): Extension, + auth: AuthenticatedProject, + Path(id): Path, + Query(p): Query, +) -> AppResult { + can_write(&state, &auth).await?; + Ok(Json( + dashboards::duplicate(&state.db, auth.project_id, id, p.duplicate_insights).await?, + )) +} + +// POST /api/v2/dashboards/{id}/tiles +pub async fn add_tile( + Extension(state): Extension, + auth: AuthenticatedProject, + Path(id): Path, + Json(t): Json, +) -> AppResult { + can_write(&state, &auth).await?; + Ok(Json( + dashboards::add_tile(&state.db, auth.project_id, id, &t).await?, + )) +} + +// PATCH /api/v2/dashboards/{id}/tiles/{tile_id} +pub async fn update_tile( + Extension(state): Extension, + auth: AuthenticatedProject, + Path((id, tile_id)): Path<(Uuid, Uuid)>, + Json(p): Json, +) -> AppResult { + can_write(&state, &auth).await?; + Ok(Json( + dashboards::update_tile(&state.db, auth.project_id, id, tile_id, &p).await?, + )) +} + +// DELETE /api/v2/dashboards/{id}/tiles/{tile_id} +pub async fn delete_tile( + Extension(state): Extension, + auth: AuthenticatedProject, + Path((id, tile_id)): Path<(Uuid, Uuid)>, +) -> AppResult { + can_write(&state, &auth).await?; + dashboards::delete_tile(&state.db, auth.project_id, id, tile_id).await?; + Ok(Json(json!({ "ok": true }))) +} + +#[derive(Debug, Deserialize)] +pub struct MoveTile { + pub to_dashboard_id: Uuid, +} + +// POST /api/v2/dashboards/{id}/tiles/{tile_id}/move +pub async fn move_tile( + Extension(state): Extension, + auth: AuthenticatedProject, + Path((id, tile_id)): Path<(Uuid, Uuid)>, + Json(m): Json, +) -> AppResult { + can_write(&state, &auth).await?; + Ok(Json( + dashboards::move_tile(&state.db, auth.project_id, id, tile_id, m.to_dashboard_id).await?, + )) +} + +#[derive(Debug, Deserialize)] +pub struct Layouts { + pub tiles: Vec, +} + +// PUT /api/v2/dashboards/{id}/layouts +pub async fn update_layouts( + Extension(state): Extension, + auth: AuthenticatedProject, + Path(id): Path, + Json(l): Json, +) -> AppResult { + can_write(&state, &auth).await?; + Ok(Json(json!({ + "data": dashboards::update_layouts(&state.db, auth.project_id, id, &l.tiles).await? + }))) +} + +#[derive(Debug, Deserialize, Default)] +pub struct RunParams { + #[serde(default)] + pub refresh: bool, +} + +// GET /api/v2/dashboards/{id}/run +pub async fn run( + Extension(state): Extension, + auth: AuthenticatedProject, + Path(id): Path, + Query(p): Query, +) -> AppResult { + can_read(&state, &auth).await?; + services::modules::require_module_read(&state, auth.project_id, "core", &auth.allowed_modules) + .await?; + // Tiles that read person properties need identity access; other tiles + // still render if the key lacks it. + let identity_ok = services::modules::require_module_read( + &state, + auth.project_id, + IDENTITY_MODULE, + &auth.allowed_modules, + ) + .await + .is_ok(); + let gate = move |q: &ParsedQuery| { + (!identity_ok && q.reads_person_properties()) + .then(|| "this tile needs access to the identity module".to_string()) + }; + let d = dashboards::get(&state.db, auth.project_id, id).await?; + let tiles = dashboards::run(&state.db, auth.project_id, &d, p.refresh, &gate).await?; + Ok(Json(json!({ "dashboard": d, "tiles": tiles }))) +} + +/// Public view of a share link: a dashboard, a saved insight, or (legacy +/// links) last-30-day overview stats. Password-protected links take the +/// password in the `x-share-password` header. +// GET /api/shared/{token} +pub async fn shared( + Extension(state): Extension, + Path(token): Path, + headers: HeaderMap, +) -> AppResult { + let link = services::sharing::resolve_shared_token(&state.db, &token) + .await? + .ok_or_else(|| AppError::NotFound("Share link not found or expired".into()))?; + + if link.password_hash.is_some() { + let fail_key = state.redis_key(&format!("share:fail:{}", link.id)); + let mut redis = state.redis.clone(); + let failures: i64 = redis.get(&fail_key).await.unwrap_or(0); + if failures >= SHARE_MAX_FAILURES { + return Err(AppError::RateLimited); + } + let given = headers + .get("x-share-password") + .and_then(|v| v.to_str().ok()) + .unwrap_or_default(); + if !services::sharing::verify_shared_password(&link, given) { + let n: i64 = redis.incr(&fail_key, 1).await.unwrap_or(1); + if n == 1 { + let _: Result<(), _> = redis.expire(&fail_key, SHARE_LOCKOUT_SECS).await; + } + return Err(AppError::Unauthorized); + } + } + + // Whoever created the link chose to share this view; no module gating. + let open_gate = |_: &ParsedQuery| None; + if let Some(dashboard_id) = link.dashboard_id { + let d = dashboards::get(&state.db, link.project_id, dashboard_id).await?; + let tiles = dashboards::run(&state.db, link.project_id, &d, false, &open_gate).await?; + return Ok(Json( + json!({ "type": "dashboard", "name": link.name, "dashboard": d, "tiles": tiles }), + )); + } + if let Some(insight_id) = link.insight_id { + let insight = saved::get(&state.db, link.project_id, insight_id).await?; + let parsed = saved::parse_saved(&insight)?; + let r = saved::run_saved(&state.db, link.project_id, insight, &parsed, false).await?; + return Ok(Json( + json!({ "type": "insight", "name": link.name, "insight": r }), + )); + } + let end = Utc::now(); + let start = end - Duration::days(30); + let today = end.date_naive(); + let stats = services::query::fetch_stats(&state.db, link.project_id, start, end, today).await?; + let pages = + services::query::fetch_pages(&state.db, link.project_id, start, end, today, 10, 0).await?; + Ok(Json(json!({ + "type": "overview", + "name": link.name, + "modules": link.modules, + "range": { "start_at": start, "end_at": end }, + "stats": { "pageviews": stats.0, "visitors": stats.1, "sessions": stats.2, "bounces": stats.3 }, + "top_pages": pages, + }))) +} diff --git a/crates/pulse-server/src/routes/features_ext.rs b/crates/pulse-server/src/routes/features_ext.rs index d4c84cb..ac288b4 100644 --- a/crates/pulse-server/src/routes/features_ext.rs +++ b/crates/pulse-server/src/routes/features_ext.rs @@ -1847,6 +1847,10 @@ pub struct CreateSharedDashboard { pub modules: Option>, pub password: Option, pub expires_at: Option>, + /// Share a dashboard (v2) instead of module stats. + pub dashboard_id: Option, + /// Share a single saved insight. + pub insight_id: Option, } pub async fn create_shared_dashboard( @@ -1863,6 +1867,18 @@ pub async fn create_shared_dashboard( ) .await?; + if input.dashboard_id.is_some() && input.insight_id.is_some() { + return Err(AppError::BadRequest( + "share either a dashboard or an insight, not both".into(), + )); + } + // The shared resource must belong to this project. + if let Some(id) = input.dashboard_id { + services::dashboards::get(&state.db, auth.project_id, id).await?; + } + if let Some(id) = input.insight_id { + services::insights::saved::get(&state.db, auth.project_id, id).await?; + } let name = input.name.unwrap_or_else(|| "Shared Dashboard".to_string()); let modules = input.modules.unwrap_or_default(); let dashboard = services::sharing::create_shared_dashboard( @@ -1872,6 +1888,8 @@ pub async fn create_shared_dashboard( &modules, input.password.as_deref(), input.expires_at, + input.dashboard_id, + input.insight_id, ) .await .map_err(|e| AppError::Internal(e.to_string()))?; diff --git a/crates/pulse-server/src/routes/mod.rs b/crates/pulse-server/src/routes/mod.rs index f3ecce9..06a4cf9 100644 --- a/crates/pulse-server/src/routes/mod.rs +++ b/crates/pulse-server/src/routes/mod.rs @@ -1,5 +1,6 @@ pub mod admin; pub mod dashboard; +pub mod dashboards; pub mod docs; pub mod features; pub mod features_ext; diff --git a/crates/pulse-server/src/services/dashboards.rs b/crates/pulse-server/src/services/dashboards.rs new file mode 100644 index 0000000..0841936 --- /dev/null +++ b/crates/pulse-server/src/services/dashboards.rs @@ -0,0 +1,815 @@ +//! Dashboards (PostHog `Dashboard` + `DashboardTile`): saved-insight and text +//! tiles on a grid, dashboard-wide filters (date range + properties) applied +//! to every insight tile, per-tile result caching, duplicate, move, and +//! built-in templates. + +use chrono::{DateTime, Duration, Utc}; +use serde::{Deserialize, Serialize}; +use serde_json::{json, Value}; +use sha2::{Digest, Sha256}; +use sqlx::{FromRow, PgPool}; +use uuid::Uuid; + +use crate::error::{AppError, AppResult}; +use crate::services::insights::saved::{ + self, Insight, InsightInput, InsightKind, ParsedQuery, INSIGHT_CACHE_TTL_SECS, +}; + +pub const MAX_TILES: i64 = 100; +pub const MAX_TEXT_LEN: usize = 10_000; + +#[derive(Debug, Clone, Serialize, FromRow)] +pub struct Dashboard { + pub id: Uuid, + pub project_id: Uuid, + pub name: String, + pub description: Option, + pub tags: Vec, + pub pinned: bool, + pub is_home: bool, + pub filters: Value, + pub created_at: DateTime, + pub updated_at: DateTime, +} + +const DASHBOARD_COLUMNS: &str = + "id, project_id, name, description, tags, pinned, is_home, filters, created_at, updated_at"; + +#[derive(Debug, Clone, Serialize, FromRow)] +pub struct Tile { + pub id: Uuid, + pub dashboard_id: Uuid, + pub insight_id: Option, + pub text: Option, + pub layout: Value, + pub color: Option, + pub created_at: DateTime, + pub updated_at: DateTime, +} + +const TILE_COLUMNS: &str = + "id, dashboard_id, insight_id, text, layout, color, created_at, updated_at"; + +/// Dashboard-wide filters, applied on top of each insight's own query. +#[derive(Debug, Clone, Default, Deserialize, Serialize)] +pub struct DashboardFilters { + /// Absolute or relative (`-7d`, `mStart`, …) dates override the insight's. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub date_from: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub date_to: Option, + /// Extra property filters ANDed into every insight. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub properties: Vec, +} + +impl DashboardFilters { + fn from_value(v: &Value) -> AppResult { + serde_json::from_value(v.clone()) + .map_err(|e| AppError::BadRequest(format!("invalid dashboard filters: {e}"))) + } + + fn is_empty(&self) -> bool { + self.date_from.is_none() && self.date_to.is_none() && self.properties.is_empty() + } + + /// The insight query with these filters applied. + pub fn apply(&self, query: &Value) -> Value { + let mut q = query.clone(); + if let Some(obj) = q.as_object_mut() { + if let Some(f) = &self.date_from { + obj.insert("date_from".into(), json!(f)); + } + if let Some(t) = &self.date_to { + obj.insert("date_to".into(), json!(t)); + } + if !self.properties.is_empty() { + let props = obj.entry("properties").or_insert_with(|| json!([])); + if let Some(arr) = props.as_array_mut() { + arr.extend(self.properties.iter().cloned()); + } + } + } + q + } +} + +#[derive(Debug, Clone, Deserialize)] +pub struct DashboardInput { + pub name: String, + #[serde(default)] + pub description: Option, + #[serde(default)] + pub tags: Vec, + #[serde(default)] + pub pinned: bool, + #[serde(default)] + pub filters: Option, + /// Create the dashboard (and its insights) from a built-in template. + #[serde(default)] + pub template: Option, +} + +#[derive(Debug, Clone, Deserialize, Default)] +pub struct DashboardPatch { + pub name: Option, + pub description: Option, + pub tags: Option>, + pub pinned: Option, + pub is_home: Option, + pub filters: Option, +} + +#[derive(Debug, Clone, Deserialize, Default)] +pub struct DashboardListQuery { + pub search: Option, + pub tags: Option, + pub pinned: Option, +} + +fn validate_meta(name: &str, tags: &[String]) -> AppResult<()> { + if name.trim().is_empty() || name.len() > 255 { + return Err(AppError::BadRequest("name must be 1-255 characters".into())); + } + if tags.len() > saved::MAX_TAGS || tags.iter().any(|t| t.trim().is_empty() || t.len() > 64) { + return Err(AppError::BadRequest("invalid tags".into())); + } + Ok(()) +} + +pub async fn create(db: &PgPool, project_id: Uuid, input: &DashboardInput) -> AppResult { + validate_meta(&input.name, &input.tags)?; + let filters = input.filters.clone().unwrap_or_else(|| json!({})); + DashboardFilters::from_value(&filters)?; + let template = match &input.template { + Some(t) => Some( + templates() + .into_iter() + .find(|x| x.id == t) + .ok_or_else(|| AppError::BadRequest(format!("unknown template '{t}'")))?, + ), + None => None, + }; + let d: Dashboard = sqlx::query_as(&format!( + "INSERT INTO dashboards (project_id, name, description, tags, pinned, filters) \ + VALUES ($1, $2, $3, $4, $5, $6) RETURNING {DASHBOARD_COLUMNS}" + )) + .bind(project_id) + .bind(input.name.trim()) + .bind(&input.description) + .bind(&input.tags) + .bind(input.pinned) + .bind(&filters) + .fetch_one(db) + .await?; + if let Some(t) = template { + for (i, ti) in t.tiles.into_iter().enumerate() { + let layout = json!({ "x": (i % 2) * 6, "y": (i / 2) * 5, "w": 6, "h": 5 }); + let insight = saved::create(db, project_id, &ti).await?; + add_tile( + db, + project_id, + d.id, + &TileInput { + insight_id: Some(insight.id), + text: None, + layout: Some(layout), + color: None, + }, + ) + .await?; + } + } + Ok(d) +} + +pub async fn get(db: &PgPool, project_id: Uuid, id: Uuid) -> AppResult { + sqlx::query_as(&format!( + "SELECT {DASHBOARD_COLUMNS} FROM dashboards WHERE id = $1 AND project_id = $2 AND NOT deleted" + )) + .bind(id) + .bind(project_id) + .fetch_optional(db) + .await? + .ok_or_else(|| AppError::NotFound("Dashboard not found".into())) +} + +pub async fn list( + db: &PgPool, + project_id: Uuid, + f: &DashboardListQuery, +) -> AppResult> { + let tags: Vec = f + .tags + .as_deref() + .map(|t| { + t.split(',') + .map(|s| s.trim().to_string()) + .filter(|s| !s.is_empty()) + .collect() + }) + .unwrap_or_default(); + let search = f.search.as_deref().map(|s| { + format!( + "%{}%", + s.replace('\\', "\\\\") + .replace('%', "\\%") + .replace('_', "\\_") + ) + }); + Ok(sqlx::query_as(&format!( + "SELECT {DASHBOARD_COLUMNS} FROM dashboards WHERE project_id = $1 AND NOT deleted \ + AND ($2::text IS NULL OR name ILIKE $2 OR description ILIKE $2) \ + AND tags @> $3 AND ($4::boolean IS NULL OR pinned = $4) \ + ORDER BY is_home DESC, pinned DESC, lower(name), id" + )) + .bind(project_id) + .bind(search) + .bind(&tags) + .bind(f.pinned) + .fetch_all(db) + .await?) +} + +pub async fn update( + db: &PgPool, + project_id: Uuid, + id: Uuid, + p: &DashboardPatch, +) -> AppResult { + let cur = get(db, project_id, id).await?; + let name = p.name.clone().unwrap_or(cur.name.clone()); + let tags = p.tags.clone().unwrap_or(cur.tags.clone()); + validate_meta(&name, &tags)?; + if let Some(f) = &p.filters { + DashboardFilters::from_value(f)?; + } + let mut tx = db.begin().await?; + if p.is_home == Some(true) { + sqlx::query("UPDATE dashboards SET is_home = false WHERE project_id = $1 AND is_home") + .bind(project_id) + .execute(&mut *tx) + .await?; + } + let d: Dashboard = sqlx::query_as(&format!( + "UPDATE dashboards SET name = $3, description = $4, tags = $5, pinned = $6, is_home = $7, \ + filters = $8, updated_at = NOW() WHERE id = $1 AND project_id = $2 RETURNING {DASHBOARD_COLUMNS}" + )) + .bind(id) + .bind(project_id) + .bind(name.trim()) + .bind(p.description.clone().or(cur.description)) + .bind(&tags) + .bind(p.pinned.unwrap_or(cur.pinned)) + .bind(p.is_home.unwrap_or(cur.is_home)) + .bind(p.filters.clone().unwrap_or(cur.filters)) + .fetch_one(&mut *tx) + .await?; + tx.commit().await?; + Ok(d) +} + +pub async fn delete(db: &PgPool, project_id: Uuid, id: Uuid) -> AppResult<()> { + let r = sqlx::query( + "UPDATE dashboards SET deleted = true, is_home = false, updated_at = NOW() \ + WHERE id = $1 AND project_id = $2 AND NOT deleted", + ) + .bind(id) + .bind(project_id) + .execute(db) + .await?; + if r.rows_affected() == 0 { + return Err(AppError::NotFound("Dashboard not found".into())); + } + Ok(()) +} + +#[derive(Debug, Clone, Deserialize)] +pub struct TileInput { + #[serde(default)] + pub insight_id: Option, + /// Markdown text tile (mutually exclusive with `insight_id`). + #[serde(default)] + pub text: Option, + #[serde(default)] + pub layout: Option, + #[serde(default)] + pub color: Option, +} + +#[derive(Debug, Clone, Deserialize, Default)] +pub struct TilePatch { + pub text: Option, + pub layout: Option, + pub color: Option, +} + +fn validate_layout(v: &Value) -> AppResult<()> { + let ok = v.as_object().is_some_and(|o| { + ["x", "y", "w", "h"].iter().all(|k| { + o.get(*k) + .is_none_or(|n| n.as_u64().is_some_and(|n| n <= 1_000)) + }) + }); + if ok { + Ok(()) + } else { + Err(AppError::BadRequest( + "layout must be {x, y, w, h} with integers 0-1000".into(), + )) + } +} + +fn validate_text(t: &str) -> AppResult<()> { + if t.trim().is_empty() || t.len() > MAX_TEXT_LEN { + return Err(AppError::BadRequest(format!( + "text must be 1-{MAX_TEXT_LEN} characters" + ))); + } + Ok(()) +} + +pub async fn tiles(db: &PgPool, dashboard_id: Uuid) -> AppResult> { + Ok(sqlx::query_as(&format!( + "SELECT {TILE_COLUMNS} FROM dashboard_tiles WHERE dashboard_id = $1 \ + ORDER BY (layout->>'y')::int NULLS LAST, (layout->>'x')::int NULLS LAST, created_at" + )) + .bind(dashboard_id) + .fetch_all(db) + .await?) +} + +pub async fn add_tile( + db: &PgPool, + project_id: Uuid, + dashboard_id: Uuid, + t: &TileInput, +) -> AppResult { + get(db, project_id, dashboard_id).await?; + match (&t.insight_id, &t.text) { + (Some(i), None) => { + saved::get(db, project_id, *i).await?; + } + (None, Some(text)) => validate_text(text)?, + _ => { + return Err(AppError::BadRequest( + "a tile needs exactly one of insight_id or text".into(), + )) + } + } + let layout = t.layout.clone().unwrap_or_else(|| json!({})); + validate_layout(&layout)?; + let (n,): (i64,) = + sqlx::query_as("SELECT COUNT(*) FROM dashboard_tiles WHERE dashboard_id = $1") + .bind(dashboard_id) + .fetch_one(db) + .await?; + if n >= MAX_TILES { + return Err(AppError::BadRequest(format!( + "dashboards hold at most {MAX_TILES} tiles" + ))); + } + Ok(sqlx::query_as(&format!( + "INSERT INTO dashboard_tiles (dashboard_id, insight_id, text, layout, color) \ + VALUES ($1, $2, $3, $4, $5) RETURNING {TILE_COLUMNS}" + )) + .bind(dashboard_id) + .bind(t.insight_id) + .bind(&t.text) + .bind(&layout) + .bind(&t.color) + .fetch_one(db) + .await?) +} + +async fn tile_owned( + db: &PgPool, + project_id: Uuid, + dashboard_id: Uuid, + tile_id: Uuid, +) -> AppResult { + get(db, project_id, dashboard_id).await?; + sqlx::query_as(&format!( + "SELECT {TILE_COLUMNS} FROM dashboard_tiles WHERE id = $1 AND dashboard_id = $2" + )) + .bind(tile_id) + .bind(dashboard_id) + .fetch_optional(db) + .await? + .ok_or_else(|| AppError::NotFound("Tile not found".into())) +} + +pub async fn update_tile( + db: &PgPool, + project_id: Uuid, + dashboard_id: Uuid, + tile_id: Uuid, + p: &TilePatch, +) -> AppResult { + let cur = tile_owned(db, project_id, dashboard_id, tile_id).await?; + if p.text.is_some() && cur.text.is_none() { + return Err(AppError::BadRequest("only text tiles have text".into())); + } + if let Some(t) = &p.text { + validate_text(t)?; + } + if let Some(l) = &p.layout { + validate_layout(l)?; + } + Ok(sqlx::query_as(&format!( + "UPDATE dashboard_tiles SET text = COALESCE($2, text), layout = COALESCE($3, layout), \ + color = COALESCE($4, color), updated_at = NOW() WHERE id = $1 RETURNING {TILE_COLUMNS}" + )) + .bind(tile_id) + .bind(&p.text) + .bind(&p.layout) + .bind(&p.color) + .fetch_one(db) + .await?) +} + +pub async fn delete_tile( + db: &PgPool, + project_id: Uuid, + dashboard_id: Uuid, + tile_id: Uuid, +) -> AppResult<()> { + tile_owned(db, project_id, dashboard_id, tile_id).await?; + sqlx::query("DELETE FROM dashboard_tiles WHERE id = $1") + .bind(tile_id) + .execute(db) + .await?; + Ok(()) +} + +/// Move a tile to another dashboard of the same project. +pub async fn move_tile( + db: &PgPool, + project_id: Uuid, + dashboard_id: Uuid, + tile_id: Uuid, + to: Uuid, +) -> AppResult { + tile_owned(db, project_id, dashboard_id, tile_id).await?; + get(db, project_id, to).await?; + Ok(sqlx::query_as(&format!( + "UPDATE dashboard_tiles SET dashboard_id = $2, cache_key = NULL, cached_result = NULL, \ + updated_at = NOW() WHERE id = $1 RETURNING {TILE_COLUMNS}" + )) + .bind(tile_id) + .bind(to) + .fetch_one(db) + .await?) +} + +#[derive(Debug, Clone, Deserialize)] +pub struct LayoutItem { + pub id: Uuid, + pub layout: Value, +} + +/// Save the whole grid at once (after a drag/resize). +pub async fn update_layouts( + db: &PgPool, + project_id: Uuid, + dashboard_id: Uuid, + items: &[LayoutItem], +) -> AppResult> { + get(db, project_id, dashboard_id).await?; + for i in items { + validate_layout(&i.layout)?; + } + let mut tx = db.begin().await?; + for i in items { + let r = sqlx::query( + "UPDATE dashboard_tiles SET layout = $3, updated_at = NOW() WHERE id = $1 AND dashboard_id = $2", + ) + .bind(i.id) + .bind(dashboard_id) + .bind(&i.layout) + .execute(&mut *tx) + .await?; + if r.rows_affected() == 0 { + return Err(AppError::NotFound(format!("Tile {} not found", i.id))); + } + } + tx.commit().await?; + tiles(db, dashboard_id).await +} + +/// Copy a dashboard; with `duplicate_insights` the insights are copied too, +/// otherwise the new tiles point at the same insights. +pub async fn duplicate( + db: &PgPool, + project_id: Uuid, + id: Uuid, + duplicate_insights: bool, +) -> AppResult { + let src = get(db, project_id, id).await?; + let name: String = format!("{} (copy)", src.name).chars().take(255).collect(); + let copy = create( + db, + project_id, + &DashboardInput { + name, + description: src.description.clone(), + tags: src.tags.clone(), + pinned: false, + filters: Some(src.filters.clone()), + template: None, + }, + ) + .await?; + for t in tiles(db, id).await? { + let insight_id = match t.insight_id { + Some(i) if duplicate_insights => Some(saved::duplicate(db, project_id, i).await?.id), + other => other, + }; + add_tile( + db, + project_id, + copy.id, + &TileInput { + insight_id, + text: t.text, + layout: Some(t.layout), + color: t.color, + }, + ) + .await?; + } + Ok(copy) +} + +/// One rendered tile. +#[derive(Debug, Serialize)] +pub struct TileResult { + pub tile: Tile, + #[serde(skip_serializing_if = "Option::is_none")] + pub insight: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub result: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub cached: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub last_refresh_at: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + pub error: Option, +} + +/// Decides whether a tile may read person data (module gating); returns an +/// error message when it may not. +pub type PersonGate<'a> = &'a (dyn Fn(&ParsedQuery) -> Option + Sync); + +fn cache_key(insight: &Insight, filters: &DashboardFilters) -> String { + let mut h = Sha256::new(); + h.update(insight.id.as_bytes()); + h.update(insight.version.to_be_bytes()); + h.update(serde_json::to_vec(filters).unwrap_or_default()); + hex::encode(h.finalize()) +} + +/// Run every insight tile with the dashboard filters applied. A failing tile +/// reports its error without failing the whole dashboard. +pub async fn run( + db: &PgPool, + project_id: Uuid, + dashboard: &Dashboard, + refresh: bool, + gate: PersonGate<'_>, +) -> AppResult> { + let filters = DashboardFilters::from_value(&dashboard.filters)?; + let mut out = Vec::new(); + for tile in tiles(db, dashboard.id).await? { + let Some(insight_id) = tile.insight_id else { + out.push(TileResult { + tile, + insight: None, + result: None, + cached: None, + last_refresh_at: None, + error: None, + }); + continue; + }; + let insight = match saved::get(db, project_id, insight_id).await { + Ok(i) => i, + Err(e) => { + out.push(TileResult { + tile, + insight: None, + result: None, + cached: None, + last_refresh_at: None, + error: Some(e.to_string()), + }); + continue; + } + }; + let rendered = run_tile(db, project_id, &tile, &insight, &filters, refresh, gate).await; + out.push(match rendered { + Ok((result, cached, at)) => TileResult { + tile, + insight: Some(insight), + result: Some(result), + cached: Some(cached), + last_refresh_at: Some(at), + error: None, + }, + Err(e) => TileResult { + tile, + insight: Some(insight), + result: None, + cached: None, + last_refresh_at: None, + error: Some(e.to_string()), + }, + }); + } + Ok(out) +} + +async fn run_tile( + db: &PgPool, + project_id: Uuid, + tile: &Tile, + insight: &Insight, + filters: &DashboardFilters, + refresh: bool, + gate: PersonGate<'_>, +) -> AppResult<(Value, bool, DateTime)> { + let query = filters.apply(&insight.query); + let parsed = ParsedQuery::parse(insight.kind, &query, Utc::now())?; + if let Some(msg) = gate(&parsed) { + return Err(AppError::Forbidden(msg)); + } + if filters.is_empty() { + // Same query as the insight itself: share its cache. + let r = saved::run_saved(db, project_id, insight.clone(), &parsed, refresh).await?; + return Ok((r.result, r.cached, r.last_refresh_at)); + } + let key = cache_key(insight, filters); + let now = Utc::now(); + if !refresh { + let row: Option<(Option, Option, Option>)> = sqlx::query_as( + "SELECT cache_key, cached_result, last_refresh_at FROM dashboard_tiles WHERE id = $1", + ) + .bind(tile.id) + .fetch_optional(db) + .await?; + if let Some((Some(k), Some(result), Some(at))) = row { + if k == key && now - at < Duration::seconds(INSIGHT_CACHE_TTL_SECS) { + return Ok((result, true, at)); + } + } + } + let result = parsed.run(db, project_id).await?; + sqlx::query( + "UPDATE dashboard_tiles SET cache_key = $2, cached_result = $3, last_refresh_at = $4 WHERE id = $1", + ) + .bind(tile.id) + .bind(&key) + .bind(&result) + .bind(now) + .execute(db) + .await?; + Ok((result, false, now)) +} + +/// A built-in dashboard template. +#[derive(Debug, Clone, Serialize)] +pub struct Template { + pub id: &'static str, + pub name: &'static str, + pub description: &'static str, + #[serde(skip)] + pub tiles: Vec, +} + +fn tpl(name: &str, kind: InsightKind, query: Value) -> InsightInput { + InsightInput { + name: name.to_string(), + description: None, + kind, + query, + tags: vec!["template".to_string()], + favorited: false, + } +} + +/// Built-in templates. Queries use relative dates so they stay current. +pub fn templates() -> Vec