From 7bb30d89338f5f78ddac920a783b5f848c8e236a Mon Sep 17 00:00:00 2001 From: Cees Date: Sun, 21 Apr 2024 19:18:22 +0200 Subject: [PATCH] Fixed mermaid for stitched-data --- src/main/mermaid.rs | 2 +- src/mermaid/mod.rs | 1 + src/stitch/api/stitched_data_set.rs | 58 +++------------------ src/stitch/stitched.rs | 79 ++++++++++++++++++++++++++++- 4 files changed, 88 insertions(+), 52 deletions(-) diff --git a/src/main/mermaid.rs b/src/main/mermaid.rs index 567164c..67cdbab 100644 --- a/src/main/mermaid.rs +++ b/src/main/mermaid.rs @@ -83,7 +83,7 @@ fn main() { ); write_diagram(&folder, &args.service_oper, mermaid); } - Err(err) => panic!("Reading '{}' failed with error: {err:?}", args.input), + Err(err) => panic!("Reading '{}' failed with error: {err:?}\nNOTE: Raw trace-json is not a valid input. You need data as returned by either 'trace_analysis' or 'stitch'.", args.input), } // PREVIOUS VERSION that only supported single format diff --git a/src/mermaid/mod.rs b/src/mermaid/mod.rs index 8a47915..a8275e2 100644 --- a/src/mermaid/mod.rs +++ b/src/mermaid/mod.rs @@ -8,3 +8,4 @@ mod tt_utils; pub use trace_data::TraceData; pub use trace_paths::TracePaths; +pub use tt_utils::split_service_operation; diff --git a/src/stitch/api/stitched_data_set.rs b/src/stitch/api/stitched_data_set.rs index d8131e6..910569e 100644 --- a/src/stitch/api/stitched_data_set.rs +++ b/src/stitch/api/stitched_data_set.rs @@ -4,9 +4,12 @@ use super::{ utils, }; use crate::{ - mermaid, - view_api::types::{ChartDataParameters, ProcessList, Selection, Table}, - MermaidScope, Metric, TraceScope, ViewError, Viewer, + view_api::types::{ChartDataParameters, ProcessList, Selection, Table}, + MermaidScope, + Metric, + TraceScope, + ViewError, + Viewer }; use log::{error, info}; use std::{path::Path, sync::Arc}; @@ -106,54 +109,9 @@ impl Viewer for StitchedDataSet { scope: MermaidScope, compact: bool, ) -> String { - let trace_tree = self + self .current - .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| { - let count: u64 = ccd - .data - .0 - .first() - .and_then(|data| data.data_avg) - .unwrap() - .round() as u64; - let avg_duration_millis = ccd - .data - .0 - .iter() - .find(|x| x.metric == Metric::AvgDurationMillis) - .and_then(|data| data.data_avg) - .expect("avg-duration missing"); - mermaid::TraceData::new( - &ccd.full_key, - ccd.rooted, - ccd.is_leaf, - count, - //TODO: some more parameters need to be passed. - None, - avg_duration_millis, - None, - None, - None, - None, - ) - }) - .collect(); - (k.clone(), trace_data) - }) - .collect(); - mermaid::TracePaths(trace_tree).get_diagram( - service_oper, - call_chain_key, - edge_value, - scope, - compact, - ) + .get_mermaid_diagram(service_oper, call_chain_key, edge_value, scope, compact) } fn get_call_chain_chart_data( diff --git a/src/stitch/stitched.rs b/src/stitch/stitched.rs index cc79907..0279557 100644 --- a/src/stitch/stitched.rs +++ b/src/stitch/stitched.rs @@ -2,6 +2,9 @@ use std::{collections::HashMap, error::Error, fs, io}; use crate::{ string_hash, + Metric, + MermaidScope, + mermaid, utils::{self, CsvFileBuffer}, view_api::Version, ServiceOperString, StitchList, @@ -201,7 +204,6 @@ impl Stitched { if CHECK_WRITTEN_FILE { println!("the basic items are: {:?}", self.basic); - use crate::Metric; println!(" And specificaly 'NumFixes': {:?}", self.basic.0.iter().find(|stitched_line| stitched_line.metric == Metric::NumFixes)); println!("\n\nFile written, now checking whether we can read it."); @@ -462,6 +464,81 @@ impl Stitched { mem::take(&mut self.service_operation).into_iter().collect() } + /// get a mermaid diagram that depicts the this stitched dataset based on proc_oper and optionally a call-chain. + pub fn get_mermaid_diagram( + &self, + service_oper: &str, + call_chain_key: Option<&str>, + edge_value: Metric, + scope: MermaidScope, + compact: bool, + ) -> String { + // bundle all data that corresponds to the same Service (currently grouped by Service-operation) + let mut grouped_cc: HashMap<&str, Vec<&Vec<_>>> = HashMap::new(); + self + .call_chain + .iter() + .for_each(|(service_oper, v)| { + let (service, oper_opt) = mermaid::split_service_operation(service_oper); + grouped_cc + .entry(service) + .and_modify(|values| values.push(&v)) + .or_insert([v].to_vec()); + }); + + + let trace_tree = grouped_cc + .into_iter() + .map(|(service, ccd_vv)| { + // TODO: it seems that we need an additional step to cummulate to Service (instead of Service_oper) + let trace_data = ccd_vv + .into_iter() + .flat_map(|ccd_v| { + ccd_v + .iter() + .map(|ccd| { + let count: u64 = ccd + .data + .0 + .first() + .and_then(|data| data.data_avg) + .unwrap() + .round() as u64; + let avg_duration_millis = ccd + .data + .0 + .iter() + .find(|x| x.metric == Metric::AvgDurationMillis) + .and_then(|data| data.data_avg) + .expect("avg-duration missing"); + mermaid::TraceData::new( + &ccd.full_key, + ccd.rooted, + ccd.is_leaf, + count, + //TODO: some more parameters need to be passed. + None, + avg_duration_millis, + None, + None, + None, + None, + ) + }) + }) + .collect(); + (service.to_string(), trace_data) + }) + .collect(); + mermaid::TracePaths(trace_tree).get_diagram( + service_oper, + call_chain_key, + edge_value, + scope, + compact, + ) + } + // /// Take the call_chain data out of the record and return as a hashmap // pub fn call_chain_as_hashmap(&mut self) -> HashMap { // mem::take(&mut self.call_chain).into_iter().collect()