deja-vu/core/src/osticket.rs

127 lines
3.7 KiB
Rust

// This file is part of Deja-Vu.
//
// Deja-Vu is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Deja-Vu is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
//
use ost_db_connector::Ticket;
use sqlx::{MySql, Pool};
use crate::system::disk::Disk;
pub enum ReportType {
Source,
Destination { label: String },
}
#[derive(Debug, Default)]
pub struct OsTicket {
pub connected: bool,
pub disabled: bool,
pub error: String,
pub server_url: String,
pub report: Vec<String>,
pub tech_note: String,
pub ticket: Ticket,
}
impl OsTicket {
pub fn new(ost_server: &str) -> Self {
Self {
connected: false,
disabled: false,
server_url: ost_server.to_string(),
..Default::default()
}
}
pub fn add_error(&mut self, text: &str) {
let lines: Vec<&str> = text
.lines()
.filter(|line| {
let line = line.trim();
!line.is_empty() && !line.eq_ignore_ascii_case("ERROR")
})
.collect();
self.error = lines.join("\n");
}
pub fn disable(&mut self) {
self.connected = false;
self.disabled = true;
}
pub fn get_report(&mut self) -> String {
if !self.error.is_empty() {
self.report.push(format!("\n\nERROR: {}", self.error));
}
self.report.join("\n")
}
pub fn set_db_pool(&mut self, pool: Option<Pool<MySql>>) {
if self.disabled {
return;
}
match pool {
Some(pool) => {
self.connected = true;
self.ticket.pool = Some(pool);
}
None => {
self.disable();
}
}
}
pub fn update_report(&mut self, disk: &Disk, report_type: ReportType) {
// Header
if self.report.is_empty() {
self.report.push(String::from("[Deja-Vu Report]"));
if !self.tech_note.is_empty() {
self.report.push(format!("Tech note: {}", self.tech_note));
}
}
// Disk Label
match report_type {
ReportType::Source => {
self.report.push(String::new());
self.report.push(String::from("Source:"));
}
ReportType::Destination { label } => {
self.report.push(String::new());
self.report.push(String::from("---"));
self.report.push(String::new());
self.report.push(format!("Destination ({label}):"));
}
}
// Disk Details
self.report.push(format!(
"... {:<8} {:>11} {:<4} {:<4} {}",
"Disk ID", "Size", "Conn", "Type", "Model (Serial)"
));
self.report.push(format!("... {}", disk.description));
self.report.push(String::new());
self.report.push(format!(
"... {:<8} {:>11} {:<7} {}",
"Part ID", "Size", "(FS)", "\"Label\""
));
disk.parts_description.iter().for_each(|desc| {
self.report.push(format!("... {desc}"));
});
if disk.parts_description.is_empty() {
self.report.push(String::from("... -None-"));
};
}
}