diff --git a/Cargo.lock b/Cargo.lock index 5256a94..9889779 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,6 +85,17 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -322,6 +333,17 @@ dependencies = [ "cc", ] +[[package]] +name = "colored" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + [[package]] name = "concolor-override" version = "1.0.0" @@ -692,6 +714,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.1" @@ -733,7 +764,7 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.1", "libc", "windows-sys 0.45.0", ] @@ -744,7 +775,7 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.1", "io-lifetimes", "rustix 0.37.7", "windows-sys 0.48.0", @@ -1236,6 +1267,7 @@ name = "rcc" version = "0.1.0" dependencies = [ "clap", + "colored", "directories", "dotenv", "log", diff --git a/Cargo.toml b/Cargo.toml index dbbb2fd..4c55450 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] clap = { version = "4.2.1", features = ["derive"] } +colored = "2.0.0" directories = "5.0.0" dotenv = "0.15.0" log = "0.4.17" diff --git a/README.md b/README.md new file mode 100644 index 0000000..e5adffb --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +## Configuration File + +Location: .config/rcc/env +``` +USERNAME=root +PASSWORD=cidb +DB=cidb +PORT=3306 +``` + diff --git a/src/cli/cli.rs b/src/cli/command.rs similarity index 94% rename from src/cli/cli.rs rename to src/cli/command.rs index a184035..19aaebc 100644 --- a/src/cli/cli.rs +++ b/src/cli/command.rs @@ -30,5 +30,7 @@ pub enum Commands { all: bool, #[arg(short, long)] config: bool, + #[arg(short, long)] + log: bool, } } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 4f77372..9fe7961 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1 +1 @@ -pub mod cli; +pub mod command; diff --git a/src/database/db.rs b/src/database/db.rs index 6f082a2..a31955b 100644 --- a/src/database/db.rs +++ b/src/database/db.rs @@ -14,13 +14,13 @@ pub struct Db { } impl Db { - pub fn initialize() -> Self { + pub fn initialize(env: String) -> Self { let base_dir: BaseDirs = match BaseDirs::new() { Some(dirs) => dirs, None => panic!("No config folder found."), }; - let config_file_path: PathBuf = base_dir.config_dir().join("rcc/.env"); + let config_file_path: PathBuf = base_dir.config_dir().join("rcc/").join(env); match dotenv::from_path(config_file_path.as_path()) { Ok(env) => env, @@ -52,7 +52,7 @@ impl Db { }; let conn: PooledConn = match pool.get_conn() { Ok(db) => db, - Err(_) => panic!("Cannot connect to DB"), + Err(e) => panic!("Cannot connect to DB: {e}"), }; conn diff --git a/src/entity/filter_log.rs b/src/entity/filter_log.rs index 73e074e..5e459de 100644 --- a/src/entity/filter_log.rs +++ b/src/entity/filter_log.rs @@ -1,6 +1,8 @@ +use time::PrimitiveDateTime; + #[derive(Debug, PartialEq, Eq)] pub struct FilterLog { - pub run_ts: String, + pub run_ts: PrimitiveDateTime, pub error_code: String, pub error_msg: String, pub mysql_error: String, diff --git a/src/main.rs b/src/main.rs index 46903d1..031197a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use clap::Parser; -use cli::cli::{Cli, Commands}; +use cli::command::{Cli, Commands}; use database::db::Db; use mysql::PooledConn; use service::{ @@ -15,7 +15,8 @@ pub mod service; fn main() -> std::result::Result<(), Box> { let args = Cli::parse(); - let db = Db::initialize(); + let env = args.env.unwrap_or("local".to_string()); + let db = Db::initialize(env); let mut conn: PooledConn = db.get_connection(); match args.mode { @@ -31,12 +32,15 @@ fn main() -> std::result::Result<(), Box> { filter_id, config, all, + log, } => { let mut filter_service: FilterService = container::get_filter_service(&mut conn); if config { filter_service.get_filter_configs(&filter_id) - } else { + } else if all { filter_service.get_filter(&filter_id, all) + } else if log { + filter_service.get_filter_log(&filter_id) } } } diff --git a/src/repository/filter_repo.rs b/src/repository/filter_repo.rs index 89d4dad..75e27fa 100644 --- a/src/repository/filter_repo.rs +++ b/src/repository/filter_repo.rs @@ -1,6 +1,6 @@ use mysql::{params, prelude::Queryable, PooledConn}; -use crate::entity::{filter::Filter, filter_config::FilterConfig}; +use crate::entity::{filter::Filter, filter_config::FilterConfig, filter_log::FilterLog}; #[derive(Debug)] pub struct FilterRepo<'a> { @@ -12,10 +12,7 @@ impl<'a> FilterRepo<'a> { Self { db_pool } } - pub fn find_by_id( - &mut self, - filter_id: &usize, - ) -> Result, mysql::Error> { + pub fn find_by_id(&mut self, filter_id: &usize) -> Result, mysql::Error> { let stat = self.db_pool .prep( "SELECT f.filter_module_id, f.file_name, f.description, fm.filter_module_no, fm.filter_user @@ -65,4 +62,31 @@ impl<'a> FilterRepo<'a> { }, ) } + + pub(crate) fn find_filter_log( + &mut self, + filter_id: &usize, + ) -> Result, mysql::Error> { + let stat = self + .db_pool + .prep( + " + SELECT t1.run_ts, t1.error_code, t1.error_msg, t1.mysql_error + FROM filter_run_error_log t1 + WHERE t1.filter_id = :filter_id + ", + ) + .unwrap(); + + self.db_pool.exec_map( + stat, + params! {"filter_id" => filter_id}, + |(run_ts, error_code, error_msg, mysql_error)| FilterLog { + run_ts, + error_code, + error_msg, + mysql_error, + }, + ) + } } diff --git a/src/service/filter_service.rs b/src/service/filter_service.rs index 3a23297..f54e4a8 100644 --- a/src/service/filter_service.rs +++ b/src/service/filter_service.rs @@ -1,5 +1,7 @@ +use colored::Colorize; + use crate::{ - entity::{filter::Filter, filter_config::FilterConfig}, + entity::{filter::Filter, filter_config::FilterConfig, filter_log::FilterLog}, repository::filter_repo::FilterRepo, }; @@ -67,4 +69,40 @@ impl<'a> FilterService<'a> { Err(err) => panic!("{}", err), }; } + + pub fn get_filter_log(&mut self, filter_id: &usize) { + let result = self.repo.find_filter_log(filter_id); + match result { + Ok(filter_log) => { + if filter_log.is_empty() { + println!("No filter log found!"); + } else { + println!("{}", "-".repeat(150)); + filter_log.into_iter().for_each(|filter_log: FilterLog| { + println!( + "TS: {} Error Code: {}", + filter_log.run_ts, filter_log.error_code + ); + match filter_log.error_code.as_str() { + "WARNING" => { + println!("{}", filter_log.error_msg.yellow()); + } + "ERROR" => { + println!("{}", filter_log.error_msg.red()); + } + "DEBUG" => { + println!("{}", filter_log.error_msg.green()); + } + _ => { + println!("{}", filter_log.error_msg); + } + } + + println!("{}", "-".repeat(150)); + }) + } + } + Err(err) => panic!("{}", err), + } + } }