Skip to content

Commit c1f767d

Browse files
committed
usage seperate by cost per second
1 parent 7e74bd8 commit c1f767d

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

lapdev-conductor/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ impl Conductor {
545545
let usage = self
546546
.enterprise
547547
.usage
548-
.get_monthly_cost(org.id, None, Utc::now().into(), None)
548+
.get_monthly_cost(org.id, None, None, Utc::now().into(), None)
549549
.await
550550
.unwrap_or(0);
551551
if usage as i64 >= org.usage_limit {

lapdev-enterprise/src/enterprise.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Enterprise {
7474
if organization.usage_limit > 0 {
7575
let usage = self
7676
.usage
77-
.get_monthly_cost(organization.id, None, Utc::now().into(), None)
77+
.get_monthly_cost(organization.id, None, None, Utc::now().into(), None)
7878
.await?;
7979
if usage as i64 >= organization.usage_limit {
8080
return Err(ApiError::InvalidRequest(
@@ -97,6 +97,7 @@ impl Enterprise {
9797
.get_monthly_cost(
9898
org.id,
9999
Some(user_id),
100+
None,
100101
Utc::now().into(),
101102
None,
102103
)

lapdev-enterprise/src/quota.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ impl Quota {
218218
QuotaKind::Project => 0,
219219
QuotaKind::DailyCost => {
220220
self.usage
221-
.get_daily_cost(organization, Some(user), Utc::now().into(), None)
221+
.get_daily_cost(organization, Some(user), None, Utc::now().into(), None)
222222
.await?
223223
}
224224
QuotaKind::MonthlyCost => {
225225
self.usage
226-
.get_monthly_cost(organization, Some(user), Utc::now().into(), None)
226+
.get_monthly_cost(organization, Some(user), None, Utc::now().into(), None)
227227
.await?
228228
}
229229
};
@@ -264,14 +264,14 @@ impl Quota {
264264
}
265265
QuotaKind::DailyCost => {
266266
self.usage
267-
.get_daily_cost(organization, None, Utc::now().into(), None)
267+
.get_daily_cost(organization, None, None, Utc::now().into(), None)
268268
.await?
269269
/ 60
270270
/ 60
271271
}
272272
QuotaKind::MonthlyCost => {
273273
self.usage
274-
.get_monthly_cost(organization, None, Utc::now().into(), None)
274+
.get_monthly_cost(organization, None, None, Utc::now().into(), None)
275275
.await?
276276
/ 60
277277
/ 60

lapdev-enterprise/src/usage.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ impl Usage {
169169
})
170170
.collect();
171171

172-
let total_cost = self.get_cost(organization, user, start, end, now).await?;
172+
let total_cost = self
173+
.get_cost(organization, user, None, start, end, now)
174+
.await?;
173175
Ok(UsageResult {
174176
total_items: items_and_pages.number_of_items,
175177
num_pages: items_and_pages.number_of_pages,
@@ -184,6 +186,7 @@ impl Usage {
184186
&self,
185187
organization: Uuid,
186188
user: Option<Uuid>,
189+
cost_per_second: Option<i32>,
187190
start: DateTime<FixedOffset>,
188191
end: DateTime<FixedOffset>,
189192
now: DateTime<FixedOffset>,
@@ -196,6 +199,9 @@ impl Usage {
196199
if let Some(user) = user {
197200
query = query.filter(entities::usage::Column::UserId.eq(user));
198201
}
202+
if let Some(cost_per_second) = cost_per_second {
203+
query = query.filter(entities::usage::Column::CostPerSecond.eq(cost_per_second));
204+
}
199205
let fixed_cost: Vec<Option<i64>> = query
200206
.select_only()
201207
.column_as(entities::usage::Column::TotalCost.sum(), "cost")
@@ -228,6 +234,9 @@ impl Usage {
228234
if let Some(user) = user {
229235
query = query.filter(entities::usage::Column::UserId.eq(user));
230236
}
237+
if let Some(cost_per_second) = cost_per_second {
238+
query = query.filter(entities::usage::Column::CostPerSecond.eq(cost_per_second));
239+
}
231240

232241
let usages = query.all(&self.db.conn).await?;
233242

@@ -256,6 +265,7 @@ impl Usage {
256265
&self,
257266
organization: Uuid,
258267
user: Option<Uuid>,
268+
cost_per_second: Option<i32>,
259269
date: DateTime<FixedOffset>,
260270
now: Option<DateTime<FixedOffset>>,
261271
) -> Result<usize> {
@@ -268,6 +278,7 @@ impl Usage {
268278
self.get_cost(
269279
organization,
270280
user,
281+
cost_per_second,
271282
day_start,
272283
day_end,
273284
now.unwrap_or_else(|| Utc::now().into()),
@@ -279,6 +290,7 @@ impl Usage {
279290
&self,
280291
organization: Uuid,
281292
user: Option<Uuid>,
293+
cost_per_second: Option<i32>,
282294
date: DateTime<FixedOffset>,
283295
now: Option<DateTime<FixedOffset>>,
284296
) -> Result<usize> {
@@ -305,6 +317,7 @@ impl Usage {
305317
self.get_cost(
306318
organization,
307319
user,
320+
cost_per_second,
308321
month_start,
309322
month_end,
310323
now.unwrap_or_else(|| Utc::now().into()),
@@ -414,6 +427,7 @@ pub mod tests {
414427
.get_daily_cost(
415428
organization,
416429
Some(user.id),
430+
None,
417431
Utc.with_ymd_and_hms(2024, 1, 29, 1, 0, 0).unwrap().into(),
418432
None,
419433
)
@@ -436,6 +450,7 @@ pub mod tests {
436450
.get_daily_cost(
437451
organization,
438452
Some(user.id),
453+
None,
439454
Utc.with_ymd_and_hms(2024, 1, 29, 1, 0, 0).unwrap().into(),
440455
None,
441456
)
@@ -458,6 +473,7 @@ pub mod tests {
458473
.get_daily_cost(
459474
organization,
460475
Some(user.id),
476+
None,
461477
Utc.with_ymd_and_hms(2024, 1, 29, 1, 0, 0).unwrap().into(),
462478
Some(Utc.with_ymd_and_hms(2024, 1, 29, 15, 1, 0).unwrap().into()),
463479
)
@@ -470,6 +486,7 @@ pub mod tests {
470486
.get_daily_cost(
471487
organization,
472488
Some(user.id),
489+
None,
473490
Utc.with_ymd_and_hms(2024, 1, 30, 1, 0, 0).unwrap().into(),
474491
Some(Utc.with_ymd_and_hms(2024, 1, 30, 1, 0, 0).unwrap().into()),
475492
)
@@ -492,6 +509,7 @@ pub mod tests {
492509
.get_daily_cost(
493510
organization,
494511
Some(user.id),
512+
None,
495513
Utc.with_ymd_and_hms(2024, 1, 29, 1, 0, 0).unwrap().into(),
496514
Some(Utc.with_ymd_and_hms(2024, 1, 29, 15, 1, 0).unwrap().into()),
497515
)
@@ -514,6 +532,7 @@ pub mod tests {
514532
.get_daily_cost(
515533
organization,
516534
Some(user.id),
535+
None,
517536
Utc.with_ymd_and_hms(2024, 1, 29, 1, 0, 0).unwrap().into(),
518537
Some(Utc.with_ymd_and_hms(2024, 1, 29, 15, 1, 0).unwrap().into()),
519538
)
@@ -561,6 +580,7 @@ pub mod tests {
561580
.get_monthly_cost(
562581
organization,
563582
Some(user.id),
583+
None,
564584
Utc.with_ymd_and_hms(2024, 1, 29, 1, 0, 0).unwrap().into(),
565585
None,
566586
)
@@ -583,6 +603,7 @@ pub mod tests {
583603
.get_monthly_cost(
584604
organization,
585605
Some(user.id),
606+
None,
586607
Utc.with_ymd_and_hms(2024, 1, 29, 1, 0, 0).unwrap().into(),
587608
None,
588609
)
@@ -595,6 +616,7 @@ pub mod tests {
595616
.get_monthly_cost(
596617
organization,
597618
Some(user.id),
619+
None,
598620
Utc.with_ymd_and_hms(2023, 12, 29, 1, 0, 0).unwrap().into(),
599621
None,
600622
)

0 commit comments

Comments
 (0)