1use std::path::Path;
2
3use crate::dashboard::{collect_jobs, repo_name, Sources};
4use crate::replay::{classify_jobs, Run, Skipped};
5
6#[derive(Clone, Debug, serde::Serialize)]
7pub struct LiveRuns {
8 pub generated_at: u64,
9 pub repo: String,
10 pub running: Vec<RunningRun>,
11 pub runs: Vec<Run>,
12 pub skipped: Vec<Skipped>,
13}
14
15#[derive(Clone, Debug, serde::Serialize)]
16pub struct RunningRun {
17 pub key: String,
18 pub id: String,
19 pub research: bool,
20 pub title: String,
21 pub task: String,
22 pub plan_file: Option<String>,
23 pub plan_task: Option<String>,
24 pub request: Option<String>,
25 pub started_at: Option<u64>,
26 pub turns: u64,
27 pub gate_running: bool,
28 pub activity: String,
29}
30
31pub fn collect(root: &Path, now: u64, days: u64) -> Result<LiveRuns, String> {
32 if !root.is_dir() {
33 return Err(format!("not a directory: {}", root.display()));
34 }
35
36 let repo = repo_name(root);
37 let wt_dir = root.join(".wt");
38 if !wt_dir.is_dir() {
39 return Ok(LiveRuns {
40 generated_at: now,
41 repo,
42 running: Vec::new(),
43 runs: Vec::new(),
44 skipped: Vec::new(),
45 });
46 }
47
48 let expected_root = wt_dir.parent();
49 let src = Sources {
50 roots: vec![root.to_path_buf()],
51 runs_dirs: Vec::new(),
52 hours: days.saturating_mul(24),
53 };
54 let jobs = collect_jobs(&src, now)
55 .into_iter()
56 .filter(|job| job.root.as_deref() == expected_root)
57 .collect();
58
59 let (running_jobs, mut runs, mut skipped) = classify_jobs(jobs, now, days);
60
61 let mut running: Vec<RunningRun> = running_jobs
62 .into_iter()
63 .map(|job| RunningRun {
64 key: job.key,
65 id: job.id,
66 research: job.research,
67 title: job.title,
68 task: job.task,
69 plan_file: job.plan_file,
70 plan_task: job.plan_task,
71 request: job.request,
72 started_at: job.started_at,
73 turns: job.turns,
74 gate_running: job.gate_running,
75 activity: job.now,
76 })
77 .collect();
78
79 running.sort_by(|a, b| {
80 a.started_at
81 .unwrap_or(u64::MAX)
82 .cmp(&b.started_at.unwrap_or(u64::MAX))
83 .then_with(|| a.key.cmp(&b.key))
84 });
85 runs.sort_by(|a, b| {
86 b.finished_at
87 .cmp(&a.finished_at)
88 .then_with(|| a.key.cmp(&b.key))
89 });
90 skipped.sort_by(|a, b| a.key.cmp(&b.key));
91
92 Ok(LiveRuns {
93 generated_at: now,
94 repo,
95 running,
96 runs,
97 skipped,
98 })
99}