db: sprinkle with metrics::histogram!

This commit is contained in:
Astro 2022-12-20 03:40:51 +01:00
parent a3c87c9311
commit 6aeab7f7aa

View file

@ -1,8 +1,8 @@
use std::sync::Arc; use std::{sync::Arc, time::Instant};
use metrics::histogram;
use tokio_postgres::{Client, Error, NoTls, Statement}; use tokio_postgres::{Client, Error, NoTls, Statement};
const CREATE_SCHEMA_COMMANDS: &[&str] = &[ const CREATE_SCHEMA_COMMANDS: &[&str] = &[
"CREATE TABLE IF NOT EXISTS follows (id TEXT, inbox TEXT, actor TEXT, UNIQUE (inbox, actor))", "CREATE TABLE IF NOT EXISTS follows (id TEXT, inbox TEXT, actor TEXT, UNIQUE (inbox, actor))",
"CREATE INDEX IF NOT EXISTS follows_actor ON follows (actor) INCLUDE (inbox)", "CREATE INDEX IF NOT EXISTS follows_actor ON follows (actor) INCLUDE (inbox)",
@ -58,20 +58,29 @@ impl Database {
} }
pub async fn add_follow(&self, id: &str, inbox: &str, actor: &str) -> Result<(), Error> { pub async fn add_follow(&self, id: &str, inbox: &str, actor: &str) -> Result<(), Error> {
let t1 = Instant::now();
self.inner.client.execute(&self.inner.add_follow, &[&id, &inbox, &actor]) self.inner.client.execute(&self.inner.add_follow, &[&id, &inbox, &actor])
.await?; .await?;
let t2 = Instant::now();
histogram!("sql", t2 - t1, "query" => "add_follow");
Ok(()) Ok(())
} }
pub async fn del_follow(&self, id: &str, actor: &str) -> Result<(), Error> { pub async fn del_follow(&self, id: &str, actor: &str) -> Result<(), Error> {
let t1 = Instant::now();
self.inner.client.execute(&self.inner.del_follow, &[&id, &actor]) self.inner.client.execute(&self.inner.del_follow, &[&id, &actor])
.await?; .await?;
let t2 = Instant::now();
histogram!("sql", t2 - t1, "query" => "del_follow");
Ok(()) Ok(())
} }
pub async fn get_following_inboxes(&self, actor: &str) -> Result<impl Iterator<Item = String>, Error> { pub async fn get_following_inboxes(&self, actor: &str) -> Result<impl Iterator<Item = String>, Error> {
let t1 = Instant::now();
let rows = self.inner.client.query(&self.inner.get_following_inboxes, &[&actor]) let rows = self.inner.client.query(&self.inner.get_following_inboxes, &[&actor])
.await?; .await?;
let t2 = Instant::now();
histogram!("sql", t2 - t1, "query" => "get_following_inboxes");
Ok(rows.into_iter() Ok(rows.into_iter()
.map(|row| row.get(0)) .map(|row| row.get(0))
) )