49
49
import org .apache .cloudstack .utils .bytescale .ByteScaleUtils ;
50
50
import org .apache .cloudstack .utils .jsinterpreter .JsInterpreter ;
51
51
import org .apache .cloudstack .utils .reflectiontostringbuilderutils .ReflectionToStringBuilderUtils ;
52
- import org .apache .cloudstack .utils .usage .UsageUtils ;
53
52
import org .apache .commons .collections .CollectionUtils ;
54
53
import org .apache .commons .lang3 .BooleanUtils ;
54
+ import org .apache .commons .lang3 .ObjectUtils ;
55
55
import org .apache .commons .lang3 .StringUtils ;
56
56
import org .apache .commons .lang3 .math .NumberUtils ;
57
57
import org .springframework .stereotype .Component ;
@@ -85,8 +85,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
85
85
@ Inject
86
86
protected PresetVariableHelper presetVariableHelper ;
87
87
88
- private TimeZone _usageTimezone ;
89
- private int _aggregationDuration = 0 ;
88
+ private static TimeZone usageAggregationTimeZone = TimeZone .getTimeZone ("GMT" );
90
89
static final BigDecimal GiB_DECIMAL = BigDecimal .valueOf (ByteScaleUtils .GiB );
91
90
List <Account .Type > lockablesAccountTypes = Arrays .asList (Account .Type .NORMAL , Account .Type .DOMAIN_ADMIN );
92
91
@@ -112,24 +111,16 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
112
111
mergeConfigs (configs , params );
113
112
}
114
113
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 );
129
116
130
117
return true ;
131
118
}
132
119
120
+ public static TimeZone getUsageAggregationTimeZone () {
121
+ return usageAggregationTimeZone ;
122
+ }
123
+
133
124
@ Override
134
125
public boolean start () {
135
126
if (logger .isInfoEnabled ()) {
@@ -158,8 +149,9 @@ protected void processQuotaBalanceForAccount(AccountVO accountVo, List<QuotaUsag
158
149
Date startDate = firstQuotaUsage .getStartDate ();
159
150
Date endDate = firstQuotaUsage .getStartDate ();
160
151
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 ()));
163
155
164
156
BigDecimal aggregatedUsage = BigDecimal .ZERO ;
165
157
long accountId = accountVo .getAccountId ();
@@ -246,17 +238,20 @@ protected void saveQuotaAccount(long accountId, BigDecimal aggregatedUsage, Date
246
238
247
239
protected BigDecimal aggregateCreditBetweenDates (Long accountId , Long domainId , Date startDate , Date endDate , String accountToString ) {
248
240
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 ));
250
243
251
244
BigDecimal aggregatedUsage = BigDecimal .ZERO ;
252
245
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 ));
254
248
255
249
for (QuotaBalanceVO credit : creditsReceived ) {
256
250
aggregatedUsage = aggregatedUsage .add (credit .getCreditBalance ());
257
251
}
258
252
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 );
260
255
261
256
return aggregatedUsage ;
262
257
}
@@ -306,15 +301,15 @@ protected List<UsageVO> getPendingUsageRecordsForQuotaAggregation(AccountVO acco
306
301
protected List <QuotaUsageVO > createQuotaUsagesAccordingToQuotaTariffs (AccountVO account , List <UsageVO > usageRecords ,
307
302
Map <Integer , Pair <List <QuotaTariffVO >, Boolean >> mapQuotaTariffsPerUsageType ) {
308
303
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 );
310
305
311
306
List <Pair <UsageVO , QuotaUsageVO >> pairsUsageAndQuotaUsage = new ArrayList <>();
312
307
313
308
try (JsInterpreter jsInterpreter = new JsInterpreter (QuotaConfig .QuotaActivationRuleTimeout .value ())) {
314
309
for (UsageVO usageRecord : usageRecords ) {
315
310
int usageType = usageRecord .getUsageType ();
316
311
317
- if (Boolean . FALSE . equals ( shouldCalculateUsageRecord (account ,usageRecord ) )) {
312
+ if (! shouldCalculateUsageRecord (account , usageRecord )) {
318
313
pairsUsageAndQuotaUsage .add (new Pair <>(usageRecord , null ));
319
314
continue ;
320
315
}
@@ -339,8 +334,8 @@ protected List<QuotaUsageVO> createQuotaUsagesAccordingToQuotaTariffs(AccountVO
339
334
340
335
protected boolean shouldCalculateUsageRecord (AccountVO accountVO , UsageVO usageRecord ) {
341
336
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 ());
344
339
return false ;
345
340
}
346
341
return true ;
@@ -366,9 +361,8 @@ protected List<QuotaUsageVO> persistUsagesAndQuotaUsagesAndRetrievePersistedQuot
366
361
367
362
protected BigDecimal aggregateQuotaTariffsValues (UsageVO usageRecord , List <QuotaTariffVO > quotaTariffs , boolean hasAnyQuotaTariffWithActivationRule ,
368
363
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 ());
372
366
373
367
PresetVariables presetVariables = getPresetVariables (hasAnyQuotaTariffWithActivationRule , usageRecord );
374
368
BigDecimal aggregatedQuotaTariffsValue = BigDecimal .ZERO ;
@@ -406,7 +400,7 @@ protected PresetVariables getPresetVariables(boolean hasAnyQuotaTariffWithActiva
406
400
protected BigDecimal getQuotaTariffValueToBeApplied (QuotaTariffVO quotaTariff , JsInterpreter jsInterpreter , PresetVariables presetVariables ) {
407
401
String activationRule = quotaTariff .getActivationRule ();
408
402
BigDecimal quotaTariffValue = quotaTariff .getCurrencyValue ();
409
- String quotaTariffToString = quotaTariff .toString ();
403
+ String quotaTariffToString = quotaTariff .toString (usageAggregationTimeZone );
410
404
411
405
if (StringUtils .isEmpty (activationRule )) {
412
406
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
468
462
Date quotaTariffEndDate = quotaTariff .getEndDate ();
469
463
470
464
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 ));
475
470
476
471
return false ;
477
472
}
@@ -497,11 +492,11 @@ protected Map<Integer, Pair<List<QuotaTariffVO>, Boolean>> createMapQuotaTariffs
497
492
}
498
493
499
494
protected QuotaUsageVO createQuotaUsageAccordingToUsageUnit (UsageVO usageRecord , BigDecimal aggregatedQuotaTariffsValue , String accountToString ) {
500
- String usageRecordToString = usageRecord .toString ();
495
+ String usageRecordToString = usageRecord .toString (usageAggregationTimeZone );
501
496
502
497
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 );
505
500
return null ;
506
501
}
507
502
0 commit comments