diff --git a/Cargo.toml b/Cargo.toml index b3fca21..00895b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ itertools = "0.12" mysql = "24.0" serde = { version = "1.0", features = ["derive"] } urlencoding = "2.1" +regex = "1.10" [profile.release] opt-level = "s" diff --git a/README.md b/README.md index 44863dc..6da8864 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,10 @@ Options: -y, --year Jahr der Diagnose ``` +Der zusätzliche Parameter `--ignore-exports-since` ist optional. +Wird er angegeben, werden keine Einträge mit Exportdatum ab diesem Datum verwendet. +Dies eignet sich um nachträglich Zahlen zu einem bestimmten Datum zu ermitteln. + ## Export aus der Onkostar-Datenbank Die Anwendung ist in der Lage, mit dem Befehl `export` die Spalten diff --git a/src/cli.rs b/src/cli.rs index cfe5655..1f9bddd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -19,6 +19,7 @@ */ use clap::{Parser, Subcommand}; +use regex::Regex; #[derive(Parser)] #[command(author, version, about)] @@ -58,6 +59,8 @@ pub enum SubCommand { user: String, #[arg(short = 'y', long, help = "Jahr der Diagnose")] year: String, + #[arg(long, value_parser = value_is_date, help = "Ignoriere LKR-Exporte seit Datum")] + ignore_exports_since: Option, }, #[command( about = "Erstellt eine (reduzierte) CSV-Datei zum direkten Vergleich mit der OPAL-CSV-Datei" @@ -88,6 +91,8 @@ pub enum SubCommand { output: String, #[arg(short = 'y', long, help = "Jahr der Diagnose")] year: String, + #[arg(long, value_parser = value_is_date, help = "Ignoriere LKR-Exporte seit Datum")] + ignore_exports_since: Option, }, #[command(about = "Abgleich zwischen CSV-Datei für OPAL und Onkostar-Datenbank")] Compare { @@ -116,5 +121,19 @@ pub enum SubCommand { file: String, #[arg(short = 'y', long, help = "Jahr der Diagnose")] year: String, + #[arg(long, value_parser = value_is_date, help = "Ignoriere LKR-Exporte seit Datum")] + ignore_exports_since: Option, }, } + +fn value_is_date(value: &str) -> Result { + let re = Regex::new(r"^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$").unwrap(); + if re.is_match(value) { + Ok(value.into()) + } else { + Err(format!( + "Ungültiges Datum '{}', bitte im Format 'yyyy-mm-dd' angeben", + value + )) + } +} diff --git a/src/database.rs b/src/database.rs index 4391b8e..820a65c 100644 --- a/src/database.rs +++ b/src/database.rs @@ -20,6 +20,7 @@ use mysql::prelude::Queryable; use mysql::{params, Pool}; +use std::time::Duration; use crate::common::{ExportData, Icd10GroupSize}; use crate::resources::{EXPORT_QUERY, SQL_QUERY}; @@ -33,13 +34,13 @@ impl DatabaseSource { DatabaseSource(url) } - pub fn check(&self, year: &str) -> Result, ()> { + pub fn check(&self, year: &str, ignore_exports_since: &str) -> Result, ()> { match Pool::new(self.0.as_str()) { Ok(pool) => { if let Ok(mut connection) = pool.get_conn() { return match connection.exec_map( SQL_QUERY, - params! {"year" => year}, + params! {"year" => year, "ignore_exports_since" => ignore_exports_since}, |(icd10_group, count)| Icd10GroupSize { name: icd10_group, size: count, @@ -58,13 +59,18 @@ impl DatabaseSource { Err(()) } - pub fn export(&self, year: &str, use_pat_id: bool) -> Result, ()> { + pub fn export( + &self, + year: &str, + ignore_exports_since: &str, + use_pat_id: bool, + ) -> Result, ()> { match Pool::new(self.0.as_str()) { Ok(pool) => { if let Ok(mut connection) = pool.get_conn() { return match connection.exec_map( EXPORT_QUERY, - params! {"year" => year}, + params! {"year" => year, "ignore_exports_since" => ignore_exports_since}, |(condition_id, icd_10_code, diagnosis_date, pat_id)| ExportData { condition_id, icd_10_code, diff --git a/src/main.rs b/src/main.rs index 6289fba..a52d2cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -104,6 +104,7 @@ fn main() -> Result<(), Box> { port, user, year, + ignore_exports_since, } => { let password = request_password_if_none(password); let year = sanitize_year(year); @@ -116,7 +117,7 @@ fn main() -> Result<(), Box> { let db = DatabaseSource::new(&database, &host, &password, port, &user); let items = db - .check(&year) + .check(&year, &ignore_exports_since.unwrap_or("9999-12-31".into())) .map_err(|_e| "Fehler bei Zugriff auf die Datenbank")?; let _ = term.clear_last_lines(1); @@ -132,6 +133,7 @@ fn main() -> Result<(), Box> { user, output, year, + ignore_exports_since, } => { let password = request_password_if_none(password); let year = sanitize_year(year); @@ -144,7 +146,11 @@ fn main() -> Result<(), Box> { let db = DatabaseSource::new(&database, &host, &password, port, &user); let items = db - .export(&year, pat_id) + .export( + &year, + &ignore_exports_since.unwrap_or("9999-12-31".into()), + pat_id, + ) .map_err(|_e| "Fehler bei Zugriff auf die Datenbank")?; let _ = term.clear_last_lines(1); @@ -175,6 +181,7 @@ fn main() -> Result<(), Box> { user, file, year, + ignore_exports_since, } => { let password = request_password_if_none(password); let year = sanitize_year(year); @@ -187,7 +194,11 @@ fn main() -> Result<(), Box> { let db = DatabaseSource::new(&database, &host, &password, port, &user); let db_items = db - .export(&year, pat_id) + .export( + &year, + &ignore_exports_since.unwrap_or("9999-12-31".into()), + pat_id, + ) .map_err(|_e| "Fehler bei Zugriff auf die Datenbank")?; let _ = term.clear_last_lines(1); diff --git a/src/resources/export.sql b/src/resources/export.sql index 5fb9778..fff3add 100644 --- a/src/resources/export.sql +++ b/src/resources/export.sql @@ -34,10 +34,11 @@ FROM ( SUBSTRING_INDEX(SUBSTRING_INDEX(EXTRACTVALUE(lme.xml_daten, '//Diagnosedatum'), ' ', 1), '.', -1) AS diagnosejahr FROM lkr_meldung_export lme JOIN lkr_meldung lm ON (lm.id = lme.lkr_meldung AND lme.typ <> '-1') + JOIN lkr_export le ON (le.id = lme.lkr_export) WHERE lme.xml_daten LIKE '%ICD_Version%' AND SUBSTRING_INDEX(SUBSTRING_INDEX(EXTRACTVALUE(lme.xml_daten, '//Diagnosedatum'), ' ', 1), '.', -1) = :year AND (lme.xml_daten LIKE '%%' OR lme.xml_daten LIKE '%%') - + AND le.exportiert_am < :ignore_exports_since ) o1 LEFT OUTER JOIN ( diff --git a/src/resources/query.sql b/src/resources/query.sql index ec3bb8b..b6ac58f 100644 --- a/src/resources/query.sql +++ b/src/resources/query.sql @@ -119,10 +119,11 @@ FROM ( SUBSTRING_INDEX(SUBSTRING_INDEX(EXTRACTVALUE(lme.xml_daten, '//Diagnosedatum'), ' ', 1), '.', -1) AS diagnosejahr FROM lkr_meldung_export lme JOIN lkr_meldung lm ON (lm.id = lme.lkr_meldung AND lme.typ <> '-1') + JOIN lkr_export le ON (le.id = lme.lkr_export) WHERE lme.xml_daten LIKE '%ICD_Version%' AND SUBSTRING_INDEX(SUBSTRING_INDEX(EXTRACTVALUE(lme.xml_daten, '//Diagnosedatum'), ' ', 1), '.', -1) = :year AND (lme.xml_daten LIKE '%%' OR lme.xml_daten LIKE '%%') - + AND le.exportiert_am < :ignore_exports_since ) o1 LEFT OUTER JOIN (