-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
collab: Add billing preferences for maximum LLM monthly spend (#18948)
This PR adds a new `billing_preferences` table. Right now there is a single preference: the maximum monthly spend for LLM usage. Release Notes: - N/A --------- Co-authored-by: Richard <richard@zed.dev>
- Loading branch information
1 parent
7111809
commit d316577
Showing
10 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
crates/collab/migrations/20241009190639_add_billing_preferences.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
create table if not exists billing_preferences ( | ||
id serial primary key, | ||
created_at timestamp without time zone not null default now(), | ||
user_id integer not null references users(id) on delete cascade, | ||
max_monthly_llm_usage_spending_in_cents integer not null | ||
); | ||
|
||
create unique index "uix_billing_preferences_on_user_id" on billing_preferences (user_id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use super::*; | ||
|
||
#[derive(Debug)] | ||
pub struct CreateBillingPreferencesParams { | ||
pub max_monthly_llm_usage_spending_in_cents: i32, | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub struct UpdateBillingPreferencesParams { | ||
pub max_monthly_llm_usage_spending_in_cents: ActiveValue<i32>, | ||
} | ||
|
||
impl Database { | ||
/// Returns the billing preferences for the given user, if they exist. | ||
pub async fn get_billing_preferences( | ||
&self, | ||
user_id: UserId, | ||
) -> Result<Option<billing_preference::Model>> { | ||
self.transaction(|tx| async move { | ||
Ok(billing_preference::Entity::find() | ||
.filter(billing_preference::Column::UserId.eq(user_id)) | ||
.one(&*tx) | ||
.await?) | ||
}) | ||
.await | ||
} | ||
|
||
/// Creates new billing preferences for the given user. | ||
pub async fn create_billing_preferences( | ||
&self, | ||
user_id: UserId, | ||
params: &CreateBillingPreferencesParams, | ||
) -> Result<billing_preference::Model> { | ||
self.transaction(|tx| async move { | ||
let preferences = billing_preference::Entity::insert(billing_preference::ActiveModel { | ||
user_id: ActiveValue::set(user_id), | ||
max_monthly_llm_usage_spending_in_cents: ActiveValue::set( | ||
params.max_monthly_llm_usage_spending_in_cents, | ||
), | ||
..Default::default() | ||
}) | ||
.exec_with_returning(&*tx) | ||
.await?; | ||
|
||
Ok(preferences) | ||
}) | ||
.await | ||
} | ||
|
||
/// Updates the billing preferences for the given user. | ||
pub async fn update_billing_preferences( | ||
&self, | ||
user_id: UserId, | ||
params: &UpdateBillingPreferencesParams, | ||
) -> Result<billing_preference::Model> { | ||
self.transaction(|tx| async move { | ||
let preferences = billing_preference::Entity::update_many() | ||
.set(billing_preference::ActiveModel { | ||
max_monthly_llm_usage_spending_in_cents: params | ||
.max_monthly_llm_usage_spending_in_cents | ||
.clone(), | ||
..Default::default() | ||
}) | ||
.filter(billing_preference::Column::UserId.eq(user_id)) | ||
.exec_with_returning(&*tx) | ||
.await?; | ||
|
||
Ok(preferences | ||
.into_iter() | ||
.next() | ||
.ok_or_else(|| anyhow!("billing preferences not found"))?) | ||
}) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::db::{BillingPreferencesId, UserId}; | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, Default, PartialEq, Eq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "billing_preferences")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: BillingPreferencesId, | ||
pub created_at: DateTime, | ||
pub user_id: UserId, | ||
pub max_monthly_llm_usage_spending_in_cents: i32, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm( | ||
belongs_to = "super::user::Entity", | ||
from = "Column::UserId", | ||
to = "super::user::Column::Id" | ||
)] | ||
User, | ||
} | ||
|
||
impl Related<super::user::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::User.def() | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters