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


##########
plugins/database/quota/src/main/java/org/apache/cloudstack/api/command/QuotaConfigureEmailCmd.java:
##########
@@ -0,0 +1,79 @@
+//Licensed to the Apache Software Foundation (ASF) under one
+//or more contributor license agreements.  See the NOTICE file
+//distributed with this work for additional information
+//regarding copyright ownership.  The ASF licenses this file
+//to you under the Apache License, Version 2.0 (the
+//"License"); you may not use this file except in compliance
+//with the License.  You may obtain a copy of the License at
+//
+//http://www.apache.org/licenses/LICENSE-2.0
+//
+//Unless required by applicable law or agreed to in writing,
+//software distributed under the License is distributed on an
+//"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+//KIND, either express or implied.  See the License for the
+//specific language governing permissions and limitations
+//under the License.
+package org.apache.cloudstack.api.command;
+
+import com.cloud.utils.Pair;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.AccountResponse;
+import org.apache.cloudstack.api.response.QuotaConfigureEmailResponse;
+import org.apache.cloudstack.api.response.QuotaResponseBuilder;
+import org.apache.cloudstack.quota.vo.QuotaEmailConfigurationVO;
+
+import javax.inject.Inject;
+
+@APICommand(name = "quotaConfigureEmail", responseObject = 
QuotaConfigureEmailResponse.class, description = "Configure a quota email 
template", since = "4.20.0.0",

Review Comment:
   Should this be changed to `20.0.0`?
   ```suggestion
   @APICommand(name = "quotaConfigureEmail", responseObject = 
QuotaConfigureEmailResponse.class, description = "Configure a quota email 
template", since = "20.0.0",
   ```



##########
plugins/database/quota/src/main/java/org/apache/cloudstack/api/command/QuotaListEmailConfigurationCmd.java:
##########
@@ -0,0 +1,54 @@
+//Licensed to the Apache Software Foundation (ASF) under one
+//or more contributor license agreements.  See the NOTICE file
+//distributed with this work for additional information
+//regarding copyright ownership.  The ASF licenses this file
+//to you under the Apache License, Version 2.0 (the
+//"License"); you may not use this file except in compliance
+//with the License.  You may obtain a copy of the License at
+//
+//http://www.apache.org/licenses/LICENSE-2.0
+//
+//Unless required by applicable law or agreed to in writing,
+//software distributed under the License is distributed on an
+//"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+//KIND, either express or implied.  See the License for the
+//specific language governing permissions and limitations
+//under the License.
+package org.apache.cloudstack.api.command;
+
+import com.cloud.user.Account;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.AccountResponse;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.QuotaConfigureEmailResponse;
+import org.apache.cloudstack.api.response.QuotaResponseBuilder;
+
+import javax.inject.Inject;
+
+@APICommand(name = "quotaListEmailConfiguration", responseObject = 
QuotaConfigureEmailResponse.class, description = "List quota email template 
configurations", since = "4.20.0.0",

Review Comment:
   Should this be changed to 20.0.0?
   ```suggestion
   @APICommand(name = "quotaListEmailConfiguration", responseObject = 
QuotaConfigureEmailResponse.class, description = "List quota email template 
configurations", since = "20.0.0",
   ```



##########
framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaAlertManagerImpl.java:
##########
@@ -138,55 +143,102 @@ 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);
+
+

Review Comment:
   ```suggestion
   ```



##########
framework/quota/src/main/java/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDaoImpl.java:
##########
@@ -68,4 +68,14 @@ public Boolean doInTransaction(final TransactionStatus 
status) {
             }
         });
     }
+
+    @Override
+    public QuotaEmailTemplatesVO findById(long id) {
+        return Transaction.execute(TransactionLegacy.USAGE_DB, new 
TransactionCallback<QuotaEmailTemplatesVO>() {
+            @Override
+            public QuotaEmailTemplatesVO doInTransaction(final 
TransactionStatus status) {
+                return QuotaEmailTemplatesDaoImpl.super.findById(id);
+            }
+        });

Review Comment:
   Why was this marked as resolved? It is not using lambda.



-- 
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