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 started_at: Option<u64>,
25 pub turns: u64,
26 pub gate_running: bool,
27 pub activity: String,
28}
29
30pub fn collect(root: &Path, now: u64, days: u64) -> Result<LiveRuns, String> {
31 if !root.is_dir() {
32 return Err(format!("not a directory: {}", root.display()));
33 }
34
35 let repo = repo_name(root);
36 let wt_dir = root.join(".wt");
37 if !wt_dir.is_dir() {
38 return Ok(LiveRuns {
39 generated_at: now,
40 repo,
41 running: Vec::new(),
42 runs: Vec::new(),
43 skipped: Vec::new(),
44 });
45 }
46
47 let expected_root = wt_dir.parent();
48 let src = Sources {
49 roots: vec![root.to_path_buf()],
50 runs_dirs: Vec::new(),
51 hours: days.saturating_mul(24),
52 };
53 let jobs = collect_jobs(&src, now)
54 .into_iter()
55 .filter(|job| job.root.as_deref() == expected_root)
56 .collect();
57
58 let (running_jobs, mut runs, mut skipped) = classify_jobs(jobs, now, days);
59
60 let mut running: Vec<RunningRun> = running_jobs
61 .into_iter()
62 .map(|job| RunningRun {
63 key: job.key,
64 id: job.id,
65 research: job.research,
66 title: job.title,
67 task: job.task,
68 plan_file: job.plan_file,
69 plan_task: job.plan_task,
70 started_at: job.started_at,
71 turns: job.turns,
72 gate_running: job.gate_running,
73 activity: job.now,
74 })
75 .collect();
76
77 running.sort_by(|a, b| {
78 a.started_at
79 .unwrap_or(u64::MAX)
80 .cmp(&b.started_at.unwrap_or(u64::MAX))
81 .then_with(|| a.key.cmp(&b.key))
82 });
83 runs.sort_by(|a, b| {
84 b.finished_at
85 .cmp(&a.finished_at)
86 .then_with(|| a.key.cmp(&b.key))
87 });
88 skipped.sort_by(|a, b| a.key.cmp(&b.key));
89
90 Ok(LiveRuns {
91 generated_at: now,
92 repo,
93 running,
94 runs,
95 skipped,
96 })
97}