Skip to content

Commit

Permalink
add ability to filter by value and occurences
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed May 22, 2024
1 parent 19d73f2 commit 003b9b0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 35 deletions.
42 changes: 42 additions & 0 deletions crates/turbopack-trace-server/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{
collections::HashSet,
net::{TcpListener, TcpStream},
num::NonZeroUsize,
sync::{Arc, Mutex},
thread::spawn,
};
Expand All @@ -9,6 +11,7 @@ use serde::{Deserialize, Serialize};
use tungstenite::{accept, Message};

use crate::{
span_ref::SpanRef,
store::SpanId,
store_container::StoreContainer,
u64_string,
Expand Down Expand Up @@ -80,6 +83,19 @@ pub struct SpanViewEvent {
pub id: Option<SpanId>,
}

#[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 {
Expand All @@ -91,6 +107,30 @@ pub struct ViewRect {
pub query: String,
pub view_mode: String,
pub value_mode: String,
pub value_filter: Option<Filter>,
pub count_filter: Option<Filter>,
}

impl ViewRect {
pub fn should_filter_ref(
&self,
span: &SpanRef,
highlighted_spans: &mut HashSet<NonZeroUsize>,
) -> bool {
let mut has_results = false;
for mut result in span.search(&self.query) {
has_results = true;
highlighted_spans.insert(result.id());
while let Some(parent) = result.parent() {
result = parent;
if !highlighted_spans.insert(result.id()) {
break;
}
}
}

!has_results
}
}

struct ConnectionState {
Expand Down Expand Up @@ -130,6 +170,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,
}));
Expand Down
85 changes: 50 additions & 35 deletions crates/turbopack-trace-server/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,21 +474,8 @@ impl Viewer {
}),
) && search_mode
{
let mut has_results = false;
for mut result in span.search(query) {
has_results = true;
highlighted_spans.insert(result.id());
while let Some(parent) = result.parent() {
result = parent;
if !highlighted_span_parents.insert(result.id()) {
break;
}
}
}
if has_results {
highlighted_spans.insert(span.id());
} else {
children.last_mut().unwrap().item.filtered = None;
if view_rect.should_filter_ref(&span, &mut highlighted_spans) {
children.last_mut().unwrap().item.filtered = true;

Check failure on line 478 in crates/turbopack-trace-server/src/viewer.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

mismatched types

Check failure on line 478 in crates/turbopack-trace-server/src/viewer.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

mismatched types
}
}
}
Expand Down Expand Up @@ -542,7 +529,7 @@ impl Viewer {
start,
placeholder,
view_mode,
filtered,
mut filtered,
}) = queue.pop()
{
let line = get_line(&mut lines, line_index);
Expand Down Expand Up @@ -900,26 +887,54 @@ impl Viewer {
// add children to queue
enqueue_children(children, &mut queue);

if !skipped_by_focus {
// add span to line
line.push(LineEntry {
start,
width,
secondary,
ty: match span {
QueueItem::Span(span) => LineEntryType::Span { span, filtered },
QueueItem::SpanGraph(span_graph) => {
LineEntryType::SpanGraph(span_graph, filtered)
}
QueueItem::SpanBottomUp(bottom_up) => {
LineEntryType::SpanBottomUp(bottom_up, filtered)
}
QueueItem::SpanBottomUpSpan(bottom_up_span) => {
LineEntryType::SpanBottomUpSpan(bottom_up_span, filtered)
}
},
});
// check if we should filter based on width or count
if !filtered {

Check failure on line 891 in crates/turbopack-trace-server/src/viewer.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

cannot apply unary operator `!` to type `std::option::Option<FilterMode>`

Check failure on line 891 in crates/turbopack-trace-server/src/viewer.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

cannot apply unary operator `!` to type `std::option::Option<FilterMode>`
let count = match &span {
QueueItem::Span(_) => 1,
QueueItem::SpanGraph(span_graph) => span_graph.count(),
QueueItem::SpanBottomUp(bottom_up) => bottom_up.count(),
QueueItem::SpanBottomUpSpan(_) => 1,
};

let emit = 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,
})
.unwrap_or(true);

let emit2 = 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,
})
.unwrap_or(true);

filtered = !emit || !emit2;

Check failure on line 917 in crates/turbopack-trace-server/src/viewer.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

mismatched types

Check failure on line 917 in crates/turbopack-trace-server/src/viewer.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

mismatched types
}

// add span to line
line.push(LineEntry {
start,
width,
secondary,
ty: match span {
QueueItem::Span(span) => LineEntryType::Span { span, filtered },
QueueItem::SpanGraph(span_graph) => {
LineEntryType::SpanGraph(span_graph, filtered)
}
QueueItem::SpanBottomUp(bottom_up) => {
LineEntryType::SpanBottomUp(bottom_up, filtered)
}
QueueItem::SpanBottomUpSpan(bottom_up_span) => {
LineEntryType::SpanBottomUpSpan(bottom_up_span, filtered)
}
},
});
}
}

Expand Down

0 comments on commit 003b9b0

Please sign in to comment.