update
This commit is contained in:
parent
5e4a4cc54f
commit
f711240faa
|
@ -1,102 +1,72 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
ops::{Add, Mul},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::types::{Dose, HydrationIngestion, IngestionKind, Nutrients, SubstanceIngestion};
|
||||
use crate::types::{
|
||||
AdministrationRoute, Dose, HydrationIngestion, IngestionKind, Nutrients, SubstanceIngestion,
|
||||
SustenanceIngestion,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
|
||||
pub struct CalculatedValues {
|
||||
pub doses: HashMap<String, Dose>,
|
||||
pub substances: HashMap<String, SubstanceIngestion>,
|
||||
pub nutrients: Nutrients,
|
||||
pub hydration: HydrationIngestion,
|
||||
}
|
||||
|
||||
impl Add<CalculatedValues> for CalculatedValues {
|
||||
type Output = CalculatedValues;
|
||||
pub fn calculate_sustenance_ingestion_amounts(sustenance: &mut SustenanceIngestion) {
|
||||
let includes = &mut sustenance.sustenance.as_mut().unwrap().includes;
|
||||
|
||||
fn add(self, rhs: CalculatedValues) -> Self::Output {
|
||||
let mut doses = self.doses.clone();
|
||||
let mut nutrients = self.nutrients.clone();
|
||||
let mut hydration = self.hydration.clone();
|
||||
|
||||
for (substance_id, substance_dose) in rhs.doses.iter() {
|
||||
let dose = self
|
||||
.doses
|
||||
.get(substance_id)
|
||||
.cloned()
|
||||
.or(Some(Dose::new_precise(0.0)))
|
||||
.unwrap();
|
||||
|
||||
doses.insert(substance_id.clone(), dose + substance_dose.clone());
|
||||
for include in includes.iter_mut() {
|
||||
match include {
|
||||
IngestionKind::Substance(ingestion) => {
|
||||
ingestion.dose = Dose::new_precise(sustenance.amount) * ingestion.dose.clone();
|
||||
}
|
||||
IngestionKind::Sustenance(ingestion) => {
|
||||
ingestion.amount *= sustenance.amount;
|
||||
|
||||
nutrients.kcal += rhs.nutrients.kcal;
|
||||
hydration.amount_ml += rhs.hydration.amount_ml;
|
||||
|
||||
CalculatedValues {
|
||||
doses,
|
||||
nutrients,
|
||||
hydration,
|
||||
calculate_sustenance_ingestion_amounts(ingestion)
|
||||
}
|
||||
IngestionKind::Hydration(hydration) => {
|
||||
hydration.amount_ml *= sustenance.amount
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<f64> for CalculatedValues {
|
||||
type Output = CalculatedValues;
|
||||
|
||||
fn mul(self, rhs: f64) -> Self::Output {
|
||||
let mut doses = self.doses.clone();
|
||||
let mut nutrients = self.nutrients.clone();
|
||||
let mut hydration = self.hydration.clone();
|
||||
|
||||
for (_substance_id, substance_dose) in doses.iter_mut() {
|
||||
*substance_dose = substance_dose.clone() * Dose::new_precise(rhs);
|
||||
}
|
||||
|
||||
nutrients.kcal = (rhs * nutrients.kcal as f64).round() as u64;
|
||||
hydration.amount_ml *= rhs;
|
||||
|
||||
CalculatedValues {
|
||||
doses,
|
||||
nutrients,
|
||||
hydration,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CalculatedValues {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn calculate_ingestion_kind(kind: IngestionKind) -> CalculatedValues {
|
||||
let mut values = CalculatedValues::new();
|
||||
pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedValues {
|
||||
let mut values = CalculatedValues::default();
|
||||
|
||||
let mut ingestions: Vec<IngestionKind> = Vec::new();
|
||||
ingestions.push(kind);
|
||||
ingestions.push(IngestionKind::Sustenance(root));
|
||||
|
||||
while !ingestions.is_empty() {
|
||||
let current_ingestions = ingestions.clone();
|
||||
for kind in current_ingestions {
|
||||
match kind {
|
||||
for ingestion in current_ingestions {
|
||||
match ingestion {
|
||||
IngestionKind::Unknown => {}
|
||||
IngestionKind::Substance(substance) => {
|
||||
let dose = values
|
||||
.doses
|
||||
let substance_ingestion = values
|
||||
.substances
|
||||
.get(&substance.id)
|
||||
.cloned()
|
||||
.or(Some(Dose::new_precise(0.0)))
|
||||
.unwrap();
|
||||
.unwrap_or(SubstanceIngestion {
|
||||
id: substance.id.clone(),
|
||||
substance: None,
|
||||
dose: Dose::new_precise(0.0),
|
||||
custom_unit_id: None,
|
||||
roa: AdministrationRoute::Oral,
|
||||
stomach_fullness: None,
|
||||
});
|
||||
|
||||
values
|
||||
.doses
|
||||
.insert(substance.id.clone(), dose + substance.dose.clone());
|
||||
values.substances.insert(
|
||||
substance.id.clone(),
|
||||
SubstanceIngestion {
|
||||
dose: substance_ingestion.dose.clone() + substance.dose,
|
||||
..substance_ingestion
|
||||
},
|
||||
);
|
||||
}
|
||||
IngestionKind::Sustenance(sustenance) => {
|
||||
let amount = sustenance.amount;
|
||||
|
|
|
@ -4,8 +4,7 @@ use chrono::Utc;
|
|||
use rand::Rng;
|
||||
|
||||
use crate::types::{
|
||||
CustomUnit, Dose, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion, Sustenance,
|
||||
SustenanceIngestion, Unit,
|
||||
CustomUnit, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion, Sustenance, Unit,
|
||||
};
|
||||
|
||||
use super::{Journal, JournalIntegrityChecks, JournalTrait, JournalType};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::{from_unix_millis, AdministrationRoute, Dose};
|
||||
use super::{AdministrationRoute, Dose};
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
#[derive(Default, PartialEq, Debug, Clone)]
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::{collections::HashMap, fs::File};
|
|||
use journal::{
|
||||
journal::JournalType,
|
||||
types::{
|
||||
Consumer, CustomUnit, Dose, Ingestion, IngestionKind, Session, Substance,
|
||||
Consumer, Dose, Ingestion, IngestionKind, Session, Substance,
|
||||
SubstanceIngestion,
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use journal::{
|
||||
calculate::calculate_sustenance_ingestion_amounts,
|
||||
journal::JournalType,
|
||||
types::{
|
||||
format_dose, Consumer, Ingestion, IngestionKind, Session, SubstanceIngestion,
|
||||
|
@ -57,17 +58,21 @@ pub fn print_ingestion(journal: &JournalType, ingestion: &Ingestion, depth: u64)
|
|||
match &ingestion.kind {
|
||||
IngestionKind::Unknown => {}
|
||||
IngestionKind::Substance(substance) => {
|
||||
print_substance_ingestion(journal, ingestion, &substance);
|
||||
print_substance_ingestion(journal, ingestion, substance);
|
||||
}
|
||||
IngestionKind::Sustenance(sustenance) => {
|
||||
print_sustenance_ingestion(journal, ingestion, sustenance);
|
||||
let sustenance = sustenance.sustenance.as_ref().unwrap();
|
||||
let mut sustenance = sustenance.clone();
|
||||
calculate_sustenance_ingestion_amounts(&mut sustenance);
|
||||
|
||||
for include in &sustenance.includes {
|
||||
print_sustenance_ingestion(journal, ingestion, &sustenance);
|
||||
|
||||
let includes = sustenance.sustenance.unwrap().includes;
|
||||
|
||||
for include in includes {
|
||||
print_ingestion(
|
||||
journal,
|
||||
&Ingestion {
|
||||
kind: include.clone(),
|
||||
kind: include,
|
||||
..ingestion.clone()
|
||||
},
|
||||
depth + 1,
|
||||
|
@ -105,8 +110,6 @@ pub fn print_ingestion_log(
|
|||
.resolve_ingestion_kind(ingestion.kind.clone())
|
||||
.unwrap();
|
||||
|
||||
//let calculated_values = calculate_ingestion_kinds(ingestion_kinds);
|
||||
|
||||
print_ingestion(journal, ingestion, 0)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue