Refactor diagnostic groups (yet again)

This commit is contained in:
2Shirt 2025-06-01 16:07:51 -07:00
parent dc49006af8
commit a9e929585f
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
2 changed files with 248 additions and 197 deletions

View file

@ -13,9 +13,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>. // along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
// //
use crate::diags;
use core::{ use core::{
action::{Action, DiagResult}, action::Action,
components::{ components::{
Component, Component,
footer::Footer, footer::Footer,
@ -57,13 +56,14 @@ use ratatui::{
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tracing::{debug, info}; use tracing::{debug, info};
use crate::diags::{DiagGroup, Type as DiagType, get_diag_type, parse_chkdsk};
pub struct App { pub struct App {
// TUI // TUI
action_rx: mpsc::UnboundedReceiver<Action>, action_rx: mpsc::UnboundedReceiver<Action>,
action_tx: mpsc::UnboundedSender<Action>, action_tx: mpsc::UnboundedSender<Action>,
components: Vec<Box<dyn Component>>, components: Vec<Box<dyn Component>>,
config: Config, config: Config,
diag_groups: diags::Groups,
frame_rate: f64, frame_rate: f64,
last_tick_key_events: Vec<KeyEvent>, last_tick_key_events: Vec<KeyEvent>,
should_quit: bool, should_quit: bool,
@ -72,11 +72,11 @@ pub struct App {
// App // App
clone: CloneSettings, clone: CloneSettings,
cur_mode: Mode, cur_mode: Mode,
diag_groups: Arc<Mutex<Vec<DiagGroup>>>,
list: StatefulList<Mode>, list: StatefulList<Mode>,
boot_modes: Vec<SafeMode>, boot_modes: Vec<SafeMode>,
selections: Vec<Option<usize>>, selections: Vec<Option<usize>>,
system32: String, system32: String,
results: Arc<Mutex<HashMap<String, String>>>,
tasks: Tasks, tasks: Tasks,
} }
@ -119,7 +119,6 @@ impl App {
)), )),
], ],
config: Config::new()?, config: Config::new()?,
diag_groups: diags::Groups::new(),
frame_rate, frame_rate,
last_tick_key_events: Vec::new(), last_tick_key_events: Vec::new(),
should_quit: false, should_quit: false,
@ -128,11 +127,11 @@ impl App {
// App // App
clone: CloneSettings::new(disk_list_arc), clone: CloneSettings::new(disk_list_arc),
cur_mode: Mode::Home, cur_mode: Mode::Home,
diag_groups: Arc::new(Mutex::new(Vec::new())),
list, list,
boot_modes: vec![SafeMode::Enable, SafeMode::Disable], boot_modes: vec![SafeMode::Enable, SafeMode::Disable],
system32: String::new(), system32: String::new(),
selections: vec![None, None], selections: vec![None, None],
results: results_arc,
tasks, tasks,
}) })
} }
@ -367,8 +366,8 @@ impl App {
self.action_tx.send(Action::NextScreen)?; self.action_tx.send(Action::NextScreen)?;
} }
Mode::BootDiags | Mode::BootSetup => { Mode::BootDiags | Mode::BootSetup => {
let new_mode = self.next_mode(); //let new_mode = self.next_mode();
self.action_tx.send(Action::SetMode(new_mode))?; //self.action_tx.send(Action::SetMode(new_mode))?;
} }
_ => {} _ => {}
}, },
@ -419,7 +418,9 @@ impl App {
self.action_tx.send(Action::DiagLineStart { self.action_tx.send(Action::DiagLineStart {
text: title.clone(), text: title.clone(),
})?; })?;
self.diag_groups.start(title.to_owned()); if let Ok(mut diag_groups) = self.diag_groups.lock() {
diag_groups.push(DiagGroup::new(get_diag_type(&title)));
}
} }
} }
Action::TasksComplete => { Action::TasksComplete => {
@ -448,90 +449,60 @@ impl App {
fn handle_task(&mut self, task: &Task) -> Result<()> { fn handle_task(&mut self, task: &Task) -> Result<()> {
info!("Handling Task: {task:?}"); info!("Handling Task: {task:?}");
match self.cur_mode { if self.cur_mode == Mode::BootScan {
Mode::BootScan => { if let Ok(mut diag_groups) = self.diag_groups.lock() {
if let Some(current_group) = diag_groups.last_mut() {
match current_group.diag_type {
DiagType::CheckDisk => {
if let Some(task_result) = &task.result {
//
parse_chkdsk(current_group, task_result.clone());
}
}
_ => (),
}
}
}
return Ok(());
}
match task.task_type {
TaskType::CommandNoWait(_, _) | TaskType::CommandWait(_, _) | TaskType::Diskpart(_) => {
// Check result
if let Some(result) = &task.result { if let Some(result) = &task.result {
let title = self.diag_groups.current_group();
let passed: bool;
let info: String;
match result { match result {
TaskResult::Error(msg) => { TaskResult::Error(msg) => {
passed = false; self.action_tx
info = msg.to_owned(); .send(Action::Error(format!("{task:?} Failed: {msg}")))?;
} }
TaskResult::Output(stdout, stderr, success) => { TaskResult::Output(stdout, stderr, success) => {
passed = *success; if !success {
if title == "Filesystem" { let msg = if !stdout.is_empty() {
info = parse_chkdsk(stdout); stdout.clone()
} else { } else if !stderr.is_empty() {
let div = if !(stdout.is_empty() || stderr.is_empty()) { stderr.clone()
"\n\n-----------\n\n"
} else { } else {
"" String::from("Unknown Error")
}; };
info = format!("{stdout}{div}{stderr}"); self.action_tx
.send(Action::Error(format!("{task:?} Failed: {msg}")))?;
} }
} }
} }
self.diag_groups.update(title, passed, info);
if passed {
self.action_tx.send(Action::DiagLineUpdate {
result: DiagResult::Pass,
text: String::from("Pass?"),
})?;
} else {
self.action_tx.send(Action::DiagLineUpdate {
result: DiagResult::Fail,
text: String::from("Fail?"),
})?;
};
} }
} }
_ => { TaskType::GroupEnd { ref label } => {
match task.task_type { self.action_tx.send(Action::DiagLineEnd {
TaskType::CommandNoWait(_, _) text: label.clone(),
| TaskType::CommandWait(_, _) })?;
| TaskType::Diskpart(_) => {
// Check result
if let Some(result) = &task.result {
match result {
TaskResult::Error(msg) => {
self.action_tx
.send(Action::Error(format!("{task:?} Failed: {msg}")))?;
}
TaskResult::Output(stdout, stderr, success) => {
if !success {
let msg = if !stdout.is_empty() {
stdout.clone()
} else if !stderr.is_empty() {
stderr.clone()
} else {
String::from("Unknown Error")
};
self.action_tx.send(Action::Error(format!(
"{task:?} Failed: {msg}"
)))?;
}
}
}
}
}
TaskType::GroupEnd { ref label } => {
self.action_tx.send(Action::DiagLineEnd {
text: label.clone(),
})?;
}
_ => {}
}
} }
_ => {}
} }
Ok(()) Ok(())
} }
fn queue_boot_scan_tasks(&mut self) -> Result<()> { fn queue_boot_scan_tasks(&mut self) -> Result<()> {
self.diag_groups.reset(); if let Ok(mut diag_groups) = self.diag_groups.lock() {
if let Ok(mut results) = self.results.lock() { diag_groups.clear();
results.clear();
} }
let disk_list = self.clone.disk_list.lock().unwrap(); let disk_list = self.clone.disk_list.lock().unwrap();
if let Some(disk_index) = self.clone.disk_index_dest { if let Some(disk_index) = self.clone.disk_index_dest {
@ -557,7 +528,7 @@ impl App {
// BCD // BCD
if !letter_boot.is_empty() { if !letter_boot.is_empty() {
self.tasks.add_group( self.tasks.add_group(
"Boot Files", DiagType::BootConfigData.to_string().as_str(),
vec![TaskType::CommandWait( vec![TaskType::CommandWait(
PathBuf::from(format!("{}\\bcdedit.exe", &self.system32)), PathBuf::from(format!("{}\\bcdedit.exe", &self.system32)),
vec![ vec![
@ -578,7 +549,7 @@ impl App {
// Bitlocker // Bitlocker
self.tasks.add_group( self.tasks.add_group(
"Bitlocker", DiagType::Bitlocker.to_string().as_str(),
vec![ vec![
TaskType::CommandWait( TaskType::CommandWait(
PathBuf::from(format!("{}\\manage-bde.exe", &self.system32)), PathBuf::from(format!("{}\\manage-bde.exe", &self.system32)),
@ -596,6 +567,24 @@ impl App {
); );
// Filesystem Health // Filesystem Health
self.tasks.add_group(
DiagType::CheckDisk.to_string().as_str(),
vec![TaskType::CommandWait(
PathBuf::from(format!("{}\\chkdsk.exe", &self.system32)),
vec![format!("{letter_os}:")],
)],
);
// DISM Health
self.tasks.add_group(
DiagType::ComponentStore.to_string().as_str(),
vec![TaskType::CommandWait(
PathBuf::from(format!("{}\\dism.exe", &self.system32)),
vec![format!("{letter_os}:")],
)],
);
// Critical Files/Folders
let paths: Vec<PathBuf> = [ let paths: Vec<PathBuf> = [
// Files/Folders // Files/Folders
"Users", "Users",
@ -609,28 +598,13 @@ impl App {
.map(|s| PathBuf::from(format!("{letter_os}:\\{s}"))) .map(|s| PathBuf::from(format!("{letter_os}:\\{s}")))
.collect(); .collect();
self.tasks.add_group( self.tasks.add_group(
"Filesystem", DiagType::SystemFiles.to_string().as_str(),
vec![ vec![TaskType::TestPaths(paths)],
TaskType::CommandWait(
PathBuf::from(format!("{}\\chkdsk.exe", &self.system32)),
vec![format!("{letter_os}:")],
),
TaskType::TestPaths(paths),
],
);
// DISM Health
self.tasks.add_group(
"System Files",
vec![TaskType::CommandWait(
PathBuf::from(format!("{}\\dism.exe", &self.system32)),
vec![format!("{letter_os}:")],
)],
); );
// Registry // Registry
self.tasks.add_group( self.tasks.add_group(
"Registry", DiagType::Registry.to_string().as_str(),
vec![ vec![
TaskType::CommandWait( TaskType::CommandWait(
PathBuf::from(format!("{}\\reg.exe", &self.system32)), PathBuf::from(format!("{}\\reg.exe", &self.system32)),
@ -762,13 +736,16 @@ fn get_chunks(r: Rect) -> Vec<Rect> {
fn build_footer_string(cur_mode: Mode) -> String { fn build_footer_string(cur_mode: Mode) -> String {
match cur_mode { match cur_mode {
Mode::BootDiags => {
String::from("(Enter) to select / (m) for menu / (s) to start over / (q) to quit")
}
Mode::BootScan | Mode::BootSetup | Mode::Home | Mode::ScanDisks => { Mode::BootScan | Mode::BootSetup | Mode::Home | Mode::ScanDisks => {
String::from("(q) to quit") String::from("(q) to quit")
} }
Mode::InstallDrivers | Mode::InjectDrivers | Mode::SetBootMode => { Mode::InstallDrivers | Mode::InjectDrivers | Mode::SetBootMode => {
String::from("(Enter) to select / (q) to quit") String::from("(Enter) to select / (q) to quit")
} }
Mode::BootDiags | Mode::DiagMenu | Mode::SelectParts => { Mode::DiagMenu | Mode::SelectParts => {
String::from("(Enter) to select / (s) to start over / (q) to quit") String::from("(Enter) to select / (s) to start over / (q) to quit")
} }
Mode::Done => String::from("(Enter) to continue / (q) to quit"), Mode::Done => String::from("(Enter) to continue / (q) to quit"),
@ -852,14 +829,21 @@ fn build_left_items(app: &App) -> Action {
select_type = SelectionType::Loop; select_type = SelectionType::Loop;
let (new_title, _) = get_mode_strings(app.cur_mode); let (new_title, _) = get_mode_strings(app.cur_mode);
title = new_title; title = new_title;
app.diag_groups.get().iter().for_each(|group| { if let Ok(diag_groups) = app.diag_groups.lock() {
info!("BootDiags Group: {:?}", group); let labels: Vec<String> = diag_groups
items.push(if group.passed { .iter()
group.title.clone() .map(|group| {
} else { let label = group.diag_type.to_string();
format!("{} - Issues detected!", group.title) let status = if group.passed {
}); "" // Leave blank if OK
}); } else {
" -- Issue(s) detected"
};
format!("{label}{status}")
})
.collect();
items.extend(labels.into_iter());
}
} }
Mode::BootSetup => { Mode::BootSetup => {
select_type = SelectionType::Loop; select_type = SelectionType::Loop;
@ -970,18 +954,13 @@ fn build_right_items(app: &App) -> Action {
}); });
} }
Mode::BootDiags => { Mode::BootDiags => {
app.diag_groups.get().iter().for_each(|group| { if let Ok(diag_groups) = app.diag_groups.lock() {
let mut lines = Vec::new(); let mut summary: Vec<DVLine> = Vec::new();
group.info.iter().for_each(|text| { diag_groups
text.lines().for_each(|line| { .iter()
lines.push(DVLine { .for_each(|group| summary.extend(group.get_logs_summary().into_iter()));
line_parts: vec![String::from(line)], items.push(summary);
line_colors: vec![Color::Reset], }
});
});
});
items.push(lines);
});
} }
Mode::InjectDrivers | Mode::InstallDrivers => { Mode::InjectDrivers | Mode::InstallDrivers => {
items.push(vec![DVLine { items.push(vec![DVLine {
@ -1070,22 +1049,3 @@ fn get_mode_strings(mode: Mode) -> (String, String) {
_ => panic!("This shouldn't happen"), _ => panic!("This shouldn't happen"),
} }
} }
fn parse_chkdsk(output: &str) -> String {
// Split lines
let lines: Vec<_> = output.split("\r\n").collect();
// Omit progress lines and unhelpful messages
lines
.into_iter()
.filter(|line| {
!(line.contains("\r")
|| line.contains("Class not registered")
|| line.contains("/F parameter")
|| line.contains("Running CHKDSK")
|| line.contains("Total duration:")
|| line.contains("Failed to transfer logged messages"))
})
.collect::<Vec<_>>()
.join("\n")
}

View file

@ -13,78 +13,169 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>. // along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
use std::collections::HashMap; use core::{line::DVLine, tasks::TaskResult};
use std::{fmt, thread::sleep, time::Duration};
use tracing::warn; use ratatui::style::Color;
use tracing::{info, warn};
#[derive(Debug)] #[derive(Clone, Copy, Debug)]
pub struct Groups { pub enum Type {
items: HashMap<String, Line>, Bitlocker,
order: Vec<String>, BootConfigData,
CheckDisk,
ComponentStore,
Registry,
SystemFiles,
Unknown,
} }
impl Groups { impl fmt::Display for Type {
pub fn new() -> Self { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Groups { match self {
items: HashMap::new(), Type::Bitlocker => write!(f, "Bitlocker"),
order: Vec::new(), Type::BootConfigData => write!(f, "Boot Files"),
} Type::CheckDisk => write!(f, "CHKDSK"),
} Type::ComponentStore => write!(f, "DISM ScanHealth"),
Type::Registry => write!(f, "Registry"),
pub fn current_group(&self) -> String { Type::SystemFiles => write!(f, "System Files"),
match self.order.last() { Type::Unknown => write!(f, "Unknown Type"),
Some(label) => label.clone(),
None => String::from("No current group"),
}
}
pub fn get(&self) -> Vec<&Line> {
let mut lines = Vec::new();
self.order.iter().for_each(|key| {
if let Some(line) = self.items.get(key) {
lines.push(line);
}
});
lines
}
pub fn reset(&mut self) {
self.items.clear();
self.order.clear();
}
pub fn start(&mut self, title: String) {
self.order.push(title.clone());
self.items.insert(
title.clone(),
Line {
title,
passed: true,
info: Vec::new(),
},
);
}
pub fn update(&mut self, title: String, passed: bool, info: String) {
if let Some(line) = self.items.get_mut(&title) {
line.update(passed, info);
} else {
warn!("WARNING/DELETEME - This shouldn't happen?!");
self.start(title);
} }
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Line { pub struct Log {
pub title: String, pub label: String,
pub passed: bool, pub summary: Vec<DVLine>,
pub info: Vec<String>, pub raw: String,
} }
impl Line { #[derive(Clone, Debug)]
pub fn update(&mut self, passed: bool, info: String) { pub struct DiagGroup {
self.passed &= passed; // We fail if any tests in this group fail pub complete: bool,
self.info.push(info); pub diag_type: Type,
pub passed: bool,
pub logs: Vec<Log>,
pub result: String,
}
impl DiagGroup {
pub fn new(diag_type: Type) -> Self {
DiagGroup {
complete: false,
diag_type,
passed: true,
logs: Vec::new(),
result: String::new(),
}
}
pub fn get_logs_raw(&self) -> String {
let raw_logs: Vec<String> = self
.logs
.iter()
.map(|log| format!("-- {} --\n{}", &log.label, &log.raw))
.collect();
raw_logs.join("\n\n\n")
}
pub fn get_logs_summary(&self) -> Vec<DVLine> {
let mut summaries: Vec<DVLine> = Vec::new();
self.logs
.iter()
.for_each(|log| summaries.extend(log.summary.clone().into_iter()));
summaries
} }
} }
pub fn get_diag_type(label: &str) -> Type {
info!("Getting Diag type for {label}");
match label {
"Bitlocker" => Type::Bitlocker,
"Boot Files" => Type::BootConfigData,
"DISM ScanHealth" => Type::ComponentStore,
"CHKDSK" => Type::CheckDisk,
"Registry" => Type::Registry,
"System Files" => Type::SystemFiles,
_ => {
warn!("Failed to determine type");
Type::Unknown
}
}
}
pub fn parse_chkdsk(diag_group: &mut DiagGroup, task_result: TaskResult) {
if !cfg!(windows) {
sleep(Duration::from_millis(500));
return ();
}
match task_result {
TaskResult::Error(err) => {
diag_group.passed = false;
diag_group.result = String::from("Error");
diag_group.logs.push(Log {
label: String::from("CHKDSK"),
summary: vec![DVLine {
line_parts: vec![String::from("CHKDSK: "), String::from("Error")],
line_colors: vec![Color::Reset, Color::Red],
}],
raw: err,
});
}
TaskResult::Output(stdout, stderr, _) => {
if stdout.contains("Windows has scanned the file system and found no problems.") {
diag_group.passed &= true;
diag_group.result = String::from("OK");
diag_group.logs.push(Log {
label: String::from("CHKDSK"),
summary: vec![DVLine {
line_parts: vec![String::from("CHKDSK: "), String::from("OK")],
line_colors: vec![Color::Reset, Color::Green],
}],
raw: format!("{stdout}\n\n-------\n\n{stderr}"),
});
} else {
diag_group.passed = false;
diag_group.result = String::from("Error");
diag_group.logs.push(Log {
label: String::from("CHKDSK"),
summary: vec![DVLine {
line_parts: vec![String::from("CHKDSK: "), String::from("Error")],
line_colors: vec![Color::Reset, Color::Red],
}],
raw: format!("{stdout}\n\n-------\n\n{stderr}"),
});
}
}
}
// let mut summary = Vec::new();
// let raw = if stderr.is_empty() {
// stdout.to_string()
// } else {
// format!("{stdout}\n\n --stderr-- \n\n{stderr}")
// };
// // TODO: Implement actual logic for result
// Log {
// label: String::from("CHKDSK"),
// raw,
// summary,
// }
// // Split lines
// let lines: Vec<_> = output.split("\r\n").collect();
//
// // Omit progress lines and unhelpful messages
// lines
// .into_iter()
// .filter(|line| {
// !(line.contains("\r")
// || line.contains("Class not registered")
// || line.contains("/F parameter")
// || line.contains("Running CHKDSK")
// || line.contains("Total duration:")
// || line.contains("Failed to transfer logged messages"))
// })
// .collect::<Vec<_>>()
// .join("\n")
}