Skip to content

Commit e97e8e9

Browse files
jankeromnesroboquat
authored andcommitted
[usage] Address nits from #14485
1 parent f61a486 commit e97e8e9

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

components/common-go/db/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/relvacode/iso8601"
13+
"google.golang.org/protobuf/types/known/timestamppb"
1314
)
1415

1516
func NewVarCharTime(t time.Time) VarcharTime {
@@ -129,3 +130,11 @@ const ISO8601Format = "2006-01-02T15:04:05.000Z"
129130
func TimeToISO8601(t time.Time) string {
130131
return t.UTC().Format(ISO8601Format)
131132
}
133+
134+
func VarcharTimeToTimestamppb(t VarcharTime) *timestamppb.Timestamp {
135+
if !t.IsSet() {
136+
return nil
137+
}
138+
139+
return timestamppb.New(t.Time())
140+
}

components/usage/pkg/apiv1/usage.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,12 @@ func (s *UsageService) GetCostCenter(ctx context.Context, in *v1.GetCostCenterRe
191191
}
192192

193193
func dbCostCenterToAPI(c db.CostCenter) *v1.CostCenter {
194-
NextBillingTime := timestamppb.New(c.NextBillingTime.Time())
195-
if !c.NextBillingTime.IsSet() {
196-
NextBillingTime = nil
197-
}
198-
BillingCycleStart := timestamppb.New(c.BillingCycleStart.Time())
199-
if !c.BillingCycleStart.IsSet() {
200-
BillingCycleStart = nil
201-
}
202194
return &v1.CostCenter{
203195
AttributionId: string(c.ID),
204196
SpendingLimit: c.SpendingLimit,
205197
BillingStrategy: convertBillingStrategyToAPI(c.BillingStrategy),
206-
NextBillingTime: NextBillingTime,
207-
BillingCycleStart: BillingCycleStart,
198+
NextBillingTime: common_db.VarcharTimeToTimestamppb(c.NextBillingTime),
199+
BillingCycleStart: common_db.VarcharTimeToTimestamppb(c.BillingCycleStart),
208200
}
209201
}
210202

components/usage/pkg/db/cost_center.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,6 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, cc CostCenter) (Cost
319319

320320
now := time.Now().UTC()
321321

322-
// Resetting the usage always resets the billing cycle start time
323-
billingCycleStart := now
324-
325322
// Default to 1 month from now, if there's no nextBillingTime set on the record.
326323
nextBillingTime := now.AddDate(0, 1, 0)
327324
if cc.NextBillingTime.IsSet() {
@@ -334,12 +331,12 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, cc CostCenter) (Cost
334331
return CostCenter{}, fmt.Errorf("failed to compute invocie usage record for AttributonID: %s: %w", cc.ID, err)
335332
}
336333

337-
// All fields on the new cost center remain the same, except for CreationTime and NextBillingTime
334+
// All fields on the new cost center remain the same, except for BillingCycleStart, NextBillingTime, and CreationTime
338335
newCostCenter := CostCenter{
339336
ID: cc.ID,
340337
SpendingLimit: spendingLimit,
341338
BillingStrategy: cc.BillingStrategy,
342-
BillingCycleStart: common_db.NewVarCharTime(billingCycleStart),
339+
BillingCycleStart: common_db.NewVarCharTime(now),
343340
NextBillingTime: common_db.NewVarCharTime(nextBillingTime),
344341
CreationTime: common_db.NewVarCharTime(now),
345342
}

0 commit comments

Comments
 (0)