This commit is contained in:
chaos 2024-11-28 00:14:50 +00:00
parent 2de88b9beb
commit 53573c878e
5 changed files with 68 additions and 47 deletions

View file

@ -14,7 +14,8 @@ pub struct CalculatedValues {
pub hydration: HydrationIngestion, pub hydration: HydrationIngestion,
} }
pub fn calculate_sustenance_ingestion_amounts(sustenance: &mut SustenanceIngestion) { pub fn calculate_ingestion_amounts(ingestion: &mut IngestionKind, recurse: bool) {
if let IngestionKind::Sustenance(sustenance) = ingestion {
let includes = &mut sustenance.sustenance.as_mut().unwrap().includes; let includes = &mut sustenance.sustenance.as_mut().unwrap().includes;
for include in includes.iter_mut() { for include in includes.iter_mut() {
@ -25,12 +26,15 @@ pub fn calculate_sustenance_ingestion_amounts(sustenance: &mut SustenanceIngesti
IngestionKind::Sustenance(ingestion) => { IngestionKind::Sustenance(ingestion) => {
ingestion.amount *= sustenance.amount; ingestion.amount *= sustenance.amount;
calculate_sustenance_ingestion_amounts(ingestion) if recurse {
calculate_ingestion_amounts(include, false);
}
} }
IngestionKind::Hydration(hydration) => hydration.amount_ml *= sustenance.amount, IngestionKind::Hydration(hydration) => hydration.amount_ml *= sustenance.amount,
_ => {} _ => {}
} }
} }
}
} }
pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedValues { pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedValues {
@ -41,6 +45,8 @@ pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedVal
while !ingestions.is_empty() { while !ingestions.is_empty() {
let current_ingestions = ingestions.clone(); let current_ingestions = ingestions.clone();
ingestions.clear();
for ingestion in current_ingestions { for ingestion in current_ingestions {
match ingestion { match ingestion {
IngestionKind::Unknown => {} IngestionKind::Unknown => {}
@ -68,6 +74,9 @@ pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedVal
IngestionKind::Sustenance(sustenance) => { IngestionKind::Sustenance(sustenance) => {
let amount = sustenance.amount; let amount = sustenance.amount;
let mut sustenance = sustenance.sustenance.clone().unwrap(); let mut sustenance = sustenance.sustenance.clone().unwrap();
if let Some(nutrients) = sustenance.nutrients {
values.nutrients.kcal += nutrients.kcal * amount;
}
for include in sustenance.includes.iter_mut() { for include in sustenance.includes.iter_mut() {
match include { match include {

View file

@ -1,5 +1,5 @@
#[derive(PartialEq, Default, Debug, Clone)] #[derive(PartialEq, Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)] #[derive(serde::Serialize, serde::Deserialize)]
pub struct Nutrients { pub struct Nutrients {
pub kcal: u64, pub kcal: f64,
} }

View file

@ -15,6 +15,9 @@ pub struct PrintSessionArgs {
#[arg(long, value_delimiter = ',')] #[arg(long, value_delimiter = ',')]
pub consumer_filter: Option<Vec<String>>, pub consumer_filter: Option<Vec<String>>,
#[arg(long)]
pub tree: bool,
} }
pub fn parse_consumer_filter(consumer_filter: Option<Vec<String>>) -> Option<Vec<Consumer>> { pub fn parse_consumer_filter(consumer_filter: Option<Vec<String>>) -> Option<Vec<Consumer>> {
@ -48,7 +51,7 @@ pub fn print_session(args: &PrintSessionArgs) -> Result<(), Box<dyn std::error::
let consumer_filter = parse_consumer_filter(args.consumer_filter.clone()); let consumer_filter = parse_consumer_filter(args.consumer_filter.clone());
print_ingestion_log(&journal, &session, consumer_filter.as_ref()); print_ingestion_log(&journal, &session, consumer_filter.as_ref(), args.tree);
Ok(()) Ok(())
} }

View file

@ -2,10 +2,7 @@ use std::{collections::HashMap, fs::File};
use journal::{ use journal::{
journal::JournalType, journal::JournalType,
types::{ types::{Consumer, Dose, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion},
Consumer, Dose, Ingestion, IngestionKind, Session, Substance,
SubstanceIngestion,
},
}; };
use psychonaut_journal_types::ExportData; use psychonaut_journal_types::ExportData;
use rand::Rng; use rand::Rng;

View file

@ -1,5 +1,5 @@
use journal::{ use journal::{
calculate::calculate_sustenance_ingestion_amounts, calculate::{calculate_ingestion_amounts, calculate_sustenance_contents},
journal::JournalType, journal::JournalType,
types::{ types::{
format_dose, Consumer, Ingestion, IngestionKind, Session, SubstanceIngestion, format_dose, Consumer, Ingestion, IngestionKind, Session, SubstanceIngestion,
@ -47,44 +47,57 @@ fn print_sustenance_ingestion(
) )
} }
pub fn print_ingestion(journal: &JournalType, ingestion: &Ingestion, depth: u64) { pub fn print_ingestion(
for _ in 0..(depth) { journal: &JournalType,
print!(" "); mut ingestion: Ingestion,
} mut depth: u64,
tree: bool,
) {
if depth > 0 { if depth > 0 {
print!("- "); print!("{}- ", " ".repeat(depth as usize));
}
if depth == 0 {
calculate_ingestion_amounts(&mut ingestion.kind, true);
} }
match &ingestion.kind { match ingestion.kind.clone() {
IngestionKind::Unknown => {} IngestionKind::Unknown => {}
IngestionKind::Substance(substance) => { IngestionKind::Substance(substance) => {
print_substance_ingestion(journal, ingestion, substance); print_substance_ingestion(journal, &ingestion, &substance);
} }
IngestionKind::Sustenance(sustenance) => { IngestionKind::Sustenance(sustenance) => {
let mut sustenance = sustenance.clone(); print_sustenance_ingestion(journal, &ingestion, &sustenance);
calculate_sustenance_ingestion_amounts(&mut sustenance);
print_sustenance_ingestion(journal, ingestion, &sustenance); if depth == 0 {
depth += 2;
let values = calculate_sustenance_contents(sustenance.clone());
print!("{}", " ".repeat(depth as usize));
println!("Nutrients:");
print!("{}", " ".repeat((depth + 1) as usize));
println!("kcal: {}", values.nutrients.kcal);
print!("{}", " ".repeat(depth as usize));
println!("Includes:");
}
if tree {
let includes = sustenance.sustenance.unwrap().includes; let includes = sustenance.sustenance.unwrap().includes;
for include in includes { for include in includes {
print_ingestion( ingestion.kind = include;
journal,
&Ingestion { print_ingestion(journal, ingestion.clone(), depth + 1, tree)
kind: include, }
..ingestion.clone()
},
depth + 1,
)
} }
} }
IngestionKind::Hydration(hydration) => { IngestionKind::Hydration(hydration) => {
println!( println!(
"Hydration|{}|{}|{}", "Hydration|{}|{}|{}",
hydration, hydration,
ingestion.consumer, ingestion.consumer.clone(),
format_ingestion_time(ingestion) format_ingestion_time(&ingestion)
) )
} }
} }
@ -94,22 +107,21 @@ pub fn print_ingestion_log(
journal: &JournalType, journal: &JournalType,
session: &Session, session: &Session,
consumer_filter: Option<&Vec<Consumer>>, consumer_filter: Option<&Vec<Consumer>>,
tree: bool,
) { ) {
let mut ingestions = journal let ingestions = journal
.get_session_ingestions(session.id) .get_session_ingestions(session.id)
.expect("could not find ingestions for session"); .expect("could not find ingestions for session");
for ingestion in ingestions.iter_mut() { for mut ingestion in ingestions.into_iter() {
if let Some(consumer_filter) = consumer_filter { if let Some(consumer_filter) = consumer_filter {
if !consumer_filter.contains(&ingestion.consumer) { if !consumer_filter.contains(&ingestion.consumer) {
continue; continue;
} }
} }
ingestion.kind = journal ingestion.kind = journal.resolve_ingestion_kind(ingestion.kind).unwrap();
.resolve_ingestion_kind(ingestion.kind.clone())
.unwrap();
print_ingestion(journal, ingestion, 0) print_ingestion(journal, ingestion, 0, tree)
} }
} }