mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-04 05:46:23 +01:00
Capture WebSocket, SSE and gRPC (#814)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
08a98d5f82
commit
1129f7aa93
32 files changed
+5228
-100
No files matched your search
@@ -32,7 +32,8 @@ pub struct FlowFilter {
|
||||
/// The method, any case.
|
||||
#[ts(optional)]
|
||||
pub method: Option<String>,
|
||||
/// A status (`404`), a class of them (`4xx`), or `error` for flows that failed.
|
||||
/// A status (`404`), a class of them (`4xx`), or `error` for flows that failed, gRPC calls
|
||||
/// that ended with a status other than OK among them.
|
||||
#[ts(optional)]
|
||||
pub status: Option<String>,
|
||||
/// At most this many flows, the newest.
|
||||
@@ -114,7 +115,9 @@ impl FlowFilter {
|
||||
}
|
||||
|
||||
/// The SQL condition for every field but `after`, `before` and `limit`, and its parameters.
|
||||
fn condition(&self) -> (String, Vec<Value>) {
|
||||
/// `grpc` says the database has gRPC statuses: one opened read-only before its migrations
|
||||
/// have run may not.
|
||||
fn condition(&self, grpc: bool) -> (String, Vec<Value>) {
|
||||
let mut sql = String::from("1");
|
||||
let mut params: Vec<Value> = Vec::new();
|
||||
for term in self.q.iter().flat_map(|q| q.split_whitespace()) {
|
||||
@@ -165,6 +168,10 @@ impl FlowFilter {
|
||||
}
|
||||
match self.status.as_deref().map(str::trim) {
|
||||
None | Some("") => {}
|
||||
// A gRPC call that ended with a status other than OK failed too.
|
||||
Some(s) if s.eq_ignore_ascii_case("error") && grpc => {
|
||||
sql.push_str(" AND (error IS NOT NULL OR coalesce(grpc_status, 0) != 0)")
|
||||
}
|
||||
Some(s) if s.eq_ignore_ascii_case("error") => sql.push_str(" AND error IS NOT NULL"),
|
||||
// A whole status or a class, not the start of one as a search term may be.
|
||||
Some(s) if s.len() == 3 => match status_condition(&s.to_ascii_lowercase(), &mut params)
|
||||
@@ -268,8 +275,13 @@ pub fn page(
|
||||
})
|
||||
}
|
||||
|
||||
/// The database has the gRPC status column, added by a later migration.
|
||||
fn has_grpc_status(conn: &rusqlite::Connection) -> bool {
|
||||
conn.prepare("SELECT grpc_status FROM flows LIMIT 0").is_ok()
|
||||
}
|
||||
|
||||
fn list_in(conn: &rusqlite::Connection, filter: &FlowFilter) -> Result<Vec<Flow>, String> {
|
||||
let (condition, mut params) = filter.condition();
|
||||
let (condition, mut params) = filter.condition(has_grpc_status(conn));
|
||||
let mut sql = format!("SELECT * FROM flows WHERE {condition}");
|
||||
if let Some(id) = &filter.after {
|
||||
// By rowid, which only grows as flows are written.
|
||||
@@ -300,7 +312,7 @@ fn list_in(conn: &rusqlite::Connection, filter: &FlowFilter) -> Result<Vec<Flow>
|
||||
}
|
||||
|
||||
fn count_matching(conn: &rusqlite::Connection, filter: &FlowFilter) -> Result<usize, String> {
|
||||
let (condition, params) = filter.condition();
|
||||
let (condition, params) = filter.condition(has_grpc_status(conn));
|
||||
conn.query_row(
|
||||
&format!("SELECT COUNT(*) FROM flows WHERE {condition}"),
|
||||
rusqlite::params_from_iter(params),
|
||||
@@ -319,12 +331,14 @@ pub fn clear(db: &ProxyQueryManager, blobs: &BlobManager) -> Result<usize, Strin
|
||||
let mut stmt = conn.prepare("SELECT id FROM flows")?;
|
||||
let ids = stmt.query_map([], |r| r.get(0))?.collect::<Result<Vec<String>, _>>()?;
|
||||
conn.execute("DELETE FROM flows", [])?;
|
||||
conn.execute("DELETE FROM flow_messages", [])?;
|
||||
// Only the deleted flows' bodies, so a flow still in flight keeps what it has written.
|
||||
blobs
|
||||
.with_tx(|b| {
|
||||
for id in &ids {
|
||||
b.delete_chunks(&Flow::body_id(id, FlowBody::Request))?;
|
||||
b.delete_chunks(&Flow::body_id(id, FlowBody::Response))?;
|
||||
b.delete_chunks_like(&format!("{id}.msg.%"))?;
|
||||
}
|
||||
Ok::<_, yaak_models::error::Error>(())
|
||||
})
|
||||
@@ -634,7 +648,7 @@ mod tests {
|
||||
let plan: Vec<String> = db.with_conn(|c| {
|
||||
let conn = c.conn().resolve();
|
||||
let (condition, params) =
|
||||
FlowFilter { q: Some("stripe".into()), ..Default::default() }.condition();
|
||||
FlowFilter { q: Some("stripe".into()), ..Default::default() }.condition(true);
|
||||
let sql = format!(
|
||||
"EXPLAIN QUERY PLAN SELECT * FROM flows WHERE {condition} \
|
||||
ORDER BY started_at DESC, id DESC LIMIT 500"
|
||||
|
||||
Reference in new issue
Block a user