@@ -71,8 +71,13 @@ import { BlockedRepository } from "@gitpod/gitpod-protocol/lib/blocked-repositor
71
71
import { EligibilityService } from "../user/eligibility-service" ;
72
72
import { AccountStatementProvider } from "../user/account-statement-provider" ;
73
73
import { GithubUpgradeURL , PlanCoupon } from "@gitpod/gitpod-protocol/lib/payment-protocol" ;
74
- import { ListBilledUsageRequest , ListBilledUsageResponse } from "@gitpod/gitpod-protocol/lib/usage" ;
75
- import { ListBilledUsageRequest as ListBilledUsage } from "@gitpod/usage-api/lib/usage/v1/usage_pb" ;
74
+ import {
75
+ ListBilledUsageRequest ,
76
+ ListBilledUsageResponse ,
77
+ ListUsageRequest ,
78
+ ListUsageResponse ,
79
+ } from "@gitpod/gitpod-protocol/lib/usage" ;
80
+ import * as usage_grpc from "@gitpod/usage-api/lib/usage/v1/usage_pb" ;
76
81
import {
77
82
AssigneeIdentityIdentifier ,
78
83
TeamSubscription ,
@@ -114,6 +119,7 @@ import { BillingMode } from "@gitpod/gitpod-protocol/lib/billing-mode";
114
119
import { BillingModes } from "../billing/billing-mode" ;
115
120
import { getExperimentsClientForBackend } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server" ;
116
121
import { BillingService } from "../billing/billing-service" ;
122
+ import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods" ;
117
123
118
124
@injectable ( )
119
125
export class GitpodServerEEImpl extends GitpodServerImpl {
@@ -2182,6 +2188,60 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
2182
2188
return result ;
2183
2189
}
2184
2190
2191
+ async listUsage ( ctx : TraceContext , req : ListUsageRequest ) : Promise < ListUsageResponse > {
2192
+ const { attributionId, from, to } = req ;
2193
+ traceAPIParams ( ctx , { attributionId } ) ;
2194
+ const user = this . checkAndBlockUser ( "listBilledUsage" ) ;
2195
+
2196
+ await this . guardCostCenterAccess ( ctx , user . id , attributionId , "get" ) ;
2197
+
2198
+ const timestampFrom = from ? Timestamp . fromDate ( new Date ( from ) ) : undefined ;
2199
+ const timestampTo = to ? Timestamp . fromDate ( new Date ( to ) ) : undefined ;
2200
+
2201
+ const usageClient = this . usageServiceClientProvider . getDefault ( ) ;
2202
+ const request = new usage_grpc . ListBilledUsageRequest ( ) ;
2203
+ request . setAttributionId ( attributionId ) ;
2204
+ request . setFrom ( timestampFrom ) ;
2205
+ if ( to ) {
2206
+ request . setTo ( timestampTo ) ;
2207
+ }
2208
+ request . setOrder ( req . order ) ;
2209
+ if ( req . pagination ) {
2210
+ const paginatedRequest = new usage_grpc . PaginatedRequest ( ) ;
2211
+ paginatedRequest . setPage ( req . pagination . page ) ;
2212
+ paginatedRequest . setPerPage ( req . pagination . perPage ) ;
2213
+ request . setPagination ( paginatedRequest ) ;
2214
+ }
2215
+ const response = await usageClient . listUsage ( ctx , request ) ;
2216
+ const pagination = response . getPagination ( ) ;
2217
+ return {
2218
+ usageEntriesList : response . getUsageEntriesList ( ) . map ( ( u ) => {
2219
+ return {
2220
+ id : u . getId ( ) ,
2221
+ attributionId : u . getAttributionId ( ) ,
2222
+ effectiveTime : u . getEffectiveTime ( ) ! . toDate ( ) . getTime ( ) ,
2223
+ credits : u . getCredits ( ) ,
2224
+ description : u . getDescription ( ) ,
2225
+ draft : u . getDraft ( ) ,
2226
+ workspaceInstanceId : u . getWorkspaceInstanceId ( ) ,
2227
+ kind :
2228
+ u . getKind ( ) === usage_grpc . Usage . Kind . KIND_WORKSPACE_INSTANCE ? "workspaceinstance" : "invoice" ,
2229
+ metadata : JSON . parse ( u . getMetadata ( ) ) ,
2230
+ } ;
2231
+ } ) ,
2232
+ pagination : pagination
2233
+ ? {
2234
+ page : pagination . getPage ( ) ,
2235
+ perPage : pagination . getPerPage ( ) ,
2236
+ total : pagination . getTotal ( ) ,
2237
+ totalPages : pagination . getTotalPages ( ) ,
2238
+ }
2239
+ : undefined ,
2240
+ creditBalanceAtEnd : response . getCreditBalanceAtEnd ( ) ,
2241
+ creditBalanceAtStart : response . getCreditBalanceAtStart ( ) ,
2242
+ } ;
2243
+ }
2244
+
2185
2245
async listBilledUsage ( ctx : TraceContext , req : ListBilledUsageRequest ) : Promise < ListBilledUsageResponse > {
2186
2246
const { attributionId, fromDate, toDate, perPage, page } = req ;
2187
2247
traceAPIParams ( ctx , { attributionId } ) ;
@@ -2201,7 +2261,7 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
2201
2261
const response = await usageClient . listBilledUsage (
2202
2262
ctx ,
2203
2263
attributionId ,
2204
- ListBilledUsage . Ordering . ORDERING_DESCENDING ,
2264
+ usage_grpc . ListBilledUsageRequest . Ordering . ORDERING_DESCENDING ,
2205
2265
perPage ,
2206
2266
page ,
2207
2267
timestampFrom ,
0 commit comments