From 0dfc6a0083d4d793ae014f50999deea77740602d Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Tue, 11 Jun 2024 19:43:13 +0200 Subject: [PATCH] feat: request data from db and compare with file --- src/cli.rs | 26 ++++++++++++++++++ src/database.rs | 28 +++++++++++++++++++- src/lkrexport.rs | 7 +++++ src/main.rs | 44 +++++++++++++++++++++++++++++++ src/resources/exported-to-lkr.sql | 25 ++++++++++++++++++ src/resources/mod.rs | 2 ++ 6 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/resources/exported-to-lkr.sql diff --git a/src/cli.rs b/src/cli.rs index 34b9783..98c0d1e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -148,6 +148,32 @@ pub enum SubCommand { )] include_histo_zyto: bool, }, + #[command(about = "Abgleich zwischen LKR-Export-Protokoll und Onkostar-Datenbank")] + CheckExport { + #[arg(short = 'D', long, help = "Datenbank-Name", default_value = "onkostar")] + database: String, + #[arg( + short = 'h', + long, + help = "Datenbank-Host", + default_value = "localhost" + )] + host: String, + #[arg(short = 'P', long, help = "Datenbank-Host", default_value = "3306")] + port: u16, + #[arg( + short = 'p', + long, + help = "Passwort. Wenn nicht angegeben, wird danach gefragt" + )] + password: Option, + #[arg(short = 'u', long, help = "Benutzername")] + user: String, + #[arg(short, long, help = "LKR-Export-Protokoll-Datei")] + file: PathBuf, + #[arg(long, help = "Exportpaketnummer", default_value = "0")] + export_package: u16, + }, } fn value_is_date(value: &str) -> Result { diff --git a/src/database.rs b/src/database.rs index 6ab88e0..595d55d 100644 --- a/src/database.rs +++ b/src/database.rs @@ -24,7 +24,7 @@ use mysql::prelude::Queryable; use mysql::{params, Pool}; use crate::common::{ExportData, Icd10GroupSize}; -use crate::resources::{EXPORT_QUERY, SQL_QUERY}; +use crate::resources::{EXPORTED_TO_LKR, EXPORT_QUERY, SQL_QUERY}; pub struct DatabaseSource(String); @@ -111,4 +111,30 @@ impl DatabaseSource { Err(()) } + + pub fn exported(&self, export_id: u16) -> Result, ()> { + match Pool::new(self.0.as_str()) { + Ok(pool) => { + if let Ok(mut connection) = pool.try_get_conn(Duration::from_secs(3)) { + return match connection.exec_map( + EXPORTED_TO_LKR, + params! { + "export_id" => export_id, + }, + |(id, xml_data)| (id, xml_data), + ) { + Ok(result) => Ok(result), + Err(_) => { + return Err(()); + } + }; + } + } + Err(_) => { + return Err(()); + } + } + + Err(()) + } } diff --git a/src/lkrexport.rs b/src/lkrexport.rs index 3c38d10..e01ea6a 100644 --- a/src/lkrexport.rs +++ b/src/lkrexport.rs @@ -50,6 +50,13 @@ impl LkrExportProtocolFile { Err(()) } + + pub fn meldungen(&self) -> Vec { + self.patients + .iter() + .flat_map(|patient| patient.meldungen()) + .collect_vec() + } } pub struct Patient { diff --git a/src/main.rs b/src/main.rs index 43c6e8e..c6d9801 100644 --- a/src/main.rs +++ b/src/main.rs @@ -423,6 +423,50 @@ fn main() -> Result<(), Box> { )); }); } + SubCommand::CheckExport { + database, + host, + password, + port, + user, + file, + export_package, + } => { + let password = request_password_if_none(password); + + let _ = term.write_line( + &style(format!( + "Warte auf Daten für den LKR-Export '{}'...", + export_package + )) + .blue() + .to_string(), + ); + + let db = DatabaseSource::new(&database, &host, &password, port, &user); + + let exported_db_msg = db + .exported(export_package) + .map_err(|_e| "Fehler bei Zugriff auf die Datenbank")?; + + let xml_file_content = LkrExportProtocolFile::parse_file(file.as_path()) + .map_err(|_e| "Fehler bei Zugriff auf die Protokolldatei")?; + + let _ = term.clear_last_lines(1); + + if exported_db_msg.len() != xml_file_content.meldungen().len() { + let _ = term.write_line( + &style(format!("Nicht übereinstimmende Anzahl an Meldungen:",)) + .yellow() + .to_string(), + ); + let _ = term.write_line(&format!( + "Datenbank: {:>10}\nProtokolldatei: {:>10}", + exported_db_msg.len(), + xml_file_content.meldungen().len() + )); + } + } } Ok(()) diff --git a/src/resources/exported-to-lkr.sql b/src/resources/exported-to-lkr.sql new file mode 100644 index 0000000..5e8655b --- /dev/null +++ b/src/resources/exported-to-lkr.sql @@ -0,0 +1,25 @@ +/* + * This file is part of bzkf-rwdp-check + * + * Copyright (C) 2024 Comprehensive Cancer Center Mainfranken and contributors. + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +SELECT + id, + xml_daten +FROM lkr_meldung_export +WHERE lkr_export = :export_id OR (0 = :export_id AND lkr_export IN (SELECT MAX(lkr_export) FROM lkr_meldung_export)); \ No newline at end of file diff --git a/src/resources/mod.rs b/src/resources/mod.rs index 78c2878..5df086a 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -21,3 +21,5 @@ pub const SQL_QUERY: &str = include_str!("query.sql"); pub const EXPORT_QUERY: &str = include_str!("export.sql"); + +pub const EXPORTED_TO_LKR: &str = include_str!("exported-to-lkr.sql");