use std::io::Read;
use std::sync::Arc;
use anyhow::{bail, Result};
use colored::Colorize;
use rand::RngExt;
use serde::Serialize;
use serde_json::{json, Value};
use toon_format::encode_default as toon_encode;
pub mod api;
use crate::config::OutputFormat;
use crate::execution::{fan_out, report_errors_and_collect_successes, ExecutionTarget};
use crate::render;
use api::{
DashboardFolderItem, DashboardSearchResult, DashboardsApi, IssueSeverity, QueryByFieldResult,
QuerySearchResult,
};
use crate::safety::confirm_destructive;
const JSON_KEY_PROFILE: &str = "profile";
fn json_str_at(v: &Value, pointer: &str) -> Option<String> {
v.pointer(pointer)
.and_then(|v| v.as_str())
.map(String::from)
}
fn dashboard_id_from_response(resp: &Value) -> Option<String> {
json_str_at(resp, "/dashboardId")
.or_else(|| json_str_at(resp, "/id"))
.or_else(|| json_str_at(resp, "/dashboard/id"))
}
fn folder_id_from_response(resp: &Value) -> Option<String> {
json_str_at(resp, "/folderId").or_else(|| json_str_at(resp, "/id"))
}
fn catalog_item_to_json(
item: &api::DashboardCatalogItem,
include_profile: bool,
profile: &str,
) -> Value {
let mut v = json!({
"id": item.id,
"name": item.name,
"description": item.description,
"slug_name": item.slug_name,
"create_time": item.create_time,
"update_time": item.update_time,
"is_default": item.is_default,
"is_pinned": item.is_pinned,
"is_locked": item.is_locked,
"folder": item.folder.as_ref().map(|f| json!({
"id": f.id,
"name": f.name,
"parent_id": f.parent_id,
})),
});
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert(
JSON_KEY_PROFILE.to_string(),
Value::String(profile.to_string()),
);
}
}
v
}
fn folder_item_to_json(item: &DashboardFolderItem, include_profile: bool, profile: &str) -> Value {
let mut v = json!({
"id": item.id_str(),
"name": item.name,
"parent_id": item.parent_id_str(),
});
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert(
JSON_KEY_PROFILE.to_string(),
Value::String(profile.to_string()),
);
}
}
v
}
pub fn profiled_api_row_to_json<T: Serialize>(
profile: &str,
row: &T,
include_profile: bool,
row_kind: &str,
) -> Result<Value> {
let mut v = serde_json::to_value(row)
.map_err(|e| anyhow::anyhow!("failed to serialize {row_kind} row: {e}"))?;
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert(
JSON_KEY_PROFILE.to_string(),
Value::String(profile.to_string()),
);
}
}
Ok(v)
}
async fn collect_semantic_search_results(
targets: &[Arc<ExecutionTarget>],
query_text: &str,
limit: u32,
) -> Result<Vec<(String, DashboardSearchResult)>> {
let query_owned = query_text.to_string();
let per_profile = fan_out(targets, |target| {
let query_clone = query_owned.clone();
async move {
let api = DashboardsApi::new(&target.client);
Ok(api.semantic_search(&query_clone, limit).await?)
}
})
.await;
let mut all_results: Vec<(String, DashboardSearchResult)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
for r in resp.results {
all_results.push((profile.clone(), r));
}
}
Ok(all_results)
}
fn semantic_search_merged_json_rows(
rows: &[(String, DashboardSearchResult)],
include_profile: bool,
) -> Result<Vec<Value>> {
rows.iter()
.map(|(profile, r)| {
profiled_api_row_to_json(profile, r, include_profile, "dashboard semantic search")
})
.collect()
}
fn render_semantic_search_text_table(
rows: &[(String, DashboardSearchResult)],
include_profile: bool,
) {
if rows.is_empty() {
println!("{}", "No matching dashboards found.".yellow());
return;
}
let table_rows: Vec<Vec<String>> = rows
.iter()
.map(|(profile, r)| {
vec![
profile.clone(),
r.dashboard_name.clone().unwrap_or_default(),
r.dashboard_folder.clone().unwrap_or_default(),
r.widget_count.map(|n| n.to_string()).unwrap_or_default(),
format!("{:.3}", r.similarity),
r.semantic_description
.as_deref()
.or(r.description.as_deref())
.unwrap_or_default()
.chars()
.take(60)
.collect(),
]
})
.collect();
render::render_table(
&["Name", "Folder", "Widgets", "Similarity", "Description"],
table_rows,
include_profile,
);
}
async fn collect_query_search_results(
targets: &[Arc<ExecutionTarget>],
query_text: &str,
limit: u32,
) -> Result<Vec<(String, QuerySearchResult)>> {
let query_owned = query_text.to_string();
let per_profile = fan_out(targets, |target| {
let query_clone = query_owned.clone();
async move {
let api = DashboardsApi::new(&target.client);
Ok(api.search_queries(&query_clone, limit).await?)
}
})
.await;
let mut all_results: Vec<(String, QuerySearchResult)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
for r in resp.results {
all_results.push((profile.clone(), r));
}
}
Ok(all_results)
}
fn query_search_merged_json_rows(
rows: &[(String, QuerySearchResult)],
include_profile: bool,
) -> Result<Vec<Value>> {
rows.iter()
.map(|(profile, r)| {
profiled_api_row_to_json(profile, r, include_profile, "dashboard query search")
})
.collect()
}
fn render_query_search_text_table(rows: &[(String, QuerySearchResult)], include_profile: bool) {
if rows.is_empty() {
println!("{}", "No matching queries found.".yellow());
return;
}
let table_rows: Vec<Vec<String>> = rows
.iter()
.map(|(profile, r)| {
vec![
profile.clone(),
r.query_text.clone(),
r.dashboard_name.clone().unwrap_or_default(),
r.widget_title.clone().unwrap_or_default(),
format!("{:.3}", r.similarity),
]
})
.collect();
render::render_table(
&["Query", "Dashboard", "Widget", "Similarity"],
table_rows,
include_profile,
);
}
async fn collect_queries_by_field_results(
targets: &[Arc<ExecutionTarget>],
field_path: &str,
limit: u32,
) -> Result<Vec<(String, QueryByFieldResult)>> {
let field_path_owned = field_path.to_string();
let per_profile = fan_out(targets, |target| {
let field_path_clone = field_path_owned.clone();
async move {
let api = DashboardsApi::new(&target.client);
Ok(api.queries_by_field(&field_path_clone, limit).await?)
}
})
.await;
let mut all_results: Vec<(String, QueryByFieldResult)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
for r in resp.queries {
all_results.push((profile.clone(), r));
}
}
Ok(all_results)
}
fn queries_by_field_merged_json_rows(
rows: &[(String, QueryByFieldResult)],
include_profile: bool,
) -> Result<Vec<Value>> {
rows.iter()
.map(|(profile, r)| {
profiled_api_row_to_json(profile, r, include_profile, "dashboard queries-by-field")
})
.collect()
}
fn render_queries_by_field_text_table(
rows: &[(String, QueryByFieldResult)],
field_path: &str,
include_profile: bool,
) {
if rows.is_empty() {
println!(
"{}",
format!("No queries found referencing {field_path:?}.").yellow()
);
return;
}
let table_rows: Vec<Vec<String>> = rows
.iter()
.map(|(profile, r)| {
vec![
profile.clone(),
r.query_text.clone(),
r.dashboard_name.clone().unwrap_or_default(),
r.widget_title.clone().unwrap_or_default(),
r.matched_fields.join(", "),
]
})
.collect();
render::render_table(
&["Query", "Dashboard", "Widget", "Matched Fields"],
table_rows,
include_profile,
);
}
pub async fn run_semantic_search(
targets: &[Arc<ExecutionTarget>],
query_text: &str,
limit: u32,
output: OutputFormat,
) -> Result<()> {
if query_text.trim().is_empty() {
bail!("query text cannot be empty");
}
eprintln!(
"{}",
format!("Searching dashboards for: {query_text:?}…").dimmed()
);
let include_profile = targets.len() > 1;
let all_results = collect_semantic_search_results(targets, query_text, limit).await?;
match output {
OutputFormat::Json => {
let json_rows = semantic_search_merged_json_rows(&all_results, include_profile)?;
render::render_json(&json_rows)?;
}
OutputFormat::Toon => {
let json_rows = semantic_search_merged_json_rows(&all_results, include_profile)?;
render::render_toon(&json_rows)?;
}
OutputFormat::Text => render_semantic_search_text_table(&all_results, include_profile),
}
Ok(())
}
pub async fn run_catalog(targets: &[Arc<ExecutionTarget>], output: OutputFormat) -> Result<()> {
eprintln!("{}", "Fetching dashboard catalog...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = DashboardsApi::new(&t.client);
Ok(api.catalog().await?)
})
.await;
let mut all_rows: Vec<Value> = Vec::new();
let mut all_items: Vec<(String, api::DashboardCatalogItem)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
if !resp.items.is_empty() {
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::dashboards_url(b)
})
.await;
}
for item in resp.items {
all_rows.push(catalog_item_to_json(&item, include_profile, &profile));
all_items.push((profile.clone(), item));
}
}
match output {
OutputFormat::Json => render::render_json(&all_rows)?,
OutputFormat::Toon => {
let toon =
toon_encode(&all_rows).map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
if all_items.is_empty() {
render::print_no_results("No dashboards found.");
return Ok(());
}
let rows: Vec<Vec<String>> = all_items
.iter()
.map(|(profile, item)| {
vec![
profile.clone(),
item.id.clone().unwrap_or_default(),
item.name.clone().unwrap_or_default(),
item.folder
.as_ref()
.and_then(|f| f.name.clone())
.unwrap_or_default(),
item.update_time.clone().unwrap_or_default(),
render::bool_display(item.is_pinned),
render::bool_display(item.is_locked),
]
})
.collect();
render::render_table(
&["ID", "Name", "Folder", "Updated", "Pinned", "Locked"],
rows,
include_profile,
);
}
}
Ok(())
}
pub async fn run_search(
targets: &[Arc<ExecutionTarget>],
query_text: &str,
limit: u32,
output: OutputFormat,
) -> Result<()> {
if query_text.trim().is_empty() {
bail!("query text cannot be empty");
}
eprintln!(
"{}",
format!("Searching dashboard queries for: {query_text:?}…").dimmed()
);
let include_profile = targets.len() > 1;
let all_results = collect_query_search_results(targets, query_text, limit).await?;
match output {
OutputFormat::Json => {
let json_rows = query_search_merged_json_rows(&all_results, include_profile)?;
render::render_json(&json_rows)?;
}
OutputFormat::Toon => {
let json_rows = query_search_merged_json_rows(&all_results, include_profile)?;
render::render_toon(&json_rows)?;
}
OutputFormat::Text => render_query_search_text_table(&all_results, include_profile),
}
Ok(())
}
pub async fn run_queries_by_field(
targets: &[Arc<ExecutionTarget>],
field_path: &str,
limit: u32,
output: OutputFormat,
) -> Result<()> {
if field_path.trim().is_empty() {
bail!("field path cannot be empty");
}
eprintln!(
"{}",
format!("Finding queries referencing field {field_path:?}…").dimmed()
);
let include_profile = targets.len() > 1;
let all_results = collect_queries_by_field_results(targets, field_path, limit).await?;
match output {
OutputFormat::Json => {
let json_rows = queries_by_field_merged_json_rows(&all_results, include_profile)?;
render::render_json(&json_rows)?;
}
OutputFormat::Toon => {
let json_rows = queries_by_field_merged_json_rows(&all_results, include_profile)?;
render::render_toon(&json_rows)?;
}
OutputFormat::Text => {
render_queries_by_field_text_table(&all_results, field_path, include_profile);
}
}
Ok(())
}
pub async fn run_get(
targets: &[Arc<ExecutionTarget>],
dashboard_id: &str,
output: OutputFormat,
) -> Result<()> {
if dashboard_id.trim().is_empty() {
bail!("dashboard id cannot be empty");
}
eprintln!(
"{}",
format!("Fetching dashboard {dashboard_id}...").dimmed()
);
let include_profile = targets.len() > 1;
let dashboard_id_owned = dashboard_id.to_string();
let per_profile = fan_out(targets, |target| {
let dashboard_id_clone = dashboard_id_owned.clone();
async move {
let api = DashboardsApi::new(&target.client);
Ok(api.get(&dashboard_id_clone).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, mut val) in report_errors_and_collect_successes(per_profile)? {
if include_profile {
render::tag_get_result(&mut val, &profile);
}
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::dashboard_url(b, dashboard_id)
})
.await;
all_results.push(val);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
render::render_get_text(
&all_results,
include_profile,
"Dashboard not found.",
Some(&|val| {
let name = val
.get("name")
.or_else(|| val.get("dashboard").and_then(|d| d.get("name")))
.and_then(|v| v.as_str())
.unwrap_or("-");
let id = val
.get("id")
.or_else(|| val.get("dashboard").and_then(|d| d.get("id")))
.and_then(|v| v.as_str())
.unwrap_or("-");
let desc = val
.get("description")
.or_else(|| val.get("dashboard").and_then(|d| d.get("description")))
.and_then(|v| v.as_str())
.unwrap_or("");
println!("{}: {}", "Name".bold(), name);
println!("{}: {}", "ID".bold(), id);
if !desc.is_empty() {
println!("{}: {}", "Desc".bold(), desc);
}
}),
)?;
}
}
Ok(())
}
fn new_request_id() -> String {
let mut rng = rand::rng();
(0..16)
.map(|_| format!("{:02x}", rng.random::<u8>()))
.collect()
}
fn read_dashboard_body(from_file: &str) -> Result<Value> {
let raw = if from_file == "-" {
eprintln!("{}", "Reading dashboard definition from stdin...".dimmed());
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf)?;
buf
} else {
eprintln!(
"{}",
format!("Reading dashboard definition from {from_file}...").dimmed()
);
std::fs::read_to_string(from_file)?
};
let parsed: Value = serde_json::from_str(&raw)?;
let dashboard = if parsed.get("dashboard").is_some() {
parsed
.get("dashboard")
.cloned()
.unwrap_or_else(|| json!({}))
} else {
parsed
};
if !dashboard.is_object() {
bail!("Dashboard JSON must be a JSON object (got {})", dashboard);
}
if dashboard.get("layout").is_none() {
bail!(
"Dashboard JSON is missing required 'layout' field. See `cx dashboards create --help`."
);
}
Ok(dashboard)
}
pub async fn run_create(
targets: &[Arc<ExecutionTarget>],
from_file: &str,
folder_id: Option<&str>,
output: OutputFormat,
) -> Result<()> {
let mut dashboard = read_dashboard_body(from_file)?;
if let Some(folder) = folder_id {
if let Value::Object(ref mut m) = dashboard {
m.insert(
"folderId".to_string(),
json!({ "value": folder.to_string() }),
);
}
}
let name = dashboard
.get("name")
.and_then(|v| v.as_str())
.unwrap_or("<unnamed>")
.to_string();
eprintln!("{}", format!("Creating dashboard '{name}'...").dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| {
let dashboard = dashboard.clone();
async move {
let body = json!({
"requestId": new_request_id(),
"dashboard": dashboard,
});
let api = DashboardsApi::new(&t.client);
Ok(api.create(&body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, mut resp) in report_errors_and_collect_successes(per_profile)? {
let created_id = dashboard_id_from_response(&resp);
render::print_created(
"Created",
"dashboard",
Some(&name),
created_id.as_deref(),
&profile,
);
if let Some(id) = &created_id {
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::dashboard_url(b, id)
})
.await;
}
if include_profile {
render::tag_get_result(&mut resp, &profile);
}
all_results.push(resp);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {}
}
Ok(())
}
pub async fn run_replace(
targets: &[Arc<ExecutionTarget>],
from_file: &str,
output: OutputFormat,
yes: bool,
agent_mode: bool,
) -> Result<()> {
let dashboard = read_dashboard_body(from_file)?;
let dash_id = dashboard
.get("id")
.and_then(|v| v.as_str())
.ok_or_else(|| {
anyhow::anyhow!(
"Dashboard JSON is missing required 'id' field. \
Use `cx dashboards get <id> -o json` to fetch the full definition, \
then pass the edited JSON to `cx dashboards replace`."
)
})?;
let name = dashboard
.get("name")
.and_then(|v| v.as_str())
.unwrap_or("<unnamed>")
.to_string();
confirm_destructive(
&format!("Replace dashboard '{name}' ({dash_id})?"),
yes,
agent_mode,
)?;
eprintln!("{}", format!("Replacing dashboard '{name}'...").dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| {
let dashboard = dashboard.clone();
async move {
let body = json!({
"requestId": new_request_id(),
"dashboard": dashboard,
});
let api = DashboardsApi::new(&t.client);
Ok(api.replace(&body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, mut resp) in report_errors_and_collect_successes(per_profile)? {
let replaced_id = dashboard_id_from_response(&resp).or_else(|| Some(dash_id.to_string()));
render::print_created(
"Replaced",
"dashboard",
Some(&name),
replaced_id.as_deref(),
&profile,
);
if let Some(id) = &replaced_id {
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::dashboard_url(b, id)
})
.await;
}
if include_profile {
render::tag_get_result(&mut resp, &profile);
}
all_results.push(resp);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {}
}
Ok(())
}
pub async fn run_delete(targets: &[Arc<ExecutionTarget>], id: &str) -> Result<()> {
eprintln!("{}", format!("Deleting dashboard {id}...").dimmed());
let dashboard_id_owned = id.to_string();
let per_profile = fan_out(targets, |target| {
let dashboard_id_clone = dashboard_id_owned.clone();
async move {
let api = DashboardsApi::new(&target.client);
api.delete(&dashboard_id_clone).await?;
Ok(())
}
})
.await;
for (profile, ()) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("Dashboard {id} deleted in profile '{profile}'.").green()
);
}
Ok(())
}
pub async fn run_folders_list(
targets: &[Arc<ExecutionTarget>],
output: OutputFormat,
) -> Result<()> {
eprintln!("{}", "Fetching dashboard folders...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = DashboardsApi::new(&t.client);
Ok(api.folders().await?)
})
.await;
let mut all_rows: Vec<Value> = Vec::new();
let mut all_items: Vec<(String, DashboardFolderItem)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
if !resp.folders.is_empty() {
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::dashboards_url(b)
})
.await;
}
for item in resp.folders {
all_rows.push(folder_item_to_json(&item, include_profile, &profile));
all_items.push((profile.clone(), item));
}
}
match output {
OutputFormat::Json => render::render_json(&all_rows)?,
OutputFormat::Toon => {
let toon =
toon_encode(&all_rows).map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
if all_items.is_empty() {
render::print_no_results("No dashboard folders found.");
return Ok(());
}
let rows: Vec<Vec<String>> = all_items
.iter()
.map(|(profile, item)| {
vec![
profile.clone(),
item.id_str().unwrap_or("").to_string(),
item.name.clone().unwrap_or_default(),
item.parent_id_str().unwrap_or("").to_string(),
]
})
.collect();
render::render_table(&["ID", "Name", "Parent ID"], rows, include_profile);
}
}
Ok(())
}
pub async fn run_folders_create(
targets: &[Arc<ExecutionTarget>],
name: &str,
parent_id: Option<&str>,
output: OutputFormat,
) -> Result<()> {
if name.trim().is_empty() {
bail!("folder name cannot be empty");
}
eprintln!(
"{}",
format!("Creating dashboard folder '{name}'...").dimmed()
);
let include_profile = targets.len() > 1;
let name_owned = name.to_string();
let parent_id_owned = parent_id.map(|s| s.to_string());
let per_profile = fan_out(targets, |t| {
let name = name_owned.clone();
let parent_id = parent_id_owned.clone();
async move {
let mut folder = json!({ "name": name });
if let (Some(p), Value::Object(ref mut m)) = (parent_id.as_ref(), &mut folder) {
m.insert("parentId".to_string(), Value::String(p.clone()));
}
let body = json!({
"requestId": new_request_id(),
"folder": folder,
});
let api = DashboardsApi::new(&t.client);
Ok(api.folders_create(&body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, mut resp) in report_errors_and_collect_successes(per_profile)? {
let created_id = folder_id_from_response(&resp);
render::print_created(
"Created",
"dashboard folder",
Some(name),
created_id.as_deref(),
&profile,
);
if include_profile {
render::tag_get_result(&mut resp, &profile);
}
all_results.push(resp);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {}
}
Ok(())
}
pub async fn run_folders_delete(targets: &[Arc<ExecutionTarget>], id: &str) -> Result<()> {
eprintln!("{}", format!("Deleting dashboard folder {id}...").dimmed());
let folder_id_owned = id.to_string();
let per_profile = fan_out(targets, |target| {
let folder_id_clone = folder_id_owned.clone();
async move {
let api = DashboardsApi::new(&target.client);
api.folders_delete(&folder_id_clone).await?;
Ok(())
}
})
.await;
for (profile, ()) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("Folder {id} deleted in profile '{profile}'.").green()
);
}
Ok(())
}
fn check_body_from_id(dashboard_id: &str) -> Value {
json!({ "dashboardId": dashboard_id })
}
fn check_body_from_file(from_file: &str) -> Result<Value> {
let dashboard = read_dashboard_body(from_file)?;
Ok(json!({ "dashboard": dashboard }))
}
fn severity_colored(severity: IssueSeverity) -> String {
let s = match severity {
IssueSeverity::SeverityError => "SEVERITY_ERROR",
IssueSeverity::SeverityWarning => "SEVERITY_WARNING",
IssueSeverity::SeverityUnspecified => "SEVERITY_UNSPECIFIED",
};
match severity {
IssueSeverity::SeverityError => s.red().to_string(),
IssueSeverity::SeverityWarning => s.yellow().to_string(),
IssueSeverity::SeverityUnspecified => s.dimmed().to_string(),
}
}
fn issue_json_row(issue: &api::DashboardCheckIssue, profile: &str, include_profile: bool) -> Value {
let mut row = serde_json::to_value(issue).unwrap_or_else(|_| json!({}));
if include_profile {
if let Value::Object(ref mut m) = row {
m.insert(
JSON_KEY_PROFILE.to_string(),
Value::String(profile.to_string()),
);
}
}
row
}
pub async fn run_check(
targets: &[Arc<ExecutionTarget>],
from_file: Option<&str>,
dashboard_id: Option<&str>,
output: OutputFormat,
) -> Result<()> {
let body = match (from_file, dashboard_id) {
(Some(path), None) => {
eprintln!("{}", "Checking dashboard from file...".dimmed());
check_body_from_file(path)?
}
(None, Some(id)) => {
if id.trim().is_empty() {
bail!("dashboard id cannot be empty");
}
eprintln!("{}", format!("Checking dashboard {id}...").dimmed());
check_body_from_id(id)
}
(Some(_), Some(_)) => {
bail!("`--from-file` and `<dashboard_id>` are mutually exclusive; specify one")
}
(None, None) => {
bail!("specify either `--from-file <path>` or a `<dashboard_id>` to check")
}
};
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| {
let body = body.clone();
async move {
let api = DashboardsApi::new(&t.client);
Ok(api.check(&body).await?)
}
})
.await;
let mut all_issues: Vec<(String, Vec<api::DashboardCheckIssue>)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
if let Some(id) = dashboard_id {
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::dashboard_url(b, id)
})
.await;
}
all_issues.push((profile, resp.issues));
}
let total_errors = all_issues
.iter()
.flat_map(|(_, issues)| issues.iter())
.filter(|i| i.severity.is_failure())
.count();
let total_issues = all_issues.iter().map(|(_, i)| i.len()).sum::<usize>();
match output {
OutputFormat::Json => {
let mut rows: Vec<Value> = Vec::new();
for (profile, issues) in &all_issues {
for issue in issues {
rows.push(issue_json_row(issue, profile, include_profile));
}
}
render::render_json(&rows)?;
}
OutputFormat::Toon => {
let mut rows: Vec<Value> = Vec::new();
for (profile, issues) in &all_issues {
for issue in issues {
rows.push(issue_json_row(issue, profile, include_profile));
}
}
render::render_toon(&rows)?;
}
OutputFormat::Text => {
if total_issues == 0 {
println!("{}", "Dashboard is valid (no issues)".green());
} else {
let mut rows: Vec<Vec<String>> = Vec::new();
for (profile, issues) in &all_issues {
for issue in issues {
let row = vec![
profile.clone(),
severity_colored(issue.severity),
issue.location.clone().unwrap_or_default(),
issue.message.clone().unwrap_or_default(),
];
rows.push(row);
}
}
render::render_table(&["Severity", "Location", "Message"], rows, include_profile);
}
}
}
if total_errors > 0 {
bail!(
"dashboard check found {total_errors} error(s) across profile(s); {} warning(s) ignored",
total_issues - total_errors
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn run_check_rows_preserve_severity_in_single_profile_output() {
let profile = "mock-profile";
let issue = api::DashboardCheckIssue {
severity: api::IssueSeverity::SeverityWarning,
message: Some("Query uses deprecated function 'timeShift'".to_string()),
location: Some("/sections/1/rows/0/widgets/0/queries/0".to_string()),
};
let row = vec![
profile.to_string(),
severity_colored(issue.severity),
issue.location.clone().unwrap_or_default(),
issue.message.clone().unwrap_or_default(),
];
let rendered = render::format_table(
&["Severity", "Location", "Message"],
vec![row],
false, );
assert!(
rendered.contains("SEVERITY_WARNING"),
"severity column dropped in single-profile output: {rendered}"
);
assert!(
rendered.contains("/sections/1/rows/0/widgets/0/queries/0"),
"location column missing in single-profile output: {rendered}"
);
assert!(
rendered.contains("Query uses deprecated function 'timeShift'"),
"message column missing in single-profile output: {rendered}"
);
}
}