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 10, 2024
1 parent 1557197 commit 01f812c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 15 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
51 changes: 36 additions & 15 deletions crates/turbopack-trace-server/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ impl ValueMode {
ValueMode::PersistentAllocations => ValueMode::Allocations,
ValueMode::AllocationCount => ValueMode::Allocations,
ValueMode::Count => ValueMode::Count,
ValueMode::AllocationsPerTime => ValueMode::Allocations,
ValueMode::AllocationCountPerTime => ValueMode::Allocations,
}
}

Expand Down Expand Up @@ -471,20 +473,7 @@ impl Viewer {
false,
) && search_mode
{
let mut has_results = false;
for mut result in span.search(&view_rect.query) {
has_results = true;
highlighted_spans.insert(result.id());
while let Some(parent) = result.parent() {
result = parent;
if !highlighted_spans.insert(result.id()) {
break;
}
}
}
if has_results {
highlighted_spans.insert(span.id());
} else {
if view_rect.should_filter_ref(&span, &mut highlighted_spans) {
children.last_mut().unwrap().item.filtered = true;
}
}
Expand All @@ -499,7 +488,7 @@ impl Viewer {
start,
placeholder,
view_mode,
filtered,
mut filtered,
}) = queue.pop()
{
let line = get_line(&mut lines, line_index);
Expand Down Expand Up @@ -823,6 +812,36 @@ impl Viewer {
// add children to queue
enqueue_children(children, &mut queue);

// check if we should filter based on width or count
if !filtered {
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;
}

// add span to line
line.push(LineEntry {
start,
Expand Down Expand Up @@ -898,6 +917,7 @@ impl Viewer {
}
LineEntryType::SpanGraph(graph, filtered) => {
let (category, text) = graph.nice_name();

ViewSpan {
id: graph.id().get() as u64,
start: entry.start,
Expand All @@ -913,6 +933,7 @@ impl Viewer {
}
LineEntryType::SpanBottomUp(bottom_up, filtered) => {
let (category, text) = bottom_up.nice_name();

ViewSpan {
id: bottom_up.id().get() as u64,
start: entry.start,
Expand Down

0 comments on commit 01f812c

Please sign in to comment.