Skip to content

[usage] handle reset usage for chargebee #15056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions components/gitpod-db/go/cost_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ var CostCenterNotFound = errors.New("CostCenter not found")
type BillingStrategy string

const (
CostCenter_Stripe BillingStrategy = "stripe"
CostCenter_Other BillingStrategy = "other"
CostCenter_Stripe BillingStrategy = "stripe"
CostCenter_Other BillingStrategy = "other"
CostCenter_ChargebeeCancelled BillingStrategy = "chargebee-cancelled"
)

type CostCenter struct {
Expand Down Expand Up @@ -103,7 +104,7 @@ func (c *CostCenterManager) GetOrCreateCostCenter(ctx context.Context, attributi
// we want to reset it immediately.
// This can happen in the following scenario:
// * User accesses gitpod just after their CostCenter expired, but just before our periodic CostCenter reset kicks in.
if result.BillingStrategy == CostCenter_Other && result.IsExpired() {
if result.BillingStrategy != CostCenter_Stripe && result.IsExpired() {
cc, err := c.ResetUsage(ctx, result.ID)
if err != nil {
logger.WithError(err).Error("Failed to reset expired usage.")
Expand Down Expand Up @@ -237,7 +238,7 @@ func (c *CostCenterManager) newInvoiceUsageRecord(ctx context.Context, attributi
}, nil
}

func (c *CostCenterManager) ListLatestCostCentersWithBillingTimeBefore(ctx context.Context, strategy BillingStrategy, billingTimeBefore time.Time) ([]CostCenter, error) {
func (c *CostCenterManager) ListManagedCostCentersWithBillingTimeBefore(ctx context.Context, billingTimeBefore time.Time) ([]CostCenter, error) {
db := c.conn.WithContext(ctx)

var results []CostCenter
Expand All @@ -250,7 +251,7 @@ func (c *CostCenterManager) ListLatestCostCentersWithBillingTimeBefore(ctx conte
tx := db.Table(fmt.Sprintf("%s as cc", (&CostCenter{}).TableName())).
// Join on our set of latest CostCenter records
Joins("INNER JOIN (?) AS expiredCC on cc.id = expiredCC.id AND cc.creationTime = expiredCC.creationTime", subquery).
Where("cc.billingStrategy = ?", strategy).
Where("cc.billingStrategy != ?", CostCenter_Stripe). // Stripe is managed externally
Where("nextBillingTime != ?", "").
Where("nextBillingTime < ?", TimeToISO8601(billingTimeBefore)).
FindInBatches(&batch, 1000, func(tx *gorm.DB, iteration int) error {
Expand All @@ -273,7 +274,7 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, id AttributionID) (C
return cc, err
}
logger = logger.WithField("cost_center", cc)
if cc.BillingStrategy != CostCenter_Other {
if cc.BillingStrategy == CostCenter_Stripe {
return CostCenter{}, fmt.Errorf("cannot reset usage for Billing Strategy %s for Cost Center ID: %s", cc.BillingStrategy, cc.ID)
}
if !cc.IsExpired() {
Expand All @@ -290,11 +291,19 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, id AttributionID) (C
nextBillingTime = cc.NextBillingTime.Time().AddDate(0, 1, 0)
}

futureSpendingLimit := cc.SpendingLimit
futurebillingStrategy := cc.BillingStrategy
// chargebee cancellations will be switched to free plan (strategy: other)
if cc.BillingStrategy == CostCenter_ChargebeeCancelled {
futureSpendingLimit = c.cfg.ForTeams
futurebillingStrategy = CostCenter_Other
}

// All fields on the new cost center remain the same, except for BillingCycleStart, NextBillingTime, and CreationTime
newCostCenter := CostCenter{
ID: cc.ID,
SpendingLimit: cc.SpendingLimit,
BillingStrategy: cc.BillingStrategy,
SpendingLimit: futureSpendingLimit,
BillingStrategy: futurebillingStrategy,
BillingCycleStart: NewVarCharTime(billingCycleStart),
NextBillingTime: NewVarCharTime(nextBillingTime),
CreationTime: NewVarCharTime(now),
Expand Down
6 changes: 3 additions & 3 deletions components/gitpod-db/go/cost_center_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {

ts := time.Date(2022, 10, 10, 10, 10, 10, 10, time.UTC)

retrieved, err := mnr.ListLatestCostCentersWithBillingTimeBefore(context.Background(), db.CostCenter_Other, ts.Add(7*24*time.Hour))
retrieved, err := mnr.ListManagedCostCentersWithBillingTimeBefore(context.Background(), ts.Add(7*24*time.Hour))
require.NoError(t, err)
require.Len(t, retrieved, 0)
})
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {

dbtest.CreateCostCenters(t, conn, costCenters...)

retrieved, err := mnr.ListLatestCostCentersWithBillingTimeBefore(context.Background(), db.CostCenter_Other, secondCreation.Add(7*24*time.Hour))
retrieved, err := mnr.ListManagedCostCentersWithBillingTimeBefore(context.Background(), secondCreation.Add(7*24*time.Hour))
require.NoError(t, err)
require.Len(t, retrieved, 1)

Expand Down Expand Up @@ -392,7 +392,7 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {

dbtest.CreateCostCenters(t, conn, costCenters...)

retrieved, err := mnr.ListLatestCostCentersWithBillingTimeBefore(context.Background(), db.CostCenter_Other, secondCreation.Add(7*24*time.Hour))
retrieved, err := mnr.ListManagedCostCentersWithBillingTimeBefore(context.Background(), secondCreation.Add(7*24*time.Hour))
require.NoError(t, err)
require.Len(t, retrieved, 0)
})
Expand Down
1 change: 1 addition & 0 deletions components/gitpod-protocol/src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ export interface CostCenterJSON {
export enum CostCenter_BillingStrategy {
BILLING_STRATEGY_STRIPE = "BILLING_STRATEGY_STRIPE",
BILLING_STRATEGY_OTHER = "BILLING_STRATEGY_OTHER",
BILLING_STRATEGY_CHARGEBEE_CANCELLATION = "BILLING_STRATEGY_CHARGEBEE_CANCELLATION",
UNRECOGNIZED = "UNRECOGNIZED",
}
128 changes: 67 additions & 61 deletions components/usage-api/go/v1/usage.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading