@@ -104,7 +104,7 @@ func (c *CostCenterManager) GetOrCreateCostCenter(ctx context.Context, attributi
104
104
// This can happen in the following scenario:
105
105
// * User accesses gitpod just after their CostCenter expired, but just before our periodic CostCenter reset kicks in.
106
106
if result .BillingStrategy == CostCenter_Other && result .IsExpired () {
107
- cc , err := c .ResetUsage (ctx , result )
107
+ cc , err := c .ResetUsage (ctx , result . ID )
108
108
if err != nil {
109
109
logger .WithError (err ).Error ("Failed to reset expired usage." )
110
110
return CostCenter {}, fmt .Errorf ("failed to reset usage for expired cost center ID: %s: %w" , result .ID , err )
@@ -262,31 +262,36 @@ func (c *CostCenterManager) ListLatestCostCentersWithBillingTimeBefore(ctx conte
262
262
return results , nil
263
263
}
264
264
265
- func (c * CostCenterManager ) ResetUsage (ctx context.Context , cc CostCenter ) (CostCenter , error ) {
265
+ func (c * CostCenterManager ) ResetUsage (ctx context.Context , id AttributionID ) (CostCenter , error ) {
266
+ logger := log .WithField ("attributionId" , id )
267
+ now := time .Now ().UTC ()
268
+ cc , err := getCostCenter (ctx , c .conn , id )
269
+ if err != nil {
270
+ return cc , err
271
+ }
266
272
if cc .BillingStrategy != CostCenter_Other {
267
273
return CostCenter {}, fmt .Errorf ("cannot reset usage for Billing Strategy %s for Cost Center ID: %s" , cc .BillingStrategy , cc .ID )
268
274
}
275
+ if ! cc .IsExpired () {
276
+ logger .Info ("Skipping ResetUsage because next billing cycle is in the future." )
277
+ return cc , nil
278
+ }
269
279
270
- now := time .Now ().UTC ()
271
-
280
+ logger .Info ("Running `ResetUsage`." )
272
281
// Default to 1 month from now, if there's no nextBillingTime set on the record.
282
+ billingCycleStart := now
273
283
nextBillingTime := now .AddDate (0 , 1 , 0 )
274
284
if cc .NextBillingTime .IsSet () {
285
+ billingCycleStart = cc .NextBillingTime .Time ()
275
286
nextBillingTime = cc .NextBillingTime .Time ().AddDate (0 , 1 , 0 )
276
287
}
277
288
278
- // Create a synthetic Invoice Usage record, to reset usage
279
- err := c .BalanceOutUsage (ctx , cc .ID )
280
- if err != nil {
281
- return CostCenter {}, fmt .Errorf ("failed to compute invocie usage record for AttributonID: %s: %w" , cc .ID , err )
282
- }
283
-
284
289
// All fields on the new cost center remain the same, except for BillingCycleStart, NextBillingTime, and CreationTime
285
290
newCostCenter := CostCenter {
286
291
ID : cc .ID ,
287
292
SpendingLimit : cc .SpendingLimit ,
288
293
BillingStrategy : cc .BillingStrategy ,
289
- BillingCycleStart : NewVarCharTime (now ),
294
+ BillingCycleStart : NewVarCharTime (billingCycleStart ),
290
295
NextBillingTime : NewVarCharTime (nextBillingTime ),
291
296
CreationTime : NewVarCharTime (now ),
292
297
}
@@ -295,5 +300,11 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, cc CostCenter) (Cost
295
300
return CostCenter {}, fmt .Errorf ("failed to store new cost center for AttribtuonID: %s: %w" , cc .ID , err )
296
301
}
297
302
303
+ // Create a synthetic Invoice Usage record, to reset usage
304
+ err = c .BalanceOutUsage (ctx , cc .ID )
305
+ if err != nil {
306
+ return CostCenter {}, fmt .Errorf ("failed to compute invocie usage record for AttributonID: %s: %w" , cc .ID , err )
307
+ }
308
+
298
309
return newCostCenter , nil
299
310
}
0 commit comments