nxtgauge-backend-rust/apps/users/src/ai/usage.rs
Ashwin Kumar Sivakumar c85e6af22e feat(ai): complete AI plans/credits implementation and build tooling
- Add AI plans, credits, model routing, LiteLLM client, and orchestrator services
- Add AI management endpoints, auto-apply/auto-request handlers, and log endpoints
- Add cron jobs for daily action reset and monthly credit reset
- Add AI credit purchase flow in payments service
- Add ai_credit_packages migration with seed data
- Update Dockerfile build tooling across services
2026-06-15 06:15:49 +05:30

40 lines
997 B
Rust

use db::models::ai::AiUsageLogRepository;
use sqlx::PgPool;
use uuid::Uuid;
/// Log an AI usage event. Errors are logged but not propagated, because usage
/// logging should never break the user-facing response.
#[allow(clippy::too_many_arguments)]
pub async fn log_usage(
pool: &PgPool,
user_id: Uuid,
role_code: Option<&str>,
feature_code: &str,
model_alias: &str,
credits_charged: i32,
input_tokens: Option<i32>,
output_tokens: Option<i32>,
total_tokens: Option<i32>,
status: &str,
request_id: Option<&str>,
error_message: Option<&str>,
) {
if let Err(e) = AiUsageLogRepository::create(
pool,
user_id,
role_code,
feature_code,
model_alias,
credits_charged,
input_tokens,
output_tokens,
total_tokens,
status,
request_id,
error_message,
)
.await
{
tracing::error!("Failed to log AI usage for user {}: {}", user_id, e);
}
}