1
1
import React , { useCallback , useState } from 'react'
2
2
3
+ import type { ConnectError } from '@connectrpc/connect'
3
4
import { mdiPencil , mdiTrashCan } from '@mdi/js'
4
- import { parseISO } from 'date-fns '
5
+ import type { UseQueryResult } from '@tanstack/react-query '
5
6
import type { GraphQLError } from 'graphql'
6
7
7
8
import { Toggle } from '@sourcegraph/branded/src/components/Toggle'
8
9
import { logger } from '@sourcegraph/common'
9
- import { useMutation , useQuery } from '@sourcegraph/http-client'
10
+ import { useMutation } from '@sourcegraph/http-client'
10
11
import { CodyGatewayRateLimitSource } from '@sourcegraph/shared/src/graphql-operations'
11
- import { TelemetryV2Props } from '@sourcegraph/shared/src/telemetry'
12
+ import type { TelemetryV2Props } from '@sourcegraph/shared/src/telemetry'
12
13
import {
13
14
H3 ,
14
15
ProductStatusBadge ,
@@ -35,26 +36,20 @@ import type {
35
36
Scalars ,
36
37
UpdateCodyGatewayConfigResult ,
37
38
UpdateCodyGatewayConfigVariables ,
38
- CodyGatewayRateLimitUsageDatapoint ,
39
39
CodyGatewayRateLimitFields ,
40
- DotComProductSubscriptionCodyGatewayCompletionsUsageResult ,
41
- DotComProductSubscriptionCodyGatewayCompletionsUsageVariables ,
42
- DotComProductSubscriptionCodyGatewayEmbeddingsUsageVariables ,
43
- DotComProductSubscriptionCodyGatewayEmbeddingsUsageResult ,
44
40
} from '../../../../graphql-operations'
45
41
import { ChartContainer } from '../../../../site-admin/analytics/components/ChartContainer'
46
42
47
- import {
48
- DOTCOM_PRODUCT_SUBSCRIPTION_CODY_GATEWAY_COMPLETIONS_USAGE ,
49
- DOTCOM_PRODUCT_SUBSCRIPTION_CODY_GATEWAY_EMBEDDINGS_USAGE ,
50
- UPDATE_CODY_GATEWAY_CONFIG ,
51
- } from './backend'
43
+ import { UPDATE_CODY_GATEWAY_CONFIG } from './backend'
52
44
import { CodyGatewayRateLimitModal } from './CodyGatewayRateLimitModal'
45
+ import { useGetCodyGatewayUsage , type EnterprisePortalEnvironment } from './enterpriseportal'
46
+ import type { CodyGatewayUsage_UsageDatapoint , GetCodyGatewayUsageResponse } from './enterpriseportalgen/codyaccess_pb'
53
47
import { numberFormatter , prettyInterval } from './utils'
54
48
55
49
import styles from './CodyServicesSection.module.scss'
56
50
57
51
interface Props extends TelemetryV2Props {
52
+ enterprisePortalEnvironment : EnterprisePortalEnvironment
58
53
productSubscriptionUUID : string
59
54
productSubscriptionID : Scalars [ 'ID' ]
60
55
currentSourcegraphAccessToken : string | null
@@ -65,6 +60,7 @@ interface Props extends TelemetryV2Props {
65
60
}
66
61
67
62
export const CodyServicesSection : React . FunctionComponent < Props > = ( {
63
+ enterprisePortalEnvironment,
68
64
productSubscriptionUUID,
69
65
productSubscriptionID,
70
66
viewerCanAdminister,
@@ -74,6 +70,9 @@ export const CodyServicesSection: React.FunctionComponent<Props> = ({
74
70
codyGatewayAccess,
75
71
telemetryRecorder,
76
72
} ) => {
73
+ // TODO: Figure out strategy for what instance to target
74
+ const codyGatewayUsageQuery = useGetCodyGatewayUsage ( enterprisePortalEnvironment , productSubscriptionUUID )
75
+
77
76
const [ updateCodyGatewayConfig , { loading : updateCodyGatewayConfigLoading , error : updateCodyGatewayConfigError } ] =
78
77
useMutation < UpdateCodyGatewayConfigResult , UpdateCodyGatewayConfigVariables > ( UPDATE_CODY_GATEWAY_CONFIG )
79
78
@@ -175,7 +174,7 @@ export const CodyServicesSection: React.FunctionComponent<Props> = ({
175
174
/>
176
175
</ tbody >
177
176
</ table >
178
- < RateLimitUsage mode = "completions" productSubscriptionUUID = { productSubscriptionUUID } />
177
+ < RateLimitUsage mode = "completions" usageQuery = { codyGatewayUsageQuery } />
179
178
180
179
< hr className = "my-3" />
181
180
@@ -201,10 +200,7 @@ export const CodyServicesSection: React.FunctionComponent<Props> = ({
201
200
/>
202
201
</ tbody >
203
202
</ table >
204
- < EmbeddingsRateLimitUsage
205
- mode = "embeddings"
206
- productSubscriptionUUID = { productSubscriptionUUID }
207
- />
203
+ < EmbeddingsRateLimitUsage mode = "embeddings" usageQuery = { codyGatewayUsageQuery } />
208
204
</ >
209
205
) }
210
206
@@ -277,8 +273,8 @@ export const CodyGatewayRateLimitSourceBadge: React.FunctionComponent<{
277
273
}
278
274
}
279
275
280
- function generateSeries ( data : CodyGatewayRateLimitUsageDatapoint [ ] ) : [ string , CodyGatewayRateLimitUsageDatapoint [ ] ] [ ] {
281
- const series : Record < string , CodyGatewayRateLimitUsageDatapoint [ ] > = { }
276
+ function generateSeries ( data : CodyGatewayUsage_UsageDatapoint [ ] ) : [ string , CodyGatewayUsage_UsageDatapoint [ ] ] [ ] {
277
+ const series : Record < string , CodyGatewayUsage_UsageDatapoint [ ] > = { }
282
278
for ( const entry of data ) {
283
279
if ( ! series [ entry . model ] ) {
284
280
series [ entry . model ] = [ ]
@@ -411,17 +407,12 @@ const RateLimitRow: React.FunctionComponent<RateLimitRowProps> = ({
411
407
}
412
408
413
409
interface RateLimitUsageProps {
414
- productSubscriptionUUID : string
410
+ usageQuery : UseQueryResult < GetCodyGatewayUsageResponse , ConnectError >
415
411
mode : 'completions' | 'embeddings'
416
412
}
417
413
418
- const RateLimitUsage : React . FunctionComponent < RateLimitUsageProps > = ( { productSubscriptionUUID } ) => {
419
- const { data, loading, error } = useQuery <
420
- DotComProductSubscriptionCodyGatewayCompletionsUsageResult ,
421
- DotComProductSubscriptionCodyGatewayCompletionsUsageVariables
422
- > ( DOTCOM_PRODUCT_SUBSCRIPTION_CODY_GATEWAY_COMPLETIONS_USAGE , { variables : { uuid : productSubscriptionUUID } } )
423
-
424
- if ( loading && ! data ) {
414
+ const RateLimitUsage : React . FunctionComponent < RateLimitUsageProps > = ( { usageQuery : { data, isLoading, error } } ) => {
415
+ if ( isLoading && ! data ) {
425
416
return (
426
417
< >
427
418
< H5 className = "mb-2" > Usage</ H5 >
@@ -439,8 +430,7 @@ const RateLimitUsage: React.FunctionComponent<RateLimitUsageProps> = ({ productS
439
430
)
440
431
}
441
432
442
- const { codyGatewayAccess } = data ! . dotcom . productSubscription
443
-
433
+ const usage = data ?. usage
444
434
return (
445
435
< >
446
436
< H5 className = "mb-2" > Usage</ H5 >
@@ -450,28 +440,28 @@ const RateLimitUsage: React.FunctionComponent<RateLimitUsageProps> = ({ productS
450
440
width = { width }
451
441
height = { 200 }
452
442
series = { [
453
- ...generateSeries ( codyGatewayAccess . chatCompletionsRateLimit ?. usage ?? [ ] ) . map (
454
- ( [ model , data ] ) : Series < CodyGatewayRateLimitUsageDatapoint > => ( {
443
+ ...generateSeries ( usage ?. chatCompletionsUsage ?? [ ] ) . map (
444
+ ( [ model , data ] ) : Series < CodyGatewayUsage_UsageDatapoint > => ( {
455
445
data,
456
446
getXValue ( datum ) {
457
- return parseISO ( datum . date )
447
+ return datum . time ?. toDate ( ) || new Date ( )
458
448
} ,
459
449
getYValue ( datum ) {
460
- return Number ( datum . count )
450
+ return Number ( datum . usage )
461
451
} ,
462
452
id : 'chat-usage' ,
463
453
name : 'Chat completions: ' + model ,
464
454
color : 'var(--purple)' ,
465
455
} )
466
456
) ,
467
- ...generateSeries ( codyGatewayAccess . codeCompletionsRateLimit ?. usage ?? [ ] ) . map (
468
- ( [ model , data ] ) : Series < CodyGatewayRateLimitUsageDatapoint > => ( {
457
+ ...generateSeries ( data ?. usage ?. codeCompletionsUsage ?? [ ] ) . map (
458
+ ( [ model , data ] ) : Series < CodyGatewayUsage_UsageDatapoint > => ( {
469
459
data,
470
460
getXValue ( datum ) {
471
- return parseISO ( datum . date )
461
+ return datum . time ?. toDate ( ) || new Date ( )
472
462
} ,
473
463
getYValue ( datum ) {
474
- return Number ( datum . count )
464
+ return Number ( datum . usage )
475
465
} ,
476
466
id : 'code-completions-usage' ,
477
467
name : 'Code completions: ' + model ,
@@ -486,13 +476,10 @@ const RateLimitUsage: React.FunctionComponent<RateLimitUsageProps> = ({ productS
486
476
)
487
477
}
488
478
489
- const EmbeddingsRateLimitUsage : React . FunctionComponent < RateLimitUsageProps > = ( { productSubscriptionUUID } ) => {
490
- const { data, loading, error } = useQuery <
491
- DotComProductSubscriptionCodyGatewayEmbeddingsUsageResult ,
492
- DotComProductSubscriptionCodyGatewayEmbeddingsUsageVariables
493
- > ( DOTCOM_PRODUCT_SUBSCRIPTION_CODY_GATEWAY_EMBEDDINGS_USAGE , { variables : { uuid : productSubscriptionUUID } } )
494
-
495
- if ( loading && ! data ) {
479
+ const EmbeddingsRateLimitUsage : React . FunctionComponent < RateLimitUsageProps > = ( {
480
+ usageQuery : { data, isLoading, error } ,
481
+ } ) => {
482
+ if ( isLoading && ! data ) {
496
483
return (
497
484
< >
498
485
< H5 className = "mb-2" > Usage</ H5 >
@@ -510,8 +497,7 @@ const EmbeddingsRateLimitUsage: React.FunctionComponent<RateLimitUsageProps> = (
510
497
)
511
498
}
512
499
513
- const { codyGatewayAccess } = data ! . dotcom . productSubscription
514
-
500
+ const usage = data ?. usage
515
501
return (
516
502
< >
517
503
< H5 className = "mb-2" > Usage</ H5 >
@@ -521,16 +507,16 @@ const EmbeddingsRateLimitUsage: React.FunctionComponent<RateLimitUsageProps> = (
521
507
width = { width }
522
508
height = { 200 }
523
509
series = { [
524
- ...generateSeries ( codyGatewayAccess . embeddingsRateLimit ?. usage ?? [ ] ) . map (
525
- ( [ model , data ] ) : Series < CodyGatewayRateLimitUsageDatapoint > => ( {
510
+ ...generateSeries ( usage ?. embeddingsUsage ?? [ ] ) . map (
511
+ ( [ model , data ] ) : Series < CodyGatewayUsage_UsageDatapoint > => ( {
526
512
data,
527
513
getXValue ( datum ) {
528
- return parseISO ( datum . date )
514
+ return datum . time ?. toDate ( ) || new Date ( )
529
515
} ,
530
516
getYValue ( datum ) {
531
- return Number ( datum . count )
517
+ return Number ( datum . usage )
532
518
} ,
533
- id : 'chat -usage' ,
519
+ id : 'embeddings -usage' ,
534
520
name : 'Embedded tokens: ' + model ,
535
521
color : 'var(--purple)' ,
536
522
} )
0 commit comments