Skip to content

Commit 794eb5f

Browse files
author
Laurie T. Malau
committed
Extend server args with time-filtering
1 parent 864214b commit 794eb5f

File tree

7 files changed

+44
-8
lines changed

7 files changed

+44
-8
lines changed

components/dashboard/src/teams/TeamUsage.tsx

+13-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ function TeamUsage() {
2626
const [currentPage, setCurrentPage] = useState(1);
2727
const [resultsPerPage] = useState(10);
2828
const [errorMessage, setErrorMessage] = useState("");
29+
const today = new Date();
30+
const startOfCurrentMonth = new Date(today.getFullYear(), today.getMonth(), 1);
31+
const timestampOfStart = startOfCurrentMonth.getTime();
32+
const [startDateOfBillMonth] = useState(timestampOfStart);
2933

3034
useEffect(() => {
3135
if (!team) {
@@ -34,7 +38,11 @@ function TeamUsage() {
3438
(async () => {
3539
const attributionId = AttributionId.render({ kind: "team", teamId: team.id });
3640
try {
37-
const billedUsageResult = await getGitpodService().server.listBilledUsage(attributionId);
41+
const billedUsageResult = await getGitpodService().server.listBilledUsage(
42+
attributionId,
43+
startDateOfBillMonth, // TODO: set based on selected month
44+
Date.now(), // TODO: set based on selected month
45+
);
3846
setBilledUsage(billedUsageResult);
3947
} catch (error) {
4048
if (error.code === ErrorCodes.PERMISSION_DENIED) {
@@ -90,7 +98,10 @@ function TeamUsage() {
9098
<div className="space-y-8 mb-6" style={{ width: "max-content" }}>
9199
<div className="flex flex-col truncate">
92100
<div className="text-base text-gray-500 truncate">Period</div>
93-
<div className="text-lg text-gray-600 font-semibold truncate">June 2022</div>
101+
<div className="text-lg text-gray-600 font-semibold truncate">
102+
{startOfCurrentMonth.toLocaleString("default", { month: "long" })}{" "}
103+
{startOfCurrentMonth.getFullYear()}
104+
</div>
94105
</div>
95106
<div className="flex flex-col truncate">
96107
<div className="text-base text-gray-500">Total usage</div>

components/gitpod-protocol/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@types/random-number-csprng": "^1.0.0",
2323
"@types/uuid": "^8.3.1",
2424
"@types/ws": "^5.1.2",
25+
"@types/google-protobuf": "^3.15.5",
2526
"chai": "^4.3.4",
2627
"chai-subset": "^1.6.0",
2728
"mocha": "^5.0.0",
@@ -62,6 +63,7 @@
6263
"vscode-languageserver-types": "3.17.0",
6364
"vscode-uri": "^3.0.3",
6465
"vscode-ws-jsonrpc": "^0.2.0",
65-
"ws": "^7.4.6"
66+
"ws": "^7.4.6",
67+
"google-protobuf": "^3.18.0-rc.2"
6668
}
6769
}

components/gitpod-protocol/src/gitpod-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
293293
getSpendingLimitForTeam(teamId: string): Promise<number | undefined>;
294294
setSpendingLimitForTeam(teamId: string, spendingLimit: number): Promise<void>;
295295

296-
listBilledUsage(attributionId: string): Promise<BillableSession[]>;
296+
listBilledUsage(attributionId: string, from?: number, to?: number): Promise<BillableSession[]>;
297297
setUsageAttribution(usageAttribution: string): Promise<void>;
298298

299299
/**

components/server/ee/src/workspace/gitpod-server-impl.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ import { getExperimentsClientForBackend } from "@gitpod/gitpod-protocol/lib/expe
110110
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
111111
import { CachingUsageServiceClientProvider } from "@gitpod/usage-api/lib/usage/v1/sugar";
112112
import * as usage from "@gitpod/usage-api/lib/usage/v1/usage_pb";
113+
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
113114

114115
@injectable()
115116
export class GitpodServerEEImpl extends GitpodServerImpl {
@@ -2095,14 +2096,27 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
20952096
return result;
20962097
}
20972098

2098-
async listBilledUsage(ctx: TraceContext, attributionId: string): Promise<BillableSession[]> {
2099+
async listBilledUsage(
2100+
ctx: TraceContext,
2101+
attributionId: string,
2102+
from?: number,
2103+
to?: number,
2104+
): Promise<BillableSession[]> {
20992105
traceAPIParams(ctx, { attributionId });
2106+
let timestampFrom;
2107+
let timestampTo;
21002108
const user = this.checkAndBlockUser("listBilledUsage");
21012109

21022110
await this.guardCostCenterAccess(ctx, user.id, attributionId, "get");
21032111

2112+
if (from) {
2113+
timestampFrom = new Timestamp().setSeconds(from);
2114+
}
2115+
if (to) {
2116+
timestampTo = new Timestamp().setSeconds(to);
2117+
}
21042118
const usageClient = this.usageServiceClientProvider.getDefault();
2105-
const response = await usageClient.listBilledUsage(ctx, attributionId);
2119+
const response = await usageClient.listBilledUsage(ctx, attributionId, timestampFrom, timestampTo);
21062120
const sessions = response.getSessionsList().map((s) => this.mapBilledSession(s));
21072121

21082122
return sessions;

components/server/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"@types/express-mysql-session": "^2.1.3",
100100
"@types/express-session": "1.17.4",
101101
"@types/fs-extra": "^9.0.12",
102+
"@types/google-protobuf": "^3.15.5",
102103
"@types/heapdump": "^0.3.1",
103104
"@types/http-proxy": "^1.17.7",
104105
"@types/js-yaml": "^4.0.3",

components/server/src/workspace/gitpod-server-impl.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -3191,7 +3191,12 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
31913191
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
31923192
}
31933193

3194-
async listBilledUsage(ctx: TraceContext, attributionId: string): Promise<BillableSession[]> {
3194+
async listBilledUsage(
3195+
ctx: TraceContext,
3196+
attributionId: string,
3197+
from?: number,
3198+
to?: number,
3199+
): Promise<BillableSession[]> {
31953200
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
31963201
}
31973202
async getSpendingLimitForTeam(ctx: TraceContext, teamId: string): Promise<number | undefined> {

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { ListBilledUsageRequest, ListBilledUsageResponse } from "./usage_pb";
1212
import { injectable, inject, optional } from "inversify";
1313
import { createClientCallMetricsInterceptor, IClientCallMetrics } from "@gitpod/gitpod-protocol/lib/util/grpc";
1414
import * as grpc from "@grpc/grpc-js";
15+
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
1516

1617
export const UsageServiceClientProvider = Symbol("UsageServiceClientProvider");
1718

@@ -90,12 +91,14 @@ export class PromisifiedUsageServiceClient {
9091
);
9192
}
9293

93-
public async listBilledUsage(_ctx: TraceContext, attributionId: string): Promise<ListBilledUsageResponse> {
94+
public async listBilledUsage(_ctx: TraceContext, attributionId: string, from?: Timestamp, to?: Timestamp): Promise<ListBilledUsageResponse> {
9495
const ctx = TraceContext.childContext(`/usage-service/listBilledUsage`, _ctx);
9596

9697
try {
9798
const req = new ListBilledUsageRequest();
9899
req.setAttributionId(attributionId);
100+
req.setFrom(from);
101+
req.setTo(to);
99102

100103
const response = await new Promise<ListBilledUsageResponse>((resolve, reject) => {
101104
this.client.listBilledUsage(

0 commit comments

Comments
 (0)