-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cli): ♻️ add dashboard command
- Loading branch information
Showing
8 changed files
with
213 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,147 @@ | ||
// TODO: Get active ingestions along their progress | ||
// TODO: Get analisis of side-effects and when they will likely occur | ||
|
||
use itertools::Itertools; | ||
use std::collections::HashMap; | ||
use std::str::FromStr; | ||
|
||
use chrono::{Duration, Local, Utc}; | ||
use futures::stream; | ||
use futures::stream::StreamExt; | ||
use humantime::format_duration; | ||
use tabled::Table; | ||
|
||
use db::sea_orm::*; | ||
use db::sea_orm::DatabaseConnection; | ||
|
||
use crate::core::dosage::Dosage; | ||
use crate::core::ingestion::Ingestion; | ||
use crate::core::phase::PhaseClassification; | ||
use crate::service::ingestion::get_ingestion_by_id; | ||
|
||
pub async fn handle_show_dashboard(database_connection: &DatabaseConnection) { | ||
println!("Dashboard is not implemented yet."); | ||
|
||
// Show average dosage per day of each substance that was ingested | ||
let ingestions = db::ingestion::Entity::find().all(database_connection).await.unwrap(); | ||
// Fetch and map all ingestions from database | ||
let ingestions = db::ingestion::Entity::find().all(database_connection).await.unwrap(); | ||
let ingestions_stream = stream::iter(ingestions.into_iter()); | ||
|
||
let ingestions = futures::future::join_all(ingestions_stream | ||
.map(|ingestion| { | ||
let ingestion_id = ingestion.id; | ||
async move { | ||
get_ingestion_by_id(ingestion_id).await.unwrap() | ||
} | ||
}).collect::<Vec<_>>().await).await; | ||
|
||
// Group ingestions by substance name | ||
let ingestions_by_substance = ingestions | ||
let ingestions_by_substance = ingestions.clone() | ||
.into_iter() | ||
.fold(HashMap::<String, Vec<Ingestion>>::new(), |mut acc, ingestion| { | ||
let substance_name = ingestion.substance_name.clone(); | ||
let ingestion_entry = acc.entry(substance_name).or_insert(vec![]); | ||
ingestion_entry.push(ingestion); | ||
acc | ||
}); | ||
|
||
// Calculate average dosage per day per substance | ||
let average_dosage_per_substance_for_last_7_days = ingestions_by_substance.clone() | ||
.into_iter() | ||
.map(|(substance_name, ingestions)| { | ||
let total_dosage: Dosage = ingestions | ||
.iter() | ||
.map(|ingestion| ingestion.dosage) | ||
.collect::<Vec<Dosage>>() | ||
.into_iter() | ||
// Use custom summing implementation | ||
.fold(Dosage::from_str( | ||
"0.0 mg" | ||
).unwrap(), |acc, dosage| acc + dosage); | ||
|
||
let average_dosage = total_dosage / ingestions.len() as f64; | ||
(substance_name, average_dosage) | ||
}) | ||
.collect::<HashMap<String, Dosage>>(); | ||
|
||
println!("Average dosage per substance for last 7 days"); | ||
for (substance_name, average_dosage) in average_dosage_per_substance_for_last_7_days { | ||
println!("{}: {1:.0}", substance_name, average_dosage); | ||
} | ||
|
||
// Calculate total dosage per substance for last 7 days | ||
let total_dosage_per_substance_for_last_7_days = ingestions_by_substance | ||
.into_iter() | ||
.map(|(substance_name, ingestions)| { | ||
let total_dosage: Dosage = ingestions | ||
.iter() | ||
.filter(|ingestion| ingestion.ingested_at >= Utc::now() - Duration::days(7)) | ||
.map(|ingestion| ingestion.dosage) | ||
.collect::<Vec<Dosage>>() | ||
.into_iter() | ||
// Use custom summing implementation | ||
.fold(Dosage::from_str( | ||
"0.0 mg" | ||
).unwrap(), |acc, dosage| acc + dosage); | ||
|
||
(substance_name, total_dosage) | ||
}) | ||
.collect::<HashMap<String, Dosage>>(); | ||
|
||
println!("Total dosage per substance for last 7 days"); | ||
|
||
let table = Table::from_iter(total_dosage_per_substance_for_last_7_days.into_iter() | ||
.map(|(substance_name, total_dosage)| { | ||
vec![ | ||
substance_name, | ||
format!("{:.0}", total_dosage).to_string() | ||
] | ||
}) | ||
.collect::<Vec<_>>().iter().cloned()); | ||
|
||
println!("{}", table.to_string()); | ||
|
||
// Filter all ingestions to find those which are active (onset, comeup, peak, offset) | ||
let active_ingestions = ingestions | ||
.into_iter() | ||
.chunk_by(|ingestion| ingestion.substance_name.as_ref().unwrap().clone()).into_iter(); | ||
.filter(|ingestion| { | ||
let ingestion_start = ingestion.phases.get(&PhaseClassification::Onset).unwrap().start_time; | ||
let ingestion_end = ingestion.phases.get(&PhaseClassification::Offset).unwrap().end_time; | ||
let daterange = ingestion_start..ingestion_end; | ||
daterange.contains( &Local::now() ) | ||
}) | ||
.collect::<Vec<Ingestion>>(); | ||
|
||
println!("Active ingestions: {:#?}", active_ingestions.len()); | ||
|
||
// Iterate through active ingestions to print procentage of completion, | ||
// time left and other useful information such as ingestion id and substance name. | ||
|
||
active_ingestions.into_iter().for_each(|ingestion| { | ||
let ingestion_start = ingestion.phases.get(&PhaseClassification::Onset).unwrap().start_time; | ||
let ingestion_end = ingestion.phases.get(&PhaseClassification::Offset).unwrap().end_time; | ||
let now = Local::now(); | ||
let total_duration = ingestion_end - ingestion_start; | ||
let elapsed_duration = now - ingestion_start; | ||
let remaining_duration = total_duration - elapsed_duration; | ||
|
||
println!("#{} {} ({:.0}) | {}", ingestion.id, ingestion.substance_name, ingestion.dosage, format_duration(remaining_duration.to_std().unwrap())); | ||
}); | ||
|
||
// We need to create progress bar for each active ingestion and | ||
// put them all into easy-readable table allowing end-user to | ||
// quickly see how long each ingestion will last. | ||
|
||
println!("Ingestions by substance:"); | ||
// active_ingestions.into_iter().for_each(|ingestion| { | ||
// let ingestion_start = ingestion.phases.get(&PhaseClassification::Onset).unwrap().start_time; | ||
// let ingestion_end = ingestion.phases.get(&PhaseClassification::Offset).unwrap().end_time; | ||
// let now = Local::now(); | ||
// let total_duration = (ingestion_end - ingestion_start).num_seconds() as u64; | ||
// let elapsed_duration = (now - ingestion_start).num_seconds() as u64; | ||
// | ||
// println!("{}:", ingestion.substance_name); | ||
// let pb = ProgressBar::new(total_duration).with_prefix(Cow::from(format!("{}:", ingestion.substance_name))); | ||
// pb.set_style(ProgressStyle::default_bar()); | ||
// pb.set_position(elapsed_duration); | ||
// | ||
// pb.finish_and_clear(); | ||
// }); | ||
} |
3 changes: 3 additions & 0 deletions
3
apps/cli/src/cli/import/import_from_psychonautwiki_journal.ts.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub fn import_from_psychonautwiki_journal() { | ||
todo!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,54 @@ | ||
direction: left | ||
|
||
iam: { | ||
label: "Identity and Access Management (IAM)" | ||
|
||
experience: "Experience" | ||
ingestion: "Ingestion" | ||
|
||
dosage: "Dosage" | ||
phase: "Phase" | ||
|
||
account: "Account" | ||
} | ||
|
||
# Account | ||
account: "Account" | ||
subject_database: { | ||
label: "Subject" | ||
|
||
# Subject | ||
subject: "Subject" | ||
subject: "Subject" | ||
|
||
subject -> account | ||
subject -> _.iam.account: (optionally) | ||
} | ||
|
||
substance_database: { | ||
label: "Substance Database" | ||
label: "Public Substance Information" | ||
|
||
# Substance | ||
substance: "Substance" | ||
route_of_administration: "Route of Administration" | ||
route_of_administration_dosage: "Route of Administration Dosage" | ||
route_of_administration_phase: "Route of Administration Phase" | ||
dosage: "Dosage" | ||
phase: "Phase" | ||
|
||
route_of_administration -> substance | ||
route_of_administration_dosage -> route_of_administration | ||
route_of_administration_phase -> route_of_administration | ||
dosage -> route_of_administration | ||
phase -> route_of_administration | ||
} | ||
|
||
journal: { | ||
label: "Journal" | ||
|
||
ingestion: "Ingestion" | ||
ingestion_phase: "Ingestion Phase" | ||
|
||
# Route of Administration | ||
ingestion -> _.substance_database.substance | ||
ingestion -> _.subject_database.subject | ||
|
||
ingestion_phase -> ingestion | ||
} | ||
|
||
route_of_administration -> substance | ||
experience_db: { | ||
label: "Experience Dataset" | ||
|
||
experience: "Expereince" | ||
|
||
# Ingestion | ||
ingestion -> subject | ||
ingestion -> substance | ||
experience -> _.subject_database.subject: { | ||
label: "may contain" | ||
} | ||
experience -> _.journal.ingestion: { | ||
label: "may contain" | ||
} | ||
} |