feat: add logging for SurrealDB queries across multiple repositories
This commit is contained in:
@@ -3,7 +3,7 @@ use crate::{AppState, ResourceEnum};
|
||||
use anyhow::{Result, bail};
|
||||
use imphnen_iam::DetailQueryBuilder;
|
||||
use std::time::Instant;
|
||||
use tracing::instrument;
|
||||
use tracing::{instrument, info};
|
||||
|
||||
pub struct GachaClaimRepository<'a> {
|
||||
state: &'a AppState,
|
||||
@@ -27,6 +27,7 @@ impl<'a> GachaClaimRepository<'a> {
|
||||
.with_fetch("item")
|
||||
.with_fetch("user");
|
||||
let sql = builder.build();
|
||||
info!(query = %sql, "Executing SurrealDB query");
|
||||
let result: Option<GachaClaimQueryDto> =
|
||||
builder.apply_bindings(db.query(sql)).await?.take(0)?;
|
||||
let elapsed = now.elapsed();
|
||||
@@ -48,6 +49,11 @@ impl<'a> GachaClaimRepository<'a> {
|
||||
) -> Result<String> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
info!(
|
||||
resource = %ResourceEnum::GachaClaims.to_string(),
|
||||
content = ?data,
|
||||
"Executing SurrealDB create query"
|
||||
);
|
||||
let record: Option<GachaClaimSchema> = db
|
||||
.create(ResourceEnum::GachaClaims.to_string())
|
||||
.content(data)
|
||||
|
||||
@@ -4,7 +4,7 @@ use anyhow::{Result, bail};
|
||||
use imphnen_iam::make_thing;
|
||||
use std::time::Instant;
|
||||
use surrealdb::Uuid;
|
||||
use tracing::instrument;
|
||||
use tracing::{instrument, info};
|
||||
|
||||
pub struct GachaCreditRepository<'a> {
|
||||
state: &'a AppState,
|
||||
@@ -27,6 +27,7 @@ impl<'a> GachaCreditRepository<'a> {
|
||||
ResourceEnum::GachaCredits,
|
||||
ResourceEnum::Users
|
||||
);
|
||||
info!(query = %sql, "Executing SurrealDB query");
|
||||
let result: Vec<GachaCreditSchema> =
|
||||
db.query(sql).bind(("user_id", user_id)).await?.take(0)?;
|
||||
let elapsed = now.elapsed();
|
||||
@@ -66,6 +67,7 @@ impl<'a> GachaCreditRepository<'a> {
|
||||
bail!("No extra roll credits remaining");
|
||||
}
|
||||
credit.available_rolls -= 1;
|
||||
info!(operation = "update", table = %ResourceEnum::GachaCredits.to_string(), id = %credit.id.id.to_raw(), "Executing SurrealDB update for consume_credit");
|
||||
let _: Option<GachaCreditSchema> = db
|
||||
.update((
|
||||
&ResourceEnum::GachaCredits.to_string(),
|
||||
@@ -91,6 +93,7 @@ impl<'a> GachaCreditRepository<'a> {
|
||||
let db = &self.state.surrealdb_ws;
|
||||
if let Some(mut credit) = self.query_by_user_id(payload.user_id.clone()).await? {
|
||||
credit.available_rolls += payload.amount;
|
||||
info!(operation = "update", table = %ResourceEnum::GachaCredits.to_string(), id = %credit.id.id.to_raw(), "Executing SurrealDB update for add_credit");
|
||||
let _: Option<GachaCreditSchema> = db
|
||||
.update((
|
||||
&ResourceEnum::GachaCredits.to_string(),
|
||||
@@ -108,6 +111,7 @@ impl<'a> GachaCreditRepository<'a> {
|
||||
available_rolls: payload.amount,
|
||||
..Default::default()
|
||||
};
|
||||
info!(operation = "create", table = %ResourceEnum::GachaCredits.to_string(), "Executing SurrealDB create for add_credit");
|
||||
let _: Option<GachaCreditSchema> = db
|
||||
.create(ResourceEnum::GachaCredits.to_string())
|
||||
.content(data)
|
||||
|
||||
@@ -9,6 +9,7 @@ use imphnen_utils::get_iso_date;
|
||||
use serde_json::{Map, Value};
|
||||
use std::time::Instant;
|
||||
use tracing::instrument;
|
||||
use tracing::info;
|
||||
|
||||
pub struct GachaItemRepository<'a> {
|
||||
state: &'a AppState,
|
||||
@@ -25,6 +26,11 @@ impl<'a> GachaItemRepository<'a> {
|
||||
meta: MetaRequestDto,
|
||||
) -> Result<ResponseListSuccessDto<Vec<GachaItemDto>>> {
|
||||
let now = Instant::now();
|
||||
let surreal_query = format!(
|
||||
"SELECT * FROM {} WHERE is_deleted = false AND name LIKE ?",
|
||||
ResourceEnum::GachaItems.to_string()
|
||||
);
|
||||
info!(query = %surreal_query, "Executing SurrealDB query");
|
||||
let raw_result: ResponseListSuccessDto<Vec<GachaItemSchema>> =
|
||||
QueryListBuilder::new(
|
||||
&self.state.surrealdb_ws,
|
||||
@@ -57,6 +63,8 @@ impl<'a> GachaItemRepository<'a> {
|
||||
pub async fn query_gacha_item_by_id(&self, id: String) -> Result<GachaItemSchema> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
let surreal_query = format!("SELECT * FROM {} WHERE id = '{}'", ResourceEnum::GachaItems.to_string(), id);
|
||||
info!(query = %surreal_query, "Executing SurrealDB query");
|
||||
let result: Option<GachaItemSchema> = db
|
||||
.select((ResourceEnum::GachaItems.to_string(), id.clone()))
|
||||
.await?;
|
||||
@@ -79,6 +87,8 @@ impl<'a> GachaItemRepository<'a> {
|
||||
) -> Result<String> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
let surreal_query = format!("CREATE {} CONTENT ...", ResourceEnum::GachaItems.to_string());
|
||||
info!(query = %surreal_query, "Executing SurrealDB query");
|
||||
let record: Option<GachaItemSchema> = db
|
||||
.create(ResourceEnum::GachaItems.to_string())
|
||||
.content(data)
|
||||
@@ -111,6 +121,8 @@ impl<'a> GachaItemRepository<'a> {
|
||||
created_at: existing.created_at,
|
||||
..data.clone()
|
||||
};
|
||||
let surreal_query = format!("UPDATE {:?} MERGE ...", record_key);
|
||||
info!(query = %surreal_query, "Executing SurrealDB query");
|
||||
let record: Option<GachaItemSchema> =
|
||||
db.update(record_key).merge(merged).await?;
|
||||
let elapsed = now.elapsed();
|
||||
@@ -139,6 +151,8 @@ impl<'a> GachaItemRepository<'a> {
|
||||
patch.insert("is_deleted".to_string(), Value::Bool(true));
|
||||
patch.insert("updated_at".to_string(), Value::String(get_iso_date()));
|
||||
|
||||
let surreal_query = format!("UPDATE {:?} MERGE ...", record_key);
|
||||
info!(query = %surreal_query, "Executing SurrealDB query");
|
||||
let record: Option<GachaItemSchema> = db.update(record_key).merge(patch).await?;
|
||||
let elapsed = now.elapsed();
|
||||
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||
|
||||
@@ -10,6 +10,7 @@ use rand_distr::weighted::WeightedIndex;
|
||||
use serde_json::{Map, Value};
|
||||
use std::time::Instant;
|
||||
use tracing::instrument;
|
||||
use tracing::info;
|
||||
|
||||
pub struct GachaRollRepository<'a> {
|
||||
state: &'a AppState,
|
||||
@@ -33,6 +34,7 @@ impl<'a> GachaRollRepository<'a> {
|
||||
.with_select_fields(vec!["*"])
|
||||
.with_fetch("item");
|
||||
let sql = builder.build();
|
||||
info!(query = %sql, "Executing SurrealDB query");
|
||||
let result: Option<GachaRollQueryDto> =
|
||||
builder.apply_bindings(db.query(sql)).await?.take(0)?;
|
||||
let elapsed = now.elapsed();
|
||||
@@ -54,6 +56,7 @@ impl<'a> GachaRollRepository<'a> {
|
||||
) -> Result<String> {
|
||||
let now = Instant::now();
|
||||
let db = &self.state.surrealdb_ws;
|
||||
info!(query = "CREATE", "Executing SurrealDB create operation for GachaRolls");
|
||||
let record: Option<GachaRollSchema> = db
|
||||
.create(ResourceEnum::GachaRolls.to_string())
|
||||
.content(data)
|
||||
@@ -77,6 +80,7 @@ impl<'a> GachaRollRepository<'a> {
|
||||
let table_name = ResourceEnum::GachaRolls.to_string();
|
||||
let sql =
|
||||
format!("SELECT * FROM {table_name} WHERE is_deleted = false FETCH item");
|
||||
info!(query = %sql, "Executing SurrealDB query");
|
||||
let result: Vec<GachaRollQueryDto> = db.query(sql).await?.take(0)?;
|
||||
let elapsed = now.elapsed();
|
||||
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||
@@ -87,6 +91,7 @@ impl<'a> GachaRollRepository<'a> {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
pub fn roll_once(rolls: &[GachaRollQueryDto]) -> Option<GachaRollQueryDto> {
|
||||
let filtered: Vec<_> = rolls
|
||||
.iter()
|
||||
@@ -120,6 +125,7 @@ impl<'a> GachaRollRepository<'a> {
|
||||
patch.insert("is_deleted".to_string(), Value::Bool(true));
|
||||
patch.insert("updated_at".to_string(), Value::String(get_iso_date()));
|
||||
|
||||
info!(query = "UPDATE", record_key = ?record_key, "Executing SurrealDB update operation for GachaRolls");
|
||||
let record: Option<GachaRollSchema> = db.update(record_key).merge(patch).await?;
|
||||
let elapsed = now.elapsed();
|
||||
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
|
||||
|
||||
Reference in New Issue
Block a user