@@ -227,6 +227,45 @@ function mongo_universe_binaries(user, type){
227
227
return cursor . toArray ( ) ;
228
228
}
229
229
230
+ /* NB Contributions are grouped by upstream url instead of package namme to avoid duplicate counting
231
+ * of contributions in repos with many packages, e.g. https://github.com/r-forge/ctm/tree/master/pkg */
232
+ function mongo_universe_contributors ( user , limit = 20 ) {
233
+ var query = { _universes : user , _type : 'src' , '_registered' : true } ;
234
+ var cursor = mongo_aggregate ( [
235
+ { $match : query } ,
236
+ { $project : {
237
+ _id : 0 ,
238
+ contributors : '$_contributors' ,
239
+ upstream : '$_upstream'
240
+ } } ,
241
+ { $unwind : "$contributors" } ,
242
+ { $group : { _id : "$contributors.user" , repos : { $addToSet : { upstream : '$upstream' , count : '$contributors.count' } } } } ,
243
+ { $project : { _id :0 , login : '$_id' , total : { $sum : '$repos.count' } , repos : 1 } } ,
244
+ { $sort :{ total : - 1 } } ,
245
+ { $limit : limit }
246
+ ] ) ;
247
+ return cursor . toArray ( ) ;
248
+ }
249
+
250
+ function mongo_universe_contributions ( user , limit = 20 ) {
251
+ var query = { _type : 'src' , '_contributors.user' : user , '_indexed' : true , '_maintainer.login' : { $ne : user } } ;
252
+ var cursor = mongo_aggregate ( [
253
+ { $match : query } ,
254
+ { $addFields : { contrib : { $arrayElemAt :[ '$_contributors' , { $indexOfArray : [ "$_contributors.user" , user ] } ] } } } ,
255
+ { $group : {
256
+ _id : "$_upstream" ,
257
+ owner : { $first : '$_user' } , //equals upstream org
258
+ packages : { $addToSet : '$Package' } ,
259
+ maintainers : { $addToSet : '$_maintainer.login' } , //upstreams can have multiple pkgs and maintainers
260
+ contributions : { $max : '$contrib.count' }
261
+ } } ,
262
+ { $project : { _id :0 , contributions :'$contributions' , upstream : '$_id' , owner : '$owner' , packages : '$packages' , maintainers : '$maintainers' } } ,
263
+ { $sort :{ contributions : - 1 } } ,
264
+ { $limit : limit }
265
+ ] ) ;
266
+ return cursor . toArray ( ) ;
267
+ }
268
+
230
269
function mongo_all_universes ( organizations_only ) {
231
270
var query = { _type : 'src' , _registered : true } ;
232
271
if ( organizations_only ) {
@@ -479,6 +518,24 @@ export function get_universe_binaries(universe, type){
479
518
}
480
519
}
481
520
521
+ export function get_universe_contributors ( universe , limit ) {
522
+ if ( production ) {
523
+ return mongo_universe_contributors ( universe , limit ) ;
524
+ } else {
525
+ console . warn ( `Fetching contributors data from API...` ) ;
526
+ return get_ndjson ( `https://${ universe } .r-universe.dev/stats/contributors?all=1&limit=${ limit } ` ) ;
527
+ }
528
+ }
529
+
530
+ export function get_universe_contributions ( universe , limit ) {
531
+ if ( production ) {
532
+ return mongo_universe_contributions ( universe , limit ) ;
533
+ } else {
534
+ console . warn ( `Fetching contributions data from API...` ) ;
535
+ return get_ndjson ( `https://${ universe } .r-universe.dev/stats/contributions?limit=${ limit } ` ) ;
536
+ }
537
+ }
538
+
482
539
export function get_repositories ( ) {
483
540
if ( production ) {
484
541
return mongo_all_universes ( )
@@ -609,6 +666,8 @@ export function get_all_universes(){
609
666
if ( production ) {
610
667
return universes ;
611
668
} else {
612
- throw "Not implemented for devel" ;
669
+ return get_json ( 'https://r-universe.dev/api/universes' ) . then ( function ( data ) {
670
+ return data . map ( x => x . universe ) ;
671
+ } ) ;
613
672
}
614
673
}
0 commit comments