Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Notebook query elapsed time#644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
montanalow merged 5 commits intopostgresml:masterfromSilasMarvin:notebook-query-elapsed-time
May 23, 2023
Merged
Show file tree
Hide file tree
Changes fromall 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
2 changes: 2 additions & 0 deletionspgml-dashboard/src/lib.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,6 +19,7 @@ pub mod guards;
pub mod models;
mod responses;
mod templates;
mod utils;

use guards::Cluster;
use responses::{BadRequest, ResponseOk};
Expand DownExpand Up@@ -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();
Expand Down
32 changes: 20 additions & 12 deletionspgml-dashboard/src/models.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -296,18 +296,19 @@ 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 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 {
Ok(sql) => sql.render_once()?,
let result = match templates::Sql::new(pool, query, render_individual_execution_duration).await {
Ok(sql) => {
total_execution_duration += sql.execution_duration;
sql.render_once()?
},
Err(err) => templates::SqlError {
error: format!("{:?}", err),
}
Expand All@@ -317,7 +318,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 => {
Expand All@@ -335,21 +341,23 @@ impl Cell {
front_matter_delimiter: None,
};

format!(
(format!(
"<div class=\"markdown-body\">{}</div>",
markdown_to_html(&self.contents, &options)
)
), None)
}
};

sqlx::query!(
"UPDATE pgml.notebook_cells SET rendering = $1WHERE id = $2",
"UPDATE pgml.notebook_cells SET rendering = $1, execution_time = $2WHERE id = $3",
rendering,
execution_time,
self.id
)
.execute(pool)
.await?;

self.execution_time = execution_time;
self.rendering = Some(rendering);

Ok(())
Expand Down
8 changes: 6 additions & 2 deletionspgml-dashboard/src/templates.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,10 +55,12 @@ pub struct Undo {
pub struct Sql {
pub columns: Vec<String>,
pub rows: Vec<Vec<String>>,
pub execution_duration: std::time::Duration,
pub render_execution_duration: bool,
}

impl Sql {
pub async fn new(pool: &PgPool, query: &str) -> anyhow::Result<Sql> {
pub async fn new(pool: &PgPool, query: &str, render_execution_duration: bool) -> anyhow::Result<Sql> {
let prepared_stmt = pool.prepare(query).await?;
let cols = prepared_stmt.columns();

Expand All@@ -67,7 +69,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();
Expand DownExpand Up@@ -199,7 +203,7 @@ impl Sql {
rows.push(values);
}

Ok(Sql { columns, rows })
Ok(Sql { columns, rows, execution_duration, render_execution_duration })
}
}

Expand Down
9 changes: 9 additions & 0 deletionspgml-dashboard/src/utils.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletionpgml-dashboard/templates/cell.html
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,7 +75,7 @@
<div class="notebook-duration">
<div class="markdown-body">
<div class="flex flex-row-reverse">
<code>TODO</code>
<code>Time: <%= crate::utils::format_microseconds(cell.execution_time.unwrap().microseconds as f64) %></code>
</div>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletionspgml-dashboard/templates/sql.html
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,6 +24,9 @@
<% } %>
</tbody>
</table>
<% if render_execution_duration { %>
<code>Time: <%= crate::utils::format_microseconds(execution_duration.as_micros() as f64) %></code>
<% } %>
</div>

<% } %>

[8]ページ先頭

©2009-2025 Movatter.jp