Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/queries.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api::ApiClient;
use crossterm::style::{Color, Stylize};
use crossterm::style::Stylize;
use serde::{Deserialize, Serialize};

const SQL_KEYWORDS: &[&str] = &[
Expand Down Expand Up @@ -125,6 +125,8 @@ struct QueryRun {
sql_hash: String,
sql_text: String,
result_id: Option<String>,
#[serde(default)]
database_id: Option<String>,
error_message: Option<String>,
warning_message: Option<String>,
trace_id: Option<String>,
Expand All @@ -139,16 +141,6 @@ struct ListResponse {
next_cursor: Option<String>,
}

fn color_status(status: &str) -> String {
let color = match status {
"succeeded" => Color::Green,
"failed" => Color::Red,
"running" | "queued" | "pending" => Color::Yellow,
_ => Color::Reset,
};
status.with(color).to_string()
}

fn truncate_sql(sql: &str, max: usize) -> String {
let flat = sql.split_whitespace().collect::<Vec<_>>().join(" ");
if flat.chars().count() <= max {
Expand Down Expand Up @@ -191,20 +183,21 @@ pub fn list(
.map(|r| {
vec![
r.id.clone(),
color_status(&r.status),
crate::util::color_status(&r.status),
crate::util::format_date(&r.created_at),
r.execution_time_ms
.map(|ms| ms.to_string())
.unwrap_or_else(|| "-".to_string()),
r.row_count
.map(|n| n.to_string())
.unwrap_or_else(|| "-".to_string()),
truncate_sql(&r.sql_text, 60),
r.result_id.as_deref().unwrap_or("-").to_string(),
truncate_sql(&r.sql_text, 40),
]
})
.collect();
crate::table::print(
&["ID", "STATUS", "CREATED", "DURATION_MS", "ROWS", "SQL"],
&["ID", "STATUS", "CREATED", "MS", "ROWS", "RESULT_ID", "SQL"],
&rows,
);
}
Expand Down Expand Up @@ -238,7 +231,7 @@ fn print_detail(r: &QueryRun, format: &str) {
"table" => {
let label = |l: &str| format!("{:<14}", l).dark_grey().to_string();
println!("{}{}", label("id:"), r.id);
println!("{}{}", label("status:"), color_status(&r.status));
println!("{}{}", label("status:"), crate::util::color_status(&r.status));
println!(
"{}{}",
label("created:"),
Expand All @@ -259,6 +252,9 @@ fn print_detail(r: &QueryRun, format: &str) {
if let Some(ref id) = r.result_id {
println!("{}{}", label("result id:"), id);
}
if let Some(ref id) = r.database_id {
println!("{}{}", label("database id:"), id);
}
if let Some(ref id) = r.saved_query_id {
let version = r
.saved_query_version
Expand Down
42 changes: 36 additions & 6 deletions src/results.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use crate::api::ApiClient;
use crossterm::style::Stylize;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct ResultEntry {
id: String,
status: String,
created_at: String,
#[serde(default)]
row_count: Option<u64>,
#[serde(default)]
query_run_id: Option<String>,
#[serde(default)]
expires_at: Option<String>,
}

#[derive(Deserialize)]
Expand All @@ -29,25 +36,48 @@ pub fn list(workspace_id: &str, limit: Option<u32>, offset: Option<u32>, format:
"yaml" => print!("{}", serde_yaml::to_string(&body.results).unwrap()),
"table" => {
if body.results.is_empty() {
use crossterm::style::Stylize;
eprintln!("{}", "No results found.".dark_grey());
} else {
let has_rows = body.results.iter().any(|r| r.row_count.is_some());
let has_query_run = body.results.iter().any(|r| r.query_run_id.is_some());
let has_expires = body.results.iter().any(|r| r.expires_at.is_some());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this results list enhancement (conditional ROWS/QUERY_RUN_ID/EXPIRES columns, status coloring, three new fields) isn't mentioned in the PR title or description, which are entirely about queries list. The code looks correct — header/row column counts stay in sync across all conditional branches — but consider either documenting these results.rs changes in the description or splitting them into a separate PR for a cleaner history. (not blocking)


let rows: Vec<Vec<String>> = body
.results
.iter()
.map(|r| {
vec![
let mut row = vec![
r.id.clone(),
r.status.clone(),
crate::util::color_status(&r.status),
crate::util::format_date(&r.created_at),
]
];
if has_rows {
row.push(r.row_count.map(|n| n.to_string()).unwrap_or_else(|| "-".to_string()));
}
if has_query_run {
row.push(r.query_run_id.as_deref().unwrap_or("-").to_string());
}
if has_expires {
row.push(
r.expires_at
.as_deref()
.map(|s| crate::util::format_date(s))
.unwrap_or_else(|| "-".to_string()),
);
}
row
})
.collect();
crate::table::print(&["ID", "STATUS", "CREATED AT"], &rows);

let mut headers = vec!["ID", "STATUS", "CREATED"];
if has_rows { headers.push("ROWS"); }
if has_query_run { headers.push("QUERY_RUN_ID"); }
if has_expires { headers.push("EXPIRES"); }

crate::table::print(&headers, &rows);
}
if body.has_more {
let next = offset.unwrap_or(0) + body.count as u32;
use crossterm::style::Stylize;
eprintln!(
"{}",
format!(
Expand Down
14 changes: 14 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,20 @@ fn colorize_json_value(v: &str) -> String {
format!("{colored}{comma}")
}

/// Color a status string for terminal output. Covers vocabulary from both
/// query runs (succeeded/failed/running/queued/pending) and results (ready/expired/processing).
pub fn color_status(status: &str) -> String {
use crossterm::style::{Color, Stylize};
let color = match status {
"succeeded" | "ready" => Color::Green,
"failed" => Color::Red,
"running" | "queued" | "pending" | "processing" => Color::Yellow,
"expired" => Color::DarkGrey,
_ => Color::Reset,
};
status.with(color).to_string()
}

/// Format an ISO date string compactly: "2024-03-15 14:23" (no seconds, no timezone).
pub fn format_date(s: &str) -> String {
let s = s.split('.').next().unwrap_or(s).trim_end_matches('Z');
Expand Down
Loading