winterhazel commented on code in PR #8307:
URL: https://github.com/apache/cloudstack/pull/8307#discussion_r1509247066


##########
engine/schema/src/main/resources/META-INF/db/schema-41900to42000.sql:
##########
@@ -34,3 +34,12 @@ UPDATE `cloud`.`service_offering` SET ram_size = 512 WHERE 
unique_name IN ("Clou
                                                                            
"Cloud.Com-InternalLBVm", "Cloud.Com-InternalLBVm-Local",
                                                                            
"Cloud.Com-ElasticLBVm", "Cloud.Com-ElasticLBVm-Local")
                                                     AND system_use = 1 AND 
ram_size < 512;
+
+-- Create table to persist quota email template configurations
+CREATE TABLE IF NOT EXISTS `cloud_usage`.`quota_email_configuration`(
+    `account_id` int(11) NOT NULL,
+    `email_template_id` bigint(20) NOT NULL,
+    `enabled` int(1) UNSIGNED NOT NULL,
+    PRIMARY KEY (`account_id`, `email_template_id`),
+    CONSTRAINT `FK_quota_email_configuration_account_id` FOREIGN KEY 
(`account_id`) REFERENCES `cloud_usage`.`quota_account`(`account_id`),
+    CONSTRAINT `FK_quota_email_configuration_email_te1mplate_id` FOREIGN KEY 
(`email_template_id`) REFERENCES `cloud_usage`.`quota_email_templates`(`id`));

Review Comment:
   ```suggestion
       CONSTRAINT `FK_quota_email_configuration_email_template_id` FOREIGN KEY 
(`email_template_id`) REFERENCES `cloud_usage`.`quota_email_templates`(`id`));
   ```



##########
framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaAlertManagerImpl.java:
##########
@@ -138,55 +143,100 @@ public boolean stop() {
         return true;
     }
 
+    /**
+     * Returns whether a Quota email type is enabled or not for the provided 
account.
+     */
+    @Override
+    public boolean isQuotaEmailTypeEnabledForAccount(AccountVO account, 
QuotaEmailTemplateTypes quotaEmailTemplateType) {
+        boolean quotaEmailsEnabled = 
QuotaConfig.QuotaEnableEmails.valueIn(account.getAccountId());
+        if (!quotaEmailsEnabled) {
+            logger.debug("Configuration [{}] is disabled for account [{}]. 
Therefore, the account will not receive Quota email of type [{}].", 
QuotaConfig.QuotaEnableEmails.key(), account, quotaEmailTemplateType);
+            return false;
+        }
+
+        QuotaEmailConfigurationVO quotaEmail = 
quotaEmailConfigurationDao.findByAccountIdAndEmailTemplateType(account.getAccountId(),
 quotaEmailTemplateType);
+
+        boolean emailEnabled = quotaEmail == null || quotaEmail.isEnabled();
+        if (emailEnabled) {
+            logger.debug("Quota email [{}] is enabled for account [{}].", 
quotaEmailTemplateType, account);
+        } else {
+            logger.debug("Quota email [{}] has been manually disabled for 
account [{}] through the API quotaConfigureEmail.", quotaEmailTemplateType, 
account);
+        }
+        return emailEnabled;
+    }
+
+
     @Override
     public void checkAndSendQuotaAlertEmails() {
         List<DeferredQuotaEmail> deferredQuotaEmailList = new 
ArrayList<DeferredQuotaEmail>();
-        final BigDecimal zeroBalance = new BigDecimal(0);
+
+        logger.info("Checking and sending quota alert emails.");
         for (final QuotaAccountVO quotaAccount : 
_quotaAcc.listAllQuotaAccount()) {
-            if (logger.isDebugEnabled()) {
-                logger.debug("checkAndSendQuotaAlertEmails accId=" + 
quotaAccount.getId());
-            }
-            BigDecimal accountBalance = quotaAccount.getQuotaBalance();
-            Date balanceDate = quotaAccount.getQuotaBalanceDate();
-            Date alertDate = quotaAccount.getQuotaAlertDate();
-            int lockable = quotaAccount.getQuotaEnforce();
-            BigDecimal thresholdBalance = quotaAccount.getQuotaMinBalance();
-            if (accountBalance != null) {
-                AccountVO account = _accountDao.findById(quotaAccount.getId());
-                if (account == null) {
-                    continue; // the account is removed
-                }
-                if (logger.isDebugEnabled()) {
-                    logger.debug("checkAndSendQuotaAlertEmails: Check id=" + 
account.getId() + " bal=" + accountBalance + ", alertDate=" + alertDate + ", 
lockable=" + lockable);
-                }
-                if (accountBalance.compareTo(zeroBalance) < 0) {
-                    if (_lockAccountEnforcement && (lockable == 1)) {
-                        if (_quotaManager.isLockable(account)) {
-                            logger.info("Locking account " + 
account.getAccountName() + " due to quota < 0.");
-                            lockAccount(account.getId());
-                        }
-                    }
-                    if (alertDate == null || (balanceDate.after(alertDate) && 
getDifferenceDays(alertDate, new Date()) > 1)) {
-                        logger.info("Sending alert " + 
account.getAccountName() + " due to quota < 0.");
-                        deferredQuotaEmailList.add(new 
DeferredQuotaEmail(account, quotaAccount, 
QuotaConfig.QuotaEmailTemplateTypes.QUOTA_EMPTY));
-                    }
-                } else if (accountBalance.compareTo(thresholdBalance) < 0) {
-                    if (alertDate == null || (balanceDate.after(alertDate) && 
getDifferenceDays(alertDate, new Date()) > 1)) {
-                        logger.info("Sending alert " + 
account.getAccountName() + " due to quota below threshold.");
-                        deferredQuotaEmailList.add(new 
DeferredQuotaEmail(account, quotaAccount, 
QuotaConfig.QuotaEmailTemplateTypes.QUOTA_LOW));
-                    }
-                }
-            }
+            checkQuotaAlertEmailForAccount(deferredQuotaEmailList, 
quotaAccount);
         }
 
         for (DeferredQuotaEmail emailToBeSent : deferredQuotaEmailList) {
-            if (logger.isDebugEnabled()) {
-                logger.debug("checkAndSendQuotaAlertEmails: Attempting to send 
quota alert email to users of account: " + 
emailToBeSent.getAccount().getAccountName());
-            }
+            logger.debug("Attempting to send a quota alert email to users of 
account [{}].", emailToBeSent.getAccount().getAccountName());
             sendQuotaAlert(emailToBeSent);
         }
     }
 
+    /**
+     * Checks a given quota account to see if they should receive any emails. 
First by checking if it has any balance at all, if its account can be found, 
then checks
+     * if they should receive either QUOTA_EMPTY or QUOTA_LOW emails, taking 
into account if these email templates are disabled or not for that account.
+     * */
+    protected void checkQuotaAlertEmailForAccount(List<DeferredQuotaEmail> 
deferredQuotaEmailList, QuotaAccountVO quotaAccount) {
+        logger.debug("Checking {} for email alerts.", quotaAccount);
+        BigDecimal accountBalance = quotaAccount.getQuotaBalance();
+
+        if (accountBalance == null) {
+            logger.debug("{} has a null balance, therefore it will not receive 
quota alert emails.", quotaAccount);
+            return;
+        }
+
+        AccountVO account = _accountDao.findById(quotaAccount.getId());
+        if (account == null) {
+            logger.debug("Account of {} is removed, thus it will not receive 
quota alert emails.", quotaAccount);
+            return;
+        }
+
+        checkBalanceAndAddToEmailList(deferredQuotaEmailList, quotaAccount, 
account, accountBalance);
+    }
+
+    private void checkBalanceAndAddToEmailList(List<DeferredQuotaEmail> 
deferredQuotaEmailList, QuotaAccountVO quotaAccount, AccountVO account, 
BigDecimal accountBalance) {
+        Date balanceDate = quotaAccount.getQuotaBalanceDate();
+        Date alertDate = quotaAccount.getQuotaAlertDate();
+        int lockable = quotaAccount.getQuotaEnforce();
+        BigDecimal thresholdBalance = quotaAccount.getQuotaMinBalance();
+
+        logger.debug("Checking {} with accountBalance [{}], alertDate [{}] and 
lockable [{}] to see if a quota alert email should be sent.", account,
+                accountBalance, alertDate, lockable);
+
+        boolean shouldSendEmail = alertDate == null || 
(balanceDate.after(alertDate) && getDifferenceDays(alertDate, new Date()) > 1);
+
+        if (accountBalance.compareTo(BigDecimal.ZERO) < 0) {
+            if (_lockAccountEnforcement && lockable == 1 && 
_quotaManager.isLockable(account)) {
+                logger.info("Locking {}, as quota balance is lower than 0.", 
account);
+                lockAccount(account.getId());
+            }
+
+            boolean quotaEmptyEmailEnabled = 
isQuotaEmailTypeEnabledForAccount(account, QuotaEmailTemplateTypes.QUOTA_EMPTY);
+            if (quotaEmptyEmailEnabled && shouldSendEmail) {
+                logger.debug("Adding {} to the deferred emails list, as quota 
balance is lower than 0.", account);
+                deferredQuotaEmailList.add(new DeferredQuotaEmail(account, 
quotaAccount, QuotaEmailTemplateTypes.QUOTA_EMPTY));
+                return;
+            }
+        } else if (accountBalance.compareTo(thresholdBalance) < 0) {
+            boolean quotaLowEmailEnabled = 
isQuotaEmailTypeEnabledForAccount(account, QuotaEmailTemplateTypes.QUOTA_LOW);
+            if (quotaLowEmailEnabled && shouldSendEmail) {
+                logger.debug("Adding {} to the deferred emails list, as quota 
balance [{}] is below the threshold [{}].", account, accountBalance, 
thresholdBalance);
+                deferredQuotaEmailList.add(new DeferredQuotaEmail(account, 
quotaAccount, QuotaEmailTemplateTypes.QUOTA_LOW));
+                return;
+            }
+        }
+            logger.debug("{} will not receive any quota alert emails in this 
round.", account);

Review Comment:
   ```suggestion
           logger.debug("{} will not receive any quota alert emails in this 
round.", account);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to