Search in sources :

Example 6 with QuotaAccountVO

use of org.apache.cloudstack.quota.vo.QuotaAccountVO in project cloudstack by apache.

the class QuotaAlertManagerImpl method checkAndSendQuotaAlertEmails.

@Override
public void checkAndSendQuotaAlertEmails() {
    List<DeferredQuotaEmail> deferredQuotaEmailList = new ArrayList<DeferredQuotaEmail>();
    final BigDecimal zeroBalance = new BigDecimal(0);
    for (final QuotaAccountVO quotaAccount : _quotaAcc.listAllQuotaAccount()) {
        if (s_logger.isDebugEnabled()) {
            s_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());
            // the account is removed
            if (account == null)
                continue;
            if (s_logger.isDebugEnabled()) {
                s_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)) {
                        s_logger.info("Locking account " + account.getAccountName() + " due to quota < 0.");
                        lockAccount(account.getId());
                    }
                }
                if (alertDate == null || (balanceDate.after(alertDate) && getDifferenceDays(alertDate, new Date()) > 1)) {
                    s_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)) {
                    s_logger.info("Sending alert " + account.getAccountName() + " due to quota below threshold.");
                    deferredQuotaEmailList.add(new DeferredQuotaEmail(account, quotaAccount, QuotaConfig.QuotaEmailTemplateTypes.QUOTA_LOW));
                }
            }
        }
    }
    for (DeferredQuotaEmail emailToBeSent : deferredQuotaEmailList) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("checkAndSendQuotaAlertEmails: Attempting to send quota alert email to users of account: " + emailToBeSent.getAccount().getAccountName());
        }
        sendQuotaAlert(emailToBeSent);
    }
}
Also used : ArrayList(java.util.ArrayList) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) BigDecimal(java.math.BigDecimal) Date(java.util.Date)

Example 7 with QuotaAccountVO

use of org.apache.cloudstack.quota.vo.QuotaAccountVO in project cloudstack by apache.

the class QuotaManagerImpl method saveQuotaAccount.

private boolean saveQuotaAccount(final AccountVO account, final BigDecimal aggrUsage, final Date endDate) {
    // update quota_accounts
    QuotaAccountVO quota_account = _quotaAcc.findByIdQuotaAccount(account.getAccountId());
    if (quota_account == null) {
        quota_account = new QuotaAccountVO(account.getAccountId());
        quota_account.setQuotaBalance(aggrUsage);
        quota_account.setQuotaBalanceDate(endDate);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug(quota_account);
        }
        _quotaAcc.persistQuotaAccount(quota_account);
        return true;
    } else {
        quota_account.setQuotaBalance(aggrUsage);
        quota_account.setQuotaBalanceDate(endDate);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug(quota_account);
        }
        return _quotaAcc.updateQuotaAccount(account.getAccountId(), quota_account);
    }
}
Also used : QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO)

Example 8 with QuotaAccountVO

use of org.apache.cloudstack.quota.vo.QuotaAccountVO in project cloudstack by apache.

the class QuotaStatementImpl method sendStatement.

@Override
public void sendStatement() {
    List<DeferredQuotaEmail> deferredQuotaEmailList = new ArrayList<DeferredQuotaEmail>();
    for (final QuotaAccountVO quotaAccount : _quotaAcc.listAllQuotaAccount()) {
        if (quotaAccount.getQuotaBalance() == null) {
            // no quota usage for this account ever, ignore
            continue;
        }
        //check if it is statement time
        Calendar[] interval = statementTime(Calendar.getInstance(), _period);
        Date lastStatementDate = quotaAccount.getLastStatementDate();
        if (interval != null) {
            AccountVO account = _accountDao.findById(quotaAccount.getId());
            if (account != null) {
                if (lastStatementDate == null || getDifferenceDays(lastStatementDate, new Date()) >= s_LAST_STATEMENT_SENT_DAYS + 1) {
                    BigDecimal quotaUsage = _quotaUsage.findTotalQuotaUsage(account.getAccountId(), account.getDomainId(), null, interval[0].getTime(), interval[1].getTime());
                    s_logger.info("For account=" + quotaAccount.getId() + ", quota used = " + quotaUsage);
                    // send statement
                    deferredQuotaEmailList.add(new DeferredQuotaEmail(account, quotaAccount, quotaUsage, QuotaConfig.QuotaEmailTemplateTypes.QUOTA_STATEMENT));
                } else {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("For " + quotaAccount.getId() + " the statement has been sent recently");
                    }
                }
            }
        } else if (lastStatementDate != null) {
            s_logger.info("For " + quotaAccount.getId() + " it is already more than " + getDifferenceDays(lastStatementDate, new Date()) + " days, will send statement in next cycle");
        }
    }
    for (DeferredQuotaEmail emailToBeSent : deferredQuotaEmailList) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Attempting to send quota STATEMENT email to users of account: " + emailToBeSent.getAccount().getAccountName());
        }
        _quotaAlert.sendQuotaAlert(emailToBeSent);
    }
}
Also used : Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) DeferredQuotaEmail(org.apache.cloudstack.quota.QuotaAlertManagerImpl.DeferredQuotaEmail) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Example 9 with QuotaAccountVO

use of org.apache.cloudstack.quota.vo.QuotaAccountVO in project cloudstack by apache.

the class QuotaServiceImplTest method testSetMinBalance.

@Test
public void testSetMinBalance() {
    final long accountId = 2L;
    final double balance = 10.3F;
    // existing account setting
    QuotaAccountVO quotaAccountVO = new QuotaAccountVO();
    Mockito.when(quotaAcc.findByIdQuotaAccount(Mockito.anyLong())).thenReturn(quotaAccountVO);
    quotaService.setMinBalance(accountId, balance);
    Mockito.verify(quotaAcc, Mockito.times(0)).persistQuotaAccount(Mockito.any(QuotaAccountVO.class));
    Mockito.verify(quotaAcc, Mockito.times(1)).updateQuotaAccount(Mockito.anyLong(), Mockito.any(QuotaAccountVO.class));
    // no account with limit set
    Mockito.when(quotaAcc.findByIdQuotaAccount(Mockito.anyLong())).thenReturn(null);
    quotaService.setMinBalance(accountId, balance);
    Mockito.verify(quotaAcc, Mockito.times(1)).persistQuotaAccount(Mockito.any(QuotaAccountVO.class));
}
Also used : QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) Test(org.junit.Test)

Example 10 with QuotaAccountVO

use of org.apache.cloudstack.quota.vo.QuotaAccountVO in project cloudstack by apache.

the class QuotaServiceImpl method setMinBalance.

@Override
public void setMinBalance(Long accountId, Double balance) {
    QuotaAccountVO acc = _quotaAcc.findByIdQuotaAccount(accountId);
    if (acc == null) {
        acc = new QuotaAccountVO(accountId);
        acc.setQuotaMinBalance(new BigDecimal(balance));
        _quotaAcc.persistQuotaAccount(acc);
    } else {
        acc.setQuotaMinBalance(new BigDecimal(balance));
        _quotaAcc.updateQuotaAccount(accountId, acc);
    }
}
Also used : QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) BigDecimal(java.math.BigDecimal)

Aggregations

QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)12 AccountVO (com.cloud.user.AccountVO)6 BigDecimal (java.math.BigDecimal)6 ArrayList (java.util.ArrayList)6 Date (java.util.Date)5 Test (org.junit.Test)5 Calendar (java.util.Calendar)2 DomainVO (com.cloud.domain.DomainVO)1 UserVO (com.cloud.user.UserVO)1 DeferredQuotaEmail (org.apache.cloudstack.quota.QuotaAlertManagerImpl.DeferredQuotaEmail)1 QuotaEmailTemplatesVO (org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO)1