1use crate::core::config;
5use crate::core::data_source::DataSource;
6use crate::metrics::index;
7use crate::report::{ReportsDirLock, iso_week_label_utc, to_json, to_markdown, write_atomic};
8use crate::retro::types::Report;
9use crate::retro::{engine, inputs};
10use crate::shell::cli::{maybe_refresh_store, workspace_path};
11use crate::shell::remote_pull::maybe_telemetry_pull;
12use crate::store::Store;
13use anyhow::Result;
14use std::path::{Path, PathBuf};
15
16fn compute_retro(
17 workspace: &Path,
18 days: u32,
19 refresh: bool,
20 source: DataSource,
21) -> Result<(PathBuf, Report)> {
22 let cfg = config::load(workspace)?;
23 let db_path = workspace.join(".kaizen/kaizen.db");
24 let store = Store::open(&db_path)?;
25 let ws_str = workspace.to_string_lossy().to_string();
26 maybe_telemetry_pull(workspace, &store, &cfg, source, refresh)?;
27 maybe_refresh_store(workspace, &store, refresh)?;
28 if let Ok(snapshot) = index::ensure_indexed(&store, workspace, refresh)
29 && let Some(ctx) = crate::sync::ingest_ctx(&cfg, workspace.to_path_buf())
30 {
31 if let (Ok(facts), Ok(edges)) = (
32 store.file_facts_for_snapshot(&snapshot.id),
33 store.repo_edges_for_snapshot(&snapshot.id),
34 ) {
35 let _ =
36 crate::sync::smart::enqueue_repo_snapshot(&store, &snapshot, &facts, &edges, &ctx);
37 }
38 let _ = crate::sync::smart::enqueue_workspace_fact_snapshot(&store, workspace, &ctx);
39 }
40
41 let end_ms = std::time::SystemTime::now()
42 .duration_since(std::time::UNIX_EPOCH)
43 .unwrap_or_default()
44 .as_millis() as u64;
45 let start_ms = end_ms.saturating_sub((days as u64).saturating_mul(86_400_000));
46
47 let team_id = if cfg.sync.team_id.is_empty() {
48 None
49 } else {
50 Some(cfg.sync.team_id.as_str())
51 };
52 let workspace_hash = crate::sync::ingest_ctx(&cfg, workspace.to_path_buf())
53 .as_ref()
54 .and_then(crate::sync::smart::workspace_hash_for);
55 let inputs = inputs::load_inputs_for_data_source(
56 &store,
57 workspace,
58 &ws_str,
59 start_ms,
60 end_ms,
61 source,
62 team_id,
63 workspace_hash.as_deref(),
64 )?;
65 let reports_dir = workspace.join(".kaizen/reports");
66 let week_label = iso_week_label_utc();
67 let prior = inputs::prior_bet_fingerprints(&reports_dir)?;
68 let mut report = engine::run(&inputs, &prior);
69 report.meta.week_label = week_label;
70 Ok((reports_dir, report))
71}
72
73pub fn run_retro_report(
75 workspace: Option<&Path>,
76 days: u32,
77 refresh: bool,
78 source: DataSource,
79) -> Result<Report> {
80 let ws = workspace_path(workspace)?;
81 let (_reports_dir, report) = compute_retro(&ws, days, refresh, source)?;
82 Ok(report)
83}
84
85pub fn retro_stdout(
87 workspace: Option<&Path>,
88 days: u32,
89 dry_run: bool,
90 json_out: bool,
91 force: bool,
92 refresh: bool,
93 source: DataSource,
94) -> Result<String> {
95 let ws = workspace_path(workspace)?;
96 let (reports_dir, report) = compute_retro(&ws, days, refresh, source)?;
97 let week_label = report.meta.week_label.clone();
98 let out_path = reports_dir.join(format!("{week_label}.md"));
99
100 if !force && !dry_run && !json_out && out_path.exists() {
101 return Ok(format!(
102 "retro: {} already exists (use --force to overwrite)\n",
103 out_path.display()
104 ));
105 }
106
107 if json_out {
108 return to_json(&report);
109 }
110
111 let md = to_markdown(&report);
112 if dry_run {
113 return Ok(md);
114 }
115
116 let _lock = ReportsDirLock::acquire(&reports_dir)?;
117 write_atomic(&out_path, md.as_bytes())?;
118 Ok(format!("wrote {}\n", out_path.display()))
119}
120
121pub fn cmd_retro(
123 workspace: Option<&Path>,
124 days: u32,
125 dry_run: bool,
126 json_out: bool,
127 force: bool,
128 refresh: bool,
129 source: DataSource,
130) -> Result<()> {
131 print!(
132 "{}",
133 retro_stdout(workspace, days, dry_run, json_out, force, refresh, source)?
134 );
135 Ok(())
136}