function getVolunteerStatsPerSegment( volunteersWithExtras, segmentSlug, timeScale ) { const volunteerGroups = _.groupBy(volunteersWithExtras, ({ createdAt }) => getScaledTimeByTimeScale(timeScale, moment.utc(createdAt)) ) return _.mapValues(volunteerGroups, volunteers => { return { count: volunteers.length, onboardedCount: _.filter( volunteers, ({ availableHours, certificationCount }) => availableHours > 0 && certificationCount > 0 ).length, certificationSum: _.sumBy(volunteers, 'certificationCount'), availableHoursAvg: parseInt(_.meanBy(volunteers, 'availableHours')) } }) }