This commit is contained in:
chaos 2024-11-25 02:28:08 +00:00
parent d7fb6f147a
commit cb2b13a9cf
8 changed files with 111 additions and 116 deletions

View file

@ -1,5 +1,6 @@
use crate::types::{ use crate::types::{
CustomUnit, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion, Sustenance, Unit, CustomUnit, Ingestion, IngestionKind, IngestionKinds, Session, Substance, SubstanceIngestion,
Sustenance, Unit,
}; };
pub type JournalType = Box<dyn Journal>; pub type JournalType = Box<dyn Journal>;
@ -11,7 +12,7 @@ pub trait Journal {
fn get_session_ingestions(&self, id: i32) -> Option<Vec<Ingestion>>; fn get_session_ingestions(&self, id: i32) -> Option<Vec<Ingestion>>;
fn get_substance(&self, name: &str) -> Option<Substance>; fn get_substance(&self, name: &str) -> Option<Substance>;
fn get_sustenance(&self, id: &str) -> Option<Sustenance>; fn get_sustenance(&self, id: &str) -> Option<Sustenance>;
fn get_sustenance_ingestions(&self, id: &str) -> Option<Vec<IngestionKind>>; fn get_ingestion_kinds(&self, ingestion: &IngestionKind) -> Option<IngestionKinds>;
fn get_custom_unit(&self, id: i32) -> Option<CustomUnit>; fn get_custom_unit(&self, id: i32) -> Option<CustomUnit>;
fn create_session(&mut self, title: &str) -> Session; fn create_session(&mut self, title: &str) -> Session;

View file

@ -4,7 +4,8 @@ use chrono::Utc;
use rand::Rng; use rand::Rng;
use crate::types::{ use crate::types::{
CustomUnit, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion, Sustenance, Unit, CustomUnit, Ingestion, IngestionKind, IngestionKinds, Session, Substance, SubstanceIngestion,
Sustenance, Unit,
}; };
use super::{Journal, JournalType}; use super::{Journal, JournalType};
@ -82,32 +83,25 @@ impl Journal for JSONJournal {
self.data.sustenances.get(sustenance_id).cloned() self.data.sustenances.get(sustenance_id).cloned()
} }
fn get_sustenance_ingestions(&self, sustenance_id: &str) -> Option<Vec<IngestionKind>> { fn get_ingestion_kinds(&self, kind: &IngestionKind) -> Option<IngestionKinds> {
let sustenance = self.get_sustenance(sustenance_id)?; let mut kinds: IngestionKinds = IngestionKinds::from(kind);
let mut ingestions: Vec<IngestionKind> = Vec::new(); if let IngestionKind::Sustenance(sustenance) = kind {
for include in sustenance.includes.into_iter() { let includes = self.get_sustenance(&sustenance.sustenance_id)?.includes;
match include {
IngestionKind::Unknown => continue,
IngestionKind::Hydration { amount } => {
ingestions.push(IngestionKind::Hydration { amount })
}
IngestionKind::Substance(substance_ingestion) => {
ingestions.push(IngestionKind::Substance(substance_ingestion))
}
IngestionKind::Sustenance(sustenance_ingestion) => {
ingestions.push(IngestionKind::Sustenance(sustenance_ingestion.clone()));
ingestions.append( for include in includes.into_iter() {
&mut self if let IngestionKind::Sustenance(_) = include {
.get_sustenance_ingestions(&sustenance_ingestion.sustenance_id) kinds.ingestions.push(IngestionKinds {
.unwrap(), ingestion: include.clone(),
); ingestions: self.get_ingestion_kinds(&include).unwrap().ingestions,
});
} else {
kinds.ingestions.push(IngestionKinds::from(&include))
} }
} }
} }
Some(ingestions) Some(kinds)
} }
fn get_custom_unit(&self, id: i32) -> Option<CustomUnit> { fn get_custom_unit(&self, id: i32) -> Option<CustomUnit> {

View file

@ -1,6 +1,15 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use super::{dose::Dose, from_unix_millis, AdministrationRoute, Consumer}; use super::{dose::Dose, from_unix_millis, Consumer};
mod kind;
pub use kind::{IngestionKind, IngestionKinds};
mod substance;
pub use substance::SubstanceIngestion;
mod sustenance;
pub use sustenance::SustenanceIngestion;
#[derive(PartialEq, Default, Debug, Clone)] #[derive(PartialEq, Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)] #[derive(serde::Serialize, serde::Deserialize)]
@ -12,36 +21,6 @@ pub struct Ingestion {
pub kind: IngestionKind, pub kind: IngestionKind,
} }
#[derive(PartialEq, Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum IngestionKind {
#[default]
Unknown,
Substance(SubstanceIngestion),
Sustenance(SustenanceIngestion),
Hydration {
amount: u64,
},
}
#[derive(PartialEq, Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct SubstanceIngestion {
pub substance_name: String,
pub dose: Dose,
pub custom_unit_id: Option<i32>,
pub roa: AdministrationRoute,
pub stomach_fullness: Option<String>,
}
#[derive(PartialEq, Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct SustenanceIngestion {
pub sustenance_id: String,
pub amount: f64,
}
impl From<psychonaut_journal_types::Ingestion> for Ingestion { impl From<psychonaut_journal_types::Ingestion> for Ingestion {
fn from(ingestion: psychonaut_journal_types::Ingestion) -> Self { fn from(ingestion: psychonaut_journal_types::Ingestion) -> Self {
Ingestion { Ingestion {

View file

@ -0,0 +1,30 @@
use super::{SubstanceIngestion, SustenanceIngestion};
#[derive(PartialEq, Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum IngestionKind {
#[default]
Unknown,
Substance(SubstanceIngestion),
Sustenance(SustenanceIngestion),
Hydration {
amount: u64,
},
}
#[derive(PartialEq, Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct IngestionKinds {
pub ingestion: IngestionKind,
pub ingestions: Vec<IngestionKinds>,
}
impl From<&IngestionKind> for IngestionKinds {
fn from(value: &IngestionKind) -> Self {
IngestionKinds {
ingestion: value.clone(),
..Default::default()
}
}
}

View file

@ -0,0 +1,11 @@
use crate::types::{AdministrationRoute, Dose};
#[derive(PartialEq, Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct SubstanceIngestion {
pub substance_name: String,
pub dose: Dose,
pub custom_unit_id: Option<i32>,
pub roa: AdministrationRoute,
pub stomach_fullness: Option<String>,
}

View file

@ -0,0 +1,6 @@
#[derive(PartialEq, Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct SustenanceIngestion {
pub sustenance_id: String,
pub amount: f64,
}

View file

@ -13,7 +13,9 @@ mod consumer;
pub use consumer::Consumer; pub use consumer::Consumer;
mod ingestion; mod ingestion;
pub use ingestion::{Ingestion, IngestionKind, SubstanceIngestion, SustenanceIngestion}; pub use ingestion::{
Ingestion, IngestionKind, IngestionKinds, SubstanceIngestion, SustenanceIngestion,
};
mod substance; mod substance;
pub use substance::Substance; pub use substance::Substance;

View file

@ -1,8 +1,8 @@
use journal::{ use journal::{
journal::JournalType, journal::JournalType,
types::{ types::{
format_dose, Consumer, Dose, Ingestion, IngestionKind, Session, SubstanceIngestion, format_dose, Consumer, Dose, Ingestion, IngestionKind, IngestionKinds, Session, Substance,
SustenanceIngestion, SubstanceIngestion, SustenanceIngestion,
}, },
}; };
@ -44,55 +44,31 @@ fn print_sustenance_ingestion(
) )
} }
pub fn resolve_ingestion(journal: &JournalType, ingestion: &IngestionKind) -> Vec<IngestionKind> { pub fn resolve_ingestion_kinds(journal: &JournalType, kind: &mut IngestionKinds) {
//println!("{ingestion:?}"); //println!("{kind:#?}");
let mut ingestions: Vec<IngestionKind> = Vec::new(); if let IngestionKind::Sustenance(sustenance) = &kind.ingestion {
for kinds in kind.ingestions.iter_mut() {
match &mut kinds.ingestion {
IngestionKind::Substance(ingestion) => {
ingestion.dose = Dose::new_precise(sustenance.amount) * ingestion.dose.clone();
}
IngestionKind::Sustenance(ingestion) => {
ingestion.amount = sustenance.amount * ingestion.amount;
match &ingestion { resolve_ingestion_kinds(journal, kinds)
IngestionKind::Substance(substance) => {
ingestions.push(IngestionKind::Substance(substance.clone()));
}
IngestionKind::Hydration { amount: _ } => {
ingestions.push(ingestion.clone());
}
IngestionKind::Sustenance(sustenance) => {
let sustenance_ingestions = journal
.get_sustenance_ingestions(&sustenance.sustenance_id)
.unwrap();
for sustenance_ingestion in sustenance_ingestions.into_iter() {
match sustenance_ingestion {
IngestionKind::Substance(substance_ingestion) => {
ingestions.push(IngestionKind::Substance(SubstanceIngestion {
dose: Dose::new_precise(sustenance.amount) * substance_ingestion.dose,
..substance_ingestion
}))
}
IngestionKind::Sustenance(sustenance_ingestion) => {
ingestions.push(IngestionKind::Sustenance(SustenanceIngestion {
amount: sustenance.amount * sustenance_ingestion.amount,
..sustenance_ingestion
}))
} }
IngestionKind::Hydration { amount } => { IngestionKind::Hydration { amount } => {
ingestions.push(IngestionKind::Hydration { *amount = (sustenance.amount * *amount as f64).round() as u64
amount: (sustenance.amount * amount as f64).round() as u64,
})
} }
_ => {} _ => {}
} }
} }
} }
_ => {}
};
//println!("{ingestions:#?}");
ingestions
} }
pub fn print_ingestion(journal: &JournalType, ingestion: &Ingestion, kind: &IngestionKind) { pub fn print_ingestion_kinds(journal: &JournalType, ingestion: &Ingestion, kinds: &IngestionKinds) {
match kind { match &kinds.ingestion {
IngestionKind::Unknown => {} IngestionKind::Unknown => {}
IngestionKind::Substance(substance_ingestion) => { IngestionKind::Substance(substance_ingestion) => {
print_substance_ingestion(journal, ingestion, substance_ingestion); print_substance_ingestion(journal, ingestion, substance_ingestion);
@ -109,6 +85,10 @@ pub fn print_ingestion(journal: &JournalType, ingestion: &Ingestion, kind: &Inge
) )
} }
} }
for kinds in &kinds.ingestions {
print_ingestion_kinds(journal, ingestion, kinds);
}
} }
pub fn print_ingestion_log( pub fn print_ingestion_log(
@ -116,29 +96,21 @@ pub fn print_ingestion_log(
session: &Session, session: &Session,
consumer_filter: Option<&Vec<Consumer>>, consumer_filter: Option<&Vec<Consumer>>,
) { ) {
for ingestion in 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");
.iter()
{ for ingestion in ingestions.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;
} }
} }
match &ingestion.kind { let mut ingestion_kinds = journal.get_ingestion_kinds(&ingestion.kind).unwrap();
IngestionKind::Sustenance(_) => { println!("{ingestion_kinds:#?}");
print_ingestion(journal, ingestion, &ingestion.kind);
for kind in resolve_ingestion(journal, &ingestion.kind) { resolve_ingestion_kinds(journal, &mut ingestion_kinds);
print!("- "); print_ingestion_kinds(journal, ingestion, &ingestion_kinds)
print_ingestion(journal, ingestion, &kind)
}
}
_ => {
print_ingestion(journal, ingestion, &ingestion.kind);
}
}
} }
} }