mirror of https://github.com/apache/cloudstack.git
quota: make tariff plans updatable and schedulable
This commit is contained in:
parent
f51eb1b396
commit
22dccd1be6
|
|
@ -57,6 +57,7 @@ public class QuotaTariffListCmd extends BaseListCmd {
|
|||
|
||||
final List<QuotaTariffResponse> responses = new ArrayList<QuotaTariffResponse>();
|
||||
for (final QuotaTariffVO resource : result) {
|
||||
s_logger.info("Result desc=" + resource.getDescription() + " date=" + resource.getEffectiveOn() + " val=" + resource.getCurrencyValue());
|
||||
responses.add(_quotaDBUtils.createQuotaTariffResponse(resource));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,9 +18,11 @@ package org.apache.cloudstack.api.response;
|
|||
|
||||
import com.cloud.serializer.Param;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import org.apache.cloudstack.api.BaseResponse;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
public class QuotaTariffResponse extends BaseResponse {
|
||||
|
||||
|
|
@ -44,6 +46,10 @@ public class QuotaTariffResponse extends BaseResponse {
|
|||
@Param(description = "tariffValue")
|
||||
private BigDecimal tariffValue;
|
||||
|
||||
@SerializedName("effective_on")
|
||||
@Param(description = "the time at whihc this quota value will be effective")
|
||||
private Date effectiveOn = null;
|
||||
|
||||
@SerializedName("include")
|
||||
@Param(description = "include")
|
||||
private int include;
|
||||
|
|
@ -118,4 +124,12 @@ public class QuotaTariffResponse extends BaseResponse {
|
|||
this.description = description;
|
||||
}
|
||||
|
||||
public Date getEffectiveOn() {
|
||||
return effectiveOn;
|
||||
}
|
||||
|
||||
public void setEffectiveOn(Date effectiveOn) {
|
||||
this.effectiveOn = effectiveOn;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,19 +66,22 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
|
|||
private UserDao _userDao;
|
||||
@Inject
|
||||
private UsageDao _usageDao;
|
||||
@Inject
|
||||
private QuotaManager _quotaManager;
|
||||
|
||||
static Long s_recordtofetch = 1000L;
|
||||
|
||||
@Override
|
||||
public QuotaTariffResponse createQuotaTariffResponse(QuotaTariffVO configuration) {
|
||||
public QuotaTariffResponse createQuotaTariffResponse(QuotaTariffVO tariff) {
|
||||
final QuotaTariffResponse response = new QuotaTariffResponse();
|
||||
response.setUsageType(configuration.getUsageType());
|
||||
response.setUsageName(configuration.getUsageName());
|
||||
response.setUsageUnit(configuration.getUsageUnit());
|
||||
response.setUsageDiscriminator(configuration.getUsageDiscriminator());
|
||||
response.setTariffValue(configuration.getCurrencyValue());
|
||||
response.setInclude(configuration.getInclude());
|
||||
response.setDescription(configuration.getDescription());
|
||||
response.setUsageType(tariff.getUsageType());
|
||||
response.setUsageName(tariff.getUsageName());
|
||||
response.setUsageUnit(tariff.getUsageUnit());
|
||||
response.setUsageDiscriminator(tariff.getUsageDiscriminator());
|
||||
response.setTariffValue(tariff.getCurrencyValue());
|
||||
response.setInclude(tariff.getInclude());
|
||||
response.setEffectiveOn(tariff.getEffectiveOn());
|
||||
response.setDescription(tariff.getDescription());
|
||||
return response;
|
||||
}
|
||||
|
||||
|
|
@ -207,13 +210,15 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
|
|||
@Override
|
||||
public List<QuotaTariffVO> listQuotaTariffPlans(final QuotaTariffListCmd cmd) {
|
||||
List<QuotaTariffVO> result = new ArrayList<QuotaTariffVO>();
|
||||
Date now = _quotaManager.computeAdjustedTime(new Date());
|
||||
s_logger.info("Now=" + now.toGMTString() + " quotatype=" + cmd.getUsageType());
|
||||
if (cmd.getUsageType() != null) {
|
||||
QuotaTariffVO tariffPlan = _quotaTariffDao.findTariffPlanByUsageType(cmd.getUsageType());
|
||||
QuotaTariffVO tariffPlan = _quotaTariffDao.findTariffPlanByUsageType(cmd.getUsageType(), now);
|
||||
if (tariffPlan != null) {
|
||||
result.add(tariffPlan);
|
||||
}
|
||||
} else {
|
||||
result = _quotaTariffDao.listAllTariffPlans();
|
||||
result = _quotaTariffDao.listAllTariffPlans(now);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -224,7 +229,8 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
|
|||
TransactionLegacy.open(TransactionLegacy.USAGE_DB).close();
|
||||
final int resourceType = cmd.getUsageType();
|
||||
final BigDecimal quotaCost = new BigDecimal(cmd.getValue());
|
||||
QuotaTariffVO result = _quotaTariffDao.findTariffPlanByUsageType(resourceType);
|
||||
Date now = _quotaManager.computeAdjustedTime(new Date());
|
||||
QuotaTariffVO result = _quotaTariffDao.findTariffPlanByUsageType(resourceType, now);
|
||||
if (result == null) {
|
||||
throw new InvalidParameterValueException(String.format("Invalid Usage Resource type=%d provided", resourceType));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,10 +16,14 @@
|
|||
//under the License.
|
||||
package org.apache.cloudstack.quota;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.cloud.utils.component.Manager;
|
||||
|
||||
public interface QuotaManager extends Manager {
|
||||
|
||||
public boolean calculateQuotaUsage();
|
||||
|
||||
Date computeAdjustedTime(Date date);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ import javax.naming.ConfigurationException;
|
|||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
|
@ -104,13 +105,6 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
|
|||
boolean jobResult = false;
|
||||
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
|
||||
try {
|
||||
// get quota tariff plans
|
||||
final List<QuotaTariffVO> result = _quotaTariffDao.listAllTariffPlans();
|
||||
HashMap<Integer, QuotaTariffVO> quotaTariffMap = new HashMap<Integer, QuotaTariffVO>();
|
||||
for (final QuotaTariffVO resource : result) {
|
||||
//s_logger.debug("QuotaConf=" + resource.getDescription() + " value=" + resource.getCurrencyValue());
|
||||
quotaTariffMap.put(resource.getUsageType(), resource);
|
||||
}
|
||||
|
||||
// get all the active accounts for which there is usage
|
||||
List<AccountVO> accounts = _accountDao.listAll();
|
||||
|
|
@ -123,6 +117,15 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
|
|||
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);
|
||||
|
||||
// get quota tariff plans effective on this usage date
|
||||
final List<QuotaTariffVO> result = _quotaTariffDao.listAllTariffPlans(computeAdjustedTime(usageRecord.getEndDate()));
|
||||
HashMap<Integer, QuotaTariffVO> quotaTariffMap = new HashMap<Integer, QuotaTariffVO>();
|
||||
for (final QuotaTariffVO resource : result) {
|
||||
//s_logger.debug("QuotaConf=" + resource.getDescription() + " value=" + resource.getCurrencyValue());
|
||||
quotaTariffMap.put(resource.getUsageType(), resource);
|
||||
}
|
||||
|
||||
switch (usageRecord.getUsageType()) {
|
||||
case QuotaTypes.RUNNING_VM:
|
||||
quotalistforaccount.addAll(updateQuotaRunningVMUsage(usageRecord, quotaTariffMap, aggregationRatio));
|
||||
|
|
@ -202,7 +205,8 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
|
|||
}
|
||||
|
||||
QuotaBalanceVO newbalance = new QuotaBalanceVO(account.getAccountId(), account.getDomainId(), aggrUsage, endDate);
|
||||
//s_logger.info("Balance entry=" + aggrUsage + " on Date=" + endDate);
|
||||
// s_logger.info("Balance entry=" + aggrUsage +
|
||||
// " on Date=" + endDate);
|
||||
_quotaBalanceDao.persist(newbalance);
|
||||
aggrUsage = new BigDecimal(0);
|
||||
}
|
||||
|
|
@ -251,8 +255,10 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
|
|||
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 = _quotaDBUtils.findServiceOffering(usageRecord.getVmInstanceId(), usageRecord.getOfferingId());
|
||||
rawusage = new BigDecimal(usageRecord.getRawUsage());
|
||||
|
|
@ -305,7 +311,8 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
|
|||
BigDecimal vmusage;
|
||||
BigDecimal onehourcostforvmusage;
|
||||
onehourcostforvmusage = quotaTariffMap.get(QuotaTypes.ALLOCATED_VM).getCurrencyValue().multiply(aggregationRatio);
|
||||
//s_logger.info("Quotatariff onehourcostforvmusage=" + onehourcostforvmusage);
|
||||
// s_logger.info("Quotatariff onehourcostforvmusage=" +
|
||||
// onehourcostforvmusage);
|
||||
vmusage = new BigDecimal(usageRecord.getRawUsage()).multiply(onehourcostforvmusage);
|
||||
quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), QuotaTypes.ALLOCATED_VM, vmusage,
|
||||
usageRecord.getStartDate(), usageRecord.getEndDate());
|
||||
|
|
@ -324,7 +331,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
|
|||
BigDecimal ruleusage;
|
||||
BigDecimal onehourcost;
|
||||
onehourcost = quotaTariffMap.get(ruleType).getCurrencyValue().multiply(aggregationRatio);
|
||||
//s_logger.info("Quotatariff onehourcost=" + onehourcost);
|
||||
// s_logger.info("Quotatariff onehourcost=" + onehourcost);
|
||||
ruleusage = new BigDecimal(usageRecord.getRawUsage()).multiply(onehourcost);
|
||||
quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), ruleType, ruleusage, usageRecord.getStartDate(),
|
||||
usageRecord.getEndDate());
|
||||
|
|
@ -344,7 +351,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
|
|||
BigDecimal rawusageingb;
|
||||
BigDecimal networkusage;
|
||||
onegbcost = quotaTariffMap.get(transferType).getCurrencyValue();
|
||||
//s_logger.info("Quotatariff onegbcost=" + onegbcost);
|
||||
// s_logger.info("Quotatariff onegbcost=" + onegbcost);
|
||||
rawusageingb = new BigDecimal(usageRecord.getRawUsage()).divide(s_gb, 8, RoundingMode.HALF_EVEN);
|
||||
networkusage = rawusageingb.multiply(onegbcost);
|
||||
quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), transferType, networkusage, usageRecord.getStartDate(),
|
||||
|
|
@ -357,8 +364,29 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
|
|||
return quota_usage;
|
||||
}
|
||||
|
||||
public TimeZone getUsageTimezone() {
|
||||
return _usageTimezone;
|
||||
@Override
|
||||
public Date computeAdjustedTime(Date date) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
TimeZone localTZ = cal.getTimeZone();
|
||||
int timezoneOffset = cal.get(Calendar.ZONE_OFFSET);
|
||||
if (localTZ.inDaylightTime(date)) {
|
||||
timezoneOffset += (60 * 60 * 1000);
|
||||
}
|
||||
cal.add(Calendar.MILLISECOND, timezoneOffset);
|
||||
|
||||
Date newTime = cal.getTime();
|
||||
|
||||
Calendar calTS = Calendar.getInstance(_usageTimezone);
|
||||
calTS.setTime(newTime);
|
||||
timezoneOffset = calTS.get(Calendar.ZONE_OFFSET);
|
||||
if (_usageTimezone.inDaylightTime(date)) {
|
||||
timezoneOffset += (60 * 60 * 1000);
|
||||
}
|
||||
|
||||
calTS.add(Calendar.MILLISECOND, -1 * timezoneOffset);
|
||||
|
||||
return calTS.getTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,7 +51,6 @@ import javax.naming.ConfigurationException;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
|
@ -74,6 +73,8 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||
private QuotaBalanceDao _quotaBalanceDao;
|
||||
@Inject
|
||||
private QuotaDBUtils _quotaDBUtils;
|
||||
@Inject
|
||||
private QuotaManager _quotaManager;
|
||||
|
||||
private TimeZone _usageTimezone;
|
||||
private int _aggregationDuration = 0;
|
||||
|
|
@ -164,8 +165,7 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||
startDate = startDate == null ? new Date() : startDate;
|
||||
cmd.setStartDate(startDate);
|
||||
|
||||
TimeZone usageTZ = getUsageTimezone();
|
||||
Date adjustedStartDate = computeAdjustedTime(startDate, usageTZ);
|
||||
Date adjustedStartDate = _quotaManager.computeAdjustedTime(startDate);
|
||||
|
||||
if (endDate == null) {
|
||||
s_logger.debug("Getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", on or before " + adjustedStartDate);
|
||||
|
|
@ -178,7 +178,7 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||
return qbrecords;
|
||||
}
|
||||
} else if (startDate.before(endDate)) {
|
||||
Date adjustedEndDate = computeAdjustedTime(endDate, usageTZ);
|
||||
Date adjustedEndDate = _quotaManager.computeAdjustedTime(endDate);
|
||||
s_logger.debug("Getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", between " + adjustedStartDate + " and " + adjustedEndDate);
|
||||
List<QuotaBalanceVO> qbrecords = _quotaBalanceDao.findQuotaBalance(accountId, domainId, adjustedStartDate, adjustedEndDate);
|
||||
s_logger.info("Found records size=" + qbrecords.size());
|
||||
|
|
@ -228,9 +228,8 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||
if (startDate.after(endDate)) {
|
||||
throw new InvalidParameterValueException("Incorrect Date Range. Start date: " + startDate + " is after end date:" + endDate);
|
||||
}
|
||||
TimeZone usageTZ = getUsageTimezone();
|
||||
Date adjustedStartDate = computeAdjustedTime(startDate, usageTZ);
|
||||
Date adjustedEndDate = computeAdjustedTime(endDate, usageTZ);
|
||||
Date adjustedStartDate = _quotaManager.computeAdjustedTime(startDate);
|
||||
Date adjustedEndDate = _quotaManager.computeAdjustedTime(endDate);
|
||||
|
||||
s_logger.debug("getting quota records for account: " + accountId + ", domainId: " + domainId + ", between " + adjustedStartDate + " and " + adjustedEndDate);
|
||||
return _quotaUsageDao.findQuotaUsage(accountId, domainId, usageType, adjustedStartDate, adjustedEndDate);
|
||||
|
|
@ -239,8 +238,7 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||
@Override
|
||||
public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy) {
|
||||
Date depositDate = new Date();
|
||||
TimeZone usageTZ = getUsageTimezone();
|
||||
Date adjustedStartDate = computeAdjustedTime(depositDate, usageTZ);
|
||||
Date adjustedStartDate = _quotaManager.computeAdjustedTime(depositDate);
|
||||
QuotaBalanceVO qb = _quotaBalanceDao.findLaterBalanceEntry(accountId, domainId, adjustedStartDate);
|
||||
|
||||
if (qb != null) {
|
||||
|
|
@ -250,32 +248,5 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||
return _quotaDBUtils.addQuotaCredits(accountId, domainId, amount, updatedBy, adjustedStartDate);
|
||||
}
|
||||
|
||||
public TimeZone getUsageTimezone() {
|
||||
return _usageTimezone;
|
||||
}
|
||||
|
||||
private Date computeAdjustedTime(Date initialDate, TimeZone targetTZ) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(initialDate);
|
||||
TimeZone localTZ = cal.getTimeZone();
|
||||
int timezoneOffset = cal.get(Calendar.ZONE_OFFSET);
|
||||
if (localTZ.inDaylightTime(initialDate)) {
|
||||
timezoneOffset += (60 * 60 * 1000);
|
||||
}
|
||||
cal.add(Calendar.MILLISECOND, timezoneOffset);
|
||||
|
||||
Date newTime = cal.getTime();
|
||||
|
||||
Calendar calTS = Calendar.getInstance(targetTZ);
|
||||
calTS.setTime(newTime);
|
||||
timezoneOffset = calTS.get(Calendar.ZONE_OFFSET);
|
||||
if (targetTZ.inDaylightTime(initialDate)) {
|
||||
timezoneOffset += (60 * 60 * 1000);
|
||||
}
|
||||
|
||||
calTS.add(Calendar.MILLISECOND, -1 * timezoneOffset);
|
||||
|
||||
return calTS.getTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,11 @@ import javax.persistence.Column;
|
|||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
@Table(name = "quota_tariff")
|
||||
|
|
@ -30,6 +34,9 @@ public class QuotaTariffVO implements InternalIdentity {
|
|||
private static final long serialVersionUID = -7117933766387653203L;
|
||||
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "usage_type")
|
||||
private int usageType;
|
||||
|
||||
|
|
@ -48,20 +55,64 @@ public class QuotaTariffVO implements InternalIdentity {
|
|||
@Column(name = "include")
|
||||
private int include;
|
||||
|
||||
@Column(name = "effective_on")
|
||||
@Temporal(value = TemporalType.TIMESTAMP)
|
||||
private Date effectiveOn = null;
|
||||
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
|
||||
@Column(name = "updated_on")
|
||||
@Temporal(value = TemporalType.TIMESTAMP)
|
||||
private Date updatedOn = null;
|
||||
|
||||
@Column(name = "updated_by")
|
||||
private Long updatedBy = null;
|
||||
|
||||
public QuotaTariffVO() {
|
||||
}
|
||||
|
||||
public QuotaTariffVO(final int usagetype, final String usagename, final String usageunit, final String usagediscriminator, final BigDecimal currencyvalue, final int include, final String description) {
|
||||
public QuotaTariffVO(final int usagetype, final String usagename, final String usageunit, final String usagediscriminator, final BigDecimal currencyvalue, final int include,
|
||||
final Date effectiveOn, final Date updatedOn, final long updatedBy, final String description) {
|
||||
this.usageType = usagetype;
|
||||
this.usageName = usagename;
|
||||
this.usageUnit = usageunit;
|
||||
this.usageDiscriminator = usagediscriminator;
|
||||
this.currencyValue = currencyvalue;
|
||||
this.include = include;
|
||||
this.effectiveOn = effectiveOn;
|
||||
this.description = description;
|
||||
this.updatedOn = updatedOn;
|
||||
this.updatedBy = updatedBy;
|
||||
}
|
||||
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Date getEffectiveOn() {
|
||||
return effectiveOn;
|
||||
}
|
||||
|
||||
public void setEffectiveOn(Date effectiveOn) {
|
||||
this.effectiveOn = effectiveOn;
|
||||
}
|
||||
|
||||
public Date getUpdatedOn() {
|
||||
return updatedOn;
|
||||
}
|
||||
|
||||
public void setUpdatedOn(Date updatedOn) {
|
||||
this.updatedOn = updatedOn;
|
||||
}
|
||||
|
||||
public Long getUpdatedBy() {
|
||||
return updatedBy;
|
||||
}
|
||||
|
||||
public void setUpdatedBy(Long updatedBy) {
|
||||
this.updatedBy = updatedBy;
|
||||
}
|
||||
|
||||
public int getUsageType() {
|
||||
|
|
@ -122,6 +173,6 @@ public class QuotaTariffVO implements InternalIdentity {
|
|||
|
||||
@Override
|
||||
public long getId() {
|
||||
return this.usageType;
|
||||
return this.id;
|
||||
}
|
||||
}
|
||||
|
|
@ -17,15 +17,17 @@
|
|||
package org.apache.cloudstack.quota.dao;
|
||||
|
||||
import com.cloud.utils.db.GenericDao;
|
||||
|
||||
import org.apache.cloudstack.quota.QuotaTariffVO;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public interface QuotaTariffDao extends GenericDao<QuotaTariffVO, Long> {
|
||||
|
||||
QuotaTariffVO findTariffPlanByUsageType(int usageType);
|
||||
QuotaTariffVO findTariffPlanByUsageType(int usageType, Date onOrBefore);
|
||||
|
||||
List<QuotaTariffVO> listAllTariffPlans();
|
||||
List<QuotaTariffVO> listAllTariffPlans(Date onOrBefore);
|
||||
|
||||
boolean updateQuotaTariff(QuotaTariffVO plan);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,19 +16,29 @@
|
|||
//under the License.
|
||||
package org.apache.cloudstack.quota.dao;
|
||||
|
||||
import com.cloud.utils.db.Filter;
|
||||
import com.cloud.utils.db.GenericDaoBase;
|
||||
import com.cloud.utils.db.SearchBuilder;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
import com.cloud.utils.db.TransactionLegacy;
|
||||
|
||||
import org.apache.cloudstack.api.response.QuotaTypeResponse;
|
||||
import org.apache.cloudstack.quota.QuotaTariffVO;
|
||||
import org.apache.cloudstack.quota.QuotaTypes;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.ejb.Local;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Local(value = { QuotaTariffDao.class })
|
||||
public class QuotaTariffDaoImpl extends GenericDaoBase<QuotaTariffVO, Long> implements QuotaTariffDao {
|
||||
private static final Logger s_logger = Logger.getLogger(QuotaTariffDaoImpl.class.getName());
|
||||
|
||||
private final SearchBuilder<QuotaTariffVO> searchUsageType;
|
||||
private final SearchBuilder<QuotaTariffVO> listAllIncludedUsageType;
|
||||
|
||||
|
|
@ -40,46 +50,67 @@ public class QuotaTariffDaoImpl extends GenericDaoBase<QuotaTariffVO, Long> impl
|
|||
|
||||
listAllIncludedUsageType = createSearchBuilder();
|
||||
listAllIncludedUsageType.and("include", listAllIncludedUsageType.entity().getInclude(), SearchCriteria.Op.EQ);
|
||||
listAllIncludedUsageType.and("onorbeforedate", listAllIncludedUsageType.entity().getEffectiveOn(), SearchCriteria.Op.LTEQ);
|
||||
listAllIncludedUsageType.and("quotatype", listAllIncludedUsageType.entity().getUsageType(), SearchCriteria.Op.EQ);
|
||||
listAllIncludedUsageType.done();
|
||||
}
|
||||
|
||||
@Override
|
||||
public QuotaTariffVO findTariffPlanByUsageType(final int usageType) {
|
||||
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
QuotaTariffVO result = null;
|
||||
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
|
||||
try {
|
||||
final SearchCriteria<QuotaTariffVO> sc = searchUsageType.create();
|
||||
sc.setParameters("usage_type", usageType);
|
||||
result = findOneBy(sc);
|
||||
} finally {
|
||||
txn.close();
|
||||
}
|
||||
// Switch back
|
||||
TransactionLegacy.open(opendb).close();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<QuotaTariffVO> listAllTariffPlans() {
|
||||
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
public QuotaTariffVO findTariffPlanByUsageType(final int quotaType, final Date onOrBeforeDate) {
|
||||
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
List<QuotaTariffVO> result = null;
|
||||
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
|
||||
try {
|
||||
final Filter filter = new Filter(QuotaTariffVO.class, "effectiveOn", false, 0L, 1L);
|
||||
final SearchCriteria<QuotaTariffVO> sc = listAllIncludedUsageType.create();
|
||||
sc.setParameters("include", 1);
|
||||
result = listBy(sc);
|
||||
sc.setParameters("onorbeforedate", onOrBeforeDate);
|
||||
sc.setParameters("quotatype", quotaType);
|
||||
result = search(sc, filter);
|
||||
} finally {
|
||||
txn.close();
|
||||
}
|
||||
// Switch back
|
||||
TransactionLegacy.open(opendb).close();
|
||||
return result;
|
||||
if (result.size() > 0) {
|
||||
//s_logger.info(onOrBeforeDate.toGMTString() + "quota type " + quotaType + " , effective Date=" + result.get(0).getEffectiveOn() + " val=" + result.get(0).getCurrencyValue());
|
||||
return result.get(0);
|
||||
} else {
|
||||
s_logger.info("Missing quota type " + quotaType);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<QuotaTariffVO> listAllTariffPlans(final Date onOrBeforeDate) {
|
||||
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
List<QuotaTariffVO> tariffs = new ArrayList<QuotaTariffVO>();
|
||||
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
|
||||
try {
|
||||
final Filter filter = new Filter(QuotaTariffVO.class, "effectiveOn", false, 0L, 1L);
|
||||
final SearchCriteria<QuotaTariffVO> sc = listAllIncludedUsageType.create();
|
||||
sc.setParameters("include", 1);
|
||||
sc.setParameters("onorbeforedate", onOrBeforeDate);
|
||||
for (QuotaTypeResponse resp : QuotaTypes.listQuotaUsageTypes()) {
|
||||
sc.setParameters("quotatype", resp.getQuotaType());
|
||||
List<QuotaTariffVO> result = search(sc, filter);
|
||||
if (result.size() > 0) {
|
||||
tariffs.add(result.get(0));
|
||||
//s_logger.info(onOrBeforeDate.toGMTString() + "quota type " + resp.getDescription() + " , effective Date=" + result.get(0).getEffectiveOn() + " val="
|
||||
//+ result.get(0).getCurrencyValue());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
txn.close();
|
||||
}
|
||||
// Switch back
|
||||
TransactionLegacy.open(opendb).close();
|
||||
return tariffs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateQuotaTariff(QuotaTariffVO plan) {
|
||||
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
TransactionLegacy.open(TransactionLegacy.USAGE_DB).close(); // Switch to
|
||||
// Usage DB
|
||||
boolean result = this.update(plan.getId(), plan);
|
||||
|
|
|
|||
|
|
@ -42,41 +42,45 @@ CREATE TABLE `cloud`.`saml_token` (
|
|||
ALTER TABLE `cloud_usage`.`cloud_usage` ADD COLUMN `quota_calculated` tinyint(1) DEFAULT 0 COMMENT "quota calculation status";
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `cloud_usage`.`quota_tariff` (
|
||||
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
|
||||
`usage_type` int(2) unsigned DEFAULT NULL,
|
||||
`usage_name` varchar(255) NOT NULL COMMENT 'usage type',
|
||||
`usage_unit` varchar(255) NOT NULL COMMENT 'usage type',
|
||||
`usage_discriminator` varchar(255) NOT NULL COMMENT 'usage type',
|
||||
`currency_value` decimal(15,2) NOT NULL COMMENT 'usage type',
|
||||
`include` tinyint(1) NOT NULL COMMENT 'usage type',
|
||||
`effective_on` datetime NOT NULL COMMENT 'date time on which this quota values will become effective',
|
||||
`description` varchar(255) NOT NULL COMMENT 'usage type',
|
||||
PRIMARY KEY (`usage_type`)
|
||||
`updated_on` datetime NOT NULL COMMENT 'date this entry was updated on',
|
||||
`updated_by` bigint unsigned NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
|
||||
LOCK TABLES `cloud_usage`.`quota_tariff` WRITE;
|
||||
INSERT INTO `cloud_usage`.`quota_tariff` VALUES
|
||||
(1,'RUNNING_VM','Compute-Month','',5.00,1,'Quota tariff for running VM'),
|
||||
(2,'ALLOCATED_VM','Compute-Month','',10.00,1,'Quota tariff for allocated VM'),
|
||||
(3,'IP_ADDRESS','IP-Month','',5.12,1,'Quota tariff for IP address in use'),
|
||||
(4,'NETWORK_BYTES_SENT','GB','',1.00,1,'Quota tariff for network bytes sent'),
|
||||
(5,'NETWORK_BYTES_RECEIVED','GB','',1.00,1,'Quota tariff for network bytes received'),
|
||||
(6,'VOLUME','GB-Month','',5.00,1,'Quota tariff for volume usage per month'),
|
||||
(7,'TEMPLATE','GB-Month','',5.00,1,'Quota tariff for template usage per month'),
|
||||
(8,'ISO','GB-Month','',5.00,1,'Quota tariff for ISO storage per month'),
|
||||
(9,'SNAPSHOT','GB-Month','',5.00,1,'Quota tariff for snapshot usage per month'),
|
||||
(10,'SECURITY_GROUP','Policy-Month','',5.00,1,'Quota tariff for Security groups'),
|
||||
(11,'LOAD_BALANCER_POLICY','Policy-Month','',5.00,1,'Quota tariff load balancer policy use per hour'),
|
||||
(12,'PORT_FORWARDING_RULE','Policy-Month','',5.00,1,'Quota tariff port forwarding rule useper hour'),
|
||||
(13,'NETWORK_OFFERING','Policy-Month','',5.00,1,'Quota tariff for network offering usage per hour'),
|
||||
(14,'VPN_USERS','Policy-Month','',5.00,1,'Quota tariff for using VPN'),
|
||||
(15,'CPU_SPEED','Compute-Month','100MHz',5.00,1,'Quota tariff for 100 MHz of CPU running for an hour'),
|
||||
(16,'vCPU','Compute-Month','1VCPU',5.00,1,'Quota tariff for running VM that has 1vCPU'),
|
||||
(17,'MEMORY','Compute-Month','1MB',5.00,1,'Quota tariff for usign 1MB or RAM for 1 hour'),
|
||||
(21,'VM_DISK_IO_READ','GB','1',5.00,1,'Quota tariff for 1GB of disk IO read'),
|
||||
(22,'VM_DISK_IO_WRITE','GB','1',5.00,1,'Quota tariff for 1GB of disk data write'),
|
||||
(23,'VM_DISK_BYTES_READ','GB','1',5.00,1,'Quota tariff for disk bytes read'),
|
||||
(24,'VM_DISK_BYTES_WRITE','GB','1',5.00,1,'Quota tariff for disk bytes write'),
|
||||
(25,'VM_SNAPSHOT','GB-Month','',5.00,1,'Quota tariff for running VM');
|
||||
INSERT INTO `cloud_usage`.`quota_tariff` (`usage_type`, `usage_name`, `usage_unit`, `usage_discriminator`, `currency_value`, `include`, `effective_on`, `description`, `updated_on`, `updated_by`) VALUES
|
||||
(1,'RUNNING_VM','Compute-Month','',5.00,1,'1970-01-01','Quota tariff for running VM', '1970-01-01',1),
|
||||
(2,'ALLOCATED_VM','Compute-Month','',10.00,1,'1970-01-01','Quota tariff for allocated VM', '1970-01-01',1),
|
||||
(3,'IP_ADDRESS','IP-Month','',5.12,1,'1970-01-01','Quota tariff for IP address in use', '1970-01-01',1),
|
||||
(4,'NETWORK_BYTES_SENT','GB','',1.00,1,'1970-01-01','Quota tariff for network bytes sent', '1970-01-01',1),
|
||||
(5,'NETWORK_BYTES_RECEIVED','GB','',1.00,1,'1970-01-01','Quota tariff for network bytes received', '1970-01-01',1),
|
||||
(6,'VOLUME','GB-Month','',5.00,1,'1970-01-01','Quota tariff for volume usage per month', '1970-01-01',1),
|
||||
(7,'TEMPLATE','GB-Month','',5.00,1,'1970-01-01','Quota tariff for template usage per month', '1970-01-01',1),
|
||||
(8,'ISO','GB-Month','',5.00,1,'1970-01-01','Quota tariff for ISO storage per month', '1970-01-01',1),
|
||||
(9,'SNAPSHOT','GB-Month','',5.00,1,'1970-01-01','Quota tariff for snapshot usage per month', '1970-01-01',1),
|
||||
(10,'SECURITY_GROUP','Policy-Month','',5.00,1,'1970-01-01','Quota tariff for Security groups', '1970-01-01',1),
|
||||
(11,'LOAD_BALANCER_POLICY','Policy-Month','',5.00,1,'1970-01-01','Quota tariff load balancer policy use per hour', '1970-01-01',1),
|
||||
(12,'PORT_FORWARDING_RULE','Policy-Month','',5.00,1,'1970-01-01','Quota tariff port forwarding rule useper hour', '1970-01-01',1),
|
||||
(13,'NETWORK_OFFERING','Policy-Month','',5.00,1,'1970-01-01','Quota tariff for network offering usage per hour', '1970-01-01',1),
|
||||
(14,'VPN_USERS','Policy-Month','',5.00,1,'1970-01-01','Quota tariff for using VPN', '1970-01-01',1),
|
||||
(15,'CPU_SPEED','Compute-Month','100MHz',5.00,1,'1970-01-01','Quota tariff for 100 MHz of CPU running for an hour', '1970-01-01',1),
|
||||
(16,'vCPU','Compute-Month','1VCPU',5.00,1,'1970-01-01','Quota tariff for running VM that has 1vCPU', '1970-01-01',1),
|
||||
(17,'MEMORY','Compute-Month','1MB',5.00,1,'1970-01-01','Quota tariff for usign 1MB or RAM for 1 hour', '1970-01-01',1),
|
||||
(21,'VM_DISK_IO_READ','GB','1',5.00,1,'1970-01-01','Quota tariff for 1GB of disk IO read', '1970-01-01',1),
|
||||
(22,'VM_DISK_IO_WRITE','GB','1',5.00,1,'1970-01-01','Quota tariff for 1GB of disk data write', '1970-01-01',1),
|
||||
(23,'VM_DISK_BYTES_READ','GB','1',5.00,1,'1970-01-01','Quota tariff for disk bytes read', '1970-01-01',1),
|
||||
(24,'VM_DISK_BYTES_WRITE','GB','1',5.00,1,'1970-01-01','Quota tariff for disk bytes write', '1970-01-01',1),
|
||||
(25,'VM_SNAPSHOT','GB-Month','',5.00,1,'1970-01-01','Quota tariff for running VM', '1970-01-01',1);
|
||||
UNLOCK TABLES;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `cloud_usage`.`quota_credits` (
|
||||
|
|
|
|||
Loading…
Reference in New Issue