quota: added checks for boundary cases

This commit is contained in:
Abhinandan Prateek 2015-07-21 12:40:03 +05:30
parent ccde3cfca9
commit c99346c30e
14 changed files with 81 additions and 32 deletions

View File

@ -60,7 +60,7 @@ public class QuotaBalanceCmd extends BaseCmd {
private Long accountId;
@Inject
QuotaService _quotaManager;
QuotaService _quotaService;
@Inject
QuotaDBUtils _quotaDBUtils;
@ -124,14 +124,16 @@ public class QuotaBalanceCmd extends BaseCmd {
@Override
public void execute() {
List<QuotaBalanceVO> quotaUsage = _quotaManager.getQuotaBalance(this);
List<QuotaBalanceVO> quotaUsage = _quotaService.getQuotaBalance(this);
QuotaBalanceResponse response;
if (getEndDate() == null) {
response = _quotaDBUtils.createQuotaLastBalanceResponse(quotaUsage);
} else {
response = _quotaDBUtils.createQuotaBalanceResponse(quotaUsage);
response.setEndDate(endDate);
}
response.setStartDate(startDate);
response.setResponseName(getCommandName());
setResponseObject(response);

View File

@ -27,7 +27,7 @@ import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.QuotaCreditsResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.quota.QuotaDBUtilsImpl;
import org.apache.cloudstack.quota.QuotaService;
import org.apache.log4j.Logger;
import javax.inject.Inject;
@ -36,7 +36,7 @@ import javax.inject.Inject;
public class QuotaCreditsCmd extends BaseCmd {
@Inject
QuotaDBUtilsImpl _quotaDBUtils;
QuotaService _quotaService;
public static final Logger s_logger = Logger.getLogger(QuotaStatementCmd.class.getName());
@ -79,9 +79,9 @@ public class QuotaCreditsCmd extends BaseCmd {
super();
}
public QuotaCreditsCmd(final QuotaDBUtilsImpl quotaDBUtils) {
public QuotaCreditsCmd(final QuotaService quotaService) {
super();
_quotaDBUtils = quotaDBUtils;
_quotaService = quotaService;
}
@Override
@ -99,7 +99,7 @@ public class QuotaCreditsCmd extends BaseCmd {
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Please send a valid non-empty quota value");
}
final QuotaCreditsResponse response = _quotaDBUtils.addQuotaCredits(accountId, domainId, value, CallContext.current().getCallingUserId());
final QuotaCreditsResponse response = _quotaService.addQuotaCredits(accountId, domainId, value, CallContext.current().getCallingUserId());
response.setResponseName(getCommandName());
response.setObjectName("quotacredits");
setResponseObject(response);

View File

@ -63,7 +63,7 @@ public class QuotaStatementCmd extends BaseCmd {
private Long accountId;
@Inject
QuotaService _quotaManager;
QuotaService _quotaService;
@Inject
QuotaDBUtils _quotaDBUtils;
@ -135,7 +135,7 @@ public class QuotaStatementCmd extends BaseCmd {
@Override
public void execute() {
List<QuotaUsageVO> quotaUsage = _quotaManager.getQuotaUsage(this);
List<QuotaUsageVO> quotaUsage = _quotaService.getQuotaUsage(this);
QuotaStatementResponse response = _quotaDBUtils.createQuotaStatementResponse(quotaUsage);

View File

@ -27,6 +27,7 @@ import org.apache.cloudstack.api.response.QuotaCreditsResponse;
import org.apache.cloudstack.api.response.QuotaStatementResponse;
import org.apache.cloudstack.api.response.QuotaTariffResponse;
import java.util.Date;
import java.util.List;
public interface QuotaDBUtils {
@ -45,8 +46,8 @@ public interface QuotaDBUtils {
ServiceOfferingVO findServiceOffering(Long vmId, long serviceOfferingId);
QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy);
QuotaBalanceResponse createQuotaLastBalanceResponse(List<QuotaBalanceVO> quotaBalance);
QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy, Date despositedOn);
}

View File

@ -27,7 +27,6 @@ import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.cloudstack.api.command.QuotaTariffListCmd;
import org.apache.cloudstack.api.command.QuotaTariffUpdateCmd;
import org.apache.cloudstack.api.response.QuotaBalanceResponse;
@ -85,6 +84,9 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
@Override
public QuotaBalanceResponse createQuotaLastBalanceResponse(List<QuotaBalanceVO> quotaBalance) {
if (quotaBalance.size() == 0) {
new InvalidParameterValueException("There are no balance entries on or before the requested date.");
}
Collections.sort(quotaBalance, new Comparator<QuotaBalanceVO>() {
public int compare(QuotaBalanceVO o1, QuotaBalanceVO o2) {
return o2.getUpdatedOn().compareTo(o1.getUpdatedOn()); // desc
@ -110,6 +112,9 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
@Override
public QuotaBalanceResponse createQuotaBalanceResponse(List<QuotaBalanceVO> quotaBalance) {
if (quotaBalance.size() == 0) {
new InvalidParameterValueException("The request period does not contain balance entries.");
}
Collections.sort(quotaBalance, new Comparator<QuotaBalanceVO>() {
public int compare(QuotaBalanceVO o1, QuotaBalanceVO o2) {
return o1.getUpdatedOn().compareTo(o2.getUpdatedOn()); //asc
@ -133,7 +138,8 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
resp.setEndDate(endItem.getUpdatedOn());
resp.setEndQuota(endItem.getCreditBalance());
} else {
new CloudRuntimeException("The request period is small and does not contain balance entries to provide any meaningful values.");
resp.setStartQuota(new BigDecimal(0));
resp.setEndQuota(new BigDecimal(0));
}
resp.setObjectName("balance");
@ -228,13 +234,13 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
}
@Override
public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy) {
public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy, Date despositedOn) {
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
QuotaCreditsVO result = null;
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
try {
QuotaCreditsVO credits = new QuotaCreditsVO(accountId, domainId, new BigDecimal(amount), updatedBy);
credits.setUpdatedOn(new Date());
credits.setUpdatedOn(despositedOn);
result = _quotaCreditsDao.saveCredits(credits);
} finally {
txn.close();

View File

@ -160,7 +160,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
BigDecimal aggrUsage = new BigDecimal(0);
for (QuotaUsageVO entry : quotalistforaccount) {
if (startDate.compareTo(entry.getStartDate()) != 0) {
QuotaBalanceVO lastrealbalanceentry = _quotaBalanceDao.getLastBalanceEntry(account.getAccountId(), account.getDomainId(), startDate);
QuotaBalanceVO lastrealbalanceentry = _quotaBalanceDao.findLastBalanceEntry(account.getAccountId(), account.getDomainId(), startDate);
Date lastbalancedate;
if (lastrealbalanceentry != null) {
lastbalancedate = lastrealbalanceentry.getUpdatedOn();
@ -169,7 +169,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
lastbalancedate = new Date(0);
}
List<QuotaBalanceVO> creditsrcvd = _quotaBalanceDao.getCreditBalance(account.getAccountId(), account.getDomainId(), lastbalancedate, endDate);
List<QuotaBalanceVO> creditsrcvd = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), lastbalancedate, endDate);
for (QuotaBalanceVO credit : creditsrcvd) {
aggrUsage = aggrUsage.add(credit.getCreditBalance());
}

View File

@ -20,6 +20,7 @@ import com.cloud.utils.component.PluggableService;
import org.apache.cloudstack.api.command.QuotaBalanceCmd;
import org.apache.cloudstack.api.command.QuotaStatementCmd;
import org.apache.cloudstack.api.response.QuotaCreditsResponse;
import java.util.List;
@ -29,4 +30,6 @@ public interface QuotaService extends PluggableService {
List<QuotaBalanceVO> getQuotaBalance(QuotaBalanceCmd cmd);
public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy);
}

View File

@ -34,6 +34,7 @@ import org.apache.cloudstack.api.command.QuotaRefreshCmd;
import org.apache.cloudstack.api.command.QuotaStatementCmd;
import org.apache.cloudstack.api.command.QuotaTariffListCmd;
import org.apache.cloudstack.api.command.QuotaTariffUpdateCmd;
import org.apache.cloudstack.api.response.QuotaCreditsResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.config.Configurable;
@ -71,6 +72,8 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
private ConfigurationDao _configDao;
@Inject
private QuotaBalanceDao _quotaBalanceDao;
@Inject
private QuotaDBUtils _quotaDBUtils;
private TimeZone _usageTimezone;
private int _aggregationDuration = 0;
@ -159,17 +162,18 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
Date startDate = cmd.getStartDate();
Date endDate = cmd.getEndDate();
startDate = startDate == null ? new Date() : startDate;
cmd.setStartDate(startDate);
TimeZone usageTZ = getUsageTimezone();
Date adjustedStartDate = computeAdjustedTime(startDate, usageTZ);
if (endDate == null) {
s_logger.debug("getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", on or before " + adjustedStartDate);
return _quotaBalanceDao.getQuotaBalance(accountId, domainId, adjustedStartDate);
return _quotaBalanceDao.findQuotaBalance(accountId, domainId, adjustedStartDate);
} else if (startDate.before(endDate)) {
Date adjustedEndDate = computeAdjustedTime(endDate, usageTZ);
s_logger.debug("getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", between " + adjustedStartDate + " and " + adjustedEndDate);
return _quotaBalanceDao.getQuotaBalance(accountId, domainId, adjustedStartDate, adjustedEndDate);
return _quotaBalanceDao.findQuotaBalance(accountId, domainId, adjustedStartDate, adjustedEndDate);
} else {
throw new InvalidParameterValueException("Incorrect Date Range. Start date: " + startDate + " is after end date:" + endDate);
}
@ -215,7 +219,22 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
Date adjustedEndDate = computeAdjustedTime(endDate, usageTZ);
s_logger.debug("getting quota records for account: " + accountId + ", domainId: " + domainId + ", between " + adjustedStartDate + " and " + adjustedEndDate);
return _quotaUsageDao.getQuotaUsage(accountId, domainId, usageType, adjustedStartDate, adjustedEndDate);
return _quotaUsageDao.findQuotaUsage(accountId, domainId, usageType, adjustedStartDate, adjustedEndDate);
}
@Override
public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy) {
Date depositDate = new Date();
TimeZone usageTZ = getUsageTimezone();
Date adjustedStartDate = computeAdjustedTime(depositDate, usageTZ);
QuotaBalanceVO qb = _quotaBalanceDao.findLaterBalanceEntry(accountId, domainId, adjustedStartDate);
if (qb != null) {
throw new InvalidParameterValueException("Incorrect deposit date: " + adjustedStartDate + " there are balance entries after this date");
}
_quotaDBUtils.addQuotaCredits(accountId, domainId, amount, updatedBy, adjustedStartDate);
return null;
}
public TimeZone getUsageTimezone() {

View File

@ -27,12 +27,14 @@ public interface QuotaBalanceDao extends GenericDao<QuotaBalanceVO, Long> {
void saveQuotaBalance(List<QuotaBalanceVO> credits);
List<QuotaBalanceVO> getCreditBalance(long accountId, long domainId, Date startDate, Date endDate);
List<QuotaBalanceVO> findCreditBalance(long accountId, long domainId, Date startDate, Date endDate);
QuotaBalanceVO getLastBalanceEntry(long accountId, long domainId, Date beforeThis);
QuotaBalanceVO findLastBalanceEntry(long accountId, long domainId, Date beforeThis);
List<QuotaBalanceVO> getQuotaBalance(Long accountId, Long domainId, Date startDate, Date endDate);
QuotaBalanceVO findLaterBalanceEntry(long accountId, long domainId, Date afterThis);
List<QuotaBalanceVO> getQuotaBalance(Long accountId, Long domainId, Date startDate);
List<QuotaBalanceVO> findQuotaBalance(Long accountId, Long domainId, Date startDate, Date endDate);
List<QuotaBalanceVO> findQuotaBalance(Long accountId, Long domainId, Date startDate);
}

View File

@ -36,7 +36,7 @@ public class QuotaBalanceDaoImpl extends GenericDaoBase<QuotaBalanceVO, Long> im
@SuppressWarnings("deprecation")
@Override
public QuotaBalanceVO getLastBalanceEntry(long accountId, long domainId, Date beforeThis) {
public QuotaBalanceVO findLastBalanceEntry(long accountId, long domainId, Date beforeThis) {
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
TransactionLegacy.open(TransactionLegacy.USAGE_DB).close();
Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", false, 0L, 1L);
@ -50,6 +50,22 @@ public class QuotaBalanceDaoImpl extends GenericDaoBase<QuotaBalanceVO, Long> im
return quotab.size() > 0 ? quotab.get(0) : null;
}
@SuppressWarnings("deprecation")
@Override
public QuotaBalanceVO findLaterBalanceEntry(long accountId, long domainId, Date afterThis) {
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
TransactionLegacy.open(TransactionLegacy.USAGE_DB).close();
Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", true, 0L, 1L);
SearchCriteria<QuotaBalanceVO> sc = createSearchCriteria();
sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId);
sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
sc.addAnd("creditsId", SearchCriteria.Op.EQ, 0);
sc.addAnd("updatedOn", SearchCriteria.Op.GT, afterThis);
List<QuotaBalanceVO> quotab = this.search(sc, filter);
TransactionLegacy.open(opendb).close();
return quotab.size() > 0 ? quotab.get(0) : null;
}
@Override
public void saveQuotaBalance(List<QuotaBalanceVO> credits) {
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
@ -66,7 +82,7 @@ public class QuotaBalanceDaoImpl extends GenericDaoBase<QuotaBalanceVO, Long> im
@SuppressWarnings("deprecation")
@Override
public List<QuotaBalanceVO> getCreditBalance(long accountId, long domainId, Date lastbalancedate, Date beforeThis) {
public List<QuotaBalanceVO> findCreditBalance(long accountId, long domainId, Date lastbalancedate, Date beforeThis) {
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
TransactionLegacy.open(TransactionLegacy.USAGE_DB);
Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", true, 0L, Long.MAX_VALUE);
@ -86,7 +102,7 @@ public class QuotaBalanceDaoImpl extends GenericDaoBase<QuotaBalanceVO, Long> im
@SuppressWarnings("deprecation")
@Override
public List<QuotaBalanceVO> getQuotaBalance(Long accountId, Long domainId, Date startDate, Date endDate) {
public List<QuotaBalanceVO> findQuotaBalance(Long accountId, Long domainId, Date startDate, Date endDate) {
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
@ -115,7 +131,7 @@ public class QuotaBalanceDaoImpl extends GenericDaoBase<QuotaBalanceVO, Long> im
@SuppressWarnings("deprecation")
@Override
public List<QuotaBalanceVO> getQuotaBalance(Long accountId, Long domainId, Date startDate) {
public List<QuotaBalanceVO> findQuotaBalance(Long accountId, Long domainId, Date startDate) {
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);

View File

@ -25,7 +25,7 @@ import com.cloud.utils.db.GenericDao;
public interface QuotaCreditsDao extends GenericDao<QuotaCreditsVO, Long> {
List<QuotaCreditsVO> getCredits(long accountId, long domainId, Date startDate, Date endDate);
List<QuotaCreditsVO> findCredits(long accountId, long domainId, Date startDate, Date endDate);
QuotaCreditsVO saveCredits(QuotaCreditsVO credits);

View File

@ -40,7 +40,7 @@ public class QuotaCreditsDaoImpl extends GenericDaoBase<QuotaCreditsVO, Long> im
@SuppressWarnings("deprecation")
@Override
public List<QuotaCreditsVO> getCredits(long accountId, long domainId, Date startDate, Date endDate) {
public List<QuotaCreditsVO> findCredits(long accountId, long domainId, Date startDate, Date endDate) {
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
TransactionLegacy.open(TransactionLegacy.USAGE_DB);
Filter filter = new Filter(QuotaCreditsVO.class, "updatedOn", true, 0L, Long.MAX_VALUE);

View File

@ -28,7 +28,7 @@ import com.cloud.utils.db.SearchCriteria;
public interface QuotaUsageDao extends GenericDao<QuotaUsageVO, Long> {
List<QuotaUsageVO> getQuotaUsage(Long accountId, Long domainId, Integer usageType, Date startDate, Date endDate);
List<QuotaUsageVO> findQuotaUsage(Long accountId, Long domainId, Integer usageType, Date startDate, Date endDate);
Pair<List<QuotaUsageVO>, Integer> searchAndCountAllRecords(SearchCriteria<QuotaUsageVO> sc, Filter filter);

View File

@ -49,7 +49,7 @@ public class QuotaUsageDaoImpl extends GenericDaoBase<QuotaUsageVO, Long> implem
@SuppressWarnings("deprecation")
@Override
public List<QuotaUsageVO> getQuotaUsage(Long accountId, Long domainId, Integer usageType, Date startDate, Date endDate) {
public List<QuotaUsageVO> findQuotaUsage(Long accountId, Long domainId, Integer usageType, Date startDate, Date endDate) {
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
List<QuotaUsageVO> quotaUsageRecords = null;