Skip to content

Commit aeaa3ae

Browse files
author
Laurie T. Malau
committed
add sorting
1 parent ea3921d commit aeaa3ae

File tree

6 files changed

+47
-27
lines changed

6 files changed

+47
-27
lines changed

components/dashboard/src/teams/TeamUsage.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useContext, useEffect, useState } from "react";
88
import { Redirect, useLocation } from "react-router";
99
import { getCurrentTeam, TeamsContext } from "./teams-context";
1010
import { getGitpodService, gitpodHostUrl } from "../service/service";
11-
import { BillableSession, BillableWorkspaceType } from "@gitpod/gitpod-protocol/lib/usage";
11+
import { BillableSession, BillableWorkspaceType, SortOrder } from "@gitpod/gitpod-protocol/lib/usage";
1212
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
1313
import { Item, ItemField, ItemsList } from "../components/ItemsList";
1414
import moment from "moment";
@@ -34,19 +34,22 @@ function TeamUsage() {
3434
const [startDateOfBillMonth, setStartDateOfBillMonth] = useState(timestampStartOfCurrentMonth);
3535
const [endDateOfBillMonth, setEndDateOfBillMonth] = useState(Date.now());
3636
const [isLoading, setIsLoading] = useState<boolean>(true);
37+
const [startedTimeOrder] = useState<SortOrder>(SortOrder.Descending);
3738

3839
useEffect(() => {
3940
if (!team) {
4041
return;
4142
}
4243
(async () => {
4344
const attributionId = AttributionId.render({ kind: "team", teamId: team.id });
45+
const request = {
46+
attributionId,
47+
startedTimeOrder,
48+
startDateOfBillMonth,
49+
endDateOfBillMonth,
50+
};
4451
try {
45-
const billedUsageResult = await getGitpodService().server.listBilledUsage(
46-
attributionId,
47-
startDateOfBillMonth,
48-
endDateOfBillMonth,
49-
);
52+
const billedUsageResult = await getGitpodService().server.listBilledUsage(request);
5053
setBilledUsage(billedUsageResult);
5154
} catch (error) {
5255
if (error.code === ErrorCodes.PERMISSION_DENIED) {
@@ -56,7 +59,7 @@ function TeamUsage() {
5659
setIsLoading(false);
5760
}
5861
})();
59-
}, [team, startDateOfBillMonth, endDateOfBillMonth]);
62+
}, [team, startDateOfBillMonth, endDateOfBillMonth, startedTimeOrder]);
6063

6164
if (!showUsageBasedPricingUI) {
6265
return <Redirect to="/" />;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import { RemotePageMessage, RemoteTrackMessage, RemoteIdentifyMessage } from "./
6161
import { IDEServer } from "./ide-protocol";
6262
import { InstallationAdminSettings, TelemetryData } from "./installation-admin-protocol";
6363
import { Currency } from "./plans";
64-
import { BillableSession } from "./usage";
64+
import { BillableSession, BillableSessionRequest } from "./usage";
6565
import { SupportedWorkspaceClass } from "./workspace-class";
6666

6767
export interface GitpodClient {
@@ -294,7 +294,8 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
294294
getSpendingLimitForTeam(teamId: string): Promise<number | undefined>;
295295
setSpendingLimitForTeam(teamId: string, spendingLimit: number): Promise<void>;
296296

297-
listBilledUsage(attributionId: string, from?: number, to?: number): Promise<BillableSession[]>;
297+
listBilledUsage(req: BillableSessionRequest): Promise<BillableSession[]>;
298+
298299
setUsageAttribution(usageAttribution: string): Promise<void>;
299300

300301
/**
@@ -308,7 +309,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
308309
* Frontend notifications
309310
*/
310311
getNotifications(): Promise<string[]>;
311-
312+
312313
getSupportedWorkspaceClasses(): Promise<SupportedWorkspaceClass[]>;
313314
}
314315

components/gitpod-protocol/src/usage.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,16 @@ export interface BillableSession {
3535
projectId?: string;
3636
}
3737

38+
export interface BillableSessionRequest {
39+
attributionId: string;
40+
startedTimeOrder: SortOrder;
41+
from?: number;
42+
to?: number;
43+
}
44+
3845
export type BillableWorkspaceType = WorkspaceType;
46+
47+
export enum SortOrder {
48+
Descending = 0,
49+
Ascending = 1,
50+
}

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import { BlockedRepository } from "@gitpod/gitpod-protocol/lib/blocked-repositor
7272
import { EligibilityService } from "../user/eligibility-service";
7373
import { AccountStatementProvider } from "../user/account-statement-provider";
7474
import { GithubUpgradeURL, PlanCoupon } from "@gitpod/gitpod-protocol/lib/payment-protocol";
75-
import { BillableSession } from "@gitpod/gitpod-protocol/lib/usage";
75+
import { BillableSession, BillableSessionRequest, SortOrder } from "@gitpod/gitpod-protocol/lib/usage";
7676
import {
7777
AssigneeIdentityIdentifier,
7878
TeamSubscription,
@@ -2080,7 +2080,13 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
20802080
const result = await super.getNotifications(ctx);
20812081
const user = this.checkAndBlockUser("getNotifications");
20822082
if (user.usageAttributionId) {
2083-
const allSessions = await this.listBilledUsage(ctx, user.usageAttributionId);
2083+
// This change doesn't matter much because the listBilledUsage() call
2084+
// will be removed anyway in https://github.com/gitpod-io/gitpod/issues/11692
2085+
const request = {
2086+
attributionId: user.usageAttributionId,
2087+
startedTimeOrder: SortOrder.Descending,
2088+
};
2089+
const allSessions = await this.listBilledUsage(ctx, request);
20842090
const totalUsage = allSessions.map((s) => s.credits).reduce((a, b) => a + b, 0);
20852091
const costCenter = await this.costCenterDB.findById(user.usageAttributionId);
20862092
if (costCenter) {
@@ -2096,12 +2102,8 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
20962102
return result;
20972103
}
20982104

2099-
async listBilledUsage(
2100-
ctx: TraceContext,
2101-
attributionId: string,
2102-
from?: number,
2103-
to?: number,
2104-
): Promise<BillableSession[]> {
2105+
async listBilledUsage(ctx: TraceContext, req: BillableSessionRequest): Promise<BillableSession[]> {
2106+
const { attributionId, startedTimeOrder, from, to } = req;
21052107
traceAPIParams(ctx, { attributionId });
21062108
let timestampFrom;
21072109
let timestampTo;
@@ -2116,7 +2118,13 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
21162118
timestampTo = Timestamp.fromDate(new Date(to));
21172119
}
21182120
const usageClient = this.usageServiceClientProvider.getDefault();
2119-
const response = await usageClient.listBilledUsage(ctx, attributionId, timestampFrom, timestampTo);
2121+
const response = await usageClient.listBilledUsage(
2122+
ctx,
2123+
attributionId,
2124+
startedTimeOrder as number,
2125+
timestampFrom,
2126+
timestampTo,
2127+
);
21202128
const sessions = response.getSessionsList().map((s) => this.mapBilledSession(s));
21212129

21222130
return sessions;

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ import { LicenseEvaluator } from "@gitpod/licensor/lib";
169169
import { Feature } from "@gitpod/licensor/lib/api";
170170
import { Currency } from "@gitpod/gitpod-protocol/lib/plans";
171171
import { getExperimentsClientForBackend } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server";
172-
import { BillableSession } from "@gitpod/gitpod-protocol/lib/usage";
172+
import { BillableSession, BillableSessionRequest } from "@gitpod/gitpod-protocol/lib/usage";
173173
import { WorkspaceClusterImagebuilderClientProvider } from "./workspace-cluster-imagebuilder-client-provider";
174174

175175
// shortcut
@@ -3207,12 +3207,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
32073207
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
32083208
}
32093209

3210-
async listBilledUsage(
3211-
ctx: TraceContext,
3212-
attributionId: string,
3213-
from?: number,
3214-
to?: number,
3215-
): Promise<BillableSession[]> {
3210+
async listBilledUsage(ctx: TraceContext, req: BillableSessionRequest): Promise<BillableSession[]> {
32163211
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
32173212
}
32183213
async getSpendingLimitForTeam(ctx: TraceContext, teamId: string): Promise<number | undefined> {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,15 @@ export class PromisifiedUsageServiceClient {
9191
);
9292
}
9393

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

9797
try {
9898
const req = new ListBilledUsageRequest();
9999
req.setAttributionId(attributionId);
100100
req.setFrom(from);
101101
req.setTo(to);
102+
req.setOrder(order);
102103

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

0 commit comments

Comments
 (0)