|
| 1 | +use crate::distillers::Distiller; |
| 2 | +use crate::pipeline::{OutputSegment, SignalTier}; |
| 3 | + |
| 4 | +pub struct DatabaseDistiller; |
| 5 | + |
| 6 | +impl Distiller for DatabaseDistiller { |
| 7 | + fn distill( |
| 8 | + &self, |
| 9 | + segments: &[OutputSegment], |
| 10 | + input: &str, |
| 11 | + _session: Option<&crate::pipeline::SessionState>, |
| 12 | + ) -> String { |
| 13 | + // Detect apakah ini query result, error, atau migration output |
| 14 | + if input.contains("ERROR:") || input.contains("FATAL:") || input.contains("error:") { |
| 15 | + distill_db_error(input) |
| 16 | + } else if input.contains("rows)") || input.contains("row)") || looks_like_table(input) { |
| 17 | + distill_query_result(input) |
| 18 | + } else { |
| 19 | + distill_db_generic(segments, input) |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +fn distill_db_error(input: &str) -> String { |
| 25 | + let mut errors: Vec<String> = vec![]; |
| 26 | + let mut hint: Option<String> = None; |
| 27 | + let mut position: Option<String> = None; |
| 28 | + |
| 29 | + for line in input.lines() { |
| 30 | + let l = line.trim(); |
| 31 | + if l.contains("ERROR:") || l.contains("FATAL:") || l.contains("error:") { |
| 32 | + errors.push(l.to_string()); |
| 33 | + } else if l.starts_with("HINT:") || l.starts_with("DETAIL:") { |
| 34 | + hint = Some(l.to_string()); |
| 35 | + } else if l.starts_with("LINE ") || l.starts_with("POSITION:") { |
| 36 | + position = Some(l.to_string()); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + let mut out = format!("DB Error ({} found):\n", errors.len()); |
| 41 | + for e in errors.iter().take(3) { |
| 42 | + out.push_str(e); |
| 43 | + out.push('\n'); |
| 44 | + } |
| 45 | + if let Some(p) = position { |
| 46 | + out.push_str(&p); |
| 47 | + out.push('\n'); |
| 48 | + } |
| 49 | + if let Some(h) = hint { |
| 50 | + out.push_str(&h); |
| 51 | + out.push('\n'); |
| 52 | + } |
| 53 | + out.trim().to_string() |
| 54 | +} |
| 55 | + |
| 56 | +fn distill_query_result(input: &str) -> String { |
| 57 | + let lines: Vec<&str> = input.lines().collect(); |
| 58 | + let total = lines.len(); |
| 59 | + |
| 60 | + // Cari baris "N rows" |
| 61 | + let row_summary = lines |
| 62 | + .iter() |
| 63 | + .rev() |
| 64 | + .take(5) |
| 65 | + .find(|l| l.contains("row") && (l.contains('(') || l.trim().parse::<usize>().is_ok())) |
| 66 | + .map(|l| l.trim().to_string()); |
| 67 | + |
| 68 | + // Header (kolom) biasanya baris pertama non-empty |
| 69 | + let header = lines |
| 70 | + .iter() |
| 71 | + .find(|l| !l.trim().is_empty() && !l.starts_with('-') && !l.starts_with('(')) |
| 72 | + .map(|l| l.trim().to_string()); |
| 73 | + |
| 74 | + let mut out = String::new(); |
| 75 | + if let Some(h) = &header { |
| 76 | + out.push_str(&format!("Query result columns: {}\n", h)); |
| 77 | + } |
| 78 | + if let Some(summary) = row_summary { |
| 79 | + out.push_str(&format!("Result: {}\n", summary)); |
| 80 | + } else { |
| 81 | + out.push_str(&format!("Result: {} lines output\n", total)); |
| 82 | + } |
| 83 | + // Show first 3 data rows as sample |
| 84 | + let data_rows: Vec<&str> = lines |
| 85 | + .iter() |
| 86 | + .filter(|l| !l.trim().is_empty() && !l.starts_with('-') && !l.starts_with('(')) |
| 87 | + .skip(1) // skip header |
| 88 | + .take(3) |
| 89 | + .copied() |
| 90 | + .collect(); |
| 91 | + if !data_rows.is_empty() { |
| 92 | + out.push_str("Sample rows:\n"); |
| 93 | + for row in &data_rows { |
| 94 | + out.push_str(row); |
| 95 | + out.push('\n'); |
| 96 | + } |
| 97 | + if total > data_rows.len() + 2 { |
| 98 | + out.push_str(&format!( |
| 99 | + " ... [{} more rows]\n", |
| 100 | + total - data_rows.len() - 2 |
| 101 | + )); |
| 102 | + } |
| 103 | + } |
| 104 | + out.trim().to_string() |
| 105 | +} |
| 106 | + |
| 107 | +fn distill_db_generic(segments: &[OutputSegment], _input: &str) -> String { |
| 108 | + let errors: Vec<&str> = segments |
| 109 | + .iter() |
| 110 | + .filter(|s| s.tier == SignalTier::Critical) |
| 111 | + .map(|s| s.content.as_str()) |
| 112 | + .collect(); |
| 113 | + if errors.is_empty() { |
| 114 | + format!("DB: ok ({} lines output)", segments.len()) |
| 115 | + } else { |
| 116 | + format!( |
| 117 | + "DB errors: {}\n{}", |
| 118 | + errors.len(), |
| 119 | + errors |
| 120 | + .iter() |
| 121 | + .take(5) |
| 122 | + .cloned() |
| 123 | + .collect::<Vec<_>>() |
| 124 | + .join("\n") |
| 125 | + ) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +fn looks_like_table(input: &str) -> bool { |
| 130 | + input |
| 131 | + .lines() |
| 132 | + .take(5) |
| 133 | + .any(|l| l.contains(" | ") || l.starts_with("---")) |
| 134 | +} |
0 commit comments