Skip to content

Commit aaee277

Browse files
committed
[usage] Introduce an explicit CostCenter.BillingCycleStart time
1 parent ddf6f3e commit aaee277

File tree

9 files changed

+172
-102
lines changed

9 files changed

+172
-102
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License-AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { MigrationInterface, QueryRunner } from "typeorm";
8+
import { columnExists } from "./helper/helper";
9+
10+
const D_B_COST_CENTER = "d_b_cost_center";
11+
const COL_BILLING_CYCLE_START = "billingCycleStart";
12+
13+
export class CostCenterNextBillingTime1663055856941 implements MigrationInterface {
14+
public async up(queryRunner: QueryRunner): Promise<void> {
15+
if (!(await columnExists(queryRunner, D_B_COST_CENTER, COL_BILLING_CYCLE_START))) {
16+
await queryRunner.query(
17+
`ALTER TABLE ${D_B_COST_CENTER} ADD COLUMN ${COL_BILLING_CYCLE_START} varchar(30) NOT NULL, ALGORITHM=INPLACE, LOCK=NONE `,
18+
);
19+
await queryRunner.query(
20+
`ALTER TABLE ${D_B_COST_CENTER} ADD INDEX(${COL_BILLING_CYCLE_START}), ALGORITHM=INPLACE, LOCK=NONE `,
21+
);
22+
}
23+
}
24+
25+
public async down(queryRunner: QueryRunner): Promise<void> {}
26+
}

components/server/ee/src/billing/billing-mode.spec.db.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ class UsageServiceClientMock implements UsageServiceClient {
7777
return {
7878
costCenter: {
7979
attributionId: request.attributionId,
80+
spendingLimit: 1234,
8081
billingStrategy,
8182
nextBillingTime: new Date(), // does not matter here.
82-
spendingLimit: 1234,
83+
billingCycleStart: new Date(), // does not matter here.
8384
},
8485
};
8586
}

components/usage-api/go/v1/usage.pb.go

Lines changed: 74 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/usage-api/typescript/src/usage/v1/usage.pb.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export interface CostCenter {
195195
billingStrategy: CostCenter_BillingStrategy;
196196
/** next_billing_time specifies when the next billing cycle happens. Only set when billing strategy is 'other'. This property is readonly. */
197197
nextBillingTime: Date | undefined;
198+
billingCycleStart: Date | undefined;
198199
}
199200

200201
export enum CostCenter_BillingStrategy {
@@ -1077,6 +1078,7 @@ function createBaseCostCenter(): CostCenter {
10771078
spendingLimit: 0,
10781079
billingStrategy: CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE,
10791080
nextBillingTime: undefined,
1081+
billingCycleStart: undefined,
10801082
};
10811083
}
10821084

@@ -1094,6 +1096,9 @@ export const CostCenter = {
10941096
if (message.nextBillingTime !== undefined) {
10951097
Timestamp.encode(toTimestamp(message.nextBillingTime), writer.uint32(34).fork()).ldelim();
10961098
}
1099+
if (message.billingCycleStart !== undefined) {
1100+
Timestamp.encode(toTimestamp(message.billingCycleStart), writer.uint32(42).fork()).ldelim();
1101+
}
10971102
return writer;
10981103
},
10991104

@@ -1116,6 +1121,9 @@ export const CostCenter = {
11161121
case 4:
11171122
message.nextBillingTime = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
11181123
break;
1124+
case 5:
1125+
message.billingCycleStart = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
1126+
break;
11191127
default:
11201128
reader.skipType(tag & 7);
11211129
break;
@@ -1132,6 +1140,7 @@ export const CostCenter = {
11321140
? costCenter_BillingStrategyFromJSON(object.billingStrategy)
11331141
: CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE,
11341142
nextBillingTime: isSet(object.nextBillingTime) ? fromJsonTimestamp(object.nextBillingTime) : undefined,
1143+
billingCycleStart: isSet(object.billingCycleStart) ? fromJsonTimestamp(object.billingCycleStart) : undefined,
11351144
};
11361145
},
11371146

@@ -1142,6 +1151,7 @@ export const CostCenter = {
11421151
message.billingStrategy !== undefined &&
11431152
(obj.billingStrategy = costCenter_BillingStrategyToJSON(message.billingStrategy));
11441153
message.nextBillingTime !== undefined && (obj.nextBillingTime = message.nextBillingTime.toISOString());
1154+
message.billingCycleStart !== undefined && (obj.billingCycleStart = message.billingCycleStart.toISOString());
11451155
return obj;
11461156
},
11471157

@@ -1151,6 +1161,7 @@ export const CostCenter = {
11511161
message.spendingLimit = object.spendingLimit ?? 0;
11521162
message.billingStrategy = object.billingStrategy ?? CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE;
11531163
message.nextBillingTime = object.nextBillingTime ?? undefined;
1164+
message.billingCycleStart = object.billingCycleStart ?? undefined;
11541165
return message;
11551166
},
11561167
};

components/usage-api/usage/v1/usage.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ message CostCenter {
129129

130130
// next_billing_time specifies when the next billing cycle happens. Only set when billing strategy is 'other'. This property is readonly.
131131
google.protobuf.Timestamp next_billing_time = 4;
132+
google.protobuf.Timestamp billing_cycle_start = 5;
132133
}
133134

134135
message ResetUsageRequest {}

components/usage/pkg/apiv1/usage.go

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

192192
func dbCostCenterToAPI(c db.CostCenter) *v1.CostCenter {
193193
return &v1.CostCenter{
194-
AttributionId: string(c.ID),
195-
SpendingLimit: c.SpendingLimit,
196-
BillingStrategy: convertBillingStrategyToAPI(c.BillingStrategy),
197-
NextBillingTime: timestamppb.New(c.NextBillingTime.Time()),
194+
AttributionId: string(c.ID),
195+
SpendingLimit: c.SpendingLimit,
196+
BillingStrategy: convertBillingStrategyToAPI(c.BillingStrategy),
197+
NextBillingTime: timestamppb.New(c.NextBillingTime.Time()),
198+
BillingCycleStart: timestamppb.New(c.BillingCycleStart.Time()),
198199
}
199200
}
200201

0 commit comments

Comments
 (0)