Skip to main content

stmo_cli/commands/
dashboards.rs

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        } else {
384            let mut options = widget.options.clone();
385
386            if let Some(query_id) = widget.query_id
387                && let Some(mappings) = auto_populate_parameter_mappings(
388                    client,
389                    query_id,
390                    options.parameter_mappings.as_ref(),
391                    &mut query_cache,
392                ).await?
393            {
394                options.parameter_mappings = Some(mappings);
395                any_widget_has_params = true;
396            }
397
398            let update_payload = CreateWidget {
399                dashboard_id: server_dashboard_id,
400                visualization_id: resolve_visualization_id(client, widget, &mut query_cache).await?,
401                text: widget.text.clone(),
402                width: widget.width,
403                options,
404            };
405            client.update_widget(widget.id, &update_payload).await?;
406        }
407    }
408
409    let updated_dashboard = Dashboard {
410        id: server_dashboard_id,
411        name: local_metadata.name.clone(),
412        slug: local_metadata.slug.clone(),
413        user_id: local_metadata.user_id,
414        is_archived: local_metadata.is_archived,
415        is_draft: local_metadata.is_draft,
416        filters_enabled: any_widget_has_params || local_metadata.filters_enabled,
417        tags: local_metadata.tags.clone(),
418        widgets: vec![],
419    };
420
421    client.update_dashboard(&updated_dashboard).await?;
422
423    let refreshed = client.get_dashboard(&slug_for_refetch).await?;
424
425    save_dashboard_yaml(&refreshed, old_yaml_path)?;
426
427    Ok(refreshed.name)
428}
429
430pub async fn archive(client: &RedashClient, dashboard_slugs: Vec<String>) -> Result<()> {
431    if dashboard_slugs.is_empty() {
432        anyhow::bail!("No dashboard slugs specified.\n\nExample:\n  stmo-cli dashboards archive firefox-desktop-on-steamos bug-2006698---ccov-build-regression");
433    }
434
435    println!("Archiving {} dashboards...\n", dashboard_slugs.len());
436
437    let mut success_count = 0;
438    let mut failed_slugs = Vec::new();
439
440    for slug in &dashboard_slugs {
441        match client.get_dashboard(slug).await {
442            Ok(dashboard) => {
443                match client.archive_dashboard(dashboard.id).await {
444                    Ok(()) => {
445                        let yaml_files: Vec<_> = fs::read_dir("dashboards")
446                            .context("Failed to read dashboards directory")?
447                            .filter_map(std::result::Result::ok)
448                            .filter(|entry| {
449                                entry.path().extension().is_some_and(|ext| ext == "yaml")
450                                    && entry
451                                        .file_name()
452                                        .to_str()
453                                        .and_then(|name| name.strip_suffix(".yaml"))
454                                        .and_then(|name| name.split_once('-'))
455                                        .map(|(_, file_slug)| file_slug)
456                                        .is_some_and(|file_slug| file_slug == slug)
457                            })
458                            .collect();
459
460                        for file in yaml_files {
461                            fs::remove_file(file.path())
462                                .context(format!("Failed to delete {}", file.path().display()))?;
463                        }
464
465                        println!("  ✓ {} archived and local file deleted", dashboard.name);
466                        success_count += 1;
467                    }
468                    Err(e) => {
469                        eprintln!("  ⚠ Dashboard '{slug}' failed to archive: {e}");
470                        failed_slugs.push(slug.clone());
471                    }
472                }
473            }
474            Err(e) => {
475                eprintln!("  ⚠ Dashboard '{slug}' failed to fetch for archival: {e}");
476                failed_slugs.push(slug.clone());
477            }
478        }
479    }
480
481    if failed_slugs.is_empty() {
482        println!("\n✓ All dashboards archived successfully");
483        Ok(())
484    } else {
485        println!("\n✓ {success_count} dashboard(s) archived successfully");
486        anyhow::bail!(
487            "{} dashboard(s) failed to archive: {}",
488            failed_slugs.len(),
489            failed_slugs.join(", ")
490        );
491    }
492}
493
494pub async fn unarchive(client: &RedashClient, dashboard_slugs: Vec<String>) -> Result<()> {
495    if dashboard_slugs.is_empty() {
496        anyhow::bail!("No dashboard slugs specified.\n\nExample:\n  stmo-cli dashboards unarchive firefox-desktop-on-steamos bug-2006698---ccov-build-regression");
497    }
498
499    println!("Unarchiving {} dashboards...\n", dashboard_slugs.len());
500
501    let mut success_count = 0;
502    let mut failed_slugs = Vec::new();
503
504    for slug in &dashboard_slugs {
505        match client.get_dashboard(slug).await {
506            Ok(dashboard) => {
507                match client.unarchive_dashboard(dashboard.id).await {
508                    Ok(unarchived) => {
509                        println!("  ✓ {} unarchived", unarchived.name);
510                        success_count += 1;
511                    }
512                    Err(e) => {
513                        eprintln!("  ⚠ Dashboard '{slug}' failed to unarchive: {e}");
514                        failed_slugs.push(slug.clone());
515                    }
516                }
517            }
518            Err(e) => {
519                eprintln!("  ⚠ Dashboard '{slug}' failed to fetch for unarchival: {e}");
520                failed_slugs.push(slug.clone());
521            }
522        }
523    }
524
525    if failed_slugs.is_empty() {
526        println!("\n✓ All dashboards unarchived successfully");
527        println!("\nUse 'dashboards fetch' to download the YAML files:");
528        println!("  stmo-cli dashboards fetch {}", dashboard_slugs.join(" "));
529        Ok(())
530    } else {
531        println!("\n✓ {success_count} dashboard(s) unarchived successfully");
532        anyhow::bail!(
533            "{} dashboard(s) failed to unarchive: {}",
534            failed_slugs.len(),
535            failed_slugs.join(", ")
536        );
537    }
538}
539
540#[cfg(test)]
541#[allow(clippy::missing_errors_doc)]
542mod tests {
543    use super::*;
544    use tempfile::TempDir;
545
546    #[test]
547    fn test_extract_dashboard_slugs_from_directory_empty() {
548        let temp_dir = TempDir::new().unwrap();
549        let result = extract_dashboard_slugs_from_path(temp_dir.path());
550        assert!(result.is_ok());
551        let slugs = result.unwrap();
552        assert!(slugs.is_empty());
553    }
554
555    #[test]
556    fn test_extract_dashboard_slugs_with_triple_dash() {
557        let temp_dir = TempDir::new().unwrap();
558        let temp_path = temp_dir.path();
559
560        fs::write(temp_path.join("2006698-bug-2006698---ccov-build-regression.yaml"), "test").unwrap();
561        fs::write(temp_path.join("2570-firefox-desktop-on-steamos.yaml"), "test").unwrap();
562
563        let result = extract_dashboard_slugs_from_path(temp_path);
564        assert!(result.is_ok());
565
566        let slugs = result.unwrap();
567
568        assert!(slugs.contains(&"bug-2006698---ccov-build-regression".to_string()));
569        assert!(slugs.contains(&"firefox-desktop-on-steamos".to_string()));
570    }
571
572    #[test]
573    fn test_extract_dashboard_slugs_deduplication() {
574        let temp_dir = TempDir::new().unwrap();
575        let temp_path = temp_dir.path();
576
577        fs::write(temp_path.join("2006698-bug-2006698---ccov-build-regression.yaml"), "test").unwrap();
578        fs::write(temp_path.join("2006699-bug-2006698---ccov-build-regression.yaml"), "test").unwrap();
579
580        let result = extract_dashboard_slugs_from_path(temp_path);
581        assert!(result.is_ok());
582
583        let slugs = result.unwrap();
584
585        assert_eq!(slugs.len(), 1);
586        assert_eq!(slugs[0], "bug-2006698---ccov-build-regression");
587    }
588
589    #[test]
590    fn test_extract_dashboard_slugs_ignores_non_yaml() {
591        let temp_dir = TempDir::new().unwrap();
592        let temp_path = temp_dir.path();
593
594        fs::write(temp_path.join("2006698-bug-2006698---ccov-build-regression.yaml"), "test").unwrap();
595        fs::write(temp_path.join("2570-firefox-desktop-on-steamos.txt"), "test").unwrap();
596        fs::write(temp_path.join("README.md"), "test").unwrap();
597
598        let result = extract_dashboard_slugs_from_path(temp_path);
599        assert!(result.is_ok());
600
601        let slugs = result.unwrap();
602
603        assert_eq!(slugs.len(), 1);
604        assert_eq!(slugs[0], "bug-2006698---ccov-build-regression");
605    }
606
607    #[test]
608    fn test_extract_dashboard_slugs_sorted() {
609        let temp_dir = TempDir::new().unwrap();
610        let temp_path = temp_dir.path();
611
612        fs::write(temp_path.join("3000-zebra-dashboard.yaml"), "test").unwrap();
613        fs::write(temp_path.join("2006698-bug-2006698---ccov-build-regression.yaml"), "test").unwrap();
614        fs::write(temp_path.join("1000-alpha-dashboard.yaml"), "test").unwrap();
615
616        let result = extract_dashboard_slugs_from_path(temp_path);
617        assert!(result.is_ok());
618
619        let slugs = result.unwrap();
620
621        assert_eq!(slugs.len(), 3);
622        assert_eq!(slugs[0], "alpha-dashboard");
623        assert_eq!(slugs[1], "bug-2006698---ccov-build-regression");
624        assert_eq!(slugs[2], "zebra-dashboard");
625    }
626}