1#![allow(clippy::missing_errors_doc)]
2
3use anyhow::{Context, Result};
4use std::collections::HashMap;
5use std::fs;
6use std::path::Path;
7
8use crate::api::RedashClient;
9use crate::models::{
10 build_dashboard_level_parameter_mappings, CreateDashboard, CreateWidget, Dashboard,
11 DashboardMetadata, Query, WidgetMetadata,
12};
13
14fn extract_dashboard_slugs_from_path(dashboards_dir: &Path) -> Result<Vec<String>> {
15 if !dashboards_dir.exists() {
16 return Ok(Vec::new());
17 }
18
19 let mut dashboard_slugs = Vec::new();
20
21 for entry in fs::read_dir(dashboards_dir).context("Failed to read dashboards directory")? {
22 let entry = entry.context("Failed to read directory entry")?;
23 let path = entry.path();
24
25 if path.extension().is_some_and(|ext| ext == "yaml")
26 && let Some(filename) = path.file_name().and_then(|f| f.to_str())
27 && let Some(slug) = filename.strip_suffix(".yaml")
28 .and_then(|s| s.split_once('-'))
29 .map(|(_, slug)| slug)
30 {
31 dashboard_slugs.push(slug.to_string());
32 }
33 }
34
35 dashboard_slugs.sort_unstable();
36 dashboard_slugs.dedup();
37
38 Ok(dashboard_slugs)
39}
40
41fn extract_dashboard_slugs_from_directory() -> Result<Vec<String>> {
42 extract_dashboard_slugs_from_path(Path::new("dashboards"))
43}
44
45pub async fn discover(client: &RedashClient) -> Result<()> {
46 println!("Fetching your favorite dashboards from Redash...\n");
47 let dashboards = client.fetch_favorite_dashboards().await?;
48
49 if dashboards.is_empty() {
50 println!("No dashboards found.");
51 return Ok(());
52 }
53
54 println!("Found {} dashboards:\n", dashboards.len());
55
56 for dashboard in &dashboards {
57 let status_flags = match (dashboard.is_draft, dashboard.is_archived) {
58 (true, true) => " [DRAFT, ARCHIVED]",
59 (true, false) => " [DRAFT]",
60 (false, true) => " [ARCHIVED]",
61 (false, false) => "",
62 };
63 println!(" {} - {}{}", dashboard.slug, dashboard.name, status_flags);
64 }
65
66 println!("\nUsage:");
67 println!(" stmo-cli dashboards fetch <slug> [<slug>...]");
68 println!(" stmo-cli dashboards fetch firefox-desktop-on-steamos bug-2006698---ccov-build-regression");
69
70 Ok(())
71}
72
73pub async fn fetch(client: &RedashClient, dashboard_slugs: Vec<String>) -> Result<()> {
74 if dashboard_slugs.is_empty() {
75 anyhow::bail!("No dashboard slugs specified. Use 'dashboards discover' to see available dashboards.\n\nExample:\n stmo-cli dashboards fetch firefox-desktop-on-steamos bug-2006698---ccov-build-regression");
76 }
77
78 fs::create_dir_all("dashboards")
79 .context("Failed to create dashboards directory")?;
80
81 println!("Fetching {} dashboards...\n", dashboard_slugs.len());
82
83 let mut success_count = 0;
84 let mut failed_slugs = Vec::new();
85
86 for slug in &dashboard_slugs {
87 match client.get_dashboard(slug).await {
88 Ok(dashboard) => {
89 let filename = format!("dashboards/{}-{}.yaml", dashboard.id, dashboard.slug);
90
91 let metadata = DashboardMetadata {
92 id: dashboard.id,
93 name: dashboard.name.clone(),
94 slug: dashboard.slug.clone(),
95 user_id: dashboard.user_id,
96 is_draft: dashboard.is_draft,
97 is_archived: dashboard.is_archived,
98 filters_enabled: dashboard.filters_enabled,
99 tags: dashboard.tags.clone(),
100 widgets: dashboard
101 .widgets
102 .iter()
103 .map(|w| WidgetMetadata {
104 id: w.id,
105 width: w.width,
106 visualization_id: w.visualization_id,
107 query_id: w.visualization.as_ref().map(|v| v.query.id),
108 visualization_name: w.visualization.as_ref().map(|v| v.name.clone()),
109 text: w.text.clone(),
110 options: w.options.clone(),
111 })
112 .collect(),
113 };
114
115 let yaml_content = serde_yaml::to_string(&metadata)
116 .context("Failed to serialize dashboard metadata")?;
117 fs::write(&filename, yaml_content)
118 .context(format!("Failed to write {filename}"))?;
119
120 let status = if dashboard.is_archived {
121 " [ARCHIVED]"
122 } else {
123 ""
124 };
125 println!(" ✓ {} - {}{}", dashboard.id, dashboard.name, status);
126 success_count += 1;
127 }
128 Err(e) => {
129 eprintln!(" ⚠ Dashboard '{slug}' failed to fetch: {e}");
130 failed_slugs.push(slug.clone());
131 }
132 }
133 }
134
135 if failed_slugs.is_empty() {
136 println!("\n✓ All dashboards fetched successfully");
137 println!("\nTip: Favorite these dashboards in the Redash web UI so they appear in 'dashboards discover'.");
138 Ok(())
139 } else {
140 println!("\n✓ {success_count} dashboard(s) fetched successfully");
141 anyhow::bail!(
142 "{} dashboard(s) failed to fetch: {}",
143 failed_slugs.len(),
144 failed_slugs.join(", ")
145 );
146 }
147}
148
149pub async fn deploy(client: &RedashClient, dashboard_slugs: Vec<String>, all: bool) -> Result<()> {
150 let existing_dashboard_slugs = extract_dashboard_slugs_from_directory()?;
151
152 let slugs_to_deploy = if all {
153 if existing_dashboard_slugs.is_empty() {
154 anyhow::bail!("No dashboards found in dashboards/ directory. Use 'fetch' first.");
155 }
156 println!("Deploying {} dashboards from local directory...\n", existing_dashboard_slugs.len());
157 existing_dashboard_slugs
158 } else if !dashboard_slugs.is_empty() {
159 println!("Deploying {} specific dashboards...\n", dashboard_slugs.len());
160 dashboard_slugs
161 } else {
162 anyhow::bail!("No dashboard slugs specified. Use --all to deploy all tracked dashboards, or provide specific slugs.\n\nExamples:\n stmo-cli dashboards deploy --all\n stmo-cli dashboards deploy firefox-desktop-on-steamos bug-2006698---ccov-build-regression");
163 };
164
165 let mut success_count = 0;
166 let mut failed_slugs = Vec::new();
167
168 for slug in &slugs_to_deploy {
169 match deploy_single_dashboard(client, slug).await {
170 Ok(name) => {
171 println!(" ✓ {name}");
172 success_count += 1;
173 }
174 Err(e) => {
175 eprintln!(" ⚠ Dashboard '{slug}' failed to deploy: {e}");
176 failed_slugs.push(slug.clone());
177 }
178 }
179 }
180
181 if failed_slugs.is_empty() {
182 println!("\n✓ All dashboards deployed successfully");
183 Ok(())
184 } else {
185 println!("\n✓ {success_count} dashboard(s) deployed successfully");
186 anyhow::bail!(
187 "{} dashboard(s) failed to deploy: {}",
188 failed_slugs.len(),
189 failed_slugs.join(", ")
190 );
191 }
192}
193
194fn save_dashboard_yaml(
195 dashboard: &crate::models::Dashboard,
196 old_yaml_path: Option<std::path::PathBuf>,
197) -> Result<()> {
198 use crate::models::Widget;
199
200 let filename = format!("dashboards/{}-{}.yaml", dashboard.id, dashboard.slug);
201
202 let metadata = DashboardMetadata {
203 id: dashboard.id,
204 name: dashboard.name.clone(),
205 slug: dashboard.slug.clone(),
206 user_id: dashboard.user_id,
207 is_draft: dashboard.is_draft,
208 is_archived: dashboard.is_archived,
209 filters_enabled: dashboard.filters_enabled,
210 tags: dashboard.tags.clone(),
211 widgets: dashboard
212 .widgets
213 .iter()
214 .map(|w: &Widget| WidgetMetadata {
215 id: w.id,
216 width: w.width,
217 visualization_id: w.visualization_id,
218 query_id: w.visualization.as_ref().map(|v| v.query.id),
219 visualization_name: w.visualization.as_ref().map(|v| v.name.clone()),
220 text: w.text.clone(),
221 options: w.options.clone(),
222 })
223 .collect(),
224 };
225
226 let yaml_content = serde_yaml::to_string(&metadata)
227 .context("Failed to serialize dashboard metadata")?;
228 fs::write(&filename, &yaml_content)
229 .context(format!("Failed to write {filename}"))?;
230
231 if let Some(old_path) = old_yaml_path
232 && old_path != std::path::Path::new(&filename)
233 {
234 fs::remove_file(&old_path)
235 .context(format!("Failed to delete {}", old_path.display()))?;
236 }
237
238 Ok(())
239}
240
241async fn resolve_visualization_id(
242 client: &RedashClient,
243 widget: &WidgetMetadata,
244 query_cache: &mut HashMap<u64, Query>,
245) -> Result<Option<u64>> {
246 if let Some(viz_id) = widget.visualization_id {
247 return Ok(Some(viz_id));
248 }
249
250 let (Some(query_id), Some(viz_name)) = (widget.query_id, widget.visualization_name.as_deref()) else {
251 return Ok(None);
252 };
253
254 if let std::collections::hash_map::Entry::Vacant(e) = query_cache.entry(query_id) {
255 e.insert(client.get_query(query_id).await?);
256 }
257
258 let query = query_cache.get(&query_id).expect("just inserted");
259 match query.visualizations.iter().find(|v| v.name == viz_name) {
260 Some(viz) => Ok(Some(viz.id)),
261 None => {
262 let available: Vec<&str> = query.visualizations.iter().map(|v| v.name.as_str()).collect();
263 anyhow::bail!(
264 "No visualization named '{viz_name}' found on query {query_id}. Available: {available:?}"
265 );
266 }
267 }
268}
269
270async fn auto_populate_parameter_mappings(
271 client: &RedashClient,
272 query_id: u64,
273 existing_mappings: Option<&serde_json::Value>,
274 query_cache: &mut HashMap<u64, Query>,
275) -> Result<Option<serde_json::Value>> {
276 let should_build = match existing_mappings {
277 None => true,
278 Some(serde_json::Value::Object(m)) => m.is_empty(),
279 Some(_) => false,
280 };
281 if !should_build {
282 return Ok(None);
283 }
284 if let std::collections::hash_map::Entry::Vacant(e) = query_cache.entry(query_id) {
285 e.insert(client.get_query(query_id).await?);
286 }
287 Ok(query_cache
288 .get(&query_id)
289 .filter(|q| !q.options.parameters.is_empty())
290 .map(|q| build_dashboard_level_parameter_mappings(&q.options.parameters)))
291}
292
293async fn deploy_single_dashboard(client: &RedashClient, dashboard_slug: &str) -> Result<String> {
294 let yaml_files: Vec<_> = fs::read_dir("dashboards")
295 .context("Failed to read dashboards directory")?
296 .filter_map(std::result::Result::ok)
297 .filter(|entry| {
298 entry.path().extension().is_some_and(|ext| ext == "yaml")
299 && entry
300 .file_name()
301 .to_str()
302 .and_then(|name| name.strip_suffix(".yaml"))
303 .and_then(|name| name.split_once('-'))
304 .map(|(_, slug)| slug)
305 .is_some_and(|slug| slug == dashboard_slug)
306 })
307 .collect();
308
309 if yaml_files.is_empty() {
310 anyhow::bail!("No YAML file found for dashboard '{dashboard_slug}'");
311 }
312
313 if yaml_files.len() > 1 {
314 anyhow::bail!("Multiple YAML files found for dashboard '{dashboard_slug}'");
315 }
316
317 let yaml_path = yaml_files[0].path();
318 let yaml_content = fs::read_to_string(&yaml_path)
319 .context(format!("Failed to read {}", yaml_path.display()))?;
320
321 let local_metadata: DashboardMetadata = serde_yaml::from_str(&yaml_content)
322 .context("Failed to parse dashboard YAML")?;
323
324 let (server_dashboard_id, slug_for_refetch, old_yaml_path) = if local_metadata.id == 0 {
325 let created = client.create_dashboard(&CreateDashboard {
326 name: local_metadata.name.clone(),
327 }).await?;
328 println!(" ✓ Created new dashboard: {} - {}", created.id, created.name);
329 client.favorite_dashboard(&created.slug).await?;
330 (created.id, created.slug.clone(), Some(yaml_path.clone()))
331 } else {
332 let server_dashboard = client.get_dashboard(dashboard_slug).await?;
333
334 let server_widget_ids: std::collections::HashSet<u64> = server_dashboard
335 .widgets
336 .iter()
337 .map(|w| w.id)
338 .collect();
339
340 let local_widget_ids: std::collections::HashSet<u64> = local_metadata
341 .widgets
342 .iter()
343 .filter(|w| w.id != 0)
344 .map(|w| w.id)
345 .collect();
346
347 for widget_id in &server_widget_ids {
348 if !local_widget_ids.contains(widget_id) {
349 client.delete_widget(*widget_id).await?;
350 }
351 }
352
353 (server_dashboard.id, dashboard_slug.to_string(), None)
354 };
355
356 let mut query_cache: HashMap<u64, Query> = HashMap::new();
357 let mut any_widget_has_params = false;
358
359 for widget in &local_metadata.widgets {
360 if widget.id == 0 {
361 let mut options = widget.options.clone();
362
363 if let Some(query_id) = widget.query_id
364 && let Some(mappings) = auto_populate_parameter_mappings(
365 client,
366 query_id,
367 options.parameter_mappings.as_ref(),
368 &mut query_cache,
369 ).await?
370 {
371 options.parameter_mappings = Some(mappings);
372 any_widget_has_params = true;
373 }
374
375 let create_widget = CreateWidget {
376 dashboard_id: server_dashboard_id,
377 visualization_id: resolve_visualization_id(client, widget, &mut query_cache).await?,
378 text: widget.text.clone(),
379 width: 1,
380 options,
381 };
382 client.create_widget(&create_widget).await?;
383 }
384 }
385
386 let updated_dashboard = Dashboard {
387 id: server_dashboard_id,
388 name: local_metadata.name.clone(),
389 slug: local_metadata.slug.clone(),
390 user_id: local_metadata.user_id,
391 is_archived: local_metadata.is_archived,
392 is_draft: local_metadata.is_draft,
393 filters_enabled: any_widget_has_params || local_metadata.filters_enabled,
394 tags: local_metadata.tags.clone(),
395 widgets: vec![],
396 };
397
398 client.update_dashboard(&updated_dashboard).await?;
399
400 let refreshed = client.get_dashboard(&slug_for_refetch).await?;
401
402 save_dashboard_yaml(&refreshed, old_yaml_path)?;
403
404 Ok(refreshed.name)
405}
406
407pub async fn archive(client: &RedashClient, dashboard_slugs: Vec<String>) -> Result<()> {
408 if dashboard_slugs.is_empty() {
409 anyhow::bail!("No dashboard slugs specified.\n\nExample:\n stmo-cli dashboards archive firefox-desktop-on-steamos bug-2006698---ccov-build-regression");
410 }
411
412 println!("Archiving {} dashboards...\n", dashboard_slugs.len());
413
414 let mut success_count = 0;
415 let mut failed_slugs = Vec::new();
416
417 for slug in &dashboard_slugs {
418 match client.get_dashboard(slug).await {
419 Ok(dashboard) => {
420 match client.archive_dashboard(dashboard.id).await {
421 Ok(()) => {
422 let yaml_files: Vec<_> = fs::read_dir("dashboards")
423 .context("Failed to read dashboards directory")?
424 .filter_map(std::result::Result::ok)
425 .filter(|entry| {
426 entry.path().extension().is_some_and(|ext| ext == "yaml")
427 && entry
428 .file_name()
429 .to_str()
430 .and_then(|name| name.strip_suffix(".yaml"))
431 .and_then(|name| name.split_once('-'))
432 .map(|(_, file_slug)| file_slug)
433 .is_some_and(|file_slug| file_slug == slug)
434 })
435 .collect();
436
437 for file in yaml_files {
438 fs::remove_file(file.path())
439 .context(format!("Failed to delete {}", file.path().display()))?;
440 }
441
442 println!(" ✓ {} archived and local file deleted", dashboard.name);
443 success_count += 1;
444 }
445 Err(e) => {
446 eprintln!(" ⚠ Dashboard '{slug}' failed to archive: {e}");
447 failed_slugs.push(slug.clone());
448 }
449 }
450 }
451 Err(e) => {
452 eprintln!(" ⚠ Dashboard '{slug}' failed to fetch for archival: {e}");
453 failed_slugs.push(slug.clone());
454 }
455 }
456 }
457
458 if failed_slugs.is_empty() {
459 println!("\n✓ All dashboards archived successfully");
460 Ok(())
461 } else {
462 println!("\n✓ {success_count} dashboard(s) archived successfully");
463 anyhow::bail!(
464 "{} dashboard(s) failed to archive: {}",
465 failed_slugs.len(),
466 failed_slugs.join(", ")
467 );
468 }
469}
470
471pub async fn unarchive(client: &RedashClient, dashboard_slugs: Vec<String>) -> Result<()> {
472 if dashboard_slugs.is_empty() {
473 anyhow::bail!("No dashboard slugs specified.\n\nExample:\n stmo-cli dashboards unarchive firefox-desktop-on-steamos bug-2006698---ccov-build-regression");
474 }
475
476 println!("Unarchiving {} dashboards...\n", dashboard_slugs.len());
477
478 let mut success_count = 0;
479 let mut failed_slugs = Vec::new();
480
481 for slug in &dashboard_slugs {
482 match client.get_dashboard(slug).await {
483 Ok(dashboard) => {
484 match client.unarchive_dashboard(dashboard.id).await {
485 Ok(unarchived) => {
486 println!(" ✓ {} unarchived", unarchived.name);
487 success_count += 1;
488 }
489 Err(e) => {
490 eprintln!(" ⚠ Dashboard '{slug}' failed to unarchive: {e}");
491 failed_slugs.push(slug.clone());
492 }
493 }
494 }
495 Err(e) => {
496 eprintln!(" ⚠ Dashboard '{slug}' failed to fetch for unarchival: {e}");
497 failed_slugs.push(slug.clone());
498 }
499 }
500 }
501
502 if failed_slugs.is_empty() {
503 println!("\n✓ All dashboards unarchived successfully");
504 println!("\nUse 'dashboards fetch' to download the YAML files:");
505 println!(" stmo-cli dashboards fetch {}", dashboard_slugs.join(" "));
506 Ok(())
507 } else {
508 println!("\n✓ {success_count} dashboard(s) unarchived successfully");
509 anyhow::bail!(
510 "{} dashboard(s) failed to unarchive: {}",
511 failed_slugs.len(),
512 failed_slugs.join(", ")
513 );
514 }
515}
516
517#[cfg(test)]
518#[allow(clippy::missing_errors_doc)]
519mod tests {
520 use super::*;
521 use tempfile::TempDir;
522
523 #[test]
524 fn test_extract_dashboard_slugs_from_directory_empty() {
525 let temp_dir = TempDir::new().unwrap();
526 let result = extract_dashboard_slugs_from_path(temp_dir.path());
527 assert!(result.is_ok());
528 let slugs = result.unwrap();
529 assert!(slugs.is_empty());
530 }
531
532 #[test]
533 fn test_extract_dashboard_slugs_with_triple_dash() {
534 let temp_dir = TempDir::new().unwrap();
535 let temp_path = temp_dir.path();
536
537 fs::write(temp_path.join("2006698-bug-2006698---ccov-build-regression.yaml"), "test").unwrap();
538 fs::write(temp_path.join("2570-firefox-desktop-on-steamos.yaml"), "test").unwrap();
539
540 let result = extract_dashboard_slugs_from_path(temp_path);
541 assert!(result.is_ok());
542
543 let slugs = result.unwrap();
544
545 assert!(slugs.contains(&"bug-2006698---ccov-build-regression".to_string()));
546 assert!(slugs.contains(&"firefox-desktop-on-steamos".to_string()));
547 }
548
549 #[test]
550 fn test_extract_dashboard_slugs_deduplication() {
551 let temp_dir = TempDir::new().unwrap();
552 let temp_path = temp_dir.path();
553
554 fs::write(temp_path.join("2006698-bug-2006698---ccov-build-regression.yaml"), "test").unwrap();
555 fs::write(temp_path.join("2006699-bug-2006698---ccov-build-regression.yaml"), "test").unwrap();
556
557 let result = extract_dashboard_slugs_from_path(temp_path);
558 assert!(result.is_ok());
559
560 let slugs = result.unwrap();
561
562 assert_eq!(slugs.len(), 1);
563 assert_eq!(slugs[0], "bug-2006698---ccov-build-regression");
564 }
565
566 #[test]
567 fn test_extract_dashboard_slugs_ignores_non_yaml() {
568 let temp_dir = TempDir::new().unwrap();
569 let temp_path = temp_dir.path();
570
571 fs::write(temp_path.join("2006698-bug-2006698---ccov-build-regression.yaml"), "test").unwrap();
572 fs::write(temp_path.join("2570-firefox-desktop-on-steamos.txt"), "test").unwrap();
573 fs::write(temp_path.join("README.md"), "test").unwrap();
574
575 let result = extract_dashboard_slugs_from_path(temp_path);
576 assert!(result.is_ok());
577
578 let slugs = result.unwrap();
579
580 assert_eq!(slugs.len(), 1);
581 assert_eq!(slugs[0], "bug-2006698---ccov-build-regression");
582 }
583
584 #[test]
585 fn test_extract_dashboard_slugs_sorted() {
586 let temp_dir = TempDir::new().unwrap();
587 let temp_path = temp_dir.path();
588
589 fs::write(temp_path.join("3000-zebra-dashboard.yaml"), "test").unwrap();
590 fs::write(temp_path.join("2006698-bug-2006698---ccov-build-regression.yaml"), "test").unwrap();
591 fs::write(temp_path.join("1000-alpha-dashboard.yaml"), "test").unwrap();
592
593 let result = extract_dashboard_slugs_from_path(temp_path);
594 assert!(result.is_ok());
595
596 let slugs = result.unwrap();
597
598 assert_eq!(slugs.len(), 3);
599 assert_eq!(slugs[0], "alpha-dashboard");
600 assert_eq!(slugs[1], "bug-2006698---ccov-build-regression");
601 assert_eq!(slugs[2], "zebra-dashboard");
602 }
603}