1use std::{
2 collections::{BTreeMap, HashMap},
3 fmt,
4 path::PathBuf,
5 str::FromStr,
6};
7
8use anyhow::{Result, bail};
9use serde::{Deserialize, Serialize};
10
11pub type NodeId = usize;
12
13#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
14pub struct Rect {
15 pub x0: f64,
16 pub y0: f64,
17 pub x1: f64,
18 pub y1: f64,
19}
20
21impl Rect {
22 pub fn width(self) -> f64 {
23 (self.x1 - self.x0).max(0.0)
24 }
25
26 pub fn height(self) -> f64 {
27 (self.y1 - self.y0).max(0.0)
28 }
29
30 pub fn area(self) -> f64 {
31 self.width() * self.height()
32 }
33
34 pub fn center(self) -> Point {
35 Point {
36 x: (self.x0 + self.x1) * 0.5,
37 y: (self.y0 + self.y1) * 0.5,
38 }
39 }
40
41 pub fn inset(self, amount: f64) -> Self {
42 let x = amount.min(self.width() * 0.22);
43 let y = amount.min(self.height() * 0.22);
44 Self {
45 x0: self.x0 + x,
46 y0: self.y0 + y,
47 x1: self.x1 - x,
48 y1: self.y1 - y,
49 }
50 }
51}
52
53#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
54pub struct Point {
55 pub x: f64,
56 pub y: f64,
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
60pub enum NodeKind {
61 Directory,
62 File,
63}
64
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct Node {
67 pub id: NodeId,
68 pub parent: Option<NodeId>,
69 pub children: Vec<NodeId>,
70 pub kind: NodeKind,
71 pub name: String,
72 pub path: String,
73 pub depth: usize,
74 pub bytes: u64,
75 pub loc: u64,
76 pub commits: u64,
77 pub weight: f64,
78 pub language: String,
79 pub rect: Rect,
80}
81
82impl Node {
83 pub fn is_file(&self) -> bool {
84 self.kind == NodeKind::File
85 }
86}
87
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct Callsite {
90 pub id: u64,
91 pub source: NodeId,
92 pub source_line: u32,
93 pub target: NodeId,
94 pub target_line: Option<u32>,
95 pub callee: String,
96 pub kind: String,
97 pub analyzer: String,
98 pub confidence: f32,
99}
100
101#[derive(Debug, Clone, Default, Serialize, Deserialize)]
102pub struct AnalyzerReport {
103 pub elixir_files_traced: usize,
104 pub elixir_script_files_scanned: usize,
105 pub typescript_files_scanned: usize,
106 pub gleam_files_scanned: usize,
107 pub rust_files_scanned: usize,
108 pub resolved_calls: usize,
109 pub unresolved_calls: usize,
110 pub rustler_calls: usize,
111 pub rust_calls: usize,
112 pub typescript_calls: usize,
113 pub gleam_calls: usize,
114 #[serde(default)]
115 pub same_file_calls_excluded: usize,
116 pub cache_hit: bool,
117 pub warnings: Vec<String>,
118 #[serde(default)]
119 pub language_coverage: BTreeMap<String, LanguageCoverage>,
120}
121
122#[derive(Debug, Clone, Default, Serialize, Deserialize)]
123pub struct LanguageCoverage {
124 pub provider: String,
125 pub fidelity: String,
126 pub files_discovered: usize,
127 pub files_analyzed: usize,
128 pub callsites_resolved: usize,
129 pub callsites_unresolved: usize,
130}
131
132#[derive(Debug, Clone, Default, Serialize, Deserialize)]
133pub struct BuildTimings {
134 pub scan_ms: u64,
135 pub analyze_ms: u64,
136}
137
138#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct Atlas {
140 pub root_path: PathBuf,
141 pub revision: String,
142 pub dirty: bool,
143 pub excluded_test_files: usize,
144 pub excluded_hidden_files: usize,
145 pub excluded_custom_files: usize,
146 pub excluded_paths: Vec<String>,
147 pub nodes: Vec<Node>,
148 pub calls: Vec<Callsite>,
149 pub path_to_id: HashMap<String, NodeId>,
150 pub report: AnalyzerReport,
151 pub timings: BuildTimings,
152}
153
154impl Atlas {
155 pub fn files(&self) -> impl Iterator<Item = &Node> {
156 self.nodes.iter().filter(|node| node.is_file())
157 }
158
159 pub fn total_loc(&self) -> u64 {
160 self.files().map(|node| node.loc).sum()
161 }
162
163 pub fn hierarchy_path(&self, source: NodeId, target: NodeId) -> Vec<NodeId> {
164 let mut source_chain = Vec::new();
165 let mut current = Some(source);
166 while let Some(id) = current {
167 source_chain.push(id);
168 current = self.nodes[id].parent;
169 }
170
171 let mut target_chain = Vec::new();
172 current = Some(target);
173 while let Some(id) = current {
174 target_chain.push(id);
175 current = self.nodes[id].parent;
176 }
177
178 let target_positions: HashMap<NodeId, usize> = target_chain
179 .iter()
180 .copied()
181 .enumerate()
182 .map(|(index, id)| (id, index))
183 .collect();
184
185 let (source_lca_index, target_lca_index) = source_chain
186 .iter()
187 .enumerate()
188 .find_map(|(source_index, id)| {
189 target_positions
190 .get(id)
191 .copied()
192 .map(|target_index| (source_index, target_index))
193 })
194 .unwrap_or((source_chain.len() - 1, target_chain.len() - 1));
195
196 let mut path = source_chain[..=source_lca_index].to_vec();
197 path.extend(target_chain[..target_lca_index].iter().rev().copied());
198 path
199 }
200}
201
202#[derive(Debug, Clone, Copy, PartialEq, Eq)]
203pub enum Metric {
204 Loc,
205 Bytes,
206 Commits,
207}
208
209impl Metric {
210 pub fn value(self, bytes: u64, loc: u64, commits: u64) -> f64 {
211 let raw = match self {
212 Self::Loc => loc,
213 Self::Bytes => bytes,
214 Self::Commits => commits,
215 };
216 (raw.max(1) as f64).sqrt()
217 }
218}
219
220impl fmt::Display for Metric {
221 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
222 formatter.write_str(match self {
223 Self::Loc => "loc",
224 Self::Bytes => "bytes",
225 Self::Commits => "commits",
226 })
227 }
228}
229
230impl FromStr for Metric {
231 type Err = anyhow::Error;
232
233 fn from_str(value: &str) -> Result<Self> {
234 match value {
235 "loc" => Ok(Self::Loc),
236 "bytes" => Ok(Self::Bytes),
237 "commits" => Ok(Self::Commits),
238 _ => bail!("unknown metric {value:?}; expected loc, bytes, or commits"),
239 }
240 }
241}