@@ -9,10 +9,11 @@ import { useLocation } from "react-router";
9
9
import { getCurrentTeam , TeamsContext } from "./teams-context" ;
10
10
import { getGitpodService , gitpodHostUrl } from "../service/service" ;
11
11
import {
12
- ListBilledUsageRequest ,
13
- BillableWorkspaceType ,
14
- ExtendedBillableSession ,
15
- ListBilledUsageResponse ,
12
+ ListUsageRequest ,
13
+ Ordering ,
14
+ ListUsageResponse ,
15
+ WorkspaceInstanceUsageData ,
16
+ Usage ,
16
17
} from "@gitpod/gitpod-protocol/lib/usage" ;
17
18
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution" ;
18
19
import { Item , ItemField , ItemsList } from "../components/ItemsList" ;
@@ -24,13 +25,14 @@ import { ReactComponent as Spinner } from "../icons/Spinner.svg";
24
25
import { ReactComponent as UsageIcon } from "../images/usage-default.svg" ;
25
26
import { BillingMode } from "@gitpod/gitpod-protocol/lib/billing-mode" ;
26
27
import { toRemoteURL } from "../projects/render-utils" ;
28
+ import { WorkspaceType } from "@gitpod/gitpod-protocol" ;
27
29
28
30
function TeamUsage ( ) {
29
31
const { teams } = useContext ( TeamsContext ) ;
30
32
const location = useLocation ( ) ;
31
33
const team = getCurrentTeam ( location , teams ) ;
32
34
const [ teamBillingMode , setTeamBillingMode ] = useState < BillingMode | undefined > ( undefined ) ;
33
- const [ usagePage , setUsagePage ] = useState < ListBilledUsageResponse | undefined > ( undefined ) ;
35
+ const [ usagePage , setUsagePage ] = useState < ListUsageResponse | undefined > ( undefined ) ;
34
36
const [ errorMessage , setErrorMessage ] = useState ( "" ) ;
35
37
const today = new Date ( ) ;
36
38
const startOfCurrentMonth = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 1 ) ;
@@ -75,17 +77,20 @@ function TeamUsage() {
75
77
setTotalCreditsUsed ( 0 ) ;
76
78
}
77
79
const attributionId = AttributionId . render ( { kind : "team" , teamId : team . id } ) ;
78
- const request : ListBilledUsageRequest = {
80
+ const request : ListUsageRequest = {
79
81
attributionId,
80
- fromDate : startDateOfBillMonth ,
81
- toDate : endDateOfBillMonth ,
82
- perPage : 50 ,
83
- page,
82
+ from : startDateOfBillMonth ,
83
+ to : endDateOfBillMonth ,
84
+ order : Ordering . ORDERING_DESCENDING ,
85
+ pagination : {
86
+ perPage : 50 ,
87
+ page,
88
+ } ,
84
89
} ;
85
90
try {
86
- const page = await getGitpodService ( ) . server . listBilledUsage ( request ) ;
91
+ const page = await getGitpodService ( ) . server . listUsage ( request ) ;
87
92
setUsagePage ( page ) ;
88
- setTotalCreditsUsed ( Math . ceil ( page . totalCreditsUsed ) ) ;
93
+ setTotalCreditsUsed ( Math . ceil ( page . creditBalanceAtEnd ) ) ;
89
94
} catch ( error ) {
90
95
if ( error . code === ErrorCodes . PERMISSION_DENIED ) {
91
96
setErrorMessage ( "Access to usage details is restricted to team owners." ) ;
@@ -97,19 +102,23 @@ function TeamUsage() {
97
102
}
98
103
} ;
99
104
100
- const getType = ( type : BillableWorkspaceType ) => {
105
+ const getType = ( type : WorkspaceType ) => {
101
106
if ( type === "regular" ) {
102
107
return "Workspace" ;
103
108
}
104
109
return "Prebuild" ;
105
110
} ;
106
111
107
- const getMinutes = ( usage : ExtendedBillableSession ) => {
108
- if ( ! usage . endTime ) {
112
+ const getMinutes = ( usage : Usage ) => {
113
+ if ( usage . kind !== "workspaceinstance" ) {
114
+ return "" ;
115
+ }
116
+ const metaData = usage . metadata as WorkspaceInstanceUsageData ;
117
+ if ( ! metaData . endTime ) {
109
118
return "running" ;
110
119
}
111
- const end = new Date ( usage . endTime ) . getTime ( ) ;
112
- const start = new Date ( usage . startTime ) . getTime ( ) ;
120
+ const end = new Date ( metaData . endTime ) . getTime ( ) ;
121
+ const start = new Date ( metaData . startTime ) . getTime ( ) ;
113
122
const lengthOfUsage = Math . floor ( end - start ) ;
114
123
const inMinutes = ( lengthOfUsage / ( 1000 * 60 ) ) . toFixed ( 1 ) ;
115
124
return inMinutes + " min" ;
@@ -142,7 +151,7 @@ function TeamUsage() {
142
151
return rows ;
143
152
} ;
144
153
145
- const displayTime = ( time : string ) => {
154
+ const displayTime = ( time : string | number ) => {
146
155
const options : Intl . DateTimeFormatOptions = {
147
156
day : "numeric" ,
148
157
month : "short" ,
@@ -153,7 +162,7 @@ function TeamUsage() {
153
162
return new Date ( time ) . toLocaleDateString ( undefined , options ) . replace ( "at " , "" ) ;
154
163
} ;
155
164
156
- const currentPaginatedResults = usagePage ?. sessions ?? [ ] ;
165
+ const currentPaginatedResults = usagePage ?. usageEntriesList ?? [ ] ;
157
166
158
167
return (
159
168
< >
@@ -237,23 +246,34 @@ function TeamUsage() {
237
246
currentPaginatedResults . map ( ( usage ) => {
238
247
return (
239
248
< div
240
- key = { usage . instanceId }
249
+ key = { usage . workspaceInstanceId }
241
250
className = "flex p-3 grid grid-cols-12 gap-x-3 justify-between transition ease-in-out rounded-xl"
242
251
>
243
252
< div className = "flex flex-col col-span-2 my-auto" >
244
253
< span className = "text-gray-600 dark:text-gray-100 text-md font-medium" >
245
- { getType ( usage . workspaceType ) }
254
+ { getType (
255
+ ( usage . metadata as WorkspaceInstanceUsageData )
256
+ . workspaceType ,
257
+ ) }
246
258
</ span >
247
259
< span className = "text-sm text-gray-400 dark:text-gray-500" >
248
- { usage . workspaceClass }
260
+ {
261
+ ( usage . metadata as WorkspaceInstanceUsageData )
262
+ . workspaceClass
263
+ }
249
264
</ span >
250
265
</ div >
251
266
< div className = "flex flex-col col-span-5 my-auto" >
252
267
< span className = "truncate text-gray-600 dark:text-gray-100 text-md font-medium" >
253
- { usage . workspaceId }
268
+ { ( usage . metadata as WorkspaceInstanceUsageData ) . workspaceId }
254
269
</ span >
255
270
< span className = "text-sm truncate text-gray-400 dark:text-gray-500" >
256
- { usage . contextURL && toRemoteURL ( usage . contextURL ) }
271
+ { ( usage . metadata as WorkspaceInstanceUsageData )
272
+ . contextURL &&
273
+ toRemoteURL (
274
+ ( usage . metadata as WorkspaceInstanceUsageData )
275
+ . contextURL ,
276
+ ) }
257
277
</ span >
258
278
</ div >
259
279
< div className = "flex flex-col my-auto" >
@@ -267,27 +287,34 @@ function TeamUsage() {
267
287
< div className = "my-auto" />
268
288
< div className = "flex flex-col col-span-3 my-auto" >
269
289
< span className = "text-gray-400 dark:text-gray-500 truncate font-medium" >
270
- { displayTime ( usage . startTime ) }
290
+ { displayTime ( usage . effectiveTime ! ) }
271
291
</ span >
272
292
< div className = "flex" >
273
- { usage . workspaceType === "prebuild" ? (
293
+ { ( usage . metadata as WorkspaceInstanceUsageData )
294
+ . workspaceType === "prebuild" ? (
274
295
< UsageIcon className = "my-auto w-4 h-4 mr-1" />
275
296
) : (
276
297
""
277
298
) }
278
- { usage . workspaceType === "prebuild" ? (
299
+ { ( usage . metadata as WorkspaceInstanceUsageData )
300
+ . workspaceType === "prebuild" ? (
279
301
< span className = "text-sm text-gray-400 dark:text-gray-500" >
280
302
Gitpod
281
303
</ span >
282
304
) : (
283
305
< div className = "flex" >
284
306
< img
285
307
className = "my-auto rounded-full w-4 h-4 inline-block align-text-bottom mr-1 overflow-hidden"
286
- src = { usage . user ?. avatarURL || "" }
308
+ src = {
309
+ (
310
+ usage . metadata as WorkspaceInstanceUsageData
311
+ ) . userAvatarURL || ""
312
+ }
287
313
alt = "user avatar"
288
314
/>
289
315
< span className = "text-sm text-gray-400 dark:text-gray-500" >
290
- { usage . user ?. name }
316
+ { ( usage . metadata as WorkspaceInstanceUsageData )
317
+ . userName || "" }
291
318
</ span >
292
319
</ div >
293
320
) }
@@ -297,11 +324,11 @@ function TeamUsage() {
297
324
) ;
298
325
} ) }
299
326
</ ItemsList >
300
- { usagePage && usagePage . totalPages > 1 && (
327
+ { usagePage && usagePage . pagination && usagePage . pagination . totalPages > 1 && (
301
328
< Pagination
302
- currentPage = { usagePage . page }
329
+ currentPage = { usagePage . pagination . page }
303
330
setPage = { ( page ) => loadPage ( page ) }
304
- totalNumberOfPages = { usagePage . totalPages }
331
+ totalNumberOfPages = { usagePage . pagination . totalPages }
305
332
/>
306
333
) }
307
334
</ div >
0 commit comments