This commit is contained in:
chaos 2024-11-21 09:57:49 +00:00
parent 83a9b08007
commit b8128ff2f4
3 changed files with 69 additions and 67 deletions

View file

@ -1,8 +1,6 @@
use crate::types::{ use crate::types::{CustomUnitIngestionDose, StandardIngestionDose};
CustomUnitDose, CustomUnitIngestionDose, Estimation, IngestionDose, StandardIngestionDose,
};
pub fn add_standard_deviation( pub fn calculate_standard_deviation(
expectation_x: f64, expectation_x: f64,
standard_deviation_x: f64, standard_deviation_x: f64,
expectation_y: f64, expectation_y: f64,
@ -24,39 +22,10 @@ pub fn add_standard_deviation(
pub fn calulate_custom_unit_ingestion_dose( pub fn calulate_custom_unit_ingestion_dose(
dose: &CustomUnitIngestionDose, dose: &CustomUnitIngestionDose,
custom_unit_dose: &CustomUnitDose, custom_unit_dose: &CustomUnitIngestionDose,
) -> IngestionDose { ) -> StandardIngestionDose {
let estimation = match (&dose.estimation, &custom_unit_dose.estimation) { let dose: StandardIngestionDose = dose.clone().into();
( let custom_unit_dose: StandardIngestionDose = custom_unit_dose.clone().into();
&Estimation::StandardDeviation(dose_standard_deviation),
&Estimation::StandardDeviation(custom_unit_standard_deviation),
) => {
let result = add_standard_deviation(
dose.dose,
dose_standard_deviation,
custom_unit_dose.dose,
custom_unit_standard_deviation,
);
if let Some(result) = result {
Estimation::StandardDeviation(result)
} else {
Estimation::Estimate
}
}
(&Estimation::StandardDeviation(dose_standard_deviation), _) => {
Estimation::StandardDeviation(dose_standard_deviation)
}
(_, &Estimation::StandardDeviation(custom_unit_standard_deviation)) => {
Estimation::StandardDeviation(custom_unit_standard_deviation)
}
(Estimation::Precise, Estimation::Precise) => Estimation::Precise,
_ => Estimation::Estimate,
};
IngestionDose::Standard(StandardIngestionDose { custom_unit_dose * dose
dose: dose.dose * custom_unit_dose.dose,
unit: custom_unit_dose.original_unit.clone(),
contains_unknown: false,
estimation,
})
} }

View file

@ -1,7 +1,7 @@
use chrono::{DateTime, TimeZone, Utc}; use chrono::{DateTime, TimeZone, Utc};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
use std::ops::Add; use std::ops::{Add, Mul};
use std::str::FromStr; use std::str::FromStr;
pub type AdministrationRoute = psychonaut_journal_types::AdministrationRoute; pub type AdministrationRoute = psychonaut_journal_types::AdministrationRoute;
@ -42,6 +42,23 @@ impl Add for Estimation {
} }
} }
impl Mul for Estimation {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Estimation::StandardDeviation(x), Estimation::StandardDeviation(y)) => {
Estimation::StandardDeviation(x * y)
}
(Estimation::StandardDeviation(x), _) | (_, Estimation::StandardDeviation(x)) => {
Estimation::StandardDeviation(x)
}
(Estimation::Estimate, _) | (_, Estimation::Estimate) => Estimation::Estimate,
(Estimation::Precise, Estimation::Precise) => Estimation::Precise,
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct UnknownIngestionDose { pub struct UnknownIngestionDose {
pub unit: String, pub unit: String,
@ -55,19 +72,6 @@ pub struct StandardIngestionDose {
pub estimation: Estimation, pub estimation: Estimation,
} }
impl Add<UnknownIngestionDose> for StandardIngestionDose {
type Output = Self;
fn add(self, _: UnknownIngestionDose) -> Self::Output {
StandardIngestionDose {
dose: self.dose,
unit: self.unit,
contains_unknown: true,
estimation: self.estimation,
}
}
}
impl Add for StandardIngestionDose { impl Add for StandardIngestionDose {
type Output = Self; type Output = Self;
@ -81,8 +85,41 @@ impl Add for StandardIngestionDose {
} }
} }
impl From<CustomUnitDose> for StandardIngestionDose { impl Add<UnknownIngestionDose> for StandardIngestionDose {
fn from(value: CustomUnitDose) -> StandardIngestionDose { type Output = Self;
fn add(self, _rhs: UnknownIngestionDose) -> Self::Output {
StandardIngestionDose {
dose: self.dose,
unit: self.unit,
contains_unknown: true,
estimation: self.estimation,
}
}
}
impl Mul for StandardIngestionDose {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
StandardIngestionDose {
dose: self.dose * rhs.dose,
unit: self.unit,
contains_unknown: self.contains_unknown || rhs.contains_unknown,
estimation: {
let estimation = self.estimation + rhs.estimation;
if let Estimation::StandardDeviation(deviation) = estimation {
Estimation::StandardDeviation(deviation * rhs.dose)
} else {
estimation
}
},
}
}
}
impl From<CustomUnitIngestionDose> for StandardIngestionDose {
fn from(value: CustomUnitIngestionDose) -> StandardIngestionDose {
StandardIngestionDose { StandardIngestionDose {
dose: value.dose, dose: value.dose,
unit: value.original_unit, unit: value.original_unit,
@ -96,6 +133,7 @@ impl From<CustomUnitDose> for StandardIngestionDose {
pub struct CustomUnitIngestionDose { pub struct CustomUnitIngestionDose {
pub dose: f64, pub dose: f64,
pub unit: String, pub unit: String,
pub original_unit: String,
pub estimation: Estimation, pub estimation: Estimation,
pub custom_unit_id: i64, pub custom_unit_id: i64,
} }
@ -162,14 +200,6 @@ pub struct Experience {
pub ingestions: Vec<Ingestion>, pub ingestions: Vec<Ingestion>,
} }
#[derive(Debug, Clone)]
pub struct CustomUnitDose {
pub dose: f64,
pub unit: String,
pub original_unit: String,
pub estimation: Estimation,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CustomUnit { pub struct CustomUnit {
pub id: i64, pub id: i64,
@ -177,7 +207,7 @@ pub struct CustomUnit {
pub substance_name: String, pub substance_name: String,
pub administration_route: AdministrationRoute, pub administration_route: AdministrationRoute,
pub dose: CustomUnitDose, pub dose: CustomUnitIngestionDose,
pub creation_time: DateTime<Utc>, pub creation_time: DateTime<Utc>,
pub is_archived: bool, pub is_archived: bool,
@ -229,10 +259,11 @@ impl Journal {
substance_name: custom_unit.substance_name, substance_name: custom_unit.substance_name,
administration_route: custom_unit.administration_route, administration_route: custom_unit.administration_route,
dose: CustomUnitDose { dose: CustomUnitIngestionDose {
dose: custom_unit.dose, dose: custom_unit.dose,
unit: custom_unit.unit, unit: custom_unit.unit,
original_unit: custom_unit.original_unit, original_unit: custom_unit.original_unit,
custom_unit_id: custom_unit.id,
estimation: if custom_unit.is_estimate { estimation: if custom_unit.is_estimate {
if let Some(standard_deviation) = if let Some(standard_deviation) =
custom_unit.estimate_standard_deviation custom_unit.estimate_standard_deviation
@ -285,6 +316,7 @@ impl Journal {
IngestionDose::CustomUnit(CustomUnitIngestionDose { IngestionDose::CustomUnit(CustomUnitIngestionDose {
dose, dose,
unit: custom_unit.dose.unit.clone(), unit: custom_unit.dose.unit.clone(),
original_unit: custom_unit.dose.original_unit.clone(),
estimation, estimation,
custom_unit_id, custom_unit_id,
}) })

View file

@ -1,7 +1,7 @@
use journal::{ use journal::{
helpers::calulate_custom_unit_ingestion_dose, helpers::calulate_custom_unit_ingestion_dose,
types::{ types::{
CustomUnit, CustomUnitDose, Estimation, Experience, Ingestion, IngestionDose, CustomUnit, CustomUnitIngestionDose, Estimation, Experience, Ingestion, IngestionDose,
StandardIngestionDose, StandardIngestionDose,
}, },
}; };
@ -12,7 +12,7 @@ pub fn format_experience_title(experience: &Experience) -> String {
pub fn format_ingestion_dose( pub fn format_ingestion_dose(
dose: &IngestionDose, dose: &IngestionDose,
custom_unit_dose: Option<&CustomUnitDose>, custom_unit_dose: Option<&CustomUnitIngestionDose>,
) -> String { ) -> String {
match dose { match dose {
IngestionDose::Unknown(dose) => format!("Unknown {}", dose.unit), IngestionDose::Unknown(dose) => format!("Unknown {}", dose.unit),
@ -37,7 +37,8 @@ pub fn format_ingestion_dose(
let ingestion_dose = calulate_custom_unit_ingestion_dose(dose, custom_unit_dose); let ingestion_dose = calulate_custom_unit_ingestion_dose(dose, custom_unit_dose);
let ingestion_dose = format_ingestion_dose(&ingestion_dose, None); let ingestion_dose =
format_ingestion_dose(&IngestionDose::Standard(ingestion_dose), None);
let dose_per_unit = format_ingestion_dose( let dose_per_unit = format_ingestion_dose(
&IngestionDose::Standard(StandardIngestionDose::from(custom_unit_dose.clone())), &IngestionDose::Standard(StandardIngestionDose::from(custom_unit_dose.clone())),