iCLOUDSTACK-8592: Incremental update for credits and balance

This commit is contained in:
Abhinandan Prateek 2015-07-15 15:49:24 +05:30
parent 13b04557fa
commit f4226a7219
9 changed files with 199 additions and 124 deletions

View File

@ -17,6 +17,7 @@
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.ApiErrorCode;
@ -41,14 +42,14 @@ public class QuotaCreditsCmd extends BaseCmd {
private static final String s_name = "quotacreditsresponse";
@Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, required=true, description = "Account Id for which quota credits need to be added")
@Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, required = true, description = "Account Id for which quota credits need to be added")
private String accountName;
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required=true, entityType = DomainResponse.class, description = "Domain for which quota credits need to be added")
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = true, entityType = DomainResponse.class, description = "Domain for which quota credits need to be added")
private Long domainId;
@Parameter(name = ApiConstants.VALUE, type = CommandType.STRING, required=true, description = "Value of the credits to be added+, subtracted-")
private String value;
@Parameter(name = ApiConstants.VALUE, type = CommandType.DOUBLE, required = true, description = "Value of the credits to be added+, subtracted-")
private Double value;
public String getAccountName() {
return accountName;
@ -66,11 +67,11 @@ public class QuotaCreditsCmd extends BaseCmd {
this.domainId = domainId;
}
public String getValue() {
public Double getValue() {
return value;
}
public void setValue(String value) {
public void setValue(Double value) {
this.value = value;
}
@ -90,15 +91,12 @@ public class QuotaCreditsCmd extends BaseCmd {
@Override
public void execute() {
Long accountId = _accountService.finalyzeAccountId(accountName,
domainId, null, true);
Long accountId = _accountService.finalyzeAccountId(accountName, domainId, null, true);
if (accountId == null) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR,
"The account does not exists or has been removed/disabled");
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "The account does not exists or has been removed/disabled");
}
final QuotaCreditsResponse response = _quotaDBUtils.addQuotaCredits(accountId, domainId, value,
CallContext.current().getCallingUserId());
final QuotaCreditsResponse response = _quotaDBUtils.addQuotaCredits(accountId, domainId, value, CallContext.current().getCallingUserId());
response.setResponseName(getCommandName());
response.setObjectName("quotacredits");
setResponseObject(response);

View File

@ -18,21 +18,19 @@ package org.apache.cloudstack.api.response;
import com.cloud.serializer.Param;
import com.google.gson.annotations.SerializedName;
import org.apache.cloudstack.api.BaseResponse;
import org.apache.cloudstack.quota.QuotaCreditsVO;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
public class QuotaCreditsResponse extends BaseResponse {
@SerializedName("credits")
@Param(description = "the credit deposited")
private String credits;
@SerializedName("balance")
@Param(description = "the balance credit in account")
private String balance;
private BigDecimal credits;
@SerializedName("updated_by")
@Param(description = "the user name of the admin who updated the credits")
@ -49,29 +47,20 @@ public class QuotaCreditsResponse extends BaseResponse {
public QuotaCreditsResponse(QuotaCreditsVO result, String updatedBy) {
super();
if (result != null) {
this.credits = result.getCredit().setScale(2, BigDecimal.ROUND_HALF_EVEN).toString();
this.balance = (new BigDecimal("200")).toString();
this.credits = result.getCredit().setScale(2, RoundingMode.HALF_EVEN);
this.updatedBy = updatedBy;
this.updatedOn = new Timestamp(System.currentTimeMillis());
}
}
public String getCredits() {
public BigDecimal getCredits() {
return credits;
}
public void setCredits(String credits) {
public void setCredits(BigDecimal credits) {
this.credits = credits;
}
public String getBalance() {
return balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
public String getUpdatedBy() {
return updatedBy;
}

View File

@ -47,6 +47,9 @@ public class QuotaBalanceVO implements InternalIdentity {
@Column(name = "credit_balance")
private BigDecimal creditBalance;
@Column(name = "credits_id")
private Long creditsId;
@Column(name = "updated_on")
@Temporal(value = TemporalType.TIMESTAMP)
private Date updatedOn = null;
@ -59,6 +62,16 @@ public class QuotaBalanceVO implements InternalIdentity {
private Date previousUpdateOn = null;
public QuotaBalanceVO() {
super();
}
public QuotaBalanceVO(QuotaCreditsVO credit) {
super();
this.accountId = credit.getAccountId();
this.domainId = credit.getDomainId();
this.creditBalance = credit.getCredit();
this.updatedOn = credit.getUpdatedOn();
this.creditsId = credit.getId();
}
public QuotaBalanceVO(Long accountId, Long domainId, BigDecimal creditBalance, Date updatedOn, Long previousUpdateId, Date previousUpdateOn) {
@ -96,6 +109,14 @@ public class QuotaBalanceVO implements InternalIdentity {
this.domainId = domainId;
}
public Long getCreditsId() {
return creditsId;
}
public void setCreditsId(Long creditsId) {
this.creditsId = creditsId;
}
public BigDecimal getCreditBalance() {
return creditBalance;
}

View File

@ -53,11 +53,11 @@ public class QuotaCreditsVO implements InternalIdentity {
public QuotaCreditsVO() {
}
public QuotaCreditsVO(long accountId, long domainId, String credit, long updatedBy) {
public QuotaCreditsVO(long accountId, long domainId, BigDecimal credit, long updatedBy) {
super();
this.accountId = accountId;
this.domainId = domainId;
this.credit = new BigDecimal(credit);
this.credit = credit;
this.updatedBy = updatedBy;
}

View File

@ -24,6 +24,8 @@ import org.apache.cloudstack.api.response.QuotaConfigurationResponse;
import org.apache.cloudstack.api.response.QuotaCreditsResponse;
import org.apache.cloudstack.api.response.QuotaStatementResponse;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.usage.UsageVO;
import com.cloud.utils.Pair;
public interface QuotaDBUtils {
@ -36,6 +38,10 @@ public interface QuotaDBUtils {
QuotaStatementResponse createQuotaStatementResponse(List<QuotaUsageVO> quotaUsage);
QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, String amount, Long updatedBy);
Pair<List<? extends UsageVO>, Integer> getUsageRecords(long accountId, long domainId);
ServiceOfferingVO findServiceOffering(Long vmId, long serviceOfferingId);
QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy);
}

View File

@ -17,9 +17,15 @@
package org.apache.cloudstack.quota;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.service.dao.ServiceOfferingDao;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.user.User;
import com.cloud.user.dao.UserDao;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.TransactionLegacy;
import org.apache.cloudstack.api.command.QuotaEditMappingCmd;
@ -53,15 +59,18 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
@Inject
private QuotaMappingDao _quotaMappingDao;
@Inject
private QuotaCreditsDao _quotaCreditsDao;
@Inject
private QuotaBalanceDao _quotaBalanceDao;
@Inject
private ServiceOfferingDao _serviceOfferingDao;
@Inject
private UserDao _userDao;
@Inject
private UsageDao _usageDao;
static Long s_recordtofetch = 1000L;
@Override
public QuotaConfigurationResponse createQuotaConfigurationResponse(QuotaMappingVO configuration) {
@ -164,13 +173,13 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
}
@Override
public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, String amount, Long updatedBy) {
public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy) {
QuotaCreditsVO result = null;
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
try {
QuotaCreditsVO credits = new QuotaCreditsVO(accountId, domainId, amount, updatedBy);
QuotaCreditsVO credits = new QuotaCreditsVO(accountId, domainId, new BigDecimal(amount), updatedBy);
credits.setUpdatedOn(new Date());
result = _quotaCreditsDao.persist(credits);
result = _quotaCreditsDao.saveCredits(credits);
} finally {
txn.close();
}
@ -183,4 +192,35 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
return new QuotaCreditsResponse(result, creditor);
}
@Override
@SuppressWarnings("deprecation")
public Pair<List<? extends UsageVO>, Integer> getUsageRecords(long accountId, long domainId) {
s_logger.debug("getting usage records for account: " + accountId + ", domainId: " + domainId);
Filter usageFilter = new Filter(UsageVO.class, "startDate", true, 0L, s_recordtofetch);
SearchCriteria<UsageVO> sc = _usageDao.createSearchCriteria();
if (accountId != -1) {
sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId);
}
if (domainId != -1) {
sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
}
sc.addAnd("quotaCalculated", SearchCriteria.Op.EQ, 0);
s_logger.debug("Getting usage records" + usageFilter.getOrderBy());
Pair<List<UsageVO>, Integer> usageRecords = _usageDao.searchAndCountAllRecords(sc, usageFilter);
return new Pair<List<? extends UsageVO>, Integer>(usageRecords.first(), usageRecords.second());
}
@Override
public ServiceOfferingVO findServiceOffering(Long vmId, long serviceOfferingId) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
ServiceOfferingVO result;
try {
result = _serviceOfferingDao.findById(vmId, serviceOfferingId);
} finally {
txn.close();
}
TransactionLegacy.open(TransactionLegacy.USAGE_DB);
return result;
}
}

View File

@ -21,7 +21,6 @@ import com.cloud.domain.dao.DomainDao;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.service.dao.ServiceOfferingDao;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.user.Account;
@ -77,13 +76,13 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
@Inject
private QuotaUsageDao _quotaUsageDao;
@Inject
private ServiceOfferingDao _serviceOfferingDao;
@Inject
private DomainDao _domainDao;
@Inject
private ConfigurationDao _configDao;
@Inject
private AccountService _accountService;
@Inject
private QuotaDBUtils _quotaDBUtils;
private TimeZone _usageTimezone;
private int _aggregationDuration = 0;
@ -91,7 +90,6 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
static BigDecimal s_hoursInMonth = new BigDecimal(30 * 24);
static BigDecimal s_minutesInMonth = new BigDecimal(30 * 24 * 60);
static BigDecimal s_gb = new BigDecimal(1024 * 1024 * 1024);
static Long s_recordtofetch=1000L;
public QuotaManagerImpl() {
super();
@ -154,59 +152,60 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
// get all the active accounts for which there is usage
List<AccountVO> accounts = _accountDao.listAll();
for (AccountVO account : accounts) {
for (AccountVO account : accounts) { // START ACCOUNT
Pair<List<? extends UsageVO>, Integer> usageRecords = null;
List<QuotaUsageVO> quotalistforaccount = new ArrayList<QuotaUsageVO>();
do {
s_logger.info("Account =" + account.getAccountName());
usageRecords = getUsageRecords(account.getAccountId(), account.getDomainId());
usageRecords = _quotaDBUtils.getUsageRecords(account.getAccountId(), account.getDomainId());
s_logger.debug("Usage records found " + usageRecords.second());
for (UsageVO usageRecord : usageRecords.first()) {
BigDecimal aggregationRatio = new BigDecimal(_aggregationDuration).divide(s_minutesInMonth, 8, RoundingMode.HALF_EVEN);
switch (usageRecord.getUsageType()) {
case QuotaTypes.RUNNING_VM:
updateQuotaRunningVMUsage(usageRecord, mapping, aggregationRatio);
quotalistforaccount.addAll(updateQuotaRunningVMUsage(usageRecord, mapping, aggregationRatio));
break;
case QuotaTypes.ALLOCATED_VM:
updateQuotaAllocatedVMUsage(usageRecord, mapping, aggregationRatio);
quotalistforaccount.add(updateQuotaAllocatedVMUsage(usageRecord, mapping, aggregationRatio));
break;
case QuotaTypes.SNAPSHOT:
updateQuotaDiskUsage(usageRecord, mapping, aggregationRatio, QuotaTypes.SNAPSHOT);
quotalistforaccount.add(updateQuotaDiskUsage(usageRecord, mapping, aggregationRatio, QuotaTypes.SNAPSHOT));
break;
case QuotaTypes.TEMPLATE:
updateQuotaDiskUsage(usageRecord, mapping, aggregationRatio, QuotaTypes.TEMPLATE);
quotalistforaccount.add(updateQuotaDiskUsage(usageRecord, mapping, aggregationRatio, QuotaTypes.TEMPLATE));
break;
case QuotaTypes.ISO:
updateQuotaDiskUsage(usageRecord, mapping, aggregationRatio, QuotaTypes.ISO);
quotalistforaccount.add(updateQuotaDiskUsage(usageRecord, mapping, aggregationRatio, QuotaTypes.ISO));
break;
case QuotaTypes.VOLUME:
updateQuotaDiskUsage(usageRecord, mapping, aggregationRatio, QuotaTypes.VOLUME);
quotalistforaccount.add(updateQuotaDiskUsage(usageRecord, mapping, aggregationRatio, QuotaTypes.VOLUME));
break;
case QuotaTypes.VM_SNAPSHOT:
updateQuotaDiskUsage(usageRecord, mapping, aggregationRatio, QuotaTypes.VM_SNAPSHOT);
quotalistforaccount.add(updateQuotaDiskUsage(usageRecord, mapping, aggregationRatio, QuotaTypes.VM_SNAPSHOT));
break;
case QuotaTypes.LOAD_BALANCER_POLICY:
updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.LOAD_BALANCER_POLICY);
quotalistforaccount.add(updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.LOAD_BALANCER_POLICY));
break;
case QuotaTypes.PORT_FORWARDING_RULE:
updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.PORT_FORWARDING_RULE);
quotalistforaccount.add(updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.PORT_FORWARDING_RULE));
break;
case QuotaTypes.IP_ADDRESS:
updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.IP_ADDRESS);
quotalistforaccount.add(updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.IP_ADDRESS));
break;
case QuotaTypes.NETWORK_OFFERING:
updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.NETWORK_OFFERING);
quotalistforaccount.add(updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.NETWORK_OFFERING));
break;
case QuotaTypes.SECURITY_GROUP:
updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.SECURITY_GROUP);
quotalistforaccount.add(updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.SECURITY_GROUP));
break;
case QuotaTypes.VPN_USERS:
updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.VPN_USERS);
quotalistforaccount.add(updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.VPN_USERS));
break;
case QuotaTypes.NETWORK_BYTES_RECEIVED:
updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.NETWORK_BYTES_RECEIVED);
quotalistforaccount.add(updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.NETWORK_BYTES_RECEIVED));
break;
case QuotaTypes.NETWORK_BYTES_SENT:
updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.NETWORK_BYTES_SENT);
quotalistforaccount.add(updateQuotaRaw(usageRecord, mapping, aggregationRatio, QuotaTypes.NETWORK_BYTES_SENT));
break;
case QuotaTypes.VM_DISK_IO_READ:
case QuotaTypes.VM_DISK_IO_WRITE:
@ -217,7 +216,23 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
}
}
} while ((usageRecords != null) && !usageRecords.first().isEmpty());
}
// list of quotas for this account
s_logger.info("Quota entries size = " + quotalistforaccount.size());
Date startDate = new Date();
Date endDate = new Date();
int count = 0;
BigDecimal aggrUsage = new BigDecimal(0);
for (QuotaUsageVO entry : quotalistforaccount) {
if (startDate.compareTo(entry.getStartDate()) != 0) {
startDate = entry.getStartDate();
endDate = entry.getEndDate();
s_logger.info("Start Date=" + startDate.toString() + " to endDate=" + endDate.toString() + "record count=" + count);
aggrUsage = aggrUsage.add(entry.getQuotaUsed());
count = 0;
}
count++;
}
} // END ACCOUNT
jobResult = true;
} catch (Exception e) {
s_logger.error("Quota Manager error", e);
@ -290,7 +305,8 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
Pair<List<QuotaUsageVO>, Integer> quotaUsageRecords = null;
try {
//TODO instead of max value query with reasonable number and iterate
// TODO instead of max value query with reasonable number and
// iterate
Filter usageFilter = new Filter(QuotaUsageVO.class, "id", true, 0L, Long.MAX_VALUE);
SearchCriteria<QuotaUsageVO> sc = _quotaUsageDao.createSearchCriteria();
if (accountId != null) {
@ -334,36 +350,44 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
}
@DB
private void updateQuotaDiskUsage(UsageVO usageRecord, HashMap<Integer, QuotaMappingVO> mapping, BigDecimal aggregationRatio, int quotaType) {
private QuotaUsageVO updateQuotaDiskUsage(UsageVO usageRecord, HashMap<Integer, QuotaMappingVO> mapping, BigDecimal aggregationRatio, int quotaType) {
QuotaUsageVO quota_usage = null;
if (mapping.get(quotaType) != null) {
QuotaUsageVO quota_usage;
BigDecimal quotaUsgage;
BigDecimal onehourcostpergb;
BigDecimal noofgbinuse;
s_logger.info(usageRecord.getDescription() + ", " + usageRecord.getType() + ", " + usageRecord.getOfferingId() + ", " + usageRecord.getTemplateId() + ", " + usageRecord.getUsageDisplay()
+ ", aggrR=" + aggregationRatio);
// s_logger.info(usageRecord.getDescription() + ", " +
// usageRecord.getType() + ", " + usageRecord.getOfferingId() + ", "
// + usageRecord.getTemplateId() + ", " +
// usageRecord.getUsageDisplay()
// + ", aggrR=" + aggregationRatio);
onehourcostpergb = mapping.get(quotaType).getCurrencyValue().multiply(aggregationRatio);
noofgbinuse = new BigDecimal(usageRecord.getSize()).divide(s_gb, 8, RoundingMode.HALF_EVEN);
quotaUsgage = new BigDecimal(usageRecord.getRawUsage()).multiply(onehourcostpergb).multiply(noofgbinuse);
s_logger.info(" No of GB In use = " + noofgbinuse + " onehour cost=" + onehourcostpergb);
// s_logger.info(" No of GB In use = " + noofgbinuse +
// " onehour cost=" + onehourcostpergb);
quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), usageRecord.getUsageType(), quotaUsgage,
usageRecord.getStartDate(), usageRecord.getEndDate());
_quotaUsageDao.persist(quota_usage);
}
usageRecord.setQuotaCalculated(1);
_usageDao.persist(usageRecord);
return quota_usage;
}
@DB
private void updateQuotaRunningVMUsage(UsageVO usageRecord, HashMap<Integer, QuotaMappingVO> mapping, BigDecimal aggregationRatio) {
private List<QuotaUsageVO> updateQuotaRunningVMUsage(UsageVO usageRecord, HashMap<Integer, QuotaMappingVO> mapping, BigDecimal aggregationRatio) {
List<QuotaUsageVO> quotalist = new ArrayList<QuotaUsageVO>();
QuotaUsageVO quota_usage;
BigDecimal cpuquotausgage, speedquotausage, memoryquotausage, vmusage;
BigDecimal onehourcostpercpu, onehourcostper100mhz, onehourcostper1mb, onehourcostforvmusage;
BigDecimal rawusage;
s_logger.info(usageRecord.getDescription() + ", " + usageRecord.getType() + ", " + usageRecord.getOfferingId() + ", " + usageRecord.getVmInstanceId() + ", " + usageRecord.getUsageDisplay()
+ ", aggrR=" + aggregationRatio);
// s_logger.info(usageRecord.getDescription() + ", " +
// usageRecord.getType() + ", " + usageRecord.getOfferingId() + ", " +
// usageRecord.getVmInstanceId() + ", " + usageRecord.getUsageDisplay()
// + ", aggrR=" + aggregationRatio);
// get service offering details
ServiceOfferingVO serviceoffering = findServiceOffering(usageRecord.getVmInstanceId(), usageRecord.getOfferingId());
ServiceOfferingVO serviceoffering = _quotaDBUtils.findServiceOffering(usageRecord.getVmInstanceId(), usageRecord.getOfferingId());
rawusage = new BigDecimal(usageRecord.getRawUsage());
if (mapping.get(QuotaTypes.CPU_NUMBER) != null) {
@ -373,6 +397,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), QuotaTypes.CPU_NUMBER, cpuquotausgage,
usageRecord.getStartDate(), usageRecord.getEndDate());
_quotaUsageDao.persist(quota_usage);
quotalist.add(quota_usage);
}
if (mapping.get(QuotaTypes.CPU_CLOCK_RATE) != null) {
BigDecimal speed = new BigDecimal(serviceoffering.getSpeed() / 100.00);
@ -381,6 +406,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), QuotaTypes.CPU_CLOCK_RATE, speedquotausage,
usageRecord.getStartDate(), usageRecord.getEndDate());
_quotaUsageDao.persist(quota_usage);
quotalist.add(quota_usage);
}
if (mapping.get(QuotaTypes.MEMORY) != null) {
BigDecimal memory = new BigDecimal(serviceoffering.getRamSize());
@ -389,6 +415,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), QuotaTypes.MEMORY, memoryquotausage,
usageRecord.getStartDate(), usageRecord.getEndDate());
_quotaUsageDao.persist(quota_usage);
quotalist.add(quota_usage);
}
if (mapping.get(QuotaTypes.RUNNING_VM) != null) {
onehourcostforvmusage = mapping.get(QuotaTypes.RUNNING_VM).getCurrencyValue().multiply(aggregationRatio);
@ -396,20 +423,24 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), QuotaTypes.RUNNING_VM, vmusage,
usageRecord.getStartDate(), usageRecord.getEndDate());
_quotaUsageDao.persist(quota_usage);
quotalist.add(quota_usage);
}
usageRecord.setQuotaCalculated(1);
_usageDao.persist(usageRecord);
return quotalist;
}
@DB
private void updateQuotaAllocatedVMUsage(UsageVO usageRecord, HashMap<Integer, QuotaMappingVO> mapping, BigDecimal aggregationRatio) {
private QuotaUsageVO updateQuotaAllocatedVMUsage(UsageVO usageRecord, HashMap<Integer, QuotaMappingVO> mapping, BigDecimal aggregationRatio) {
QuotaUsageVO quota_usage = null;
if (mapping.get(QuotaTypes.ALLOCATED_VM) != null) {
QuotaUsageVO quota_usage;
BigDecimal vmusage;
BigDecimal onehourcostforvmusage;
s_logger.info(usageRecord.getDescription() + ", " + usageRecord.getType() + ", " + usageRecord.getOfferingId() + ", " + usageRecord.getVmInstanceId() + ", "
+ usageRecord.getUsageDisplay() + ", aggrR=" + aggregationRatio);
// s_logger.info(usageRecord.getDescription() + ", " +
// usageRecord.getType() + ", " + usageRecord.getOfferingId() + ", "
// + usageRecord.getVmInstanceId() + ", "
// + usageRecord.getUsageDisplay() + ", aggrR=" + aggregationRatio);
onehourcostforvmusage = mapping.get(QuotaTypes.ALLOCATED_VM).getCurrencyValue().multiply(aggregationRatio);
vmusage = new BigDecimal(usageRecord.getRawUsage()).multiply(onehourcostforvmusage);
@ -420,16 +451,19 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
usageRecord.setQuotaCalculated(1);
_usageDao.persist(usageRecord);
return quota_usage;
}
@DB
private void updateQuotaRaw(UsageVO usageRecord, HashMap<Integer, QuotaMappingVO> mapping, BigDecimal aggregationRatio, int ruleType) {
private QuotaUsageVO updateQuotaRaw(UsageVO usageRecord, HashMap<Integer, QuotaMappingVO> mapping, BigDecimal aggregationRatio, int ruleType) {
QuotaUsageVO quota_usage = null;
if (mapping.get(ruleType) != null) {
QuotaUsageVO quota_usage;
BigDecimal ruleusage;
BigDecimal onehourcost;
s_logger.info(usageRecord.getDescription() + ", " + usageRecord.getType() + ", " + usageRecord.getOfferingId() + ", " + usageRecord.getVmInstanceId() + ", "
+ usageRecord.getUsageDisplay() + ", aggrR=" + aggregationRatio);
// s_logger.info(usageRecord.getDescription() + ", " +
// usageRecord.getType() + ", " + usageRecord.getOfferingId() + ", "
// + usageRecord.getVmInstanceId() + ", "
// + usageRecord.getUsageDisplay() + ", aggrR=" + aggregationRatio);
onehourcost = mapping.get(ruleType).getCurrencyValue().multiply(aggregationRatio);
ruleusage = new BigDecimal(usageRecord.getRawUsage()).multiply(onehourcost);
@ -440,17 +474,20 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
usageRecord.setQuotaCalculated(1);
_usageDao.persist(usageRecord);
return quota_usage;
}
@DB
private void updateQuotaNetwork(UsageVO usageRecord, HashMap<Integer, QuotaMappingVO> mapping, int transferType) {
private QuotaUsageVO updateQuotaNetwork(UsageVO usageRecord, HashMap<Integer, QuotaMappingVO> mapping, int transferType) {
QuotaUsageVO quota_usage = null;
if (mapping.get(transferType) != null) {
QuotaUsageVO quota_usage;
BigDecimal onegbcost;
BigDecimal rawusageingb;
BigDecimal networkusage;
s_logger.info(usageRecord.getDescription() + ", " + usageRecord.getType() + ", " + usageRecord.getOfferingId() + ", " + usageRecord.getVmInstanceId() + ", "
+ usageRecord.getUsageDisplay());
// s_logger.info(usageRecord.getDescription() + ", " +
// usageRecord.getType() + ", " + usageRecord.getOfferingId() + ", "
// + usageRecord.getVmInstanceId() + ", "
// + usageRecord.getUsageDisplay());
onegbcost = mapping.get(transferType).getCurrencyValue();
rawusageingb = new BigDecimal(usageRecord.getRawUsage()).divide(s_gb, 8, RoundingMode.HALF_EVEN);
@ -462,35 +499,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi
usageRecord.setQuotaCalculated(1);
_usageDao.persist(usageRecord);
}
@SuppressWarnings("deprecation")
public Pair<List<? extends UsageVO>, Integer> getUsageRecords(long accountId, long domainId) {
s_logger.debug("getting usage records for account: " + accountId + ", domainId: " + domainId);
Filter usageFilter = new Filter(UsageVO.class, "id", true, s_recordtofetch, null);
SearchCriteria<UsageVO> sc = _usageDao.createSearchCriteria();
if (accountId != -1) {
sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId);
}
if (domainId != -1) {
sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
}
sc.addAnd("quotaCalculated", SearchCriteria.Op.EQ, 0);
s_logger.debug("Getting usage records" + usageFilter.getOrderBy());
Pair<List<UsageVO>, Integer> usageRecords = _usageDao.searchAndCountAllRecords(sc, usageFilter);
return new Pair<List<? extends UsageVO>, Integer>(usageRecords.first(), usageRecords.second());
}
public ServiceOfferingVO findServiceOffering(Long vmId, long serviceOfferingId) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
ServiceOfferingVO result;
try {
result = _serviceOfferingDao.findById(vmId, serviceOfferingId);
} finally {
txn.close();
}
TransactionLegacy.open(TransactionLegacy.USAGE_DB);
return result;
return quota_usage;
}
public TimeZone getUsageTimezone() {

View File

@ -16,19 +16,17 @@
//under the License.
package org.apache.cloudstack.quota.dao;
import java.util.Date;
import java.util.List;
import org.apache.cloudstack.quota.QuotaCreditsVO;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDao;
import com.cloud.utils.db.SearchCriteria;
public interface QuotaCreditsDao extends GenericDao<QuotaCreditsVO, Long> {
Pair<List<QuotaCreditsVO>, Integer> searchAndCountAllRecords(SearchCriteria<QuotaCreditsVO> sc, Filter filter);
List<QuotaCreditsVO> getCredits(long accountId, long domainId, Date startDate, Date endDate);
void saveQuotaCredits(List<QuotaCreditsVO> credits);
QuotaCreditsVO saveCredits(QuotaCreditsVO credits);
}

View File

@ -16,32 +16,46 @@
//under the License.
package org.apache.cloudstack.quota.dao;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Local;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.quota.QuotaBalanceVO;
import org.apache.cloudstack.quota.QuotaCreditsVO;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchCriteria;
@Component
@Local(value = { QuotaCreditsDao.class })
public class QuotaCreditsDaoImpl extends GenericDaoBase<QuotaCreditsVO, Long> implements QuotaCreditsDao {
@Inject
QuotaBalanceDao _quotaBalanceDao;
@SuppressWarnings("deprecation")
@Override
public Pair<List<QuotaCreditsVO>, Integer> searchAndCountAllRecords(SearchCriteria<QuotaCreditsVO> sc, Filter filter) {
// TODO Auto-generated method stub
return null;
public List<QuotaCreditsVO> getCredits(long accountId, long domainId, Date startDate, Date endDate) {
SearchCriteria<QuotaCreditsVO> sc = createSearchCriteria();
if ((startDate != null) && (endDate != null) && startDate.before(endDate)) {
sc.addAnd("startDate", SearchCriteria.Op.BETWEEN, startDate, endDate);
sc.addAnd("endDate", SearchCriteria.Op.BETWEEN, startDate, endDate);
} else {
return new ArrayList<QuotaCreditsVO>();
}
return listBy(sc);
}
@Override
public void saveQuotaCredits(List<QuotaCreditsVO> credits) {
// TODO Auto-generated method stub
//
public QuotaCreditsVO saveCredits(QuotaCreditsVO credits) {
persist(credits);
// make an entry in the balance table
QuotaBalanceVO bal = new QuotaBalanceVO(credits);
_quotaBalanceDao.persist(bal);
return credits;
}
}