Skip to content

Commit 33b5657

Browse files
feat: release/v0.1.13 (#496)
* feat: add usage per day/user/model (#494) * feat: add usage per day/user/model * update db to date * rename timestamp to date * fix * add user id to request headers (#495) --------- Co-authored-by: Martin Stefcek <35243812+Cifko@users.noreply.github.com>
1 parent 1e285f1 commit 33b5657

File tree

7 files changed

+103
-4
lines changed

7 files changed

+103
-4
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atoma-proxy/src/server/handlers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ pub fn update_state_manager_fiat(
186186
user_id,
187187
estimated_input_amount,
188188
input_amount,
189+
input_tokens,
189190
estimated_output_amount,
190191
output_amount,
192+
output_tokens,
191193
model_name,
192194
})
193195
.map_err(|e| AtomaProxyError::InternalError {

atoma-proxy/src/server/middleware.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ pub async fn confidential_compute_middleware(
607607
req_parts
608608
.headers
609609
.insert(constants::SIGNATURE, signature_header);
610+
req_parts.headers.insert(
611+
constants::USER_ID,
612+
HeaderValue::from_str(&user_id.to_string()).unwrap(),
613+
);
610614
let request_metadata = req_parts
611615
.extensions
612616
.get::<RequestMetadataExtension>()
@@ -2514,6 +2518,10 @@ pub mod utils {
25142518
req_parts
25152519
.headers
25162520
.insert(CONTENT_LENGTH, content_length_header);
2521+
req_parts.headers.insert(
2522+
constants::USER_ID,
2523+
HeaderValue::from_str(&user_id.to_string()).unwrap(),
2524+
);
25172525
let request_model = body_json
25182526
.get(MODEL)
25192527
.and_then(|m| m.as_str())

atoma-state/src/handlers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,8 +1435,10 @@ pub async fn handle_state_manager_event(
14351435
model_name,
14361436
estimated_input_amount,
14371437
input_amount,
1438+
input_tokens,
14381439
estimated_output_amount,
14391440
output_amount,
1441+
output_tokens,
14401442
} => {
14411443
state_manager
14421444
.state
@@ -1449,6 +1451,17 @@ pub async fn handle_state_manager_event(
14491451
)
14501452
.await?;
14511453
if output_amount > 0 {
1454+
state_manager
1455+
.state
1456+
.update_per_day_table(
1457+
user_id,
1458+
model_name.clone(),
1459+
input_amount,
1460+
input_tokens,
1461+
output_amount,
1462+
output_tokens,
1463+
)
1464+
.await?;
14521465
state_manager
14531466
.state
14541467
.update_usage_per_model(user_id, model_name, input_amount, output_amount)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE
2+
IF NOT EXISTS usage_per_day (
3+
user_id BIGINT NOT NULL,
4+
date DATE DEFAULT CURRENT_DATE NOT NULL,
5+
model TEXT NOT NULL,
6+
input_amount BIGINT NOT NULL DEFAULT 0,
7+
input_tokens BIGINT NOT NULL DEFAULT 0,
8+
output_amount BIGINT NOT NULL DEFAULT 0,
9+
output_tokens BIGINT NOT NULL DEFAULT 0,
10+
UNIQUE (user_id, date, model)
11+
);

atoma-state/src/state_manager.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4499,6 +4499,67 @@ impl AtomaState {
44994499
Ok(())
45004500
}
45014501

4502+
/// Updates the usage per day for a user and model.
4503+
///
4504+
/// This method updates the `usage_per_day` table for the specified user and model.
4505+
///
4506+
/// # Arguments
4507+
///
4508+
/// * `user_id` - The unique identifier of the user.
4509+
/// * `model` - The name of the model.
4510+
/// * `input_amount` - The input amount for the model.
4511+
/// * `input_tokens` - The input tokens for the model.
4512+
/// * `output_amount` - The output amount for the model.
4513+
/// * `output_tokens` - The output tokens for the model.
4514+
///
4515+
/// # Returns
4516+
///
4517+
/// - `Result<()>`: A result indicating success (Ok(())) or failure (Err(AtomaStateManagerError)).
4518+
///
4519+
/// # Errors
4520+
///
4521+
/// This function will return an error if:
4522+
/// - The database query fails to execute.
4523+
///
4524+
/// # Example
4525+
///
4526+
/// ```rust,ignore
4527+
/// use atoma_node::atoma_state::AtomaStateManager;
4528+
/// use chrono::Utc;
4529+
/// async fn update_usage_per_day(state_manager: &AtomaStateManager, user_id: i64, model: String, input_amount: i64, input_tokens: i64, output_amount: i64, output_tokens: i64) -> Result<(), AtomaStateManagerError> {
4530+
/// state_manager.update_per_day_table(user_id, model, input_amount, input_tokens, output_amount, output_tokens).await
4531+
/// }
4532+
/// ```
4533+
#[instrument(level = "trace", skip(self))]
4534+
pub async fn update_per_day_table(
4535+
&self,
4536+
user_id: i64,
4537+
model: String,
4538+
input_amount: i64,
4539+
input_tokens: i64,
4540+
output_amount: i64,
4541+
output_tokens: i64,
4542+
) -> Result<()> {
4543+
sqlx::query(
4544+
"INSERT INTO usage_per_day (user_id, model, input_amount, input_tokens, output_amount, output_tokens)
4545+
VALUES ($1, $2, $3, $4, $5, $6)
4546+
ON CONFLICT (user_id, model, date) DO UPDATE SET
4547+
input_amount = usage_per_day.input_amount + EXCLUDED.input_amount,
4548+
input_tokens = usage_per_day.input_tokens + EXCLUDED.input_tokens,
4549+
output_amount = usage_per_day.output_amount + EXCLUDED.output_amount,
4550+
output_tokens = usage_per_day.output_tokens + EXCLUDED.output_tokens",
4551+
)
4552+
.bind(user_id)
4553+
.bind(model)
4554+
.bind(input_amount)
4555+
.bind(input_tokens)
4556+
.bind(output_amount)
4557+
.bind(output_tokens)
4558+
.execute(&self.db)
4559+
.await?;
4560+
Ok(())
4561+
}
4562+
45024563
/// Updates the usage per model for a user.
45034564
///
45044565
/// This method updates the `usage_per_model` table for the specified user and model.

atoma-state/src/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,13 @@ pub enum AtomaAtomaStateManagerEvent {
918918
estimated_input_amount: i64,
919919
/// The actual input amount
920920
input_amount: i64,
921+
/// Number of input tokens,
922+
input_tokens: i64,
921923
/// The original estimated output amount
922924
estimated_output_amount: i64,
923925
/// The actual output amount
924926
output_amount: i64,
927+
/// Number of output tokens
928+
output_tokens: i64,
925929
},
926930
}

0 commit comments

Comments
 (0)