Skip to content

Commit

Permalink
clean up logging
Browse files Browse the repository at this point in the history
  • Loading branch information
XiangpengHao committed Dec 14, 2024
1 parent c88c14f commit bceb223
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
25 changes: 12 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ async fn execute_query_async(
table_name: String,
parquet_info: ParquetInfo,
) -> Result<(Vec<arrow::array::RecordBatch>, Arc<dyn ExecutionPlan>), String> {
web_sys::console::log_1(&table_name.clone().into());

let (results, physical_plan) = execute_query_inner(&table_name, parquet_info, bytes, &query)
.await
.map_err(|e| format!("Failed to execute query: {}", e))?;
Expand Down Expand Up @@ -287,12 +285,10 @@ fn App() -> impl IntoView {
file_name,
server_address,
} => {
web_sys::console::log_1(
&format!(
"Received file: {}, server_address: {}",
file_name, server_address
)
.into(),
logging::log!(
"Received file: {}, server_address: {}",
file_name,
server_address
);
let builder = Http::default().endpoint(&server_address);
let Ok(op) = Operator::new(builder) else {
Expand Down Expand Up @@ -328,7 +324,7 @@ fn App() -> impl IntoView {
move || parquet_info(),
move |info, _, _| match info {
Some(info) => {
web_sys::console::log_1(&info.to_string().into());
logging::log!("{}", info.to_string());
let default_query =
format!("select * from \"{}\" limit 10", file_name.get_untracked());
set_user_input.set(default_query);
Expand Down Expand Up @@ -357,12 +353,12 @@ fn App() -> impl IntoView {
{
Ok(response) => response,
Err(e) => {
web_sys::console::log_1(&e.clone().into());
logging::log!("{}", e);
set_error_message.set(Some(e));
return;
}
};
web_sys::console::log_1(&sql.clone().into());
logging::log!("{}", sql);
set_sql_query.set(sql);
});
},
Expand Down Expand Up @@ -526,7 +522,6 @@ fn App() -> impl IntoView {
view! {
<QueryResults
sql_query=sql_query.get()
set_user_query=set_user_input
query_result=result
physical_plan=physical_plan
/>
Expand Down Expand Up @@ -565,7 +560,11 @@ fn App() -> impl IntoView {
</div>

</div>
<Settings show=show_settings set_show=set_show_settings connection_info=connection_info />
<Settings
show=show_settings
set_show=set_show_settings
connection_info=connection_info
/>
</div>
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/query_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use datafusion::{
},
prelude::SessionConfig,
};
use leptos::prelude::*;
use leptos::wasm_bindgen::{JsCast, JsValue};
use leptos::{logging, prelude::*};
use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
use serde_json::json;
use wasm_bindgen_futures::JsFuture;
Expand Down Expand Up @@ -49,7 +49,6 @@ pub(crate) async fn execute_query_inner(
data: Bytes,
query: &str,
) -> Result<(Vec<RecordBatch>, Arc<dyn ExecutionPlan>), DataFusionError> {
web_sys::console::log_1(&table_name.into());
let mut config = SessionConfig::new();
config.options_mut().sql_parser.dialect = "PostgreSQL".to_string();

Expand All @@ -72,7 +71,7 @@ pub(crate) async fn execute_query_inner(
let (state, plan) = plan.into_parts();
let plan = state.optimize(&plan)?;

web_sys::console::log_1(&plan.display_indent().to_string().into());
logging::log!("{}", &plan.display_indent());

let physical_plan = state.create_physical_plan(&plan).await?;

Expand Down Expand Up @@ -157,23 +156,23 @@ pub(crate) async fn user_input_to_sql(
// otherwise, treat it as some natural language

let schema_str = schema_to_brief_str(schema);
web_sys::console::log_1(&format!("Processing user input: {}", input).into());
logging::log!("Processing user input: {}", input);

let prompt = format!(
"Generate a SQL query to answer the following question: {}. You should generate PostgreSQL SQL dialect, all field names and table names should be double quoted, and the output SQL should be executable, be careful about the available columns. The table name is: {}, the schema of the table is: {}. ",
input, file_name, schema_str
);
web_sys::console::log_1(&prompt.clone().into());
logging::log!("{}", prompt);

let sql = match generate_sql_via_claude(&prompt, api_key).await {
Ok(response) => response,
Err(e) => {
web_sys::console::log_1(&e.clone().into());
logging::log!("{}", e);
let claude_error = format!("Failed to generate SQL through Claude: {}", e);
return Err(claude_error);
}
};
web_sys::console::log_1(&sql.clone().into());
logging::log!("{}", sql);
Ok(sql)
}

Expand Down
36 changes: 28 additions & 8 deletions src/query_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use datafusion::{
ExecutionPlanVisitor,
},
};
use leptos::prelude::*;
use leptos::{logging, prelude::*};
use parquet::arrow::ArrowWriter;
use web_sys::js_sys;
use web_sys::wasm_bindgen::JsCast;
Expand Down Expand Up @@ -93,7 +93,6 @@ fn export_to_parquet_inner(query_result: &Vec<RecordBatch>) {
#[component]
pub fn QueryResults(
sql_query: String,
set_user_query: WriteSignal<String>,
query_result: Vec<RecordBatch>,
physical_plan: Arc<dyn ExecutionPlan>,
) -> impl IntoView {
Expand All @@ -103,11 +102,32 @@ pub fn QueryResults(

view! {
<div class="mt-4 p-4 bg-white border border-gray-300 rounded-md">
<div
class="mb-4 p-3 bg-gray-50 rounded border border-gray-200 font-mono text-sm overflow-x-auto cursor-pointer"
on:click=move |_| set_user_query(sql_query.to_string())
>
{sql}
<div class="mb-4 p-3 bg-gray-50 rounded border border-gray-200 font-mono text-sm overflow-x-auto relative group">
{sql.clone()}
<button
class="absolute top-2 right-2 p-2 text-gray-500 hover:text-gray-700"
on:click=move |_| {
let window = web_sys::window().unwrap();
let navigator = window.navigator();
let clipboard = navigator.clipboard();
let _ = clipboard.write_text(&sql);
}
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2m0 0h2a2 2 0 012 2v3m2 4H10m0 0l3-3m-3 3l3 3"
/>
</svg>
</button>
</div>
<div class="mb-4 border-b border-gray-300 flex items-center">
<button
Expand Down Expand Up @@ -465,7 +485,7 @@ pub fn PhysicalPlan(physical_plan: Arc<dyn ExecutionPlan>) -> impl IntoView {
let displayable_plan = DisplayableExecutionPlan::with_metrics(physical_plan.as_ref());
accept(physical_plan.as_ref(), &mut builder).unwrap();
let root = builder.current_path.pop().unwrap();
web_sys::console::log_1(&displayable_plan.indent(true).to_string().into());
logging::log!("{}", displayable_plan.indent(true).to_string());

view! {
<div class="relative">
Expand Down

0 comments on commit bceb223

Please sign in to comment.