@@ -12,7 +12,7 @@ const parser = require('mongodb-query-parser');
1212const { boolean } = require ( 'boolean' ) ;
1313const _ = require ( '#helpers/lodash' ) ;
1414
15- const { Users, Emails } = require ( '#models' ) ;
15+ const { Users } = require ( '#models' ) ;
1616const config = require ( '#config' ) ;
1717
1818const REGEX_BYTES = new RE2 ( / ^ ( ( - | \+ ) ? ( \d + (?: \. \d + ) ? ) ) * ( k b | m b | g b | t b | p b ) $ / i) ;
@@ -52,77 +52,25 @@ async function list(ctx) {
5252 }
5353 }
5454
55- const now = new Date ( ) ;
56- const oneHourAgo = new Date ( now - 60 * 60 * 1000 ) ;
57- const twentyFourHoursAgo = new Date ( now - 24 * 60 * 60 * 1000 ) ;
58- const seventyTwoHoursAgo = new Date ( now - 72 * 60 * 60 * 1000 ) ;
59-
60- const [ users , itemCount , emailCounts ] = await Promise . all ( [
55+ const [ users , itemCount ] = await Promise . all ( [
6156 // eslint-disable-next-line unicorn/no-array-callback-reference
6257 Users . find ( query )
6358 . limit ( ctx . query . limit )
6459 . skip ( ctx . paginate . skip )
6560 . lean ( )
6661 . sort ( ctx . query . sort || '-created_at' )
6762 . exec ( ) ,
68- Users . countDocuments ( query ) ,
69- // Get SMTP outbound email counts per user with time-based breakdowns
70- Emails . aggregate ( [
71- {
72- $match : {
73- // Only count delivered/sent emails (not failed/bounced)
74- status : { $in : [ 'delivered' , 'deferred' , 'sent' ] }
75- }
76- } ,
77- {
78- $group : {
79- _id : '$user' ,
80- totalEmails : { $sum : 1 } ,
81- lastEmailAt : { $max : '$created_at' } ,
82- // Count emails within time periods
83- emailsLast1Hour : {
84- $sum : {
85- $cond : [ { $gte : [ '$created_at' , oneHourAgo ] } , 1 , 0 ]
86- }
87- } ,
88- emailsLast24Hours : {
89- $sum : {
90- $cond : [ { $gte : [ '$created_at' , twentyFourHoursAgo ] } , 1 , 0 ]
91- }
92- } ,
93- emailsLast72Hours : {
94- $sum : {
95- $cond : [ { $gte : [ '$created_at' , seventyTwoHoursAgo ] } , 1 , 0 ]
96- }
97- }
98- }
99- }
100- ] )
63+ Users . countDocuments ( query )
10164 ] ) ;
10265
103- // Create a map for quick lookup of email counts
104- const emailCountMap = new Map ( ) ;
105- for ( const count of emailCounts ) {
106- emailCountMap . set ( count . _id . toString ( ) , {
107- totalEmails : count . totalEmails ,
108- lastEmailAt : count . lastEmailAt ,
109- emailsLast1Hour : count . emailsLast1Hour ,
110- emailsLast24Hours : count . emailsLast24Hours ,
111- emailsLast72Hours : count . emailsLast72Hours
112- } ) ;
113- }
114-
115- // Add email counts to each user
66+ // Use the optimized user model counters instead of aggregation
11667 const usersWithEmailCounts = users . map ( ( user ) => ( {
11768 ...user ,
118- totalEmails : emailCountMap . get ( user . _id . toString ( ) ) ?. totalEmails || 0 ,
119- lastEmailAt : emailCountMap . get ( user . _id . toString ( ) ) ?. lastEmailAt || null ,
120- emailsLast1Hour :
121- emailCountMap . get ( user . _id . toString ( ) ) ?. emailsLast1Hour || 0 ,
122- emailsLast24Hours :
123- emailCountMap . get ( user . _id . toString ( ) ) ?. emailsLast24Hours || 0 ,
124- emailsLast72Hours :
125- emailCountMap . get ( user . _id . toString ( ) ) ?. emailsLast72Hours || 0
69+ totalEmails : user . smtp_emails_sent_total || 0 ,
70+ lastEmailAt : user . smtp_last_email_sent_at || null ,
71+ emailsLast1Hour : user . smtp_emails_sent_1h || 0 ,
72+ emailsLast24Hours : user . smtp_emails_sent_24h || 0 ,
73+ emailsLast72Hours : user . smtp_emails_sent_72h || 0
12674 } ) ) ;
12775
12876 const pageCount = Math . ceil ( itemCount / ctx . query . limit ) ;
0 commit comments