Skip to content

Commit

Permalink
feat(counter):Use crate(counter) instead of util::counter.
Browse files Browse the repository at this point in the history
  • Loading branch information
zzkluck committed Nov 17, 2023
1 parent aa1d9e1 commit 3668b92
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 64 deletions.
78 changes: 76 additions & 2 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ log = "*"
simplelog = "*"
toml = "*"
serde = {version = "*", features = ["derive"]}
clap = {version = "*", features = ["derive"]}
clap = {version = "*", features = ["derive"]}
colored = "*"
counter = "*"
198 changes: 198 additions & 0 deletions easy.log

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub struct BenchmarkArgs {

#[derive(Args, Debug)]
pub struct ParseArgs {
#[arg(short, long, value_name = "LOG_TYPE")]
pub(crate) log_type: String,
#[arg(short, long, value_name = "LOG_PATH")]
pub(crate) log_path: String,
#[arg(short, long, value_name = "CONFIG_PATH")]
pub(crate) config_path: PathBuf,
pub(crate) config_path: Option<PathBuf>,
#[arg(short, long, value_name = "METHOD")]
pub(crate) method: Option<String>,
}
29 changes: 23 additions & 6 deletions src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::log_parser::easylog::EasyLog;
use crate::log_parser::{LogParser};
use crate::utils;

fn get_parse_method(method: &Option<String>, config_path: &Path) -> Box<dyn LogParser> {
fn get_parse_method(method: &Option<String>, config_path: Option<&Path>) -> Box<dyn LogParser> {
match method {
None => { unimplemented!() }
Some(parser_type) => {
Expand All @@ -33,10 +33,6 @@ fn parse_from_loghub(parser: Box<dyn LogParser>, log_type: &str) {
get_accuracy_detail(dataset.get_event_ids(), &pl);
}

pub fn parse_command(args: ParseArgs) {
let parser = get_parse_method(&args.method, &args.config_path);
parse_from_loghub(parser, &args.log_type);
}

pub fn benchmark_command(args: BenchmarkArgs) {
println!("Benchmark enable.");
Expand All @@ -48,7 +44,28 @@ pub fn benchmark_command(args: BenchmarkArgs) {
// let log_path = format!("{}/{}_2k.log", data_root, log_type);
// let structured_path = format!("{}/{}_2k.log_structured_corrected.csv", data_root, log_type);

let parser = get_parse_method(&args.method, config_path.as_ref());
let parser = get_parse_method(&args.method, Some(config_path.as_ref()));
parse_from_loghub(parser, &log_type);
}
}

pub fn parse_command(args: ParseArgs) {
let config_path = match args.config_path {
Some(ref path) => {
Some(path.as_path())
},
None => {
let default_path = Path::new("./config.toml");
if default_path.exists() {
Some(default_path)
} else {
None
}
}
};
let parser = get_parse_method(&args.method, config_path);
let pl = parser.parse_from_file(args.log_path.as_ref());
for t in pl.templates {
println!("{t}");
}
}
58 changes: 8 additions & 50 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::{Debug};
use std::hash::Hash;
use log::{debug, trace};
use crate::log_parser::ParsedLog;
use counter::Counter;

fn comb_2(n: u64) -> u64 {
if n == 0 { panic!("Comb number should not be 0."); }
Expand All @@ -12,13 +13,6 @@ fn comb_2(n: u64) -> u64 {
n * (n - 1) / 2
}

fn counter<'a, T: Eq + Hash + 'a>(list: impl Iterator<Item=&'a T>) -> HashMap<&'a T, u64> {
let mut counter: HashMap<&T, u64> = HashMap::new();
for item in list {
*counter.entry(item).or_insert(0) += 1;
}
counter
}

pub(crate) fn get_accuracy<T1: Eq + Hash + Debug, T2: Eq + Hash + Debug>
(ground_truth: &Vec<T1>, parsed_result: &Vec<T2>) -> (f64, f64, f64, f64) {
Expand All @@ -42,9 +36,8 @@ pub(crate) fn get_accuracy<T1: Eq + Hash + Debug, T2: Eq + Hash + Debug>
let mut accurate_pairs: u64 = 0;
let mut accurate_events: u64 = 0;
for (parsed_event_id, parsed_event_cluster) in pr_counter.iter() {
let error_counter:HashMap<&T1, u64> = counter(
parsed_event_cluster.iter().map(|&idx| &ground_truth[idx])
);
let error_counter =
parsed_event_cluster.iter().map(|&idx| &ground_truth[idx]).collect::<Counter<_>>();
if error_counter.len() == 1 {
let ground_truth_event_id = error_counter.keys().next().unwrap();
if gt_counter[ground_truth_event_id].len() == pr_counter[parsed_event_id].len() {
Expand All @@ -53,7 +46,7 @@ pub(crate) fn get_accuracy<T1: Eq + Hash + Debug, T2: Eq + Hash + Debug>
}
for &count in error_counter.values() {
if count > 1 {
accurate_pairs += comb_2(count);
accurate_pairs += comb_2(count as u64);
}
}
}
Expand All @@ -80,18 +73,16 @@ pub(crate) fn get_accuracy_detail<T1: Eq + Hash + Debug>
}

for pr_event_id in pr_counter.keys() {
let error_counter = counter(
pr_counter[pr_event_id].iter().map(|&idx| &ground_truth[idx])
);
let error_counter =
pr_counter[pr_event_id].iter().map(|&idx| &ground_truth[idx]).collect::<Counter<_>>();
if error_counter.len() != 1 {
debug!("{} - {:?} -> {:?}", pr_counter[pr_event_id].len(), pr_event_id, error_counter);
trace!("{:?}", parsed_result.templates[**pr_event_id]);
}
}
for gt_event_id in gt_counter.keys() {
let error_counter = counter(
gt_counter[gt_event_id].iter().map(|&idx| &parsed_result.parsed_list[idx])
);
let error_counter =
gt_counter[gt_event_id].iter().map(|&idx| &parsed_result.parsed_list[idx]).collect::<Counter<_>>();
if error_counter.len() != 1 {
debug!("{} - {:?} -> {:?}", gt_counter[gt_event_id].len(), gt_event_id, error_counter);
for i in error_counter.keys() {
Expand Down Expand Up @@ -134,40 +125,7 @@ mod tests {
comb_2( u64::MAX);
}

#[test]
fn counter_normal_success() {
let stub: Vec<i32> = vec![1, 2, 2, 3, 3, 3, 4, 4, 4, 4];
let test_counter: HashMap<&i32, u64> = counter(stub.iter());
assert_eq!(test_counter[&1], 1);
assert_eq!(test_counter[&2], 2);
assert_eq!(test_counter[&3], 3);
assert_eq!(test_counter[&4], 4);
}

#[test]
fn counter_str_slice_success() {
let mut stub: Vec<&str> = vec!["1", "2", "2", "3", "3", "3", "4", "4", "4", "4"];
stub.sort();
let test_counter: HashMap<&&str, u64> = counter(stub.iter());
assert_eq!(test_counter[&"1"], 1);
assert_eq!(test_counter[&"2"], 2);
assert_eq!(test_counter[&"3"], 3);
assert_eq!(test_counter[&"4"], 4);
}

#[test]
fn counter_string_success() {
let stub: Vec<String> =
vec!["1", "2", "2", "3", "3", "3", "4", "4", "4", "4"]
.into_iter()
.map(|s| s.to_string())
.collect();
let test_counter: HashMap<&String, u64> = counter(stub.iter());
assert_eq!(test_counter[&"1".to_string()], 1);
assert_eq!(test_counter[&"2".to_string()], 2);
assert_eq!(test_counter[&"3".to_string()], 3);
assert_eq!(test_counter[&"4".to_string()], 4);
}

#[test]
fn get_accuracy_trivial_success() {
Expand Down
6 changes: 5 additions & 1 deletion src/log_parser/easylog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ pub struct EasyLog {
}

impl EasyLog {
pub fn new(config_path: &Path) -> Self{
pub fn new(config_path: Option<&Path>) -> Self{
if config_path.is_none() {
return EasyLog {specific:vec![], substitute:vec![]}
}
let config_path = config_path.unwrap();
let mut f = File::open(config_path)
.expect(&format!("Fail to open {}", config_path.to_str().unwrap()));
let mut buffer = String::new();
Expand Down
Loading

0 comments on commit 3668b92

Please sign in to comment.