update
This commit is contained in:
parent
2de88b9beb
commit
53573c878e
|
@ -14,7 +14,8 @@ pub struct CalculatedValues {
|
|||
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;
|
||||
|
||||
for include in includes.iter_mut() {
|
||||
|
@ -25,12 +26,15 @@ pub fn calculate_sustenance_ingestion_amounts(sustenance: &mut SustenanceIngesti
|
|||
IngestionKind::Sustenance(ingestion) => {
|
||||
ingestion.amount *= sustenance.amount;
|
||||
|
||||
calculate_sustenance_ingestion_amounts(ingestion)
|
||||
if recurse {
|
||||
calculate_ingestion_amounts(include, false);
|
||||
}
|
||||
}
|
||||
IngestionKind::Hydration(hydration) => hydration.amount_ml *= sustenance.amount,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedValues {
|
||||
|
@ -41,6 +45,8 @@ pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedVal
|
|||
|
||||
while !ingestions.is_empty() {
|
||||
let current_ingestions = ingestions.clone();
|
||||
ingestions.clear();
|
||||
|
||||
for ingestion in current_ingestions {
|
||||
match ingestion {
|
||||
IngestionKind::Unknown => {}
|
||||
|
@ -68,6 +74,9 @@ pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedVal
|
|||
IngestionKind::Sustenance(sustenance) => {
|
||||
let amount = sustenance.amount;
|
||||
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() {
|
||||
match include {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#[derive(PartialEq, Default, Debug, Clone)]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub struct Nutrients {
|
||||
pub kcal: u64,
|
||||
pub kcal: f64,
|
||||
}
|
||||
|
|
|
@ -15,6 +15,9 @@ pub struct PrintSessionArgs {
|
|||
|
||||
#[arg(long, value_delimiter = ',')]
|
||||
pub consumer_filter: Option<Vec<String>>,
|
||||
|
||||
#[arg(long)]
|
||||
pub tree: bool,
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
print_ingestion_log(&journal, &session, consumer_filter.as_ref());
|
||||
print_ingestion_log(&journal, &session, consumer_filter.as_ref(), args.tree);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -2,10 +2,7 @@ use std::{collections::HashMap, fs::File};
|
|||
|
||||
use journal::{
|
||||
journal::JournalType,
|
||||
types::{
|
||||
Consumer, Dose, Ingestion, IngestionKind, Session, Substance,
|
||||
SubstanceIngestion,
|
||||
},
|
||||
types::{Consumer, Dose, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion},
|
||||
};
|
||||
use psychonaut_journal_types::ExportData;
|
||||
use rand::Rng;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use journal::{
|
||||
calculate::calculate_sustenance_ingestion_amounts,
|
||||
calculate::{calculate_ingestion_amounts, calculate_sustenance_contents},
|
||||
journal::JournalType,
|
||||
types::{
|
||||
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) {
|
||||
for _ in 0..(depth) {
|
||||
print!(" ");
|
||||
}
|
||||
pub fn print_ingestion(
|
||||
journal: &JournalType,
|
||||
mut ingestion: Ingestion,
|
||||
mut depth: u64,
|
||||
tree: bool,
|
||||
) {
|
||||
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::Substance(substance) => {
|
||||
print_substance_ingestion(journal, ingestion, substance);
|
||||
print_substance_ingestion(journal, &ingestion, &substance);
|
||||
}
|
||||
IngestionKind::Sustenance(sustenance) => {
|
||||
let mut sustenance = sustenance.clone();
|
||||
calculate_sustenance_ingestion_amounts(&mut sustenance);
|
||||
print_sustenance_ingestion(journal, &ingestion, &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;
|
||||
|
||||
for include in includes {
|
||||
print_ingestion(
|
||||
journal,
|
||||
&Ingestion {
|
||||
kind: include,
|
||||
..ingestion.clone()
|
||||
},
|
||||
depth + 1,
|
||||
)
|
||||
ingestion.kind = include;
|
||||
|
||||
print_ingestion(journal, ingestion.clone(), depth + 1, tree)
|
||||
}
|
||||
}
|
||||
}
|
||||
IngestionKind::Hydration(hydration) => {
|
||||
println!(
|
||||
"Hydration|{}|{}|{}",
|
||||
hydration,
|
||||
ingestion.consumer,
|
||||
format_ingestion_time(ingestion)
|
||||
ingestion.consumer.clone(),
|
||||
format_ingestion_time(&ingestion)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -94,22 +107,21 @@ pub fn print_ingestion_log(
|
|||
journal: &JournalType,
|
||||
session: &Session,
|
||||
consumer_filter: Option<&Vec<Consumer>>,
|
||||
tree: bool,
|
||||
) {
|
||||
let mut ingestions = journal
|
||||
let ingestions = journal
|
||||
.get_session_ingestions(session.id)
|
||||
.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 !consumer_filter.contains(&ingestion.consumer) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
ingestion.kind = journal
|
||||
.resolve_ingestion_kind(ingestion.kind.clone())
|
||||
.unwrap();
|
||||
ingestion.kind = journal.resolve_ingestion_kind(ingestion.kind).unwrap();
|
||||
|
||||
print_ingestion(journal, ingestion, 0)
|
||||
print_ingestion(journal, ingestion, 0, tree)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue