use std::sync::Mutex;
use std::time::Instant;
use crate::load_events::constants::SYSTEM_NAMES;
use crate::load_events::models::{PhaseTiming, Role};
#[derive(Debug)]
pub(crate) struct Step {
pub(crate) id: u64,
pub(crate) subject: String,
pub(crate) doing: String,
pub(crate) started: Instant,
}
#[derive(Debug, Default)]
pub(crate) struct Steps {
pub(crate) tracking: bool,
pub(crate) current: Option<Step>,
pub(crate) lines: Vec<(String, bool)>,
pub(crate) timings: Vec<PhaseTiming>,
pub(crate) next_id: u64,
pub(crate) connected: Vec<String>,
pub(crate) connect_ms: u64,
pub(crate) discovered: (usize, usize, u64),
}
pub(crate) static STEPS: Mutex<Steps> = Mutex::new(Steps {
tracking: false,
current: None,
lines: Vec::new(),
timings: Vec::new(),
next_id: 0,
connected: Vec::new(),
connect_ms: 0,
discovered: (0, 0, 0),
});
pub(crate) fn phase_line(
(role, phase, ok): (Role, &str, bool),
(subject, detail): (&str, &str),
seconds: f64,
) -> String {
let who = match role {
Role::Source => "Source",
Role::Target => "Target",
};
let (label, time) = match (phase, ok) {
("connect", true) => (format!("{who} connected"), format!("({seconds:.2}s)")),
("connect", false) => (
format!("{who} connection failed"),
format!("(after {seconds:.2}s)"),
),
(_, true) => ("Discovered".to_owned(), format!("({seconds:.2}s)")),
(_, false) => (
"Discovery failed".to_owned(),
format!("(after {seconds:.2}s)"),
),
};
let mark = match ok {
true => '\u{2713}',
false => '\u{2717}',
};
match detail.is_empty() {
true => format!("{mark} {label} {subject} {time}"),
false => format!("{mark} {label} {subject} {detail} {time}"),
}
}
pub(crate) fn system_name(label: &str) -> String {
let system = label.strip_suffix(" through ODBC").unwrap_or(label);
SYSTEM_NAMES
.iter()
.find(|(upper, _)| *upper == system)
.map_or_else(|| system.to_owned(), |(_, name)| (*name).to_owned())
}