@@ -397,5 +397,58 @@ module.exports = {
397397
398398 return factory . findChartData ( days , timezoneOffset ) ;
399399 } ,
400+
401+ /**
402+ * Returns list of not archived releases with number of events that were introduced in this release
403+ * We count events as new, cause payload.release only contain the same release name if the event is original
404+ *
405+ * @param {ProjectDBScheme } project - result of parent resolver
406+ * @returns {Promise<Array<{release: string, timestamp: number, newEventsCount: number, commitsCount: number, filesCount: number}>> }
407+ */
408+ async releases ( project ) {
409+ const releasesCol = mongo . databases . events . collection ( 'releases' ) ;
410+
411+ const pipeline = [
412+ { $match : { projectId : project . _id . toString ( ) } } ,
413+ {
414+ $project : {
415+ release : { $convert : { input : '$release' , to : 'string' , onError : '' , onNull : '' } } ,
416+ commitsCount : { $size : { $ifNull : [ '$commits' , [ ] ] } } ,
417+ filesCount : { $size : { $ifNull : [ '$files' , [ ] ] } } ,
418+ _releaseIdSec : { $floor : { $divide : [ { $toLong : { $toDate : '$_id' } } , 1000 ] } } ,
419+ } ,
420+ } ,
421+ { $match : { release : { $ne : '' } } } ,
422+ {
423+ $lookup : {
424+ from : 'events:' + project . _id ,
425+ let : { rel : '$release' } ,
426+ pipeline : [
427+ { $match : { $expr : { $eq : [ { $convert : { input : '$payload.release' , to : 'string' , onError : '' , onNull : '' } } , '$$rel' ] } } } ,
428+ { $group : { _id : null , minTs : { $min : '$timestamp' } , count : { $sum : 1 } } } ,
429+ ] ,
430+ as : 'eventAgg' ,
431+ } ,
432+ } ,
433+ {
434+ $project : {
435+ _id : 0 ,
436+ release : 1 ,
437+ commitsCount : 1 ,
438+ filesCount : 1 ,
439+ newEventsCount : { $ifNull : [ { $arrayElemAt : [ '$eventAgg.count' , 0 ] } , 0 ] } ,
440+ timestamp : {
441+ $ifNull : [ { $arrayElemAt : [ '$eventAgg.minTs' , 0 ] } , '$_releaseIdSec' ] ,
442+ } ,
443+ } ,
444+ } ,
445+ { $sort : { timestamp : - 1 } } ,
446+ ] ;
447+
448+ const cursor = releasesCol . aggregate ( pipeline ) ;
449+ const result = await cursor . toArray ( ) ;
450+
451+ return result ;
452+ } ,
400453 } ,
401454} ;
0 commit comments