artifactize_core/monitor/model/
graph.rs1use std::collections::{BTreeMap, BTreeSet};
5
6use super::{
7 NodeId, Target,
8 states::{Activity, Busy, Completion, EvalView, NotRun},
9 tree::{Kind, Node, Tally},
10};
11use crate::types::ArtifactName;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub enum Light {
18 Red,
20 Error,
22 Active,
24 Blocked,
26 Waiting,
29 Green,
31}
32
33impl From<Completion> for Light {
34 fn from(completion: Completion) -> Self {
36 match completion {
37 Completion::Complete => Self::Green,
38 Completion::Failed { verdict: true } => Self::Red,
39 Completion::Failed { verdict: false } => Self::Error,
40 Completion::InProgress(Busy::Running | Busy::Human) => Self::Active,
41 Completion::InProgress(Busy::Queued) => Self::Waiting,
42 Completion::Waiting { blocked: true } => Self::Blocked,
43 Completion::Waiting { blocked: false } => Self::Waiting,
44 }
45 }
46}
47
48impl From<&EvalView> for Light {
49 fn from(view: &EvalView) -> Self {
50 match view {
51 EvalView::Done(_) => Self::Green,
52 EvalView::Failed { verdict: true } => Self::Red,
53 EvalView::Failed { verdict: false } => Self::Error,
54 EvalView::InProgress(Activity::Running | Activity::Human(_)) => Self::Active,
55 EvalView::BlockedBy(_) => Self::Blocked,
56 EvalView::InProgress(Activity::Queued(_))
57 | EvalView::WaitingOn(_)
58 | EvalView::NotRun(
59 NotRun::Dependency(_)
60 | NotRun::Budget
61 | NotRun::Stale
62 | NotRun::Unreviewed
63 | NotRun::Absent,
64 ) => Self::Waiting,
65 }
66 }
67}
68
69fn eval_light(eval: &Node, view: &EvalView) -> Light {
71 if eval.held
72 && matches!(
73 view,
74 EvalView::WaitingOn(_) | EvalView::NotRun(NotRun::Dependency(_))
75 )
76 {
77 Light::Blocked
78 } else {
79 Light::from(view)
80 }
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
84pub struct GraphNode {
85 pub id: NodeId,
87 pub name: ArtifactName,
88 pub light: Light,
89 pub tally: Option<Tally>,
91 pub parts: Vec<ArtifactName>,
93}
94
95#[derive(Debug, Clone, PartialEq, Eq)]
97pub struct GraphEdge {
98 pub from: ArtifactName,
99 pub to: ArtifactName,
100 pub light: Light,
102}
103
104#[derive(Debug, Clone, Default, PartialEq, Eq)]
105pub struct Graph {
106 pub nodes: Vec<GraphNode>,
108 pub edges: Vec<GraphEdge>,
110}
111
112impl Graph {
113 pub fn same_shape(&self, other: &Self) -> bool {
116 self.nodes.len() == other.nodes.len()
117 && self.edges.len() == other.edges.len()
118 && self
119 .nodes
120 .iter()
121 .zip(&other.nodes)
122 .all(|(one, other)| one.id == other.id && one.parts == other.parts)
123 && self
124 .edges
125 .iter()
126 .zip(&other.edges)
127 .all(|(one, other)| one.from == other.from && one.to == other.to)
128 }
129}
130
131pub fn graph(tree: &[Node]) -> Graph {
134 let artifacts: Vec<(ArtifactName, &Node, Completion)> = tree
135 .iter()
136 .filter_map(|node| match node.kind {
137 Kind::Artifact { completion, .. } => match Target::from(&node.id) {
138 Target::Artifact(name) => Some((name, node, completion)),
139 _ => None,
140 },
141 Kind::Eval(_) => None,
142 })
143 .collect();
144 let lights: BTreeMap<_, _> = artifacts
145 .iter()
146 .map(|(name, node, completion)| {
147 let evals = node
149 .children
150 .iter()
151 .filter_map(|eval| match &eval.kind {
152 Kind::Eval(view) => Some(eval_light(eval, view)),
153 Kind::Artifact { .. } => None,
154 })
155 .min()
156 .unwrap_or(Light::Green);
157 let light = match evals {
160 Light::Green => Light::from(*completion),
161 light => light,
162 };
163 (name.clone(), light)
164 })
165 .collect();
166 let position: BTreeMap<&ArtifactName, usize> = artifacts
167 .iter()
168 .enumerate()
169 .map(|(index, (name, _, _))| (name, index))
170 .collect();
171 let mut edges = Vec::new();
172 for (name, node, _) in &artifacts {
173 let upstream: BTreeSet<_> = node
174 .children
175 .iter()
176 .flat_map(|eval| eval.upstream.iter().map(|dependency| &dependency.artifact))
177 .collect();
178 for from in upstream {
179 let Some(&own) = lights.get(from).filter(|_| from != name) else {
180 continue;
181 };
182 edges.push(GraphEdge {
183 from: from.clone(),
184 to: name.clone(),
185 light: own,
186 });
187 }
188 }
189 edges.sort_by_key(|edge| (position[&edge.to], position[&edge.from]));
190 Graph {
191 nodes: artifacts
192 .into_iter()
193 .map(|(name, node, _)| GraphNode {
194 id: node.id.clone(),
195 light: lights[&name],
196 tally: node.tally,
197 parts: node
198 .parts
199 .iter()
200 .filter(|part| lights.contains_key(*part))
201 .cloned()
202 .collect(),
203 name,
204 })
205 .collect(),
206 edges,
207 }
208}