1
- //Make a call to the GitHub API to get Copilot Metrics, the API is /api/github/orgs/toussaintt/copilot/usage
2
- //Add the header Accept: application/vnd.github+json to the request
3
- //Add also the Authorization: Bearer <token> header where <token> is hardcoded for now
4
- //Also add X-GitHub-Api-Version: 2022-11-28 header
5
- //Return the response from the API
6
-
7
1
import axios from "axios" ;
8
-
9
2
import { Metrics } from "../model/Metrics" ;
10
- import organizationMockedResponse from '../assets/organization_response_sample.json' ;
11
- import enterpriseMockedResponse from '../assets/enterprise_response_sample.json' ;
3
+ import { CopilotMetrics } from '../model/Copilot_Metrics' ;
4
+ import { convertToMetrics } from './MetricsToUsageConverter' ;
5
+ import organizationMockedResponse from '../assets/organization_usage_response_sample.json' ;
6
+ import enterpriseMockedResponse from '../assets/enterprise_usage_response_sample.json' ;
7
+ import organizationMockedMetricsResponse from '../assets/organization_metrics_response_sample.json' ;
8
+ import enterpriseMockedMetricsResponse from '../assets/enterprise_metrics_response_sample.json' ;
12
9
import config from '../config' ;
13
10
14
11
const headers = {
@@ -17,27 +14,43 @@ const headers = {
17
14
...( config . github . token ? { Authorization : `token ${ config . github . token } ` } : { } )
18
15
} ;
19
16
20
- export const getMetricsApi = async ( ) : Promise < Metrics [ ] > => {
17
+ const ensureCopilotMetrics = ( data : any [ ] ) : CopilotMetrics [ ] => {
18
+ return data . map ( item => {
19
+ if ( ! item . copilot_ide_code_completions ) {
20
+ item . copilot_ide_code_completions = { editors : [ ] } ;
21
+ }
22
+ item . copilot_ide_code_completions . editors ?. forEach ( ( editor : any ) => {
23
+ editor . models ?. forEach ( ( model : any ) => {
24
+ if ( ! model . languages ) {
25
+ model . languages = [ ] ;
26
+ }
27
+ } ) ;
28
+ } ) ;
29
+ return item as CopilotMetrics ;
30
+ } ) ;
31
+ } ;
21
32
33
+ export const getMetricsApi = async ( ) : Promise < { metrics : Metrics [ ] , original : CopilotMetrics [ ] } > => {
22
34
let response ;
23
- let metricsData ;
35
+ let metricsData : Metrics [ ] ;
36
+ let originalData : CopilotMetrics [ ] ;
24
37
25
38
if ( config . mockedData ) {
26
39
console . log ( "Using mock data. Check VUE_APP_MOCKED_DATA variable." ) ;
27
- response = config . scope . type === "organization" ? organizationMockedResponse : enterpriseMockedResponse ;
28
- metricsData = response . map ( ( item : any ) => new Metrics ( item ) ) ;
40
+ response = config . scope . type === "organization" ? organizationMockedMetricsResponse : enterpriseMockedMetricsResponse ;
41
+ originalData = ensureCopilotMetrics ( response ) ;
42
+ metricsData = convertToMetrics ( originalData ) ;
29
43
} else {
30
44
response = await axios . get (
31
- `${ config . github . apiUrl } /copilot/usage ` ,
45
+ `${ config . github . apiUrl } /copilot/metrics ` ,
32
46
{
33
- headers
47
+ headers
34
48
}
35
49
) ;
36
-
37
-
38
- metricsData = response . data . map ( ( item : any ) => new Metrics ( item ) ) ;
50
+ originalData = ensureCopilotMetrics ( response . data ) ;
51
+ metricsData = convertToMetrics ( originalData ) ;
39
52
}
40
- return metricsData ;
53
+ return { metrics : metricsData , original : originalData } ;
41
54
} ;
42
55
43
56
export const getTeams = async ( ) : Promise < string [ ] > => {
@@ -48,12 +61,12 @@ export const getTeams = async (): Promise<string[]> => {
48
61
return response . data ;
49
62
}
50
63
51
- export const getTeamMetricsApi = async ( ) : Promise < Metrics [ ] > => {
64
+ export const getTeamMetricsApi = async ( ) : Promise < { metrics : Metrics [ ] , original : CopilotMetrics [ ] } > => {
52
65
console . log ( "config.github.team: " + config . github . team ) ;
53
66
54
67
if ( config . github . team && config . github . team . trim ( ) !== '' ) {
55
68
const response = await axios . get (
56
- `${ config . github . apiUrl } /team/${ config . github . team } /copilot/usage ` ,
69
+ `${ config . github . apiUrl } /team/${ config . github . team } /copilot/metrics ` ,
57
70
{
58
71
headers : {
59
72
Accept : "application/vnd.github+json" ,
@@ -63,9 +76,10 @@ export const getTeamMetricsApi = async (): Promise<Metrics[]> => {
63
76
}
64
77
) ;
65
78
66
- return response . data . map ( ( item : any ) => new Metrics ( item ) ) ;
79
+ const originalData = ensureCopilotMetrics ( response . data ) ;
80
+ const metricsData = convertToMetrics ( originalData ) ;
81
+ return { metrics : metricsData , original : originalData } ;
67
82
}
68
83
69
- return [ ] ;
70
-
84
+ return { metrics : [ ] , original : [ ] } ;
71
85
}
0 commit comments