journal/journal_cli/src/display.rs

130 lines
3.1 KiB
Rust
Raw Normal View History

2024-11-21 13:58:29 +00:00
use journal::{
journal::JournalType,
2024-11-24 19:22:58 +00:00
types::{
2024-11-25 23:40:36 +00:00
format_dose, Consumer, Dose, Ingestion, IngestionKind, IngestionKinds, Session,
2024-11-25 02:28:08 +00:00
SubstanceIngestion, SustenanceIngestion,
2024-11-24 19:22:58 +00:00
},
2024-11-21 13:58:29 +00:00
};
2024-11-20 19:15:07 +00:00
2024-11-24 14:26:48 +00:00
use crate::formatting::{format_ingestion_time, format_substance_ingestion_roa};
2024-11-20 19:15:07 +00:00
2024-11-24 19:22:58 +00:00
fn print_substance_ingestion(
journal: &JournalType,
ingestion: &Ingestion,
substance_ingestion: &SubstanceIngestion,
) {
let unit = journal.resolve_substance_unit(substance_ingestion).unwrap();
println!(
"Substance|{}|{}|{}|{}|{}",
substance_ingestion.substance_name,
format_dose(&substance_ingestion.dose, &unit),
format_substance_ingestion_roa(substance_ingestion, &unit),
ingestion.consumer,
format_ingestion_time(ingestion)
)
}
fn print_sustenance_ingestion(
journal: &JournalType,
ingestion: &Ingestion,
sustenance_ingestion: &SustenanceIngestion,
) {
let sustenance = journal
.get_sustenance(&sustenance_ingestion.sustenance_id)
.unwrap();
println!(
"Sustenance|{}|{} {}|{}|{}",
sustenance.name,
sustenance_ingestion.amount,
sustenance.unit,
ingestion.consumer,
format_ingestion_time(ingestion)
)
}
2024-11-25 23:40:36 +00:00
pub fn calculate_ingestion_kinds(kind: &mut IngestionKinds) {
2024-11-25 02:28:08 +00:00
//println!("{kind:#?}");
2024-11-24 19:22:58 +00:00
2024-11-25 02:28:08 +00:00
if let IngestionKind::Sustenance(sustenance) = &kind.ingestion {
2024-11-25 23:40:36 +00:00
for kinds in kind.includes.iter_mut() {
2024-11-25 02:28:08 +00:00
match &mut kinds.ingestion {
IngestionKind::Substance(ingestion) => {
ingestion.dose = Dose::new_precise(sustenance.amount) * ingestion.dose.clone();
}
IngestionKind::Sustenance(ingestion) => {
2024-11-25 23:40:36 +00:00
ingestion.amount *= sustenance.amount;
2024-11-24 19:22:58 +00:00
2024-11-25 23:40:36 +00:00
calculate_ingestion_kinds(kinds)
2024-11-24 19:22:58 +00:00
}
2024-11-25 02:28:08 +00:00
IngestionKind::Hydration { amount } => {
*amount = (sustenance.amount * *amount as f64).round() as u64
}
_ => {}
2024-11-24 19:22:58 +00:00
}
}
2024-11-25 02:28:08 +00:00
}
2024-11-24 19:22:58 +00:00
}
2024-11-25 03:28:07 +00:00
pub fn print_ingestion_kinds(
journal: &JournalType,
ingestion: &Ingestion,
kinds: &IngestionKinds,
depth: u64,
) {
for _ in 0..(depth) {
print!(" ");
}
if depth > 0 {
print!("- ");
}
2024-11-25 02:28:08 +00:00
match &kinds.ingestion {
2024-11-24 19:22:58 +00:00
IngestionKind::Unknown => {}
IngestionKind::Substance(substance_ingestion) => {
print_substance_ingestion(journal, ingestion, substance_ingestion);
}
IngestionKind::Sustenance(sustenance_ingestion) => {
print_sustenance_ingestion(journal, ingestion, sustenance_ingestion);
}
IngestionKind::Hydration { amount } => {
println!(
"Hydration|{} ml|{}|{}",
amount,
ingestion.consumer,
format_ingestion_time(ingestion)
)
}
}
2024-11-25 02:28:08 +00:00
2024-11-25 23:40:36 +00:00
for kinds in &kinds.includes {
2024-11-25 03:28:07 +00:00
print_ingestion_kinds(journal, ingestion, kinds, depth + 1);
2024-11-25 02:28:08 +00:00
}
2024-11-24 19:22:58 +00:00
}
2024-11-20 19:15:07 +00:00
pub fn print_ingestion_log(
2024-11-21 13:58:29 +00:00
journal: &JournalType,
2024-11-23 21:43:28 +00:00
session: &Session,
2024-11-20 19:15:07 +00:00
consumer_filter: Option<&Vec<Consumer>>,
) {
2024-11-25 02:28:08 +00:00
let ingestions = journal
2024-11-24 19:22:58 +00:00
.get_session_ingestions(session.id)
2024-11-25 02:28:08 +00:00
.expect("could not find ingestions for session");
for ingestion in ingestions.iter() {
2024-11-24 19:22:58 +00:00
if let Some(consumer_filter) = consumer_filter {
if !consumer_filter.contains(&ingestion.consumer) {
continue;
}
}
2024-11-25 23:40:36 +00:00
let ingestion_kinds = journal
.resolve_ingestion_kind(&ingestion.kind, true)
.unwrap();
2024-11-25 02:28:08 +00:00
println!("{ingestion_kinds:#?}");
2024-11-24 19:22:58 +00:00
2024-11-25 03:28:07 +00:00
print_ingestion_kinds(journal, ingestion, &ingestion_kinds, 0)
2024-11-20 19:15:07 +00:00
}
}