4949import org .apache .cloudstack .utils .bytescale .ByteScaleUtils ;
5050import org .apache .cloudstack .utils .jsinterpreter .JsInterpreter ;
5151import org .apache .cloudstack .utils .reflectiontostringbuilderutils .ReflectionToStringBuilderUtils ;
52- import org .apache .cloudstack .utils .usage .UsageUtils ;
5352import org .apache .commons .collections .CollectionUtils ;
5453import org .apache .commons .lang3 .BooleanUtils ;
54+ import org .apache .commons .lang3 .ObjectUtils ;
5555import org .apache .commons .lang3 .StringUtils ;
5656import org .apache .commons .lang3 .math .NumberUtils ;
5757import org .springframework .stereotype .Component ;
@@ -85,8 +85,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
8585 @ Inject
8686 protected PresetVariableHelper presetVariableHelper ;
8787
88- private TimeZone _usageTimezone ;
89- private int _aggregationDuration = 0 ;
88+ private static TimeZone usageAggregationTimeZone = TimeZone .getTimeZone ("GMT" );
9089 static final BigDecimal GiB_DECIMAL = BigDecimal .valueOf (ByteScaleUtils .GiB );
9190 List <Account .Type > lockablesAccountTypes = Arrays .asList (Account .Type .NORMAL , Account .Type .DOMAIN_ADMIN );
9291
@@ -112,24 +111,16 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
112111 mergeConfigs (configs , params );
113112 }
114113
115- String aggregationRange = configs .get ("usage.stats.job.aggregation.range" );
116- String timeZoneStr = configs .get ("usage.aggregation.timezone" );
117-
118- if (timeZoneStr == null ) {
119- timeZoneStr = "GMT" ;
120- }
121- _usageTimezone = TimeZone .getTimeZone (timeZoneStr );
122-
123- _aggregationDuration = Integer .parseInt (aggregationRange );
124- if (_aggregationDuration < UsageUtils .USAGE_AGGREGATION_RANGE_MIN ) {
125- logger .warn ("Usage stats job aggregation range is to small, using the minimum value of " + UsageUtils .USAGE_AGGREGATION_RANGE_MIN );
126- _aggregationDuration = UsageUtils .USAGE_AGGREGATION_RANGE_MIN ;
127- }
128- logger .info ("Usage timezone = " + _usageTimezone + " AggregationDuration=" + _aggregationDuration );
114+ String usageAggregationTimeZoneStr = ObjectUtils .defaultIfNull (configs .get ("usage.aggregation.timezone" ), "GMT" );
115+ usageAggregationTimeZone = TimeZone .getTimeZone (usageAggregationTimeZoneStr );
129116
130117 return true ;
131118 }
132119
120+ public static TimeZone getUsageAggregationTimeZone () {
121+ return usageAggregationTimeZone ;
122+ }
123+
133124 @ Override
134125 public boolean start () {
135126 if (logger .isInfoEnabled ()) {
@@ -158,8 +149,9 @@ protected void processQuotaBalanceForAccount(AccountVO accountVo, List<QuotaUsag
158149 Date startDate = firstQuotaUsage .getStartDate ();
159150 Date endDate = firstQuotaUsage .getStartDate ();
160151
161- logger .info (String .format ("Processing quota balance for account [%s] between [%s] and [%s]." , accountToString , startDate ,
162- accountQuotaUsages .get (accountQuotaUsages .size () - 1 ).getEndDate ()));
152+ logger .info ("Processing quota balance for account [{}] between [{}] and [{}]." , accountToString ,
153+ DateUtil .displayDateInTimezone (usageAggregationTimeZone , startDate ),
154+ DateUtil .displayDateInTimezone (usageAggregationTimeZone , accountQuotaUsages .get (accountQuotaUsages .size () - 1 ).getEndDate ()));
163155
164156 BigDecimal aggregatedUsage = BigDecimal .ZERO ;
165157 long accountId = accountVo .getAccountId ();
@@ -246,17 +238,20 @@ protected void saveQuotaAccount(long accountId, BigDecimal aggregatedUsage, Date
246238
247239 protected BigDecimal aggregateCreditBetweenDates (Long accountId , Long domainId , Date startDate , Date endDate , String accountToString ) {
248240 List <QuotaBalanceVO > creditsReceived = _quotaBalanceDao .findCreditBalance (accountId , domainId , startDate , endDate );
249- logger .debug (String .format ("Account [%s] has [%s] credit entries before [%s]." , accountToString , creditsReceived .size (), endDate ));
241+ logger .debug ("Account [{}] has [{}] credit entries before [{}]." , accountToString , creditsReceived .size (),
242+ DateUtil .displayDateInTimezone (usageAggregationTimeZone , endDate ));
250243
251244 BigDecimal aggregatedUsage = BigDecimal .ZERO ;
252245
253- logger .debug (String .format ("Aggregating the account [%s] credit entries before [%s]." , accountToString , endDate ));
246+ logger .debug ("Aggregating the account [{}] credit entries before [{}]." , accountToString ,
247+ DateUtil .displayDateInTimezone (usageAggregationTimeZone , endDate ));
254248
255249 for (QuotaBalanceVO credit : creditsReceived ) {
256250 aggregatedUsage = aggregatedUsage .add (credit .getCreditBalance ());
257251 }
258252
259- logger .debug (String .format ("The aggregation of the account [%s] credit entries before [%s] resulted in the value [%s]." , accountToString , endDate , aggregatedUsage ));
253+ logger .debug ("The aggregation of the account [{}] credit entries before [{}] resulted in the value [{}]." ,
254+ accountToString , DateUtil .displayDateInTimezone (usageAggregationTimeZone , endDate ), aggregatedUsage );
260255
261256 return aggregatedUsage ;
262257 }
@@ -306,15 +301,15 @@ protected List<UsageVO> getPendingUsageRecordsForQuotaAggregation(AccountVO acco
306301 protected List <QuotaUsageVO > createQuotaUsagesAccordingToQuotaTariffs (AccountVO account , List <UsageVO > usageRecords ,
307302 Map <Integer , Pair <List <QuotaTariffVO >, Boolean >> mapQuotaTariffsPerUsageType ) {
308303 String accountToString = account .reflectionToString ();
309- logger .info (String . format ( "Calculating quota usage of [%s ] usage records for account [%s ]." , usageRecords .size (), accountToString ) );
304+ logger .info ("Calculating quota usage of [{} ] usage records for account [{} ]." , usageRecords .size (), accountToString );
310305
311306 List <Pair <UsageVO , QuotaUsageVO >> pairsUsageAndQuotaUsage = new ArrayList <>();
312307
313308 try (JsInterpreter jsInterpreter = new JsInterpreter (QuotaConfig .QuotaActivationRuleTimeout .value ())) {
314309 for (UsageVO usageRecord : usageRecords ) {
315310 int usageType = usageRecord .getUsageType ();
316311
317- if (Boolean . FALSE . equals ( shouldCalculateUsageRecord (account ,usageRecord ) )) {
312+ if (! shouldCalculateUsageRecord (account , usageRecord )) {
318313 pairsUsageAndQuotaUsage .add (new Pair <>(usageRecord , null ));
319314 continue ;
320315 }
@@ -339,8 +334,8 @@ protected List<QuotaUsageVO> createQuotaUsagesAccordingToQuotaTariffs(AccountVO
339334
340335 protected boolean shouldCalculateUsageRecord (AccountVO accountVO , UsageVO usageRecord ) {
341336 if (Boolean .FALSE .equals (QuotaConfig .QuotaAccountEnabled .valueIn (accountVO .getAccountId ()))) {
342- logger .debug (String . format ( "Considering usage record [%s ] as calculated and skipping it because account [%s ] has the quota plugin disabled." ,
343- usageRecord , accountVO .reflectionToString () ));
337+ logger .debug ("Considering usage record [{} ] as calculated and skipping it because account [{} ] has the quota plugin disabled." ,
338+ usageRecord . toString ( usageAggregationTimeZone ) , accountVO .reflectionToString ());
344339 return false ;
345340 }
346341 return true ;
@@ -366,9 +361,8 @@ protected List<QuotaUsageVO> persistUsagesAndQuotaUsagesAndRetrievePersistedQuot
366361
367362 protected BigDecimal aggregateQuotaTariffsValues (UsageVO usageRecord , List <QuotaTariffVO > quotaTariffs , boolean hasAnyQuotaTariffWithActivationRule ,
368363 JsInterpreter jsInterpreter , String accountToString ) {
369- String usageRecordToString = usageRecord .toString ();
370- logger .debug (String .format ("Validating usage record [%s] for account [%s] against [%s] quota tariffs." , usageRecordToString , accountToString ,
371- quotaTariffs .size ()));
364+ String usageRecordToString = usageRecord .toString (usageAggregationTimeZone );
365+ logger .debug ("Validating usage record [{}] for account [{}] against [{}] quota tariffs." , usageRecordToString , accountToString , quotaTariffs .size ());
372366
373367 PresetVariables presetVariables = getPresetVariables (hasAnyQuotaTariffWithActivationRule , usageRecord );
374368 BigDecimal aggregatedQuotaTariffsValue = BigDecimal .ZERO ;
@@ -406,7 +400,7 @@ protected PresetVariables getPresetVariables(boolean hasAnyQuotaTariffWithActiva
406400 protected BigDecimal getQuotaTariffValueToBeApplied (QuotaTariffVO quotaTariff , JsInterpreter jsInterpreter , PresetVariables presetVariables ) {
407401 String activationRule = quotaTariff .getActivationRule ();
408402 BigDecimal quotaTariffValue = quotaTariff .getCurrencyValue ();
409- String quotaTariffToString = quotaTariff .toString ();
403+ String quotaTariffToString = quotaTariff .toString (usageAggregationTimeZone );
410404
411405 if (StringUtils .isEmpty (activationRule )) {
412406 logger .debug (String .format ("Quota tariff [%s] does not have an activation rule, therefore we will use the quota tariff value [%s] in the calculation." ,
@@ -468,10 +462,11 @@ protected boolean isQuotaTariffInPeriodToBeApplied(UsageVO usageRecord, QuotaTar
468462 Date quotaTariffEndDate = quotaTariff .getEndDate ();
469463
470464 if ((quotaTariffEndDate != null && usageRecordStartDate .after (quotaTariffEndDate )) || usageRecordEndDate .before (quotaTariffStartDate )) {
471- logger .debug (String .format ("Not applying quota tariff [%s] in usage record [%s] of account [%s] due to it is out of the period to be applied. Period of the usage"
472- + " record [startDate: %s, endDate: %s], period of the quota tariff [startDate: %s, endDate: %s]." , quotaTariff , usageRecord .toString (), accountToString ,
473- DateUtil .getOutputString (usageRecordStartDate ), DateUtil .getOutputString (usageRecordEndDate ), DateUtil .getOutputString (quotaTariffStartDate ),
474- DateUtil .getOutputString (quotaTariffEndDate )));
465+ logger .debug ("Not applying quota tariff [{}] in usage record [{}] of account [{}] due to it is out of the period to be applied. Period of the usage"
466+ + " record [startDate: {}, endDate: {}], period of the quota tariff [startDate: {}, endDate: {}]." , quotaTariff .toString (usageAggregationTimeZone ),
467+ usageRecord .toString (usageAggregationTimeZone ), accountToString , DateUtil .displayDateInTimezone (usageAggregationTimeZone , usageRecordStartDate ),
468+ DateUtil .displayDateInTimezone (usageAggregationTimeZone , usageRecordEndDate ), DateUtil .displayDateInTimezone (usageAggregationTimeZone , quotaTariffStartDate ),
469+ DateUtil .displayDateInTimezone (usageAggregationTimeZone , quotaTariffEndDate ));
475470
476471 return false ;
477472 }
@@ -497,11 +492,11 @@ protected Map<Integer, Pair<List<QuotaTariffVO>, Boolean>> createMapQuotaTariffs
497492 }
498493
499494 protected QuotaUsageVO createQuotaUsageAccordingToUsageUnit (UsageVO usageRecord , BigDecimal aggregatedQuotaTariffsValue , String accountToString ) {
500- String usageRecordToString = usageRecord .toString ();
495+ String usageRecordToString = usageRecord .toString (usageAggregationTimeZone );
501496
502497 if (aggregatedQuotaTariffsValue .equals (BigDecimal .ZERO )) {
503- logger .debug (String . format ( "Usage record [%s] for account [%s] does not have quota tariffs to be calculated, therefore we will mark it as calculated." ,
504- usageRecordToString , accountToString )) ;
498+ logger .debug ("No tariffs were applied to usage record [{}] of account [{}] or they resulted in 0; We will only mark the usage record as calculated." ,
499+ usageRecordToString , accountToString );
505500 return null ;
506501 }
507502
0 commit comments