Skip to content

Commit

Permalink
fixed for NaN in exponential and improvements + bson
Browse files Browse the repository at this point in the history
  • Loading branch information
cvkem committed Apr 21, 2024
1 parent 4c8f2b3 commit 49658b0
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Cargo.lock
# ignore json in the root
*.csv
*.json
*.bson
*.bincode
*.txt
*.mermaid
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"

[dependencies]
bincode = "1.3.3"
bson = "2.9.0"
chrono = "0.4.26"
clap = { version = "4.3.14", features = ["derive"] }
datetime = "0.5.2"
Expand Down
65 changes: 65 additions & 0 deletions examples/bson_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use bson::{SerializerOptions, Document};
use serde::{Serialize, Deserialize};

#[derive(Debug,Serialize,Deserialize)]
struct Bison {
name: String,
age: u16,
place: String,
phone: u16,
}


pub fn check_bson() {
let i = 5;
let bison = Bison {
name: format!("Name {}", i),
age: i as u16,
place: format!("Place {}", i),
phone: i as u16,
};

let options = SerializerOptions::builder().human_readable(false).build();
let bson = bson::to_bson_with_options(&bison, options).unwrap();
println!("{:?}", bson);

// let mut doc = Document::new();
// doc.insert("array".to_string(), bson);

// let mut buf = Vec::new();
// bson.to_writer(&mut buf).unwrap();
match bson::to_vec(&bison) {
Ok(buf) => std::fs::write("data.bson", buf).expect("Failed to create file"),
Err(err) => panic!("Failed to serialized bison.\n\tError: {err:?}")
}
}


pub fn check_bson_vec() {
let mut bisons: Vec<Bison> = Vec::with_capacity(1000);
for i in 1..3 {
bisons.push(Bison {
name: format!("Name {}", i),
age: i as u16,
place: format!("Place {}", i),
phone: i as u16,
});
}

let options = SerializerOptions::builder().human_readable(false).build();
let bson = bson::to_bson_with_options(&bisons, options).unwrap();
println!("{:?}", bson);

let mut doc = Document::new();
doc.insert("array".to_string(), bson);

let mut buf = Vec::new();
doc.to_writer(&mut buf).unwrap();

std::fs::write("data.bson", buf).expect("Failed to create file");
}


pub fn main () {
check_bson();
}
65 changes: 45 additions & 20 deletions src/main/mermaid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Parser;
use jaeger_stats::{load_viewer, utils, MermaidScope, Metric, TraceDataSet, Viewer};
use std::path::Path;

/// Parsing and analyzin}g Jaeger traces
Expand Down Expand Up @@ -48,6 +49,18 @@ fn get_numbered_lines(data: Vec<String>) -> String {
output.join("\n")
}

/// helper function used internally
fn write_diagram(folder: &Path, proc_oper: &str, diagram: String) {
let mut file_path = folder.to_path_buf();

let clean_proc_oper = proc_oper.replace(['/', '\\'], "_"); // The / is not allowed in a file-path
file_path.push(format!("{}.mermaid", clean_proc_oper));
let file_path = file_path.to_str().expect("invalid file-path");
if let Err(err) = utils::write_string_to_file(file_path, diagram) {
panic!("Writing to file '{file_path}' failed with error: {err:?}");
};
}

fn main() {
let args = Args::parse();

Expand All @@ -58,35 +71,47 @@ fn main() {
args.input,
viewer.is_time_series()
);
}
Err(err) => panic!("Reading '{}' failed with error: {err:?}", args.input),
}

match TraceDataSet::from_file(&args.input) {
Ok(trace_data_set) => {
println!("Successfully read a TraceDataSet from file {}", args.input);

println!("The edge_value: {:?}", args.edge_value);

if args.display {
let po = trace_data_set.0.get_service_oper_list();
println!("Service-Operation:\n{}\n\n", get_numbered_lines(po));

let cc = trace_data_set.0.call_chain_sorted();
println!("Call-chains:\n{}\n\n", get_numbered_lines(cc));
}

let folder = utils::current_folder();
println!("found folder = {}", folder.to_str().unwrap());
trace_data_set.write_mermaid_diagram(
&folder,
let mermaid = viewer.get_mermaid_diagram(
&args.service_oper,
to_opt_str(&args.call_chain),
args.edge_value,
args.scope,
args.compact,
)
);
write_diagram(&folder, &args.service_oper, mermaid);
}
Err(err) => panic!("Reading '{}' failed with error: {err:?}", args.input),
}

// PREVIOUS VERSION that only supported single format
// match TraceDataSet::from_file(&args.input) {
// Ok(trace_data_set) => {
// println!("Successfully read a TraceDataSet from file {}", args.input);

// println!("The edge_value: {:?}", args.edge_value);

// if args.display {
// let po = trace_data_set.0.get_service_oper_list();
// println!("Service-Operation:\n{}\n\n", get_numbered_lines(po));

// let cc = trace_data_set.0.call_chain_sorted();
// println!("Call-chains:\n{}\n\n", get_numbered_lines(cc));
// }

// let folder = utils::current_folder();
// println!("found folder = {}", folder.to_str().unwrap());
// trace_data_set.write_mermaid_diagram(
// &folder,
// &args.service_oper,
// to_opt_str(&args.call_chain),
// args.edge_value,
// args.scope,
// args.compact,
// )
// }
// Err(err) => panic!("Reading '{}' failed with error: {err:?}", args.input),
// }
}
35 changes: 28 additions & 7 deletions src/main/stitch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@ use std::path::Path;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
// List of files to be stitched
/// List of files to be stitched
#[arg(short, long, default_value_t = String::from("input.stitch"))]
stitch_list: String,

#[arg(short, long, default_value_t = String::from("stitched.csv"))]
/// The base of the outputfile
#[arg(short, long, default_value_t = String::from("stitched"))]
output: String,

#[arg(short, long, default_value_t = String::from("anomalies.csv"))]
anomalies: String,
/// The extension of the outputfile, which can be either 'json' or 'bincode' (bincode is faster, but not guaranteed to be backward portable across rust-versions)
#[arg(short, long, default_value_t = String::from("json"))]
ext: String,

/// generate a csv-output (based on the base as set by the argument output).
#[arg(short, long, default_value_t = false)]
csv_output: bool,

#[arg(short, long, default_value_t = true)]
/// Handle input-files that use ',' as floating point separator
#[arg(long, default_value_t = true)]
comma_float: bool,

#[arg(short, long, default_value_t = String::from("anomalies.csv"))]
anomalies: String,

#[arg(short, long, default_value_t = 0)]
drop_count: usize,

Expand Down Expand Up @@ -63,8 +73,19 @@ fn main() {
let stitched = Stitched::build(stitch_list, &stitch_pars);

let path = Path::new(&args.output);
stitched.write_csv(path);
stitched.to_json("stitched.bincode");

if args.csv_output {
let path = path.with_extension("csv");
println!("Writing output in CSV-format to: {}", path.display());
stitched.write_csv(&path);

}

{
let path = path.with_extension(args.ext);
println!("Writing output in machine-readable format to: {}", path.display());
stitched.to_json(&path.into_os_string().into_string().unwrap());
}

println!("Stitched output written to: '{}'", path.display());

Expand Down
4 changes: 2 additions & 2 deletions src/main/trace_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ struct Args {
#[arg(short, long, default_value_t = false)]
trace_output: bool,

/// The output-extension determines the output-types are 'json' and 'bincode' (which is also used as the file-extension).
#[arg(short, long, default_value_t = String::from("json"))]
/// The output-extension determines the output-types are 'bson', 'json' and 'bincode' (which is also used as the file-extension).
#[arg(short, long, default_value_t = String::from("bson"))]
output_ext: String,

#[arg(short, long, default_value_t = String::from(EMPTY_ARG))]
Expand Down
3 changes: 3 additions & 0 deletions src/mermaid/trace_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use std::collections::HashMap;
pub struct TracePaths(pub HashMap<LeafService, Vec<TraceData>>);

impl TracePaths {

/// build a trace-forrest of a series of TracePaths to easily see and analyze sthe common prefixes (overlap) between the different paths
fn build_trace_forrest(&self, service_oper: &str) -> TraceForrest {
let (service, oper_opt) = split_service_operation(service_oper);
// find all paths that end in this 'service' and build a trace-tree out of it (filtered on the 'operation')
Expand All @@ -49,6 +51,7 @@ impl TracePaths {
.collect::<Vec<&str>>()
.join("\n")
);
println!("\nIMPORTANT NOTE: It seems that this structure contains service/Oper data instead of just Service. The service_oper = {service_oper} which might be in the list above.");
panic!("Failure to find the paths that terminate in service '{service}'.");
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/stats/file/bson.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use bson;
use super::super::StatsRec;
use std::{fs, io};
use super::StatsRecJson;


pub fn dump_file(file_name: &str, stats: StatsRec) {
let srj: StatsRecJson = stats.into();
println!("input:\n{srj:#?}");
// let buf = bson::to_vec(&srj).unwrap();
// match fs::write(file_name, buf) {
// Ok(()) => (),
// Err(err) => panic!("failed to Serialize !!\n\tError={err:?}"),
// }

let options = bson::SerializerOptions::builder().human_readable(false).build();
let doc = bson::to_document_with_options(&srj, options).unwrap();
let f = fs::File::create(file_name).expect("Failed to open file");
let writer = io::BufWriter::new(f);
match doc.to_writer(writer) {
Ok(()) => (),
Err(err) => panic!("failed to Serialize !!\n\tError={err:?}")
}
}

2 changes: 1 addition & 1 deletion src/stats/file/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ pub fn dump_file(file_name: &str, stats: StatsRec) {
// on a large dataset to_write pretty takes 15.5 seconds while to_write takes 12 sec (so 30% extra for pretty printing to make it human readible)
match serde_json::to_writer_pretty(writer, &srj) {
Ok(()) => (),
Err(err) => panic!("failed to Serialize !! {err:?}"),
Err(err) => panic!("failed to Serialize !!\n\tError={err:?}"),
}
}
2 changes: 2 additions & 0 deletions src/stats/file/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Write the statistics to a JSON file and read them back in memory
//!
mod bincode;
mod bson;
mod json;
mod operation_stats_json;

Expand All @@ -12,6 +13,7 @@ pub use operation_stats_json::{OperationStatsJson, StatsRecJson};
pub fn write_stats(file_name: &str, stats: StatsRec, ext: &str) {
let file_name = file_name.replace(".csv", &format!(".{ext}"));
match ext {
"bson" => bson::dump_file(&file_name, stats),
"json" => json::dump_file(&file_name, stats),
"bincode" => bincode::dump_file(&file_name, stats),
unknown => panic!("Unknown output format: '{unknown}'"),
Expand Down
1 change: 1 addition & 0 deletions src/stats/file/operation_stats_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl StatsRecJson {
let ext = ext.to_str().unwrap();

let sj = match ext {
"bson" => bson::from_reader(reader)?,
"json" => serde_json::from_reader(reader)?,
"bincode" => bincode::deserialize_from(reader)?,
ext => panic!(
Expand Down
3 changes: 2 additions & 1 deletion src/stitch/api/stitched_data_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl StitchedDataSet {
impl Viewer for StitchedDataSet {
fn from_file(file_name: &str) -> Result<Box<Self>, ViewError> {
if Path::new(file_name).exists() {
info!("Trying to load the file {file_name}");
info!("Trying to load the file: '{file_name}'");

match Stitched::from_file(file_name) {
Ok(data) => {
Expand Down Expand Up @@ -111,6 +111,7 @@ impl Viewer for StitchedDataSet {
.call_chain
.iter()
.map(|(k, ccd)| {
/// TODO: it seems that we need an additional step to cummulate to Service (instead of Service_oper)
let trace_data = ccd
.iter()
.map(|ccd| {
Expand Down
2 changes: 1 addition & 1 deletion src/stitch/legacy/lineair_regr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct LegacyLinearRegression {
pub avg_growth_per_period: Option<f64>,
}

/// Implement Into directly as we do not want or need/accept the Operation from Stitched to LegacyStitched.
/// Implement Into directly as we do not want or need/accept the Operation from Stitched to LegacyStitched, thus From-trait is not needed.
impl TryInto<LinearRegression> for LegacyLinearRegression {
type Error = &'static str;

Expand Down
Loading

0 comments on commit 49658b0

Please sign in to comment.