-
Couldn't load subscription status.
- Fork 176
feat: add smtp counts to user admin page for 1 hour, 24 hours and 72 … #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shaunwarman
wants to merge
3
commits into
master
Choose a base branch
from
feat/admin-show-smtp-list
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /** | ||
| * Copyright (c) Forward Email LLC | ||
| * SPDX-License-Identifier: BUSL-1.1 | ||
| */ | ||
|
|
||
| // eslint-disable-next-line import/no-unassigned-import | ||
| require('#config/env'); | ||
|
|
||
| const process = require('node:process'); | ||
| const { parentPort } = require('node:worker_threads'); | ||
|
|
||
| // eslint-disable-next-line import/no-unassigned-import | ||
| require('#config/mongoose'); | ||
|
|
||
| const Graceful = require('@ladjs/graceful'); | ||
| const mongoose = require('mongoose'); | ||
|
|
||
| const Emails = require('#models/emails'); | ||
| const Users = require('#models/users'); | ||
| const logger = require('#helpers/logger'); | ||
| const monitorServer = require('#helpers/monitor-server'); | ||
| const setupMongoose = require('#helpers/setup-mongoose'); | ||
|
|
||
| monitorServer(); | ||
|
|
||
| const graceful = new Graceful({ | ||
| mongooses: [mongoose], | ||
| logger | ||
| }); | ||
|
|
||
| graceful.listen(); | ||
|
|
||
| (async () => { | ||
| await setupMongoose(logger); | ||
|
|
||
| try { | ||
| const now = new Date(); | ||
| const oneHourAgo = new Date(now - 60 * 60 * 1000); | ||
| const twentyFourHoursAgo = new Date(now - 24 * 60 * 60 * 1000); | ||
| const seventyTwoHoursAgo = new Date(now - 72 * 60 * 60 * 1000); | ||
|
|
||
| logger.info('Starting SMTP counter update job'); | ||
|
|
||
| // Get SMTP counts per user with time-based breakdowns | ||
| const emailCounts = await Emails.aggregate([ | ||
| { | ||
| $match: { | ||
| // Only count delivered/sent emails (not failed/bounced) | ||
| status: { $in: ['delivered', 'deferred', 'sent'] } | ||
| } | ||
| }, | ||
| { | ||
| $group: { | ||
| _id: '$user', | ||
| totalEmails: { $sum: 1 }, | ||
| lastEmailAt: { $max: '$created_at' }, | ||
| // Count emails within time periods | ||
| emailsLast1Hour: { | ||
| $sum: { | ||
| $cond: [{ $gte: ['$created_at', oneHourAgo] }, 1, 0] | ||
| } | ||
| }, | ||
| emailsLast24Hours: { | ||
| $sum: { | ||
| $cond: [{ $gte: ['$created_at', twentyFourHoursAgo] }, 1, 0] | ||
| } | ||
| }, | ||
| emailsLast72Hours: { | ||
| $sum: { | ||
| $cond: [{ $gte: ['$created_at', seventyTwoHoursAgo] }, 1, 0] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ]); | ||
|
|
||
| logger.info(`Found email counts for ${emailCounts.length} users`); | ||
|
|
||
| // Create bulk operations to update all users | ||
| const bulkOperations = []; | ||
|
|
||
| // First, reset all counters to 0 for all users | ||
| bulkOperations.push({ | ||
| updateMany: { | ||
| filter: {}, | ||
| update: { | ||
| $set: { | ||
| smtp_emails_sent_1h: 0, | ||
| smtp_emails_sent_24h: 0, | ||
| smtp_emails_sent_72h: 0, | ||
| smtp_emails_sent_total: 0, | ||
| smtp_last_email_sent_at: null | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Then update users who have email counts | ||
| for (const count of emailCounts) { | ||
| bulkOperations.push({ | ||
| updateOne: { | ||
| filter: { _id: count._id }, | ||
| update: { | ||
| $set: { | ||
| smtp_emails_sent_1h: count.emailsLast1Hour, | ||
| smtp_emails_sent_24h: count.emailsLast24Hours, | ||
| smtp_emails_sent_72h: count.emailsLast72Hours, | ||
| smtp_emails_sent_total: count.totalEmails, | ||
| smtp_last_email_sent_at: count.lastEmailAt | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| if (bulkOperations.length > 0) { | ||
| await Users.bulkWrite(bulkOperations, { ordered: false }); | ||
| logger.info(`Updated SMTP counters for all users`); | ||
| } | ||
|
|
||
| logger.info('SMTP counter update job completed successfully'); | ||
| } catch (err) { | ||
| await logger.error(err); | ||
| } | ||
|
|
||
| if (parentPort) parentPort.postMessage('done'); | ||
| else process.exit(0); | ||
| })(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be a cursor instead of aggregate. You can refer to other jobs as to how cursor is used for iterating in a loop. Basically copy-pasta it over. Otherwise this will timeout and error since aggregate will take too long (there are millions of emails and only growing).
You also might want to keep a track of emails rejected and soft bounced or something, not sure. It would probably be very clear who the abusers are if we saw at a glance how many got rejected or bounced. Or the number with
err.bounce_categoryof"spam"or"virus". Just sharing thoughts.