Skip to content

Commit

Permalink
wip GetLatestInvoice
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTugarev committed Aug 23, 2022
1 parent b8fd596 commit 10389ce
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 87 deletions.
52 changes: 34 additions & 18 deletions components/server/ee/src/billing/billing-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
import { CostCenterDB } from "@gitpod/gitpod-db/lib";
import { User } from "@gitpod/gitpod-protocol";
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
import { BillableSession, BillableSessionRequest, SortOrder } from "@gitpod/gitpod-protocol/lib/usage";
import { BillableSession, BillableSessionRequest } from "@gitpod/gitpod-protocol/lib/usage";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { CachingUsageServiceClientProvider, UsageService } from "@gitpod/usage-api/lib/usage/v1/sugar";
import { GetLatestInvoiceResponse } from "@gitpod/usage-api/lib/usage/v1/billing_pb";
import {
CachingUsageServiceClientProvider,
UsageService,
CachingBillingServiceClientProvider,
} from "@gitpod/usage-api/lib/usage/v1/sugar";
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
import { inject, injectable } from "inversify";
import { UserService } from "../../../src/user/user-service";
Expand All @@ -26,6 +31,8 @@ export class BillingService {
@inject(CostCenterDB) protected readonly costCenterDB: CostCenterDB;
@inject(CachingUsageServiceClientProvider)
protected readonly usageServiceClientProvider: CachingUsageServiceClientProvider;
@inject(CachingBillingServiceClientProvider)
protected readonly billingServiceClientProvider: CachingBillingServiceClientProvider;

async checkSpendingLimitReached(user: User): Promise<SpendingLimitReachedResult> {
const attributionId = await this.userService.getWorkspaceUsageAttributionId(user);
Expand All @@ -40,23 +47,27 @@ export class BillingService {
};
}

const allSessions = await this.listBilledUsage({
attributionId: AttributionId.render(attributionId),
startedTimeOrder: SortOrder.Descending,
});
const totalUsage = allSessions.map((s) => s.credits).reduce((a, b) => a + b, 0);
if (totalUsage >= costCenter.spendingLimit) {
return {
reached: true,
attributionId,
};
} else if (totalUsage > costCenter.spendingLimit * 0.8) {
return {
reached: false,
almostReached: true,
attributionId,
};
if (attributionId.kind === "team") {
const latestInvoice = await this.getLatestInvoice(attributionId.teamId);
const currentUsage = latestInvoice.getCredits();
if (currentUsage >= costCenter.spendingLimit) {
return {
reached: true,
attributionId,
};
} else if (currentUsage > costCenter.spendingLimit * 0.8) {
return {
reached: false,
almostReached: true,
attributionId,
};
}
}

if (attributionId.kind === "user") {
// TODO
}

return {
reached: false,
attributionId,
Expand Down Expand Up @@ -86,4 +97,9 @@ export class BillingService {
const sessions = response.getSessionsList().map((s) => UsageService.mapBilledSession(s));
return sessions;
}

async getLatestInvoice(teamId: string): Promise<GetLatestInvoiceResponse> {
const response = await this.billingServiceClientProvider.getDefault().getLatestInvoice(teamId);
return response;
}
}
122 changes: 65 additions & 57 deletions components/usage-api/go/v1/billing.pb.go

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

3 changes: 3 additions & 0 deletions components/usage-api/typescript/src/usage/v1/billing_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export class GetLatestInvoiceResponse extends jspb.Message {
setCurrency(value: string): GetLatestInvoiceResponse;
getAmount(): number;
setAmount(value: number): GetLatestInvoiceResponse;
getCredits(): number;
setCredits(value: number): GetLatestInvoiceResponse;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): GetLatestInvoiceResponse.AsObject;
Expand All @@ -126,6 +128,7 @@ export namespace GetLatestInvoiceResponse {
invoiceId: string,
currency: string,
amount: number,
credits: number,
}
}

Expand Down
32 changes: 31 additions & 1 deletion components/usage-api/typescript/src/usage/v1/billing_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,8 @@ proto.usage.v1.GetLatestInvoiceResponse.toObject = function(includeInstance, msg
var f, obj = {
invoiceId: jspb.Message.getFieldWithDefault(msg, 1, ""),
currency: jspb.Message.getFieldWithDefault(msg, 2, ""),
amount: jspb.Message.getFloatingPointFieldWithDefault(msg, 3, 0.0)
amount: jspb.Message.getFloatingPointFieldWithDefault(msg, 3, 0.0),
credits: jspb.Message.getFieldWithDefault(msg, 4, 0)
};

if (includeInstance) {
Expand Down Expand Up @@ -871,6 +872,10 @@ proto.usage.v1.GetLatestInvoiceResponse.deserializeBinaryFromReader = function(m
var value = /** @type {number} */ (reader.readDouble());
msg.setAmount(value);
break;
case 4:
var value = /** @type {number} */ (reader.readInt64());
msg.setCredits(value);
break;
default:
reader.skipField();
break;
Expand Down Expand Up @@ -921,6 +926,13 @@ proto.usage.v1.GetLatestInvoiceResponse.serializeBinaryToWriter = function(messa
f
);
}
f = message.getCredits();
if (f !== 0) {
writer.writeInt64(
4,
f
);
}
};


Expand Down Expand Up @@ -978,6 +990,24 @@ proto.usage.v1.GetLatestInvoiceResponse.prototype.setAmount = function(value) {
};


/**
* optional int64 credits = 4;
* @return {number}
*/
proto.usage.v1.GetLatestInvoiceResponse.prototype.getCredits = function() {
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 4, 0));
};


/**
* @param {number} value
* @return {!proto.usage.v1.GetLatestInvoiceResponse} returns this
*/
proto.usage.v1.GetLatestInvoiceResponse.prototype.setCredits = function(value) {
return jspb.Message.setProto3IntField(this, 4, value);
};





Expand Down
24 changes: 23 additions & 1 deletion components/usage-api/typescript/src/usage/v1/sugar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
import * as opentracing from "opentracing";
import { Metadata } from "@grpc/grpc-js";
import { BilledSession, ListBilledUsageRequest, ListBilledUsageResponse } from "./usage_pb";
import { SetBilledSessionRequest, SetBilledSessionResponse, System } from "./billing_pb";
import {
GetLatestInvoiceRequest,
GetLatestInvoiceResponse,
SetBilledSessionRequest,
SetBilledSessionResponse,
System,
} from "./billing_pb";
import { injectable, inject, optional } from "inversify";
import { createClientCallMetricsInterceptor, IClientCallMetrics } from "@gitpod/gitpod-protocol/lib/util/grpc";
import * as grpc from "@grpc/grpc-js";
Expand Down Expand Up @@ -254,4 +260,20 @@ export class PromisifiedBillingServiceClient {
interceptors: this.interceptor,
};
}

public async getLatestInvoice(teamId: string) {
const req = new GetLatestInvoiceRequest();
req.setTeamId(teamId);

const response = await new Promise<GetLatestInvoiceResponse>((resolve, reject) => {
this.client.getLatestInvoice(req, (err: grpc.ServiceError | null, response: GetLatestInvoiceResponse) => {
if (err) {
reject(err);
return;
}
resolve(response);
});
});
return response;
}
}
1 change: 1 addition & 0 deletions components/usage-api/usage/v1/billing.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ message GetLatestInvoiceResponse {
string invoice_id = 1;
string currency = 2;
double amount = 3;
int64 credits = 4;
}

message FinalizeInvoiceRequest {
Expand Down
Loading

0 comments on commit 10389ce

Please sign in to comment.