feat: export data from database into OPAL-like CSV file

This commit is contained in:
Paul-Christian Volkmer 2024-02-28 13:15:44 +01:00
parent 97bfb383bd
commit 200a042e05
6 changed files with 197 additions and 4 deletions

View File

@ -57,3 +57,23 @@ Options:
-u, --user <USER> Benutzername
-y, --year <YEAR> Jahr der Diagnose
```
## Export aus der Onkostar-Datenbank
Die Anwendung ist in der Lage, die Spalten
* `pat_id`: Patienten-ID (optional über Parameter `--pat-id`)
* `cond_id`: Condition-ID
* `conditiondate`: Datum der Diagnose
* `condcodingcode`: Der ICD-10-Code der Diagnose
in eine CSV-Datei zum Abgleich mit der OPAL-CSV-Datei zu exportieren.
Hierbei gelten die gleichen Datenbank-Parameter wie unter [Kennzahlen aus der Onkostar-Datenbank](#kennzahlen-aus-der-onkostar-datenbank),
zusätzlich gibt es noch die folgenden Parameter:
```
Options:
--pat-id Export mit Klartext-Patienten-OD
-o, --output <OUTPUT> Ausgabedatei
```

View File

@ -59,4 +59,34 @@ pub enum SubCommand {
#[arg(short = 'y', long, help = "Jahr der Diagnose")]
year: String,
},
#[command(
about = "Erstellt eine (reduzierte) CSV-Datei zum direkten Vergleich mit der OPAL-CSV-Datei"
)]
Export {
#[arg(long, help = "Export mit Klartext-Patienten-ID")]
pat_id: bool,
#[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<String>,
#[arg(short = 'u', long, help = "Benutzername")]
user: String,
#[arg(short = 'o', long, help = "Ausgabedatei")]
output: String,
#[arg(short = 'y', long, help = "Jahr der Diagnose")]
year: String,
},
}

View File

@ -20,12 +20,25 @@
use mysql::prelude::Queryable;
use mysql::{params, Pool};
use serde::Serialize;
use crate::common::Icd10GroupSize;
use crate::resources::SQL_QUERY;
use crate::resources::{EXPORT_QUERY, SQL_QUERY};
pub struct DatabaseSource(String);
#[derive(Serialize, Debug)]
pub struct ExportData {
#[serde(rename = "pat_id")]
pat_id: Option<String>,
#[serde(rename = "cond_id")]
condition_id: String,
#[serde(rename = "condition_date")]
diagnosis_date: String,
#[serde(rename = "condcodingcode")]
icd_10_code: String,
}
impl DatabaseSource {
pub fn new(database: &str, host: &str, password: &str, port: u16, user: &str) -> Self {
let password = urlencoding::encode(password);
@ -50,8 +63,36 @@ impl DatabaseSource {
};
}
}
Err(e) => {
println!("{}", e);
Err(_) => {
return Err(());
}
}
Err(())
}
pub fn export(&self, year: &str, use_pat_id: bool) -> Result<Vec<ExportData>, ()> {
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},
|(condition_id, icd_10_code, diagnosis_date, pat_id)| ExportData {
condition_id,
icd_10_code,
diagnosis_date,
pat_id: if use_pat_id { Some(pat_id) } else { None },
},
) {
Ok(result) => Ok(result),
Err(_) => {
return Err(());
}
};
}
}
Err(_) => {
return Err(());
}
}

View File

@ -23,6 +23,7 @@ use std::path::Path;
use clap::Parser;
use console::{style, Term};
use csv::Writer;
use crate::cli::{Cli, SubCommand};
use crate::common::Icd10GroupSize;
@ -96,6 +97,52 @@ fn main() -> Result<(), Box<dyn Error>> {
print_items(&items);
}
SubCommand::Export {
pat_id,
database,
host,
password,
port,
user,
output,
year,
} => {
let password = if let Some(password) = password {
password
} else {
let password = dialoguer::Password::new()
.with_prompt("Password")
.interact()
.unwrap_or_default();
let _ = term.clear_last_lines(1);
password
};
let year = if year.len() == 4 {
year
} else {
format!("2{:0>3}", year)
};
let _ = term.write_line(
&style(format!("Warte auf Daten für das Diagnosejahr {}...", year))
.blue()
.to_string(),
);
let db = DatabaseSource::new(&database, &host, &password, port, &user);
let items = db
.export(&year, pat_id)
.map_err(|_e| "Fehler bei Zugriff auf die Datenbank")?;
let _ = term.clear_last_lines(1);
let mut writer = Writer::from_path(Path::new(&output)).expect("writeable file");
items
.iter()
.for_each(|item| writer.serialize(item).unwrap());
}
}
Ok(())

53
src/resources/export.sql Normal file
View File

@ -0,0 +1,53 @@
/*
* 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
o1.cond_id,
o1.condcodingcode,
o1.diagnosedatum,
o1.pid
FROM (
SELECT DISTINCT
EXTRACTVALUE(lme.xml_daten, '//Patienten_Stammdaten/@Patient_ID') AS pid,
lme.versionsnummer,
SHA2(CONCAT('https://fhir.diz.uk-erlangen.de/identifiers/onkostar-xml-condition-id|', EXTRACTVALUE(lme.xml_daten, '//Patienten_Stammdaten/@Patient_ID'), 'condition', EXTRACTVALUE(lme.xml_daten, '//Diagnose/@Tumor_ID')), 256) AS cond_id,
SUBSTRING_INDEX(EXTRACTVALUE(lme.xml_daten, '//Primaertumor_ICD_Code'), ' ', 1) AS condcodingcode,
SUBSTRING_INDEX(EXTRACTVALUE(lme.xml_daten, '//Diagnosedatum'), ' ', 1) AS diagnosedatum,
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')
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 '%<cTNM%' OR lme.xml_daten LIKE '%<pTNM%' OR lme.xml_daten LIKE '%<Menge_Histologie>%')
) o1
LEFT OUTER JOIN (
SELECT
SHA2(CONCAT('https://fhir.diz.uk-erlangen.de/identifiers/onkostar-xml-condition-id|', EXTRACTVALUE(lme.xml_daten, '//Patienten_Stammdaten/@Patient_ID'), 'condition', EXTRACTVALUE(lme.xml_daten, '//Diagnose/@Tumor_ID')), 256) AS cond_id,
MAX(versionsnummer) AS max_version
FROM lkr_meldung_export lme
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(EXTRACTVALUE(lme.xml_daten, '//Diagnosedatum'), ' ', 1), '.', -1) = :year
GROUP BY cond_id ORDER BY cond_id
) o2
ON (o1.cond_id = o2.cond_id AND o1.versionsnummer < max_version)
WHERE diagnosejahr = :year AND o2.cond_id IS NULL;

View File

@ -19,3 +19,5 @@
*/
pub const SQL_QUERY: &str = include_str!("query.sql");
pub const EXPORT_QUERY: &str = include_str!("export.sql");