2016-01-29 15:54:16 +01:00
package org.gcube.accounting.aggregator.plugin ;
2017-05-10 15:01:46 +02:00
import java.text.DateFormat ;
2016-07-29 10:48:26 +02:00
import java.text.SimpleDateFormat ;
2017-05-10 15:01:46 +02:00
import java.util.Date ;
2016-01-29 15:54:16 +01:00
import java.util.Map ;
2018-02-20 18:26:37 +01:00
import org.gcube.accounting.aggregator.RegexRulesAggregator ;
2017-09-05 17:13:50 +02:00
import org.gcube.accounting.aggregator.aggregation.AggregationType ;
import org.gcube.accounting.aggregator.elaboration.AggregatorManager ;
import org.gcube.accounting.aggregator.elaboration.RecoveryManager ;
import org.gcube.accounting.aggregator.utility.Utility ;
import org.gcube.accounting.datamodel.UsageRecord ;
2016-07-29 10:48:26 +02:00
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord ;
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord ;
2021-03-25 12:38:28 +01:00
import org.gcube.documentstore.persistence.PersistenceBackendFactory ;
2017-09-05 17:13:50 +02:00
import org.gcube.documentstore.records.Record ;
2016-07-29 10:48:26 +02:00
import org.gcube.documentstore.records.RecordUtility ;
2016-01-29 15:54:16 +01:00
import org.gcube.vremanagement.executor.plugin.Plugin ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
2017-09-05 17:13:50 +02:00
* @author Alessandro Pieve ( ISTI - CNR )
* @author Luca Frosini ( ISTI - CNR )
2016-01-29 15:54:16 +01:00
* /
2020-09-30 12:12:22 +02:00
public class AccountingAggregatorPlugin extends Plugin {
2016-01-29 15:54:16 +01:00
private static Logger logger = LoggerFactory . getLogger ( AccountingAggregatorPlugin . class ) ;
2016-07-29 10:48:26 +02:00
2017-09-05 17:13:50 +02:00
static {
/// One Record per package is enough
RecordUtility . addRecordPackage ( ServiceUsageRecord . class . getPackage ( ) ) ;
RecordUtility . addRecordPackage ( AggregatedServiceUsageRecord . class . getPackage ( ) ) ;
}
2020-09-30 12:12:22 +02:00
public static final String NAME = " Accounting-Aggregator-Plugin " ;
2017-09-05 17:13:50 +02:00
/ * *
* Key to indicate { @link AggregationType }
* /
public static final String AGGREGATION_TYPE_INPUT_PARAMETER = " aggregationType " ;
/ * *
* Day is ignored for MONTHLY aggregation
* Month and Day are ignored for YEARLY aggregation
* /
public static final String AGGREGATION_START_DATE_INPUT_PARAMETER = " aggregationStartDate " ;
2017-09-22 14:30:48 +02:00
public static final String AGGREGATION_END_DATE_INPUT_PARAMETER = " aggregationEndDate " ;
2017-09-05 17:13:50 +02:00
public static final String RESTART_FROM_LAST_AGGREGATION_DATE_INPUT_PARAMETER = " restartFromLastAggregationDate " ;
public static final String AGGREGATION_START_DATE_DATE_FORMAT_PATTERN = " yyyy/MM/dd " ;
public static final DateFormat AGGREGATION_START_DATE_DATE_FORMAT ;
2017-09-22 14:30:48 +02:00
private static final String AGGREGATION_START_END_DATE_UTC_DATE_FORMAT_PATTERN = " yyyy/MM/dd Z " ;
private static final DateFormat AGGREGATION_START_END_DATE_UTC_DATE_FORMAT ;
2017-09-05 17:13:50 +02:00
private static final String UTC = " +0000 " ;
2018-02-22 14:49:54 +01:00
public static final String FORCE_EARLY_AGGREGATION = " forceEarlyAggregation " ;
public static final String FORCE_RERUN = " forceRerun " ;
public static final String FORCE_RESTART = " forceRestart " ;
2017-09-05 17:13:50 +02:00
public enum ElaborationType {
AGGREGATE , // Aggregate
RECOVERY // Recover unterminated executions
}
2018-02-22 14:49:54 +01:00
2017-09-05 17:13:50 +02:00
/ * *
* Indicate which types of elaboration the plugin must perform
* /
public static final String ELABORATION_TYPE_INPUT_PARAMETER = " elaborationType " ;
2021-11-11 14:29:09 +01:00
// public static final String PERSIST_START_TIME_INPUT_PARAMETER = "persistStartTime";
// public static final String PERSIST_END_TIME_INPUT_PARAMETER = "persistEndTime";
// public static final String PERSIST_TIME_DATE_FORMAT_PATTERN = "HH:mm";
// public static final DateFormat PERSIST_TIME_DATE_FORMAT;
2017-09-05 17:13:50 +02:00
public static final String LOCAL_TIME_DATE_FORMAT_PATTERN = " HH:mm Z " ;
public static final DateFormat LOCAL_TIME_DATE_FORMAT ;
public static final String RECORD_TYPE_INPUT_PARAMETER = Record . RECORD_TYPE ;
static {
AGGREGATION_START_DATE_DATE_FORMAT = Utility . getUTCDateFormat ( AGGREGATION_START_DATE_DATE_FORMAT_PATTERN ) ;
2017-09-22 14:30:48 +02:00
AGGREGATION_START_END_DATE_UTC_DATE_FORMAT = Utility . getUTCDateFormat ( AGGREGATION_START_END_DATE_UTC_DATE_FORMAT_PATTERN ) ;
2017-09-05 17:13:50 +02:00
2021-11-11 14:29:09 +01:00
// PERSIST_TIME_DATE_FORMAT = new SimpleDateFormat(PERSIST_TIME_DATE_FORMAT_PATTERN);
2017-09-05 17:13:50 +02:00
LOCAL_TIME_DATE_FORMAT = new SimpleDateFormat ( LOCAL_TIME_DATE_FORMAT_PATTERN ) ;
}
2017-01-25 15:06:16 +01:00
2020-09-30 12:12:22 +02:00
public AccountingAggregatorPlugin ( ) {
super ( ) ;
2018-02-20 18:26:37 +01:00
RegexRulesAggregator . getInstance ( ) ;
2016-09-30 14:44:58 +02:00
}
2016-07-29 10:48:26 +02:00
2021-11-11 14:29:09 +01:00
// private Date getPersistTime(Map<String, Object> inputs, String parameterName) throws ParseException{
// Date persistTime = null;
// if (inputs.containsKey(parameterName)) {
// String persistTimeString = (String) inputs.get(parameterName);
// persistTime = Utility.getPersistTimeDate(persistTimeString);
// }
//
// if(persistTime==null){
// throw new IllegalArgumentException("Please set a valid '" + parameterName +"' by using " + PERSIST_TIME_DATE_FORMAT_PATTERN + " format.");
// }
//
// return persistTime;
// }
2017-09-05 17:13:50 +02:00
/** {@inheritDoc} */
@SuppressWarnings ( " unchecked " )
@Override
public void launch ( Map < String , Object > inputs ) throws Exception {
2021-03-25 12:38:28 +01:00
PersistenceBackendFactory . getPersistenceBackend ( Utility . getCurrentContext ( ) ) ;
2017-09-05 17:13:50 +02:00
AggregationType aggregationType = null ;
Date aggregationStartDate = null ;
2017-09-22 14:30:48 +02:00
Date aggregationEndDate = null ;
2017-09-05 17:13:50 +02:00
boolean restartFromLastAggregationDate = false ;
ElaborationType elaborationType = ElaborationType . AGGREGATE ;
2021-05-06 13:13:30 +02:00
String recordType = null ;
2017-09-05 17:13:50 +02:00
Class < ? extends UsageRecord > usageRecordClass = null ;
2018-02-22 14:49:54 +01:00
boolean forceEarlyAggregation = false ;
boolean forceRerun = false ;
boolean forceRestart = false ;
2017-09-05 17:13:50 +02:00
if ( inputs = = null | | inputs . isEmpty ( ) ) {
throw new IllegalArgumentException ( " The can only be launched providing valid input parameters " ) ;
2017-01-25 15:06:16 +01:00
}
2017-02-21 14:17:41 +01:00
2017-09-05 17:13:50 +02:00
if ( inputs . containsKey ( ELABORATION_TYPE_INPUT_PARAMETER ) ) {
elaborationType = ElaborationType . valueOf ( ( String ) inputs . get ( ELABORATION_TYPE_INPUT_PARAMETER ) ) ;
2016-07-29 10:48:26 +02:00
}
2017-05-10 15:01:46 +02:00
2021-11-11 14:29:09 +01:00
// Date persistStartTime = getPersistTime(inputs, PERSIST_START_TIME_INPUT_PARAMETER);
// Date persistEndTime = getPersistTime(inputs, PERSIST_END_TIME_INPUT_PARAMETER);
2017-09-05 17:13:50 +02:00
2017-09-22 14:30:48 +02:00
if ( inputs . containsKey ( AGGREGATION_START_DATE_INPUT_PARAMETER ) ) {
String aggregationStartDateString = ( String ) inputs . get ( AGGREGATION_START_DATE_INPUT_PARAMETER ) ;
2021-11-08 14:57:35 +01:00
aggregationStartDateString = aggregationStartDateString + " " + UTC ;
aggregationStartDate = AGGREGATION_START_END_DATE_UTC_DATE_FORMAT . parse ( aggregationStartDateString ) ;
2017-09-22 14:30:48 +02:00
}
if ( inputs . containsKey ( AGGREGATION_END_DATE_INPUT_PARAMETER ) ) {
String aggregationEndDateString = ( String ) inputs . get ( AGGREGATION_END_DATE_INPUT_PARAMETER ) ;
2021-11-08 14:57:35 +01:00
aggregationEndDateString = aggregationEndDateString + " " + UTC ;
aggregationEndDate = AGGREGATION_START_END_DATE_UTC_DATE_FORMAT . parse ( aggregationEndDateString ) ;
2017-09-22 14:30:48 +02:00
}
2021-11-09 11:53:29 +01:00
if ( inputs . containsKey ( FORCE_RESTART ) ) {
forceRestart = ( boolean ) inputs . get ( FORCE_RESTART ) ;
}
if ( inputs . containsKey ( AGGREGATION_TYPE_INPUT_PARAMETER ) ) {
aggregationType = AggregationType . valueOf ( ( String ) inputs . get ( AGGREGATION_TYPE_INPUT_PARAMETER ) ) ;
}
if ( inputs . containsKey ( RECORD_TYPE_INPUT_PARAMETER ) ) {
recordType = ( String ) inputs . get ( RECORD_TYPE_INPUT_PARAMETER ) ;
usageRecordClass = ( Class < ? extends UsageRecord > ) RecordUtility . getRecordClass ( recordType ) ;
logger . debug ( " Requested record type is {} which is implemented by {} " , recordType , usageRecordClass ) ;
}
2017-09-05 17:13:50 +02:00
switch ( elaborationType ) {
case AGGREGATE :
if ( ! inputs . containsKey ( AGGREGATION_TYPE_INPUT_PARAMETER ) ) {
throw new IllegalArgumentException ( " Please set required parameter ' " + AGGREGATION_TYPE_INPUT_PARAMETER + " ' " ) ;
}
2017-05-12 11:56:53 +02:00
2017-09-05 17:13:50 +02:00
if ( inputs . containsKey ( RESTART_FROM_LAST_AGGREGATION_DATE_INPUT_PARAMETER ) ) {
restartFromLastAggregationDate = ( boolean ) inputs . get ( RESTART_FROM_LAST_AGGREGATION_DATE_INPUT_PARAMETER ) ;
2017-05-10 15:01:46 +02:00
}
2017-09-05 17:13:50 +02:00
2018-02-22 14:49:54 +01:00
if ( inputs . containsKey ( FORCE_EARLY_AGGREGATION ) ) {
forceEarlyAggregation = ( boolean ) inputs . get ( FORCE_EARLY_AGGREGATION ) ;
}
if ( inputs . containsKey ( FORCE_RERUN ) ) {
forceRerun = ( boolean ) inputs . get ( FORCE_RERUN ) ;
}
2017-09-05 17:13:50 +02:00
if ( restartFromLastAggregationDate = = false & & aggregationStartDate = = null ) {
throw new IllegalArgumentException ( " Aggregation Start Date cannot be found. Please provide it as parameter or set ' " + RESTART_FROM_LAST_AGGREGATION_DATE_INPUT_PARAMETER + " ' input parameter to 'true'. " ) ;
}
2017-09-22 14:30:48 +02:00
AggregatorManager aggregatorManager = new AggregatorManager ( aggregationType , restartFromLastAggregationDate , aggregationStartDate , aggregationEndDate ) ;
2018-02-22 14:49:54 +01:00
aggregatorManager . setForceEarlyAggregation ( forceEarlyAggregation ) ;
aggregatorManager . setForceRerun ( forceRerun ) ;
aggregatorManager . setForceRestart ( forceRestart ) ;
2021-11-11 14:29:09 +01:00
// aggregatorManager.elaborate(persistStartTime, persistEndTime, recordType);
aggregatorManager . elaborate ( recordType ) ;
2016-07-29 10:48:26 +02:00
break ;
2017-09-05 17:13:50 +02:00
case RECOVERY :
2021-11-11 14:29:09 +01:00
// RecoveryManager recoveryManager = new RecoveryManager(persistStartTime, persistEndTime, aggregationStartDate, aggregationEndDate);
RecoveryManager recoveryManager = new RecoveryManager ( aggregationStartDate , aggregationEndDate ) ;
2021-11-09 11:53:29 +01:00
recoveryManager . setForceRestart ( forceRestart ) ;
recoveryManager . setRecordType ( recordType ) ;
recoveryManager . setAggregationType ( aggregationType ) ;
2017-09-05 17:13:50 +02:00
recoveryManager . recovery ( ) ;
break ;
default :
throw new IllegalArgumentException ( " No ElaborationType provided. You should not be here. Please Contact the administrator " ) ;
2016-07-29 10:48:26 +02:00
}
2017-05-10 15:01:46 +02:00
2016-07-29 10:48:26 +02:00
}
2017-09-05 17:13:50 +02:00
/** {@inheritDoc} */
@Override
protected void onStop ( ) throws Exception {
2020-09-30 12:12:22 +02:00
logger . trace ( " Stopping execution of {}, UUID : {} " , getName ( ) , this . uuid ) ;
2017-09-05 17:13:50 +02:00
Thread . currentThread ( ) . interrupt ( ) ;
2016-07-29 10:48:26 +02:00
}
2017-01-25 15:06:16 +01:00
2017-09-05 17:13:50 +02:00
2016-01-29 15:54:16 +01:00
}