@@ -373,5 +373,58 @@ module.exports = {
373373
374374 return factory . findChartData ( days , timezoneOffset ) ;
375375 } ,
376+
377+ /**
378+ * Returns list of not archived releases with number of events that were introduced in this release
379+ * We count events as new, cause payload.release only contain the same release name if the event is original
380+ *
381+ * @param {ProjectDBScheme } project - result of parent resolver
382+ * @returns {Promise<Array<{release: string, timestamp: number, newEventsCount: number, commitsCount: number, filesCount: number}>> }
383+ */
384+ async releases ( project ) {
385+ const releasesCol = mongo . databases . events . collection ( 'releases' ) ;
386+
387+ const pipeline = [
388+ { $match : { projectId : project . _id . toString ( ) } } ,
389+ {
390+ $project : {
391+ release : { $convert : { input : '$release' , to : 'string' , onError : '' , onNull : '' } } ,
392+ commitsCount : { $size : { $ifNull : [ '$commits' , [ ] ] } } ,
393+ filesCount : { $size : { $ifNull : [ '$files' , [ ] ] } } ,
394+ _releaseIdSec : { $floor : { $divide : [ { $toLong : { $toDate : '$_id' } } , 1000 ] } } ,
395+ } ,
396+ } ,
397+ { $match : { release : { $ne : '' } } } ,
398+ {
399+ $lookup : {
400+ from : 'events:' + project . _id ,
401+ let : { rel : '$release' } ,
402+ pipeline : [
403+ { $match : { $expr : { $eq : [ { $convert : { input : '$payload.release' , to : 'string' , onError : '' , onNull : '' } } , '$$rel' ] } } } ,
404+ { $group : { _id : null , minTs : { $min : '$timestamp' } , count : { $sum : 1 } } } ,
405+ ] ,
406+ as : 'eventAgg' ,
407+ } ,
408+ } ,
409+ {
410+ $project : {
411+ _id : 0 ,
412+ release : 1 ,
413+ commitsCount : 1 ,
414+ filesCount : 1 ,
415+ newEventsCount : { $ifNull : [ { $arrayElemAt : [ '$eventAgg.count' , 0 ] } , 0 ] } ,
416+ timestamp : {
417+ $ifNull : [ { $arrayElemAt : [ '$eventAgg.minTs' , 0 ] } , '$_releaseIdSec' ] ,
418+ } ,
419+ } ,
420+ } ,
421+ { $sort : { timestamp : - 1 } } ,
422+ ] ;
423+
424+ const cursor = releasesCol . aggregate ( pipeline ) ;
425+ const result = await cursor . toArray ( ) ;
426+
427+ return result ;
428+ } ,
376429 } ,
377430} ;
0 commit comments