@@ -8,6 +8,7 @@ use once_cell::sync::Lazy;
8
8
use postgres:: Client ;
9
9
use regex:: Regex ;
10
10
use std:: collections:: { HashMap , HashSet } ;
11
+ use std:: fmt;
11
12
use std:: sync:: Arc ;
12
13
use std:: time:: Duration ;
13
14
@@ -17,7 +18,7 @@ pub trait RepositoryForge {
17
18
fn host ( & self ) -> & str ;
18
19
19
20
/// FontAwesome icon used in the front-end.
20
- fn icon ( & self ) -> & str ;
21
+ fn icon ( & self ) -> & ' static str ;
21
22
22
23
/// How many items we can query in one graphql request.
23
24
fn chunk_size ( & self ) -> usize ;
@@ -49,11 +50,23 @@ pub struct FetchRepositoriesResult {
49
50
pub missing : Vec < String > ,
50
51
}
51
52
52
- pub struct RepositoryStatsUpdater ;
53
+ pub struct RepositoryStatsUpdater {
54
+ updaters : Vec < Box < dyn RepositoryForge + Send + Sync > > ,
55
+ }
56
+
57
+ impl fmt:: Debug for RepositoryStatsUpdater {
58
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
59
+ write ! (
60
+ f,
61
+ "RepositoryStatsUpdater {{ {:?} }}" ,
62
+ self . updaters. iter( ) . map( |u| u. host( ) ) . collect:: <Vec <_>>( )
63
+ )
64
+ }
65
+ }
53
66
54
67
impl RepositoryStatsUpdater {
55
- fn get_updaters ( config : & Arc < Config > ) -> Vec < Box < dyn RepositoryForge > > {
56
- let mut updaters: Vec < Box < dyn RepositoryForge > > = Vec :: with_capacity ( 3 ) ;
68
+ pub fn new ( config : & Config ) -> Self {
69
+ let mut updaters: Vec < Box < dyn RepositoryForge + Send + Sync > > = Vec :: with_capacity ( 3 ) ;
57
70
if let Ok ( Some ( updater) ) = GitHub :: new ( & config) {
58
71
updaters. push ( Box :: new ( updater) ) ;
59
72
}
@@ -63,15 +76,13 @@ impl RepositoryStatsUpdater {
63
76
if let Ok ( Some ( updater) ) = GitLab :: new ( "gitlab.freedesktop.org" , & None ) {
64
77
updaters. push ( Box :: new ( updater) ) ;
65
78
}
66
- updaters
79
+ Self { updaters }
67
80
}
68
- }
69
81
70
- impl RepositoryStatsUpdater {
71
82
pub ( crate ) fn load_repository (
83
+ & self ,
72
84
conn : & mut Client ,
73
85
metadata : & MetadataPackage ,
74
- config : Arc < Config > ,
75
86
) -> Result < Option < i32 > > {
76
87
let url = match & metadata. repository {
77
88
Some ( url) => url,
@@ -80,16 +91,10 @@ impl RepositoryStatsUpdater {
80
91
return Ok ( None ) ;
81
92
}
82
93
} ;
83
- let updaters = Self :: get_updaters ( & config) ;
84
-
85
- Self :: load_repository_inner ( conn, url, & updaters)
94
+ self . load_repository_inner ( conn, url)
86
95
}
87
96
88
- fn load_repository_inner (
89
- conn : & mut Client ,
90
- url : & str ,
91
- updaters : & [ Box < dyn RepositoryForge > ] ,
92
- ) -> Result < Option < i32 > > {
97
+ fn load_repository_inner ( & self , conn : & mut Client , url : & str ) -> Result < Option < i32 > > {
93
98
let name = match repository_name ( url) {
94
99
Some ( name) => name,
95
100
None => return Ok ( None ) ,
@@ -102,7 +107,7 @@ impl RepositoryStatsUpdater {
102
107
) ? {
103
108
return Ok ( Some ( row. get ( "id" ) ) ) ;
104
109
}
105
- for updater in updaters. iter ( ) . filter ( |u| u. host ( ) == name. host ) {
110
+ for updater in self . updaters . iter ( ) . filter ( |u| u. host ( ) == name. host ) {
106
111
let res = match updater. fetch_repository ( & name) {
107
112
Ok ( Some ( repo) ) => Self :: store_repository ( conn, updater. host ( ) , repo) ,
108
113
Err ( err) => {
@@ -124,22 +129,19 @@ impl RepositoryStatsUpdater {
124
129
}
125
130
126
131
pub fn start_crons ( config : Arc < Config > , pool : Pool ) -> Result < ( ) > {
132
+ let instance = Self :: new ( & config) ;
127
133
cron (
128
134
"repositories stats updater" ,
129
135
Duration :: from_secs ( 60 * 60 ) ,
130
- move || {
131
- Self :: update_all_crates ( & config, & pool) ?;
132
- Ok ( ( ) )
133
- } ,
136
+ move || instance. update_all_crates ( & pool) ,
134
137
) ?;
135
138
136
139
Ok ( ( ) )
137
140
}
138
141
139
- pub fn update_all_crates ( config : & Arc < Config > , pool : & Pool ) -> Result < ( ) > {
142
+ pub fn update_all_crates ( & self , pool : & Pool ) -> Result < ( ) > {
140
143
let mut conn = pool. get ( ) ?;
141
- let updaters = Self :: get_updaters ( config) ;
142
- for updater in updaters {
144
+ for updater in & self . updaters {
143
145
info ! ( "started updating `{}` repositories stats" , updater. host( ) ) ;
144
146
145
147
let needs_update = conn
@@ -174,11 +176,10 @@ impl RepositoryStatsUpdater {
174
176
Ok ( ( ) )
175
177
}
176
178
177
- pub fn backfill_repositories ( ctx : & dyn Context ) -> Result < ( ) > {
179
+ pub fn backfill_repositories ( & self , ctx : & dyn Context ) -> Result < ( ) > {
178
180
let pool = ctx. pool ( ) ?;
179
181
let mut conn = pool. get ( ) ?;
180
- let updaters = Self :: get_updaters ( & ctx. config ( ) ?) ;
181
- for updater in updaters. iter ( ) {
182
+ for updater in & self . updaters {
182
183
info ! (
183
184
"started backfilling `{}` repositories stats" ,
184
185
updater. host( )
@@ -201,9 +202,7 @@ impl RepositoryStatsUpdater {
201
202
202
203
if missing_urls. contains ( & url) {
203
204
debug ! ( "{} {} points to a known missing repo" , name, version) ;
204
- } else if let Some ( node_id) =
205
- Self :: load_repository_inner ( & mut conn, & url, & updaters) ?
206
- {
205
+ } else if let Some ( node_id) = self . load_repository_inner ( & mut conn, & url) ? {
207
206
conn. execute (
208
207
"UPDATE releases SET repository_id = $1 WHERE id = $2;" ,
209
208
& [ & node_id, & id] ,
@@ -229,6 +228,15 @@ impl RepositoryStatsUpdater {
229
228
Ok ( ( ) )
230
229
}
231
230
231
+ pub fn get_icon_name ( & self , host : & str ) -> & ' static str {
232
+ for updater in & self . updaters {
233
+ if updater. host ( ) == host {
234
+ return updater. icon ( ) ;
235
+ }
236
+ }
237
+ ""
238
+ }
239
+
232
240
fn store_repository ( conn : & mut Client , host : & str , repo : Repository ) -> Result < i32 > {
233
241
trace ! (
234
242
"storing {} repository stats for {}" ,
@@ -300,20 +308,6 @@ pub struct RepositoryName<'a> {
300
308
pub host : & ' a str ,
301
309
}
302
310
303
- pub fn get_icon_name ( host : & str ) -> & ' static str {
304
- // macro_rules! return_if_true {
305
- // ($t:ty, $icon:expr) => {
306
- // if <$t>::hosts().iter().any(|&h| h == host) {
307
- // return $icon;
308
- // }
309
- // };
310
- // }
311
-
312
- // return_if_true!(GitHub, "github");
313
- // return_if_true!(GitLab, "gitlab");
314
- ""
315
- }
316
-
317
311
#[ cfg( test) ]
318
312
mod test {
319
313
use super :: * ;
@@ -347,8 +341,6 @@ mod test {
347
341
assert_name ! ( "https://gitlab.freedesktop.org/test/test" => (
348
342
"test" , "test" , "gitlab.freedesktop.org"
349
343
) ) ;
350
- assert_name ! ( "https://www.github.com/onur/cratesfyi" => None ) ;
351
- assert_name ! ( "https://github.com/onur/cratesfyi" => None ) ;
352
344
353
345
// github checks
354
346
assert_name ! ( "https://github.com/onur/cratesfyi" => ( "onur" , "cratesfyi" , "github.com" ) ) ;
@@ -358,8 +350,5 @@ mod test {
358
350
assert_name ! ( "https://github.com/onur23cmD_M_R_L_/crates_fy-i" => (
359
351
"onur23cmD_M_R_L_" , "crates_fy-i" , "github.com"
360
352
) ) ;
361
- assert_name ! ( "https://www.github.com/onur/cratesfyi" => None ) ;
362
- assert_name ! ( "http://www.github.com/onur/cratesfyi" => None ) ;
363
- assert_name ! ( "http://www.gitlab.com/onur/cratesfyi" => None ) ;
364
353
}
365
354
}
0 commit comments