Skip to content

Commit

Permalink
refactor(cli): ♻️ add tests for ingestion analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
keinsell committed Jun 9, 2024
1 parent c51f319 commit 3612d24
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 111 deletions.
88 changes: 88 additions & 0 deletions apps/cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ statrs = "0.16.1"
clap-markdown = "0.1.3"
human-panic = "2.0.0"
clap_complete = "4.4.10"
dateless = "0.3.1"

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/src/cli/ingestion/create_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub async fn handle_create_ingestion(
if create_ingestion_command.plan {
analyze_future_ingestion(&create_ingestion_payload)
.await
.unwrap_or_else(|e| println!("Error: {}", e));
.unwrap();
return;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/cli/src/cli/ingestion/plan_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ pub async fn handle_plan_ingestion(plan_ingestion_command: PlanIngestionCommand)

analyze_future_ingestion(&create_ingestion_payload)
.await
.unwrap_or_else(|e| println!("Error: {}", e));
.unwrap();
}
10 changes: 4 additions & 6 deletions apps/cli/src/core/ingestion.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
use std::collections::HashMap;
use std::ops::Range;

use chrono::{DateTime, Local, Utc};
use chrono_humanize::HumanTime;

use crate::core::mass::Mass;
use crate::core::phase::{DurationRange, PhaseClassification};
use crate::core::route_of_administration::RouteOfAdministrationClassification;
use crate::core::route_of_administration_phase::{PhaseClassification, PhaseDuration};

pub type HumanizedTimeRange = Range<HumanTime>;

#[derive(Debug)]
pub struct IngestionPhase {
pub(crate) phase_classification: PhaseClassification,
pub(crate) duration: PhaseDuration,
pub(crate) humanized_duration: HumanizedTimeRange,
pub(crate) duration: DurationRange,
pub(crate) start_time: DateTime<Local>,
pub(crate) end_time: DateTime<Local>,
}

pub type IngestionPhases = HashMap<PhaseClassification, IngestionPhase>;
Expand All @@ -27,4 +24,5 @@ pub struct Ingestion {
pub(crate) ingested_at: DateTime<Utc>,
pub(crate) humanized_ingested_at: HumanTime,
pub(crate) dosage: Mass,
pub(crate) phases: IngestionPhases,
}
13 changes: 3 additions & 10 deletions apps/cli/src/core/mass.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use uom::fmt::DisplayStyle::Abbreviation;
use uom::si::f32::{Mass as MassUom, V};
use uom::si::fmt::QuantityArguments;
use uom::si::mass::{Dimension, gram, kilogram, milligram};
use uom::si::SI;
use uom::si::f32::Mass as MassUom;
use uom::si::mass::{gram, kilogram, milligram};

pub type Mass = MassUom;

// Implement functionality to parse Mass from string in format "1.0 kg", "1.0 kg", "50mg" "50 mg" and so on...

pub fn deserialize_mass_unit(mass_str: &str) -> Result<Mass, &'static str> {
let mut mass_str = mass_str.trim().split_whitespace();
let mut mass_str = mass_str.split_whitespace();
let mass = mass_str.next().unwrap().parse::<f32>().unwrap();
let unit = mass_str.next().unwrap();
match unit {
Expand All @@ -19,7 +16,3 @@ pub fn deserialize_mass_unit(mass_str: &str) -> Result<Mass, &'static str> {
_ => Err("Invalid unit"),
}
}

pub fn mg(mass: Mass) -> QuantityArguments<Dimension, SI<V>, V, milligram> {
return Mass::into_format_args(mass, milligram, Abbreviation);
}
2 changes: 1 addition & 1 deletion apps/cli/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod ingestion;
pub mod mass;
pub mod phase;
pub mod route_of_administration;
pub mod route_of_administration_dosage;
pub mod route_of_administration_phase;
pub mod substance;
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ impl FromStr for PhaseClassification {
}
}

pub type PhaseDuration = Range<TimeDelta>;
pub type DurationRange = Range<TimeDelta>;

#[derive(Debug, Clone)]
pub struct RouteOfAdministrationPhase {
pub struct Phase {
pub id: String,
pub route_of_administration_id: String,
pub phase_classification: PhaseClassification,
pub duration_range: PhaseDuration,
pub duration_range: DurationRange,
}
4 changes: 2 additions & 2 deletions apps/cli/src/core/route_of_administration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use serde::{Deserialize, Serialize};
use strsim::normalized_levenshtein;

use crate::core::mass::Mass;
use crate::core::phase::{Phase, PhaseClassification};
use crate::core::route_of_administration_dosage::{
DosageClassification, RouteOfAdministrationDosage,
};
use crate::core::route_of_administration_phase::{PhaseClassification, RouteOfAdministrationPhase};

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Eq, Hash)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -91,7 +91,7 @@ fn should_match_insufflated_input_with_insufflated_enum() {
}

pub type RouteOfAdministrationDosages = HashMap<DosageClassification, RouteOfAdministrationDosage>;
pub type RouteOfAdministrationPhases = HashMap<PhaseClassification, RouteOfAdministrationPhase>;
pub type RouteOfAdministrationPhases = HashMap<PhaseClassification, Phase>;

#[derive(Debug, Clone)]
pub struct RouteOfAdministration {
Expand Down
12 changes: 12 additions & 0 deletions apps/cli/src/core/substance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use crate::core::phase::Phase;
use crate::core::route_of_administration::{
RouteOfAdministration, RouteOfAdministrationClassification,
};
Expand Down Expand Up @@ -27,3 +28,14 @@ pub fn get_route_of_administration_by_classification_and_substance(
None => Err("Route of administration classification not found"),
}
}

pub fn get_phases_by_route_of_administration(
route_of_administration: &RouteOfAdministration,
) -> Vec<Phase> {
route_of_administration
.phases
.clone()
.into_iter()
.map(|phase| phase.1)
.collect()
}
4 changes: 1 addition & 3 deletions apps/cli/src/ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ pub async fn create_ingestion(db: &DatabaseConnection, create_ingestion: CreateI
}
};

analyze_future_ingestion(&create_ingestion)
.await
.unwrap_or_else(|e| println!("Error: {}", e));
analyze_future_ingestion(&create_ingestion).await.unwrap();

let ingestion_active_model: ActiveModel = ActiveModel {
id: ActiveValue::default(),
Expand Down
Loading

0 comments on commit 3612d24

Please sign in to comment.