kitchen_shell/
activity.rs1use gpui_kit::{App, AppContext as _, Entity};
7pub use kitchen_services::activity::{
8 Actor, Entry, clear, close, command, focus, generation, job_ended, job_started, now, open, produced,
9};
10use kitchen_ui::timeline::{Activity, Timeline};
11
12pub fn to_activity(e: &Entry) -> Activity {
14 Activity {
15 start: e.start,
16 end: e.end,
17 lane: e.lane.clone().into(),
18 kind: e.kind.clone().into(),
19 title: e.title.clone().into(),
20 detail: e.detail.clone().into(),
21 source: e.source.clone().into(),
22 assets: e.assets.iter().cloned().map(Into::into).collect(),
23 by: e.by.clone().into(),
24 }
25}
26
27pub fn record(a: Activity) {
29 kitchen_services::activity::record(Entry {
30 start: a.start,
31 end: a.end,
32 lane: a.lane.to_string(),
33 kind: a.kind.to_string(),
34 title: a.title.to_string(),
35 detail: a.detail.to_string(),
36 source: a.source.to_string(),
37 assets: a.assets.iter().map(|s| s.to_string()).collect(),
38 by: a.by.to_string(),
39 });
40}
41
42pub fn all() -> Vec<Activity> {
44 kitchen_services::activity::all().iter().map(to_activity).collect()
45}
46
47pub fn made(a: &Activity) -> bool {
49 a.lane == "Jobs" || !a.assets.is_empty()
50}
51
52pub fn view(title: &str, cx: &mut App) -> Entity<Timeline> {
54 view_of(title, |_| true, cx)
55}
56
57pub fn view_of(title: &str, keep: impl Fn(&Activity) -> bool + 'static, cx: &mut App) -> Entity<Timeline> {
60 let keep = std::rc::Rc::new(keep);
61 let shown = {
62 let keep = keep.clone();
63 move || all().into_iter().filter(|a| keep(a)).collect::<Vec<_>>()
64 };
65 let timeline = cx.new(|_| Timeline::new(title.to_string()));
66 timeline.update(cx, |t, cx| t.set_activities(shown(), now(), cx));
67 let weak = timeline.downgrade();
68 cx.spawn(async move |cx| {
69 let mut seen = generation();
70 loop {
71 cx.background_executor().timer(std::time::Duration::from_secs(1)).await;
72 let g = generation();
73 let stale = g != seen;
75 seen = g;
76 let alive = weak.update(cx, |t, cx| {
77 if stale || !t.activities().is_empty() {
78 t.set_activities(shown(), now(), cx)
79 }
80 });
81 if alive.is_err() {
82 break;
83 }
84 }
85 })
86 .detach();
87 timeline
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93 use gpui_kit::SharedString;
94 use serde_json::Value;
95
96 #[test]
98 fn commands_spans_and_jobs_are_recorded() {
99 clear();
100 focus("Explore");
101 command(
102 Actor::Person,
103 "hist.find_cells",
104 "Find cells",
105 "Analyze",
106 "menu",
107 &Value::Null,
108 &Ok(serde_json::json!({"cells": 12})),
109 );
110 command(
111 Actor::Agent,
112 "process.op.blur",
113 "Gaussian blur",
114 "Filter",
115 "agent",
116 &serde_json::json!({"sigma": 2}),
117 &Err("Open an image first".into()),
118 );
119 job_started(7, "histology", "Finding cells");
120 produced("Find cells", vec!["cells.parquet".into()]);
121 let a = all();
122 let find = a.iter().find(|x| x.title == "Find cells").unwrap();
123 assert_eq!(
124 (find.lane.as_ref(), find.kind.as_ref(), find.source.as_ref()),
125 ("You", "Analyze", "Explore ยท menu")
126 );
127 assert_eq!(find.by.as_ref(), crate::people::who(), "done for the person working");
128 assert!(made(find), "it produced a file: made");
129 assert!(!made(a.iter().find(|x| x.title == "Gaussian blur").unwrap()), "a command that made nothing is not");
130 assert!(find.detail.contains("cells"));
131 assert_eq!(find.assets, vec![SharedString::from("cells.parquet")]);
132 let blur = a.iter().find(|x| x.title == "Gaussian blur").unwrap();
133 assert_eq!(blur.lane.as_ref(), "Agent");
134 assert!(blur.detail.contains("refused"));
135 assert!(a.iter().any(|x| x.lane == "Jobs" && x.end > x.start), "a running job lasts until now");
136 job_ended(7);
137 focus("Analyze");
138 let a = all();
139 assert_eq!(a.iter().filter(|x| x.lane == "Focus").count(), 2);
140 assert!(a.iter().filter(|x| x.lane == "Focus").all(|x| x.end > x.start));
141 }
142}