From 0d4c1880ee2bf4e1c8bafefca98dd0829f607a3c Mon Sep 17 00:00:00 2001 From: Alexander Lyon Date: Tue, 4 Jun 2024 13:45:12 +0100 Subject: [PATCH] [turbo trace] add ability to filter by value and occurences (vercel/turbo#7674) ### Description This adds extra filtering options to the trace server to limit by some concrete value and occurences. There is an accompanying PR for the trace-viewer to add filtering options to the web UI Closes TURBO-2575 --- crates/turbopack-trace-server/src/server.rs | 17 +++++++++++++ crates/turbopack-trace-server/src/viewer.rs | 28 ++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/crates/turbopack-trace-server/src/server.rs b/crates/turbopack-trace-server/src/server.rs index dc1a898531257..3b42d5f3f667a 100644 --- a/crates/turbopack-trace-server/src/server.rs +++ b/crates/turbopack-trace-server/src/server.rs @@ -80,6 +80,19 @@ pub struct SpanViewEvent { pub id: Option, } +#[derive(Serialize, Deserialize, Debug)] +pub struct Filter { + pub op: Op, + pub value: u64, +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(rename_all = "snake_case")] +pub enum Op { + Gt, + Lt, +} + #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct ViewRect { @@ -91,6 +104,8 @@ pub struct ViewRect { pub query: String, pub view_mode: String, pub value_mode: String, + pub value_filter: Option, + pub count_filter: Option, } struct ConnectionState { @@ -130,6 +145,8 @@ fn handle_connection( query: String::new(), view_mode: "aggregated".to_string(), value_mode: "duration".to_string(), + count_filter: None, + value_filter: None, }, last_update_generation: 0, })); diff --git a/crates/turbopack-trace-server/src/viewer.rs b/crates/turbopack-trace-server/src/viewer.rs index 116bc27dca29f..5aeab22df7258 100644 --- a/crates/turbopack-trace-server/src/viewer.rs +++ b/crates/turbopack-trace-server/src/viewer.rs @@ -542,7 +542,7 @@ impl Viewer { start, placeholder, view_mode, - filtered, + mut filtered, }) = queue.pop() { let line = get_line(&mut lines, line_index); @@ -900,7 +900,33 @@ impl Viewer { // add children to queue enqueue_children(children, &mut queue); + // check if we should filter based on width or count if !skipped_by_focus { + let count = match &span { + QueueItem::Span(_) => 1, + QueueItem::SpanGraph(span_graph) => span_graph.count(), + QueueItem::SpanBottomUp(bottom_up) => bottom_up.count(), + QueueItem::SpanBottomUpSpan(_) => 1, + }; + + if let Some(false) = view_rect.count_filter.as_ref().map(|filter| match filter + .op + { + crate::server::Op::Gt => count > filter.value as usize, + crate::server::Op::Lt => count < filter.value as usize, + }) { + filtered = Some(FilterMode::SelectedItem) + } + + if let Some(false) = view_rect.value_filter.as_ref().map(|filter| match filter + .op + { + crate::server::Op::Gt => width > filter.value, + crate::server::Op::Lt => width < filter.value, + }) { + filtered = Some(FilterMode::SelectedItem) + } + // add span to line line.push(LineEntry { start,