1818
1919import java .math .BigDecimal ;
2020import java .math .RoundingMode ;
21+ import java .time .LocalDate ;
22+ import java .time .Month ;
23+ import java .time .YearMonth ;
2124import java .util .ArrayList ;
2225import java .util .Arrays ;
2326import 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 ());
0 commit comments