Returns aggregate statistics about the code graph.
(&self)
| 8 | impl Database { |
| 9 | /// Returns aggregate statistics about the code graph. |
| 10 | pub async fn get_stats(&self) -> Result<GraphStats> { |
| 11 | // Single query for all scalar counts: nodes, edges, files, last_updated, total_source_bytes |
| 12 | let mut counts_rows = self |
| 13 | .conn() |
| 14 | .query( |
| 15 | "SELECT \ |
| 16 | (SELECT COUNT(*) FROM nodes), \ |
| 17 | (SELECT COUNT(*) FROM edges), \ |
| 18 | (SELECT COUNT(*) FROM files), \ |
| 19 | (SELECT COALESCE(MAX(indexed_at), 0) FROM files), \ |
| 20 | (SELECT COALESCE(SUM(size), 0) FROM files)", |
| 21 | (), |
| 22 | ) |
| 23 | .await |
| 24 | .map_err(|e| TraceDecayError::Database { |
| 25 | message: format!("failed to query counts: {e}"), |
| 26 | operation: "get_stats".to_string(), |
| 27 | })?; |
| 28 | let counts_row = counts_rows |
| 29 | .next() |
| 30 | .await |
| 31 | .map_err(|e| TraceDecayError::Database { |
| 32 | message: format!("failed to read counts row: {e}"), |
| 33 | operation: "get_stats".to_string(), |
| 34 | })?; |
| 35 | let (node_count, edge_count, file_count, last_updated, total_source_bytes) = |
| 36 | match counts_row { |
| 37 | Some(r) => { |
| 38 | let nc: i64 = r.get(0).unwrap_or(0); |
| 39 | let ec: i64 = r.get(1).unwrap_or(0); |
| 40 | let fc: i64 = r.get(2).unwrap_or(0); |
| 41 | let lu: i64 = r.get(3).unwrap_or(0); |
| 42 | let ts: i64 = r.get(4).unwrap_or(0); |
| 43 | (nc as u64, ec as u64, fc as u64, lu as u64, ts as u64) |
| 44 | } |
| 45 | None => (0, 0, 0, 0, 0), |
| 46 | }; |
| 47 | |
| 48 | // Nodes grouped by kind |
| 49 | let nodes_by_kind = query_kind_counts( |
| 50 | self.conn(), |
| 51 | "SELECT kind, COUNT(*) FROM nodes GROUP BY kind", |
| 52 | ) |
| 53 | .await?; |
| 54 | |
| 55 | // Edges grouped by kind |
| 56 | let edges_by_kind = query_kind_counts( |
| 57 | self.conn(), |
| 58 | "SELECT kind, COUNT(*) FROM edges GROUP BY kind", |
| 59 | ) |
| 60 | .await?; |
| 61 | |
| 62 | let db_size_bytes = self.size().await.unwrap_or(0); |
| 63 | |
| 64 | // Files grouped by language. Done in Rust (not SQL) so the label set |
| 65 | // stays in sync with the extractor registry without an ever-growing |
| 66 | // CASE expression. See `display_language_for_path`. |
| 67 | let files_by_language = { |