Skip to content

Commit b62e20e

Browse files
author
Stephan Krug
committed
Calculate hours per month according to each month
1 parent 933970d commit b62e20e

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import java.math.BigDecimal;
2020
import java.math.RoundingMode;
21+
import java.time.LocalDate;
22+
import java.time.Month;
23+
import java.time.YearMonth;
2124
import java.util.ArrayList;
2225
import java.util.Arrays;
2326
import java.util.Date;
@@ -89,11 +92,11 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
8992

9093
private TimeZone _usageTimezone;
9194
private int _aggregationDuration = 0;
92-
93-
static final BigDecimal s_hoursInMonth = BigDecimal.valueOf(DateUtil.HOURS_IN_A_MONTH);
9495
static final BigDecimal GiB_DECIMAL = BigDecimal.valueOf(ByteScaleUtils.GiB);
9596
List<Account.Type> lockablesAccountTypes = Arrays.asList(Account.Type.NORMAL, Account.Type.DOMAIN_ADMIN);
9697

98+
static BigDecimal hoursInCurrentMonth;
99+
97100
public QuotaManagerImpl() {
98101
super();
99102
}
@@ -270,6 +273,8 @@ public boolean calculateQuotaUsage() {
270273

271274
s_logger.info(String.format("Starting quota usage calculation for accounts [%s].", accountsToString));
272275

276+
setHoursInCurrentMonth();
277+
273278
Map<Integer, Pair<List<QuotaTariffVO>, Boolean>> mapQuotaTariffsPerUsageType = createMapQuotaTariffsPerUsageType();
274279

275280
for (AccountVO account : accounts) {
@@ -533,7 +538,7 @@ protected QuotaUsageVO createQuotaUsageAccordingToUsageUnit(UsageVO usageRecord,
533538

534539
protected BigDecimal getUsageValueAccordingToUsageUnitType(UsageVO usageRecord, BigDecimal aggregatedQuotaTariffsValue, String quotaUnit) {
535540
BigDecimal rawUsage = BigDecimal.valueOf(usageRecord.getRawUsage());
536-
BigDecimal costPerHour = aggregatedQuotaTariffsValue.divide(s_hoursInMonth, 8, RoundingMode.HALF_EVEN);
541+
BigDecimal costPerHour = aggregatedQuotaTariffsValue.divide(hoursInCurrentMonth, 8, RoundingMode.HALF_EVEN);
537542

538543
switch (UsageUnitTypes.getByDescription(quotaUnit)) {
539544
case COMPUTE_MONTH:
@@ -558,6 +563,14 @@ protected BigDecimal getUsageValueAccordingToUsageUnitType(UsageVO usageRecord,
558563
}
559564
}
560565

566+
protected void setHoursInCurrentMonth() {
567+
LocalDate currentDate = LocalDate.now();
568+
Month currentMonth = currentDate.getMonth();
569+
int hoursInMonth = YearMonth.of(currentDate.getYear(), currentMonth).lengthOfMonth() * 24;
570+
hoursInCurrentMonth = new BigDecimal(hoursInMonth);
571+
s_logger.debug(String.format("Considering [%s] as the total hours in the current month [%s] for the Quota calculation.", hoursInCurrentMonth, currentMonth));
572+
}
573+
561574
@Override
562575
public boolean isLockable(AccountVO account) {
563576
return lockablesAccountTypes.contains(account.getType());

framework/quota/src/test/java/org/apache/cloudstack/quota/QuotaManagerImplTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public void getUsageValueAccordingToUsageUnitTypeTestAllTypes() {
143143
Mockito.doReturn(10.0).when(usageVoMock).getRawUsage();
144144
Mockito.doReturn(ByteScaleUtils.GiB).when(usageVoMock).getSize();
145145
BigDecimal aggregatedQuotaTariffsValue = new BigDecimal(400);
146+
quotaManagerImplSpy.hoursInCurrentMonth = new BigDecimal(720);
146147

147148
Arrays.asList(UsageUnitTypes.values()).forEach(type -> {
148149
BigDecimal result = quotaManagerImplSpy.getUsageValueAccordingToUsageUnitType(usageVoMock, aggregatedQuotaTariffsValue, type.toString());

0 commit comments

Comments
 (0)