Skip to content

Commit

Permalink
add sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
laushinka authored and roboquat committed Aug 1, 2022
1 parent 03849f9 commit 04f122c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
17 changes: 10 additions & 7 deletions components/dashboard/src/teams/TeamUsage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useContext, useEffect, useState } from "react";
import { Redirect, useLocation } from "react-router";
import { getCurrentTeam, TeamsContext } from "./teams-context";
import { getGitpodService, gitpodHostUrl } from "../service/service";
import { BillableSession, BillableWorkspaceType } from "@gitpod/gitpod-protocol/lib/usage";
import { BillableSession, BillableWorkspaceType, SortOrder } from "@gitpod/gitpod-protocol/lib/usage";
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
import { Item, ItemField, ItemsList } from "../components/ItemsList";
import moment from "moment";
Expand All @@ -34,19 +34,22 @@ function TeamUsage() {
const [startDateOfBillMonth, setStartDateOfBillMonth] = useState(timestampStartOfCurrentMonth);
const [endDateOfBillMonth, setEndDateOfBillMonth] = useState(Date.now());
const [isLoading, setIsLoading] = useState<boolean>(true);
const [startedTimeOrder] = useState<SortOrder>(SortOrder.Descending);

useEffect(() => {
if (!team) {
return;
}
(async () => {
const attributionId = AttributionId.render({ kind: "team", teamId: team.id });
const request = {
attributionId,
startedTimeOrder,
startDateOfBillMonth,
endDateOfBillMonth,
};
try {
const billedUsageResult = await getGitpodService().server.listBilledUsage(
attributionId,
startDateOfBillMonth,
endDateOfBillMonth,
);
const billedUsageResult = await getGitpodService().server.listBilledUsage(request);
setBilledUsage(billedUsageResult);
} catch (error) {
if (error.code === ErrorCodes.PERMISSION_DENIED) {
Expand All @@ -56,7 +59,7 @@ function TeamUsage() {
setIsLoading(false);
}
})();
}, [team, startDateOfBillMonth, endDateOfBillMonth]);
}, [team, startDateOfBillMonth, endDateOfBillMonth, startedTimeOrder]);

if (!showUsageBasedPricingUI) {
return <Redirect to="/" />;
Expand Down
7 changes: 4 additions & 3 deletions components/gitpod-protocol/src/gitpod-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import { RemotePageMessage, RemoteTrackMessage, RemoteIdentifyMessage } from "./
import { IDEServer } from "./ide-protocol";
import { InstallationAdminSettings, TelemetryData } from "./installation-admin-protocol";
import { Currency } from "./plans";
import { BillableSession } from "./usage";
import { BillableSession, BillableSessionRequest } from "./usage";
import { SupportedWorkspaceClass } from "./workspace-class";

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

listBilledUsage(attributionId: string, from?: number, to?: number): Promise<BillableSession[]>;
listBilledUsage(req: BillableSessionRequest): Promise<BillableSession[]>;

setUsageAttribution(usageAttribution: string): Promise<void>;

/**
Expand All @@ -308,7 +309,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
* Frontend notifications
*/
getNotifications(): Promise<string[]>;

getSupportedWorkspaceClasses(): Promise<SupportedWorkspaceClass[]>;
}

Expand Down
12 changes: 12 additions & 0 deletions components/gitpod-protocol/src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ export interface BillableSession {
projectId?: string;
}

export interface BillableSessionRequest {
attributionId: string;
startedTimeOrder: SortOrder;
from?: number;
to?: number;
}

export type BillableWorkspaceType = WorkspaceType;

export enum SortOrder {
Descending = 0,
Ascending = 1,
}
26 changes: 17 additions & 9 deletions components/server/ee/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import { BlockedRepository } from "@gitpod/gitpod-protocol/lib/blocked-repositor
import { EligibilityService } from "../user/eligibility-service";
import { AccountStatementProvider } from "../user/account-statement-provider";
import { GithubUpgradeURL, PlanCoupon } from "@gitpod/gitpod-protocol/lib/payment-protocol";
import { BillableSession } from "@gitpod/gitpod-protocol/lib/usage";
import { BillableSession, BillableSessionRequest, SortOrder } from "@gitpod/gitpod-protocol/lib/usage";
import {
AssigneeIdentityIdentifier,
TeamSubscription,
Expand Down Expand Up @@ -2080,7 +2080,13 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
const result = await super.getNotifications(ctx);
const user = this.checkAndBlockUser("getNotifications");
if (user.usageAttributionId) {
const allSessions = await this.listBilledUsage(ctx, user.usageAttributionId);
// This change doesn't matter much because the listBilledUsage() call
// will be removed anyway in https://github.com/gitpod-io/gitpod/issues/11692
const request = {
attributionId: user.usageAttributionId,
startedTimeOrder: SortOrder.Descending,
};
const allSessions = await this.listBilledUsage(ctx, request);
const totalUsage = allSessions.map((s) => s.credits).reduce((a, b) => a + b, 0);
const costCenter = await this.costCenterDB.findById(user.usageAttributionId);
if (costCenter) {
Expand All @@ -2096,12 +2102,8 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
return result;
}

async listBilledUsage(
ctx: TraceContext,
attributionId: string,
from?: number,
to?: number,
): Promise<BillableSession[]> {
async listBilledUsage(ctx: TraceContext, req: BillableSessionRequest): Promise<BillableSession[]> {
const { attributionId, startedTimeOrder, from, to } = req;
traceAPIParams(ctx, { attributionId });
let timestampFrom;
let timestampTo;
Expand All @@ -2116,7 +2118,13 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
timestampTo = Timestamp.fromDate(new Date(to));
}
const usageClient = this.usageServiceClientProvider.getDefault();
const response = await usageClient.listBilledUsage(ctx, attributionId, timestampFrom, timestampTo);
const response = await usageClient.listBilledUsage(
ctx,
attributionId,
startedTimeOrder as number,
timestampFrom,
timestampTo,
);
const sessions = response.getSessionsList().map((s) => this.mapBilledSession(s));

return sessions;
Expand Down
9 changes: 2 additions & 7 deletions components/server/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ import { LicenseEvaluator } from "@gitpod/licensor/lib";
import { Feature } from "@gitpod/licensor/lib/api";
import { Currency } from "@gitpod/gitpod-protocol/lib/plans";
import { getExperimentsClientForBackend } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server";
import { BillableSession } from "@gitpod/gitpod-protocol/lib/usage";
import { BillableSession, BillableSessionRequest } from "@gitpod/gitpod-protocol/lib/usage";
import { WorkspaceClusterImagebuilderClientProvider } from "./workspace-cluster-imagebuilder-client-provider";

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

async listBilledUsage(
ctx: TraceContext,
attributionId: string,
from?: number,
to?: number,
): Promise<BillableSession[]> {
async listBilledUsage(ctx: TraceContext, req: BillableSessionRequest): Promise<BillableSession[]> {
throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`);
}
async getSpendingLimitForTeam(ctx: TraceContext, teamId: string): Promise<number | undefined> {
Expand Down
3 changes: 2 additions & 1 deletion components/usage-api/typescript/src/usage/v1/sugar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ export class PromisifiedUsageServiceClient {
);
}

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

try {
const req = new ListBilledUsageRequest();
req.setAttributionId(attributionId);
req.setFrom(from);
req.setTo(to);
req.setOrder(order);

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

0 comments on commit 04f122c

Please sign in to comment.