@@ -4,148 +4,70 @@ import { Hex } from "viem";
4
4
import { gql , GraphQLClient } from "graphql-request" ;
5
5
6
6
const osoApiKey = process . env . REACT_APP_OSO_API_KEY as string ;
7
- const osoUrl = "https://opensource-observer.hasura.app /v1/graphql" ;
7
+ const osoUrl = "https://www. opensource.observer/api /v1/graphql" ;
8
8
const graphQLClient = new GraphQLClient ( osoUrl , {
9
9
headers : {
10
10
authorization : `Bearer ${ osoApiKey } ` ,
11
11
} ,
12
12
} ) ;
13
- let hasFetched = false ;
14
-
15
- interface IOSOId {
16
- projects_v1 : {
17
- project_id : Hex ;
18
- } ;
19
- }
13
+ let fetchedProject = "" ;
20
14
21
15
export interface IOSOStats {
22
- code_metrics_by_project_v1 : {
23
- contributor_count : number ;
24
- first_commit_date : number ;
25
- } ;
26
- events_monthly_to_project : [
27
- {
28
- bucket_month : number ;
29
- amount : number ;
30
- } ,
31
- {
32
- bucket_month : number ;
33
- amount : number ;
34
- } ,
35
- {
36
- bucket_month : number ;
37
- amount : number ;
38
- } ,
39
- {
40
- bucket_month : number ;
41
- amount : number ;
42
- } ,
43
- {
44
- bucket_month : number ;
45
- amount : number ;
46
- } ,
16
+ oso_codeMetricsByProjectV1 : [
47
17
{
48
- bucket_month : number ;
49
- amount : number ;
18
+ contributorCount : number ;
19
+ firstCommitDate : number ;
20
+ activeDeveloperCount6Months : number ;
50
21
} ,
51
22
] ;
52
23
}
53
24
54
25
export function useOSO ( projectGithub ?: string ) {
55
26
const emptyReturn : IOSOStats = {
56
- code_metrics_by_project_v1 : {
57
- contributor_count : 0 ,
58
- first_commit_date : 0 ,
59
- } ,
60
- events_monthly_to_project : [
61
- {
62
- bucket_month : 0 ,
63
- amount : 0 ,
64
- } ,
65
- {
66
- bucket_month : 0 ,
67
- amount : 0 ,
68
- } ,
69
- {
70
- bucket_month : 0 ,
71
- amount : 0 ,
72
- } ,
73
- {
74
- bucket_month : 0 ,
75
- amount : 0 ,
76
- } ,
27
+ oso_codeMetricsByProjectV1 : [
77
28
{
78
- bucket_month : 0 ,
79
- amount : 0 ,
80
- } ,
81
- {
82
- bucket_month : 0 ,
83
- amount : 0 ,
29
+ contributorCount : 0 ,
30
+ firstCommitDate : 0 ,
31
+ activeDeveloperCount6Months : 0 ,
84
32
} ,
85
33
] ,
86
34
} ;
87
35
const [ stats , setStats ] = useState < IOSOStats | null > ( null ) ;
88
36
89
37
const getStatsFor = async ( projectRegistryGithub : string ) => {
38
+ fetchedProject = projectRegistryGithub ;
90
39
if ( osoApiKey === "" )
91
40
throw new Error ( "OpenSourceObserver API key not set." ) ;
92
- const queryId = gql `{
93
- projects_v1(where: {display_name: {_ilike: "${ projectRegistryGithub } "}}
94
- distinct_on: project_id
95
- ) {
96
- project_id
41
+ const queryVars = {
42
+ where : {
43
+ displayName : {
44
+ _ilike : `${ projectRegistryGithub } ` ,
45
+ } ,
46
+ } ,
47
+ } ;
48
+ const queryStats = gql `
49
+ query myQuery($where: Oso_CodeMetricsByProjectV1BoolExp) {
50
+ oso_codeMetricsByProjectV1(where: $where) {
51
+ contributorCount
52
+ firstCommitDate
53
+ activeDeveloperCount6Months
54
+ }
97
55
}
98
- } ` ;
56
+ ` ;
99
57
100
58
try {
101
- hasFetched = true ;
102
- const idData : IOSOId = await graphQLClient . request < IOSOId > ( queryId ) ;
59
+ const items : IOSOStats = await graphQLClient . request < IOSOStats > (
60
+ queryStats ,
61
+ queryVars
62
+ ) ;
103
63
104
- if ( ! Array . isArray ( idData . projects_v1 ) ) {
105
- setStats ( emptyReturn ) ;
106
- return ;
64
+ if ( ! items . oso_codeMetricsByProjectV1 ?. length ) {
65
+ throw new Error ( "no stats returned" ) ;
107
66
}
108
-
109
- const parsedId : IOSOId = {
110
- projects_v1 : idData . projects_v1 [ 0 ] ,
67
+ const parsedItems : IOSOStats = {
68
+ oso_codeMetricsByProjectV1 : items . oso_codeMetricsByProjectV1 ,
111
69
} ;
112
-
113
- const queryStats = gql `{
114
- code_metrics_by_project_v1(where: {project_id: {_eq: "${ parsedId . projects_v1 . project_id } "}}) {
115
- contributor_count
116
- first_commit_date
117
- }
118
- events_monthly_to_project(
119
- where: {project_id: {_eq: "${ parsedId . projects_v1 . project_id } "}, event_type: {_eq: "COMMIT_CODE"}}
120
- limit: 6
121
- order_by: {bucket_month: desc}
122
- ) {
123
- bucket_month
124
- amount
125
- }
126
- }` ;
127
-
128
- const items : IOSOStats =
129
- await graphQLClient . request < IOSOStats > ( queryStats ) ;
130
-
131
- if ( ! Array . isArray ( items . code_metrics_by_project_v1 ) ) {
132
- setStats ( emptyReturn ) ;
133
- return ;
134
- }
135
-
136
- if ( items . events_monthly_to_project . length === 6 ) {
137
- const parsedItems : IOSOStats = {
138
- code_metrics_by_project_v1 : items . code_metrics_by_project_v1 [ 0 ] ,
139
- events_monthly_to_project : items . events_monthly_to_project ,
140
- } ;
141
- setStats ( parsedItems ) ;
142
- } else {
143
- const parsedItems : IOSOStats = {
144
- code_metrics_by_project_v1 : items . code_metrics_by_project_v1 [ 0 ] ,
145
- events_monthly_to_project : emptyReturn . events_monthly_to_project ,
146
- } ;
147
- setStats ( parsedItems ) ;
148
- }
70
+ setStats ( parsedItems ) ;
149
71
} catch ( e ) {
150
72
console . error ( `No stats found for project: ${ projectGithub } ` ) ;
151
73
console . error ( e ) ;
@@ -158,8 +80,9 @@ export function useOSO(projectGithub?: string) {
158
80
revalidateOnMount : true ,
159
81
} ) ;
160
82
161
- if ( stats === null && ! hasFetched )
162
- projectGithub && getStatsFor ( projectGithub ) ;
83
+ if ( fetchedProject !== projectGithub )
84
+ // check if currently loaded stats are for viewed project
85
+ projectGithub && getStatsFor ( projectGithub ) ; // fetch if not
163
86
return {
164
87
/**
165
88
* Fetch OSO for stats on a project
0 commit comments