Set number of hours per month with actual number in Quota (#7210)

Co-authored-by: Stephan Krug <stephan.krug@scclouds.com.br>
Co-authored-by: Gabriel <gabriel.fernandes@scclouds.com.br>
This commit is contained in:
Stephan Krug 2023-11-17 06:01:38 -03:00 committed by GitHub
parent 6eb04a86a6
commit db27c0ad2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View File

@ -89,11 +89,11 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
private TimeZone _usageTimezone;
private int _aggregationDuration = 0;
static final BigDecimal s_hoursInMonth = BigDecimal.valueOf(DateUtil.HOURS_IN_A_MONTH);
static final BigDecimal GiB_DECIMAL = BigDecimal.valueOf(ByteScaleUtils.GiB);
List<Account.Type> lockablesAccountTypes = Arrays.asList(Account.Type.NORMAL, Account.Type.DOMAIN_ADMIN);
static BigDecimal hoursInCurrentMonth;
public QuotaManagerImpl() {
super();
}
@ -533,7 +533,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
protected BigDecimal getUsageValueAccordingToUsageUnitType(UsageVO usageRecord, BigDecimal aggregatedQuotaTariffsValue, String quotaUnit) {
BigDecimal rawUsage = BigDecimal.valueOf(usageRecord.getRawUsage());
BigDecimal costPerHour = aggregatedQuotaTariffsValue.divide(s_hoursInMonth, 8, RoundingMode.HALF_EVEN);
BigDecimal costPerHour = getCostPerHour(aggregatedQuotaTariffsValue, usageRecord.getStartDate());
switch (UsageUnitTypes.getByDescription(quotaUnit)) {
case COMPUTE_MONTH:
@ -558,6 +558,12 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
}
}
protected BigDecimal getCostPerHour(BigDecimal costPerMonth, Date date) {
BigDecimal hoursInCurrentMonth = BigDecimal.valueOf(DateUtil.getHoursInCurrentMonth(date));
s_logger.trace(String.format("Dividing tariff cost per month [%s] by [%s] to get the tariffs cost per hour.", costPerMonth, hoursInCurrentMonth));
return costPerMonth.divide(hoursInCurrentMonth, 8, RoundingMode.HALF_EVEN);
}
@Override
public boolean isLockable(AccountVO account) {
return lockablesAccountTypes.contains(account.getType());

View File

@ -142,8 +142,10 @@ public class QuotaManagerImplTest {
public void getUsageValueAccordingToUsageUnitTypeTestAllTypes() {
Mockito.doReturn(10.0).when(usageVoMock).getRawUsage();
Mockito.doReturn(ByteScaleUtils.GiB).when(usageVoMock).getSize();
Mockito.doReturn(new Date(0, 8, 10)).when(usageVoMock).getStartDate();
BigDecimal aggregatedQuotaTariffsValue = new BigDecimal(400);
Arrays.asList(UsageUnitTypes.values()).forEach(type -> {
BigDecimal result = quotaManagerImplSpy.getUsageValueAccordingToUsageUnitType(usageVoMock, aggregatedQuotaTariffsValue, type.toString());
Double expected = null;

View File

@ -25,6 +25,7 @@ import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.YearMonth;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
@ -336,4 +337,8 @@ public class DateUtil {
.withSecond(localDateTime.getSecond());
return zonedDate;
}
public static int getHoursInCurrentMonth(Date date) {
return YearMonth.of(date.getYear(), date.getMonth() + 1).lengthOfMonth() * 24;
}
}