@@ -18,6 +18,7 @@ const MAX_DB_READ_BATCH_SIZE = Number(process.env.MAX_DB_READ_BATCH_SIZE);
1818const ChartType = {
1919 Accepted : 'accepted' ,
2020 RateLimited : 'rate-limited' ,
21+ AffectedUsers : 'affected-users' ,
2122} ;
2223
2324/**
@@ -523,15 +524,35 @@ class EventsFactory extends Factory {
523524 ] ;
524525 }
525526
527+ /**
528+ * Get event daily chart data (affected users) from MongoDB only
529+ *
530+ * @param {string } groupHash - event's group hash
531+ * @param {number } days - how many days to fetch
532+ * @param {number } timezoneOffset - user's local timezone offset in minutes
533+ * @returns {Promise<Array> }
534+ */
535+ async getEventDailyAffectedUsersChart ( groupHash , days , timezoneOffset = 0 ) {
536+ const data = await this . findChartData ( days , timezoneOffset , groupHash , 'affectedUsers' ) ;
537+
538+ return [
539+ {
540+ label : ChartType . AffectedUsers ,
541+ data,
542+ } ,
543+ ] ;
544+ }
545+
526546 /**
527547 * Fetch timestamps and total count of errors (or target error) for each day since
528548 *
529549 * @param {number } days - how many days we need to fetch for displaying in a chart
530550 * @param {number } timezoneOffset - user's local timezone offset in minutes
531551 * @param {string } groupHash - event's group hash for showing only target event
552+ * @param {'count'|'affectedUsers' } valueField - field to aggregate
532553 * @return {ProjectChartItem[] }
533554 */
534- async findChartData ( days , timezoneOffset = 0 , groupHash = '' ) {
555+ async findChartData ( days , timezoneOffset = 0 , groupHash = '' , valueField = 'count' ) {
535556 const today = new Date ( ) ;
536557 const since = today . setDate ( today . getDate ( ) - days ) / 1000 ;
537558
@@ -554,13 +575,15 @@ class EventsFactory extends Factory {
554575 } ;
555576 }
556577
578+ const projection = {
579+ lastRepetitionTime : 1 ,
580+ groupingTimestamp : 1 ,
581+ [ valueField ] : 1 ,
582+ } ;
583+
557584 const dailyEventsCursor = await this . getCollection ( this . TYPES . DAILY_EVENTS )
558585 . find ( options , {
559- projection : {
560- lastRepetitionTime : 1 ,
561- groupingTimestamp : 1 ,
562- count : 1 ,
563- } ,
586+ projection,
564587 } )
565588 . batchSize ( MAX_DB_READ_BATCH_SIZE ) ;
566589
@@ -576,11 +599,13 @@ class EventsFactory extends Factory {
576599 const key = `groupingTimestamp:${ groupingTimestamp } ` ;
577600 const current = groupedCounts [ key ] || 0 ;
578601
579- if ( item . count === undefined || item . count === null ) {
580- console . warn ( `Missing 'count' field for daily event with key ${ key } . Defaulting to 0.` ) ;
602+ const value = item [ valueField ] ;
603+
604+ if ( value === undefined || value === null ) {
605+ console . warn ( `Missing '${ valueField } ' field for daily event with key ${ key } . Defaulting to 0.` ) ;
581606 groupedCounts [ key ] = current ;
582607 } else {
583- groupedCounts [ key ] = current + item . count ;
608+ groupedCounts [ key ] = current + value ;
584609 }
585610 }
586611
0 commit comments