fix: Face processor now saves status and total_faces to face.json
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
use std::cell::RefCell;
|
||||
|
||||
thread_local! {
|
||||
pub static CURRENT_REQUEST_ID: RefCell<Option<String>> = RefCell::new(None);
|
||||
pub static CURRENT_API_KEY_ID: RefCell<Option<String>> = RefCell::new(None);
|
||||
}
|
||||
|
||||
pub fn set_request_context(request_id: String, key_id: Option<String>) {
|
||||
CURRENT_REQUEST_ID.with(|cell| *cell.borrow_mut() = Some(request_id));
|
||||
CURRENT_API_KEY_ID.with(|cell| *cell.borrow_mut() = key_id);
|
||||
}
|
||||
|
||||
pub fn get_request_id() -> Option<String> {
|
||||
CURRENT_REQUEST_ID.with(|cell| cell.borrow().clone())
|
||||
}
|
||||
|
||||
pub fn get_key_id() -> Option<String> {
|
||||
CURRENT_API_KEY_ID.with(|cell| cell.borrow().clone())
|
||||
}
|
||||
|
||||
pub fn clear_request_context() {
|
||||
CURRENT_REQUEST_ID.with(|cell| *cell.borrow_mut() = None);
|
||||
CURRENT_API_KEY_ID.with(|cell| *cell.borrow_mut() = None);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
use anyhow::Result;
|
||||
use tracing::error;
|
||||
|
||||
use crate::core::db::PostgresDb;
|
||||
|
||||
use super::context::{get_key_id, get_request_id};
|
||||
use super::models::{LogTokenUsage, TokenUsage};
|
||||
|
||||
pub struct TokenLogger;
|
||||
|
||||
impl TokenLogger {
|
||||
pub async fn log(
|
||||
db: &PostgresDb,
|
||||
service: &str,
|
||||
model: Option<&str>,
|
||||
usage: TokenUsage,
|
||||
endpoint: Option<&str>,
|
||||
duration_ms: Option<i32>,
|
||||
) -> Result<()> {
|
||||
let request_id =
|
||||
get_request_id().unwrap_or_else(|| uuid::Uuid::new_v4().to_string().replace('-', ""));
|
||||
let key_id = get_key_id();
|
||||
|
||||
let log = LogTokenUsage {
|
||||
request_id,
|
||||
key_id,
|
||||
service: service.to_string(),
|
||||
model: model.map(|s| s.to_string()),
|
||||
endpoint: endpoint.map(|s| s.to_string()),
|
||||
input_tokens: usage.input_tokens,
|
||||
output_tokens: usage.output_tokens,
|
||||
total_tokens: usage.total_tokens,
|
||||
duration_ms,
|
||||
status_code: Some(200),
|
||||
error_message: None,
|
||||
};
|
||||
|
||||
if let Err(e) = db.log_token_usage(&log).await {
|
||||
error!("[BILLING] Failed to log token usage: {:?}", e);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn log_error(
|
||||
db: &PostgresDb,
|
||||
service: &str,
|
||||
model: Option<&str>,
|
||||
endpoint: Option<&str>,
|
||||
error_message: &str,
|
||||
) -> Result<()> {
|
||||
let request_id =
|
||||
get_request_id().unwrap_or_else(|| uuid::Uuid::new_v4().to_string().replace('-', ""));
|
||||
let key_id = get_key_id();
|
||||
|
||||
let log = LogTokenUsage {
|
||||
request_id,
|
||||
key_id,
|
||||
service: service.to_string(),
|
||||
model: model.map(|s| s.to_string()),
|
||||
endpoint: endpoint.map(|s| s.to_string()),
|
||||
input_tokens: 0,
|
||||
output_tokens: 0,
|
||||
total_tokens: 0,
|
||||
duration_ms: None,
|
||||
status_code: Some(500),
|
||||
error_message: Some(error_message.to_string()),
|
||||
};
|
||||
|
||||
if let Err(e) = db.log_token_usage(&log).await {
|
||||
error!("[BILLING] Failed to log error: {:?}", e);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
pub mod context;
|
||||
pub mod logger;
|
||||
pub mod models;
|
||||
|
||||
pub use context::{clear_request_context, get_key_id, get_request_id, set_request_context};
|
||||
pub use logger::TokenLogger;
|
||||
pub use models::{
|
||||
BillingPeriod, HistoryResponse, LogTokenUsage, PeriodSummary, TokenStats, TokenUsage,
|
||||
UsageResponse,
|
||||
};
|
||||
@@ -0,0 +1,68 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TokenUsage {
|
||||
pub input_tokens: u32,
|
||||
pub output_tokens: u32,
|
||||
pub total_tokens: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LogTokenUsage {
|
||||
pub request_id: String,
|
||||
pub key_id: Option<String>,
|
||||
pub service: String,
|
||||
pub model: Option<String>,
|
||||
pub endpoint: Option<String>,
|
||||
pub input_tokens: u32,
|
||||
pub output_tokens: u32,
|
||||
pub total_tokens: u32,
|
||||
pub duration_ms: Option<i32>,
|
||||
pub status_code: Option<i32>,
|
||||
pub error_message: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BillingPeriod {
|
||||
pub id: i32,
|
||||
pub key_id: Option<String>,
|
||||
pub period_month: String,
|
||||
pub total_input_tokens: i64,
|
||||
pub total_output_tokens: i64,
|
||||
pub total_tokens: i64,
|
||||
pub request_count: i64,
|
||||
pub error_count: i64,
|
||||
pub service_breakdown: serde_json::Value,
|
||||
pub model_breakdown: serde_json::Value,
|
||||
pub avg_duration_ms: Option<i32>,
|
||||
pub total_duration_ms: Option<i64>,
|
||||
pub period_start: String,
|
||||
pub period_end: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UsageResponse {
|
||||
pub key_id: Option<String>,
|
||||
pub period: String,
|
||||
pub tokens: TokenStats,
|
||||
pub services: serde_json::Value,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TokenStats {
|
||||
pub input: i64,
|
||||
pub output: i64,
|
||||
pub total: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct HistoryResponse {
|
||||
pub periods: Vec<PeriodSummary>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PeriodSummary {
|
||||
pub period: String,
|
||||
pub total_tokens: i64,
|
||||
pub request_count: i64,
|
||||
}
|
||||
Reference in New Issue
Block a user