Skip to content

Commit

Permalink
feat: print with stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
albugowy15 committed Feb 14, 2024
1 parent 7619b2d commit c435f25
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 309 deletions.
101 changes: 16 additions & 85 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "auto-frs-schedule"
version = "2.6.2"
version = "2.6.3"
edition = "2021"
description = "Automatically generate TC FRS schedule from Excel file"
authors = ["Mohamad Kholid Bughowi <[email protected]>"]
Expand All @@ -27,5 +27,3 @@ sqlx = { version = "0.7", features = [
"mysql",
] }
indicatif = "0.17.7"
log = "0.4.20"
env_logger = "0.11.1"
22 changes: 8 additions & 14 deletions src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@ use crate::db::{
repository::{many_to_many::ManyToManyRepository, Repository},
};

pub async fn clean_handler() {
let pool = match db::Database::create_connection().await {
Ok(pool) => pool,
Err(e) => {
log::error!("{}", e);
return;
}
};
log::info!("Clean up invalid foreign key");
pub async fn clean_handler() -> anyhow::Result<()> {
println!("Open db connection...");
let pool = db::Database::create_connection().await?;
println!("Clean up invalid foreign key");
let many_to_many_repo = ManyToManyRepository::new(&pool);

if let Err(e) = try_join!(
try_join!(
many_to_many_repo.drop_invalid_class_to_plan(),
many_to_many_repo.drop_invalid_class_to_lecturer()
) {
log::error!("Cleaning failed: {}", e);
return;
}
)?;
pool.close().await;
println!("Done");
Ok(())
}
75 changes: 17 additions & 58 deletions src/commands/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,84 +81,43 @@ fn compare_schedule(
(added, deleted, changed)
}

pub async fn compare_handler(file: &PathBuf, sheet: &str, outdir: &PathBuf) {
let pool = match db::Database::create_connection().await {
Ok(pool) => Arc::new(pool),
Err(e) => {
log::error!("{}", e);
return;
}
};
log::info!("Get existing schedule from DB");
pub async fn compare_handler(file: &PathBuf, sheet: &str, outdir: &PathBuf) -> anyhow::Result<()> {
println!("Open db connection...");
let pool = Arc::new(db::Database::create_connection().await?);

println!("Get existing schedule from DB");
let class_repo_schedule_task = spawn_get_schedule(&pool);
let prepare_data_task = spawn_prepare_data(&pool);
let (db_classes_res, repo_data_res) =
match tokio::try_join!(class_repo_schedule_task, prepare_data_task) {
Ok((db, repo)) => (db, repo),
Err(e) => {
log::error!("{}", e);
return;
}
};

let db_classes = match db_classes_res {
Ok(res) => res,
Err(e) => {
log::error!("Error getting db classes: {}", e);
return;
}
};
tokio::try_join!(class_repo_schedule_task, prepare_data_task)?;
let db_classes = db_classes_res?;
let repo_data = repo_data_res?;

let repo_data = match repo_data_res {
Ok(res) => res,
Err(e) => {
log::error!("Error preparing data: {}", e);
return;
}
};

log::info!("Get latest schedule from Excel");
let excel = match Excel::new(file, sheet) {
Ok(excel) => excel.with_repo_data(repo_data),
Err(e) => {
log::error!("Error opening excel file: {}", e);
return;
}
};
println!("Get latest schedule from Excel");
let excel = Excel::new(file, sheet)?.with_repo_data(repo_data);
let excel_classes: Vec<ClassFromSchedule> = excel.get_schedule();

log::info!(
println!(
"Comparing {} classes from Excel with existing schedule",
excel_classes.len()
);
let (added, deleted, changed) = compare_schedule(excel_classes, db_classes);

log::info!(
println!(
"Detected {} changed, {} added, {} deleted class",
changed.len(),
added.len(),
deleted.len()
);
log::info!("Write the result to {:?}", &outdir);

let mut writer = match OutWriter::new(outdir).await {
Ok(writer) => writer,
Err(e) => {
log::error!("{}", e);
return;
}
};

if let Err(e) = writer
println!("Write the result to {:?}", &outdir);
let mut writer = OutWriter::new(outdir).await?;
writer
.write_compare_result(&added, &changed, &deleted)
.await
{
log::error!("Error writing result: {}", e);
return;
};
.await?;

pool.close().await;
println!("Done");
Ok(())
}

#[cfg(test)]
Expand Down
23 changes: 8 additions & 15 deletions src/commands/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@ use std::path::PathBuf;

use crate::utils::excel::{Excel, FindClassSchedule};

pub async fn find_handler(file: &PathBuf, sheet: &str, subject: &str) {
log::info!("Parse class schedule from Excel");
let excel = match Excel::new(file, sheet) {
Ok(excel) => excel,
Err(e) => {
log::error!("Error opening excel file: {}", e);
return;
}
};
pub async fn find_handler(file: &PathBuf, sheet: &str, subject: &str) -> anyhow::Result<()> {
println!("Parse class schedule from Excel");
let excel = Excel::new(file, sheet)?;

let class_schedule = excel.find_schedule_from_class(subject);
log::info!("Found {} schedules for {}", class_schedule.len(), subject);
println!("Found {} schedules for {}", class_schedule.len(), subject);
for schedule in class_schedule.into_iter() {
log::info!(
println!(
"class:{}, lec_codes:{:?}, day: {}, session:{}",
schedule.class,
schedule.lecturers_code,
schedule.day,
schedule.session_start
schedule.class, schedule.lecturers_code, schedule.day, schedule.session_start
);
}
println!("Done");
Ok(())
}
Loading

0 comments on commit c435f25

Please sign in to comment.