improvement: logging failed queries and timing

This commit is contained in:
Greg Heartsfield 2022-01-01 19:25:09 -06:00
parent 3d56262386
commit afc9a0096a
2 changed files with 16 additions and 2 deletions

View File

@ -13,6 +13,7 @@ use rusqlite::OpenFlags;
use crate::config::SETTINGS; use crate::config::SETTINGS;
use std::path::Path; use std::path::Path;
use std::thread; use std::thread;
use std::time::Instant;
use tokio::task; use tokio::task;
/// Database file /// Database file
@ -157,12 +158,17 @@ pub async fn db_writer(
} }
let mut event_write = false; let mut event_write = false;
let event = next_event.unwrap(); let event = next_event.unwrap();
let start = Instant::now();
match write_event(&mut conn, &event) { match write_event(&mut conn, &event) {
Ok(updated) => { Ok(updated) => {
if updated == 0 { if updated == 0 {
debug!("ignoring duplicate event"); debug!("ignoring duplicate event");
} else { } else {
info!("persisted event: {}", event.get_event_id_prefix()); info!(
"persisted event: {} in {:?}",
event.get_event_id_prefix(),
start.elapsed()
);
event_write = true; event_write = true;
// send this out to all clients // send this out to all clients
bcast_tx.send(event.clone()).ok(); bcast_tx.send(event.clone()).ok();
@ -402,6 +408,8 @@ pub async fn db_query(
Connection::open_with_flags(&full_path, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap(); Connection::open_with_flags(&full_path, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap();
debug!("opened database for reading"); debug!("opened database for reading");
debug!("going to query for: {:?}", sub); debug!("going to query for: {:?}", sub);
let mut row_count: usize = 0;
let start = Instant::now();
// generate SQL query // generate SQL query
let q = query_from_sub(&sub); let q = query_from_sub(&sub);
// execute the query // execute the query
@ -413,6 +421,7 @@ pub async fn db_query(
debug!("query aborted"); debug!("query aborted");
return; return;
} }
row_count += 1;
// TODO: check before unwrapping // TODO: check before unwrapping
let event_json = row.get(0).unwrap(); let event_json = row.get(0).unwrap();
query_tx query_tx
@ -422,6 +431,10 @@ pub async fn db_query(
}) })
.ok(); .ok();
} }
debug!("query completed"); debug!(
"query completed ({} rows) in {:?}",
row_count,
start.elapsed()
);
}); });
} }

View File

@ -71,6 +71,7 @@ impl Stream for NostrStream {
} }
Err(e) => { Err(e) => {
debug!("proto parse error: {:?}", e); debug!("proto parse error: {:?}", e);
debug!("parse error on message: {}", msg.trim());
Err(Error::ProtoParseError) Err(Error::ProtoParseError)
} }
} }