feat: add logging for SurrealDB queries across multiple repositories
This commit is contained in:
@@ -7,6 +7,8 @@ use crate::{
|
||||
use anyhow::{Result, anyhow, bail};
|
||||
use chrono::{Duration, Utc};
|
||||
use surrealdb::sql::Thing;
|
||||
use tracing::instrument;
|
||||
use tracing::info;
|
||||
|
||||
pub struct AuthRepository<'a> {
|
||||
pub state: &'a AppState,
|
||||
@@ -17,6 +19,7 @@ impl<'a> AuthRepository<'a> {
|
||||
Self { state }
|
||||
}
|
||||
|
||||
#[instrument(skip(self, user), err)]
|
||||
pub async fn query_store_user(&self, user: UsersDetailQueryDto) -> Result<String> {
|
||||
if user.email.trim().is_empty() {
|
||||
bail!("Email is required");
|
||||
@@ -30,12 +33,14 @@ impl<'a> AuthRepository<'a> {
|
||||
permissions,
|
||||
};
|
||||
|
||||
info!(query = %format!("DELETE FROM {} WHERE id = '{}'", table, user_id), "Executing SurrealDB query");
|
||||
let _record: Option<UserCacheSchema> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
.delete::<Option<UserCacheSchema>>((table.clone(), user_id.clone()))
|
||||
.await?;
|
||||
|
||||
info!(query = %format!("CREATE {}:{}", table, user_id), "Executing SurrealDB query");
|
||||
let record: Option<UserCacheSchema> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
@@ -49,10 +54,12 @@ impl<'a> AuthRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email), err)]
|
||||
pub async fn query_get_stored_user(
|
||||
&self,
|
||||
email: String,
|
||||
) -> Result<UsersDetailQueryDto> {
|
||||
info!(query = %format!("SELECT FROM {} WHERE id = '{}'", ResourceEnum::UsersCache.to_string(), email), "Executing SurrealDB query");
|
||||
let user_cache: Option<UserCacheSchema> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
@@ -105,7 +112,9 @@ impl<'a> AuthRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email), err)]
|
||||
pub async fn query_delete_stored_user(&self, email: String) -> Result<String> {
|
||||
info!(query = %format!("DELETE FROM {} WHERE id = '{}'", ResourceEnum::UsersCache.to_string(), email), "Executing SurrealDB query");
|
||||
let record: Option<UsersDetailQueryDto> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
@@ -117,13 +126,16 @@ impl<'a> AuthRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email), err)]
|
||||
pub async fn query_get_stored_otp(&self, email: String) -> Result<u32> {
|
||||
let table = ResourceEnum::OtpCache.to_string();
|
||||
let key = (table.as_str(), email.as_str());
|
||||
info!(query = %format!("SELECT FROM {} WHERE id = '{}'", table, email), "Executing SurrealDB query");
|
||||
let result: Option<AuthOtpSchema> = self.state.surrealdb_mem.select(key).await?;
|
||||
match result {
|
||||
Some(data) => match Utc::now() > data.expires_at {
|
||||
true => {
|
||||
info!(query = %format!("DELETE FROM {} WHERE id = '{}'", table, email), "Executing SurrealDB query");
|
||||
let _ = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
@@ -137,9 +149,11 @@ impl<'a> AuthRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email, otp), err)]
|
||||
pub async fn query_store_otp(&self, email: String, otp: u32) -> Result<String> {
|
||||
let expires_at = Utc::now() + Duration::seconds(300);
|
||||
let table: String = ResourceEnum::OtpCache.to_string();
|
||||
info!(query = %format!("CREATE {}:{}", table, email), "Executing SurrealDB query");
|
||||
let record: Option<AuthOtpSchema> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
@@ -152,7 +166,9 @@ impl<'a> AuthRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email), err)]
|
||||
pub async fn query_delete_stored_otp(&self, email: String) -> Result<String> {
|
||||
info!(query = %format!("DELETE FROM {} WHERE id = '{}'", ResourceEnum::OtpCache.to_string(), email), "Executing SurrealDB query");
|
||||
let record: Option<AuthOtpSchema> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
|
||||
@@ -7,6 +7,7 @@ use imphnen_utils::{DetailQueryBuilder, QueryListBuilder, extract_id};
|
||||
use serde_json;
|
||||
use std::time::Instant;
|
||||
use tracing::instrument;
|
||||
use tracing::info;
|
||||
|
||||
pub struct PermissionsRepository<'a> {
|
||||
state: &'a AppState,
|
||||
@@ -23,6 +24,7 @@ impl<'a> PermissionsRepository<'a> {
|
||||
meta: MetaRequestDto,
|
||||
) -> Result<ResponseListSuccessDto<Vec<PermissionsItemDto>>> {
|
||||
let now = Instant::now();
|
||||
info!("Executing SurrealDB query: QueryListBuilder for Permissions with meta: {:?}", meta);
|
||||
let raw_result: ResponseListSuccessDto<Vec<PermissionsSchema>> =
|
||||
QueryListBuilder::new(
|
||||
&self.state.surrealdb_ws,
|
||||
@@ -61,6 +63,7 @@ impl<'a> PermissionsRepository<'a> {
|
||||
) -> Result<PermissionsSchema> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
info!(id = %id, "Executing SurrealDB select for Permissions");
|
||||
let result: Option<PermissionsSchema> = db
|
||||
.select((ResourceEnum::Permissions.to_string(), id.clone()))
|
||||
.await?;
|
||||
@@ -82,6 +85,7 @@ impl<'a> PermissionsRepository<'a> {
|
||||
id: String,
|
||||
) -> Result<PermissionsItemDto> {
|
||||
let now = Instant::now();
|
||||
info!(id = %id, "Executing transformed_query_permission_by_id (delegates to query_permission_by_id)");
|
||||
let raw_result = self.query_permission_by_id(id.clone()).await?;
|
||||
let elapsed = now.elapsed();
|
||||
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||
@@ -109,6 +113,7 @@ impl<'a> PermissionsRepository<'a> {
|
||||
.with_where("name", Some(name.clone()))
|
||||
.with_select_fields(vec!["*"]);
|
||||
let sql = builder.build();
|
||||
info!(query = %sql, "Executing SurrealDB query");
|
||||
let result: Option<PermissionsSchema> =
|
||||
builder.apply_bindings(db.query(sql)).await?.take(0)?;
|
||||
let elapsed = now.elapsed();
|
||||
@@ -130,6 +135,7 @@ impl<'a> PermissionsRepository<'a> {
|
||||
) -> Result<String> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
info!("Executing SurrealDB create for Permissions with data: {:?}", data);
|
||||
let record: Option<PermissionsSchema> = db
|
||||
.create(ResourceEnum::Permissions.to_string())
|
||||
.content(data)
|
||||
@@ -162,6 +168,7 @@ impl<'a> PermissionsRepository<'a> {
|
||||
created_at: existing.created_at,
|
||||
..data.clone()
|
||||
};
|
||||
info!(record_key = ?record_key, "Executing SurrealDB update for Permissions");
|
||||
let record: Option<PermissionsSchema> =
|
||||
db.update(record_key).merge(merged).await?;
|
||||
let elapsed = now.elapsed();
|
||||
@@ -188,6 +195,7 @@ impl<'a> PermissionsRepository<'a> {
|
||||
bail!("Permission already deleted");
|
||||
}
|
||||
let record_key = get_id(&permission.id)?;
|
||||
info!(record_key = ?record_key, "Executing SurrealDB soft delete for Permissions");
|
||||
let record: Option<PermissionsSchema> = db
|
||||
.update(record_key)
|
||||
.merge(serde_json::json!({ "is_deleted": true }))
|
||||
|
||||
Reference in New Issue
Block a user