From 3eac9b25bf58e4d909260022bf2c394882d4c70e Mon Sep 17 00:00:00 2001 From: SilasMarvin <19626586+SilasMarvin@users.noreply.github.com> Date: Mon, 22 May 2023 10:46:16 -0700 Subject: [PATCH 1/5] Working millisecond execution_time display in cells --- pgml-dashboard/src/models.rs | 23 +++++++++++++++++------ pgml-dashboard/src/templates.rs | 7 +++++-- pgml-dashboard/templates/cell.html | 3 ++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pgml-dashboard/src/models.rs b/pgml-dashboard/src/models.rs index 3fbcaaf79..3c5740313 100644 --- a/pgml-dashboard/src/models.rs +++ b/pgml-dashboard/src/models.rs @@ -296,10 +296,11 @@ impl Cell { pub async fn render(&mut self, pool: &PgPool) -> anyhow::Result<()> { let cell_type: CellType = self.cell_type.into(); - let rendering = match cell_type { + let (rendering, execution_time) = match cell_type { CellType::Sql => { let queries = self.contents.split(";"); let mut rendering = String::new(); + let mut total_execution_duration = std::time::Duration::default(); for query in queries { if query.trim().is_empty() { @@ -307,7 +308,10 @@ impl Cell { } let result = match templates::Sql::new(pool, query).await { - Ok(sql) => sql.render_once()?, + Ok(sql) => { + total_execution_duration += sql.execution_duration; + sql.render_once()? + }, Err(err) => templates::SqlError { error: format!("{:?}", err), } @@ -317,7 +321,12 @@ impl Cell { rendering.push_str(&result); } - rendering + let execution_time = PgInterval{ + months: 0, + days: 0, + microseconds: total_execution_duration.as_micros().try_into().unwrap_or(0) + }; + (rendering, Some(execution_time)) } CellType::Markdown => { @@ -335,21 +344,23 @@ impl Cell { front_matter_delimiter: None, }; - format!( + (format!( "
{}
", markdown_to_html(&self.contents, &options) - ) + ), None) } }; sqlx::query!( - "UPDATE pgml.notebook_cells SET rendering = $1 WHERE id = $2", + "UPDATE pgml.notebook_cells SET rendering = $1, execution_time = $2 WHERE id = $3", rendering, + execution_time, self.id ) .execute(pool) .await?; + self.execution_time = execution_time; self.rendering = Some(rendering); Ok(()) diff --git a/pgml-dashboard/src/templates.rs b/pgml-dashboard/src/templates.rs index 24bf423be..9ccbe5ff8 100644 --- a/pgml-dashboard/src/templates.rs +++ b/pgml-dashboard/src/templates.rs @@ -1,5 +1,5 @@ use sailfish::TemplateOnce; -use sqlx::postgres::types::PgMoney; +use sqlx::postgres::types::{PgMoney, PgInterval}; use sqlx::types::time::PrimitiveDateTime; use sqlx::{Column, Executor, PgPool, Row, Statement, TypeInfo, ValueRef}; @@ -55,6 +55,7 @@ pub struct Undo { pub struct Sql { pub columns: Vec, pub rows: Vec>, + pub execution_duration: std::time::Duration, } impl Sql { @@ -67,7 +68,9 @@ impl Sql { cols.iter().for_each(|c| columns.push(c.name().to_string())); + let now = std::time::Instant::now(); let result = prepared_stmt.query().fetch_all(pool).await?; + let execution_duration = now.elapsed(); for row in result.iter() { let mut values = Vec::new(); @@ -199,7 +202,7 @@ impl Sql { rows.push(values); } - Ok(Sql { columns, rows }) + Ok(Sql { columns, rows, execution_duration }) } } diff --git a/pgml-dashboard/templates/cell.html b/pgml-dashboard/templates/cell.html index 7ba13f121..d2be0054e 100644 --- a/pgml-dashboard/templates/cell.html +++ b/pgml-dashboard/templates/cell.html @@ -75,7 +75,8 @@
- TODO + Time: <%- (cell.execution_time.unwrap().microseconds as f64) / 1000.0 %>ms +
From eaf2a662b70f4f661d543343f177f94828bcf429 Mon Sep 17 00:00:00 2001 From: SilasMarvin <19626586+SilasMarvin@users.noreply.github.com> Date: Mon, 22 May 2023 11:09:31 -0700 Subject: [PATCH 2/5] Added support for displaying cell execution_time in seconds, milliseconds, and microseconds --- pgml-dashboard/templates/cell.html | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pgml-dashboard/templates/cell.html b/pgml-dashboard/templates/cell.html index d2be0054e..25e2c18fb 100644 --- a/pgml-dashboard/templates/cell.html +++ b/pgml-dashboard/templates/cell.html @@ -75,8 +75,14 @@
- Time: <%- (cell.execution_time.unwrap().microseconds as f64) / 1000.0 %>ms - + Time: <%- + match cell.execution_time.unwrap().microseconds { + microseconds @ 1000000.. => format!("{}s", (microseconds as f64) / 1000000.), + microseconds @ 1000..=999999 => format!("{}ms", (microseconds as f64) / 1000.), + microseconds @ 0..=999 => format!("{}μs", microseconds), + _ => "0".to_string(), + } + %>
From abefee51cbdb9454329169f9cba734ccaf1abf54 Mon Sep 17 00:00:00 2001 From: SilasMarvin <19626586+SilasMarvin@users.noreply.github.com> Date: Mon, 22 May 2023 12:50:52 -0700 Subject: [PATCH 3/5] Removed unnecessary import --- pgml-dashboard/src/templates.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pgml-dashboard/src/templates.rs b/pgml-dashboard/src/templates.rs index 9ccbe5ff8..0704ab245 100644 --- a/pgml-dashboard/src/templates.rs +++ b/pgml-dashboard/src/templates.rs @@ -1,5 +1,5 @@ use sailfish::TemplateOnce; -use sqlx::postgres::types::{PgMoney, PgInterval}; +use sqlx::postgres::types::PgMoney; use sqlx::types::time::PrimitiveDateTime; use sqlx::{Column, Executor, PgPool, Row, Statement, TypeInfo, ValueRef}; From b9c707fd87e4d3191bf867a9f2afa94d6e6593fb Mon Sep 17 00:00:00 2001 From: SilasMarvin <19626586+SilasMarvin@users.noreply.github.com> Date: Mon, 22 May 2023 16:34:47 -0700 Subject: [PATCH 4/5] Show per query timing --- pgml-dashboard/templates/sql.html | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pgml-dashboard/templates/sql.html b/pgml-dashboard/templates/sql.html index 48f0182f7..0ad7f4d89 100644 --- a/pgml-dashboard/templates/sql.html +++ b/pgml-dashboard/templates/sql.html @@ -24,6 +24,15 @@ <% } %> + + Time: + <%- match execution_duration.as_micros() { + microseconds @ 1000000.. => format!("{}s", (microseconds as f64) / 1000000.), + microseconds @ 1000..=999999 => format!("{}ms", (microseconds as f64) / 1000.), + microseconds @ 0..=999 => format!("{}μs", microseconds), + _ => "0".to_string(), + } %> + <% } %> From b38ebe8f4277f1f3ec60cc076473da14043eb4b6 Mon Sep 17 00:00:00 2001 From: SilasMarvin <19626586+SilasMarvin@users.noreply.github.com> Date: Mon, 22 May 2023 17:33:33 -0700 Subject: [PATCH 5/5] Added utils.rs, prevented double rendering --- pgml-dashboard/src/lib.rs | 2 ++ pgml-dashboard/src/models.rs | 9 +++------ pgml-dashboard/src/templates.rs | 5 +++-- pgml-dashboard/src/utils.rs | 9 +++++++++ pgml-dashboard/templates/cell.html | 9 +-------- pgml-dashboard/templates/sql.html | 12 +++--------- 6 files changed, 21 insertions(+), 25 deletions(-) create mode 100644 pgml-dashboard/src/utils.rs diff --git a/pgml-dashboard/src/lib.rs b/pgml-dashboard/src/lib.rs index f8e747d4e..0fa686a2f 100644 --- a/pgml-dashboard/src/lib.rs +++ b/pgml-dashboard/src/lib.rs @@ -19,6 +19,7 @@ pub mod guards; pub mod models; mod responses; mod templates; +mod utils; use guards::Cluster; use responses::{BadRequest, ResponseOk}; @@ -558,6 +559,7 @@ pub async fn uploaded_index(cluster: Cluster, table_name: &str) -> ResponseOk { let sql = templates::Sql::new( cluster.pool(), &format!("SELECT * FROM {} LIMIT 10", table_name), + true, ) .await .unwrap(); diff --git a/pgml-dashboard/src/models.rs b/pgml-dashboard/src/models.rs index 3c5740313..13fb5fb63 100644 --- a/pgml-dashboard/src/models.rs +++ b/pgml-dashboard/src/models.rs @@ -298,16 +298,13 @@ impl Cell { let (rendering, execution_time) = match cell_type { CellType::Sql => { - let queries = self.contents.split(";"); + let queries: Vec<&str> = self.contents.split(';').filter(|q| !q.trim().is_empty()).collect(); let mut rendering = String::new(); let mut total_execution_duration = std::time::Duration::default(); + let render_individual_execution_duration = queries.len() > 1; for query in queries { - if query.trim().is_empty() { - continue; - } - - let result = match templates::Sql::new(pool, query).await { + let result = match templates::Sql::new(pool, query, render_individual_execution_duration).await { Ok(sql) => { total_execution_duration += sql.execution_duration; sql.render_once()? diff --git a/pgml-dashboard/src/templates.rs b/pgml-dashboard/src/templates.rs index 0704ab245..fe2425cb0 100644 --- a/pgml-dashboard/src/templates.rs +++ b/pgml-dashboard/src/templates.rs @@ -56,10 +56,11 @@ pub struct Sql { pub columns: Vec, pub rows: Vec>, pub execution_duration: std::time::Duration, + pub render_execution_duration: bool, } impl Sql { - pub async fn new(pool: &PgPool, query: &str) -> anyhow::Result { + pub async fn new(pool: &PgPool, query: &str, render_execution_duration: bool) -> anyhow::Result { let prepared_stmt = pool.prepare(query).await?; let cols = prepared_stmt.columns(); @@ -202,7 +203,7 @@ impl Sql { rows.push(values); } - Ok(Sql { columns, rows, execution_duration }) + Ok(Sql { columns, rows, execution_duration, render_execution_duration }) } } diff --git a/pgml-dashboard/src/utils.rs b/pgml-dashboard/src/utils.rs new file mode 100644 index 000000000..5c0e69fdb --- /dev/null +++ b/pgml-dashboard/src/utils.rs @@ -0,0 +1,9 @@ +pub fn format_microseconds(microseconds: f64) -> String { + if microseconds >= 1000000. { + format!("{}s", microseconds / 1000000.) + } else if microseconds >= 1000. { + format!("{}ms", microseconds / 1000.) + } else { + format!("{}μs", microseconds) + } +} diff --git a/pgml-dashboard/templates/cell.html b/pgml-dashboard/templates/cell.html index 25e2c18fb..f9f87839f 100644 --- a/pgml-dashboard/templates/cell.html +++ b/pgml-dashboard/templates/cell.html @@ -75,14 +75,7 @@
- Time: <%- - match cell.execution_time.unwrap().microseconds { - microseconds @ 1000000.. => format!("{}s", (microseconds as f64) / 1000000.), - microseconds @ 1000..=999999 => format!("{}ms", (microseconds as f64) / 1000.), - microseconds @ 0..=999 => format!("{}μs", microseconds), - _ => "0".to_string(), - } - %> + Time: <%= crate::utils::format_microseconds(cell.execution_time.unwrap().microseconds as f64) %>
diff --git a/pgml-dashboard/templates/sql.html b/pgml-dashboard/templates/sql.html index 0ad7f4d89..8328bb052 100644 --- a/pgml-dashboard/templates/sql.html +++ b/pgml-dashboard/templates/sql.html @@ -24,15 +24,9 @@ <% } %> - - Time: - <%- match execution_duration.as_micros() { - microseconds @ 1000000.. => format!("{}s", (microseconds as f64) / 1000000.), - microseconds @ 1000..=999999 => format!("{}ms", (microseconds as f64) / 1000.), - microseconds @ 0..=999 => format!("{}μs", microseconds), - _ => "0".to_string(), - } %> - + <% if render_execution_duration { %> + Time: <%= crate::utils::format_microseconds(execution_duration.as_micros() as f64) %> + <% } %> <% } %> pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy