2015-05-28 13:19:25 +02:00
/ * *
*
* /
package org.gcube.accounting.datamodel.implementations ;
2015-06-09 15:53:21 +02:00
import java.io.Serializable ;
2015-06-09 17:36:38 +02:00
import java.lang.annotation.ElementType ;
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
import java.lang.annotation.Target ;
2015-05-29 16:08:21 +02:00
import java.util.Calendar ;
2015-06-09 15:53:21 +02:00
import java.util.Map ;
2015-05-29 16:08:21 +02:00
2015-06-10 17:36:50 +02:00
import org.gcube.accounting.datamodel.BasicUsageRecord ;
2015-06-05 14:43:04 +02:00
import org.gcube.accounting.datamodel.SingleUsageRecord ;
2015-06-09 17:36:38 +02:00
import org.gcube.accounting.datamodel.UsageRecord ;
2015-06-09 10:41:32 +02:00
import org.gcube.accounting.datamodel.decorators.ComputedField ;
2015-06-09 17:36:38 +02:00
import org.gcube.accounting.datamodel.decorators.FieldAction ;
import org.gcube.accounting.datamodel.decorators.FieldDecorator ;
2015-06-08 17:30:08 +02:00
import org.gcube.accounting.datamodel.decorators.RequiredField ;
2015-06-09 14:03:59 +02:00
import org.gcube.accounting.datamodel.deprecationmanagement.annotations.MoveToOperationResult ;
2015-06-08 17:19:09 +02:00
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty ;
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger ;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong ;
2015-05-29 16:08:21 +02:00
import org.gcube.accounting.exception.InvalidValueException ;
2015-06-10 14:01:33 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2015-05-28 13:19:25 +02:00
/ * *
* @author Luca Frosini ( ISTI - CNR ) http : //www.lucafrosini.com/
*
* /
2015-06-10 17:36:50 +02:00
public class JobUsageRecord extends BasicUsageRecord implements SingleUsageRecord {
2015-06-10 14:01:33 +02:00
private static Logger logger = LoggerFactory . getLogger ( JobUsageRecord . class ) ;
2015-05-28 13:19:25 +02:00
/ * *
* Generated Serial Version UID
* /
private static final long serialVersionUID = - 8648691183939346858L ;
2015-06-10 12:42:39 +02:00
protected static enum JobStatus {
2015-05-29 17:39:24 +02:00
completed , failed
} ;
2015-06-08 17:30:08 +02:00
@RequiredField @NotEmpty
2015-05-28 13:19:25 +02:00
public static final String JOB_ID = " jobId " ;
2015-06-08 17:30:08 +02:00
@RequiredField @NotEmpty
2015-05-28 13:19:25 +02:00
public static final String JOB_QUALIFIER = " jobQualifier " ;
2015-06-08 17:30:08 +02:00
@RequiredField @NotEmpty
2015-05-28 13:19:25 +02:00
public static final String JOB_NAME = " jobName " ;
2015-06-09 17:36:38 +02:00
@RequiredField @ValidLong @CalculateWallDuration
2015-05-29 16:08:21 +02:00
public static final String JOB_START_TIME = " jobStartTime " ;
2015-06-09 17:36:38 +02:00
@RequiredField @ValidLong @CalculateWallDuration
2015-05-29 16:08:21 +02:00
public static final String JOB_END_TIME = " jobEndTime " ;
2015-06-10 12:42:39 +02:00
@MoveToOperationResult
2015-06-05 15:55:22 +02:00
protected static final String JOB_STATUS = " jobStatus " ;
2015-06-03 11:57:38 +02:00
@ValidInteger
2015-05-28 13:19:25 +02:00
public static final String VMS_USED = " vmsUsed " ;
2015-06-09 17:36:38 +02:00
@ComputedField @ValidLong @CalculateWallDuration
2015-06-05 15:55:22 +02:00
protected static final String WALL_DURATION = " wallDuration " ;
2015-05-28 13:19:25 +02:00
2015-06-09 17:36:38 +02:00
@Target ( ElementType . FIELD )
@Retention ( RetentionPolicy . RUNTIME )
@FieldDecorator ( managed = CalculateWallDurationAction . class )
2015-06-10 12:42:39 +02:00
protected @interface CalculateWallDuration { }
2015-06-09 17:36:38 +02:00
protected class CalculateWallDurationAction implements FieldAction {
@Override
public Serializable validate ( String key , Serializable value , UsageRecord usageRecord ) throws InvalidValueException {
try {
long wallDuration = calculateWallDuration ( ) ;
if ( key . compareTo ( WALL_DURATION ) = = 0 ) {
2015-06-10 14:01:33 +02:00
logger . warn ( " {} is automatically computed using {} and {}. This invocation has the only effect of recalculating the value. Any provided value is ignored. " ,
WALL_DURATION , JOB_START_TIME , JOB_END_TIME ) ;
2015-06-09 17:36:38 +02:00
value = wallDuration ;
}
2015-06-10 14:01:33 +02:00
} catch ( InvalidValueException e ) { }
2015-06-09 17:36:38 +02:00
return value ;
}
}
2015-05-28 13:19:25 +02:00
public JobUsageRecord ( ) {
super ( ) ;
}
2015-06-09 15:53:21 +02:00
public JobUsageRecord ( Map < String , Serializable > properties ) throws InvalidValueException {
super ( properties ) ;
}
2015-05-29 16:08:21 +02:00
/ * *
* @return the Job Id
* /
public String getJobId ( ) {
2015-06-09 14:54:14 +02:00
return ( String ) this . resourceProperties . get ( JOB_ID ) ;
2015-05-29 16:08:21 +02:00
}
/ * *
* @param jobId Job Id
* @throws InvalidValueException if fails
* /
public void setJobId ( String jobId ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( JOB_ID , jobId ) ;
2015-05-29 16:08:21 +02:00
}
public String getJobQualifier ( ) {
2015-06-09 14:54:14 +02:00
return ( String ) this . resourceProperties . get ( JOB_QUALIFIER ) ;
2015-05-29 16:08:21 +02:00
}
public void setJobQualifier ( String jobQualifier ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( JOB_QUALIFIER , jobQualifier ) ;
2015-05-29 16:08:21 +02:00
}
public String getJobName ( ) {
2015-06-09 14:54:14 +02:00
return ( String ) this . resourceProperties . get ( JOB_NAME ) ;
2015-05-29 16:08:21 +02:00
}
public void setJobName ( String jobName ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( JOB_NAME , jobName ) ;
2015-05-29 16:08:21 +02:00
}
public Calendar getJobStartTime ( ) {
2015-06-09 14:54:14 +02:00
long millis = ( Long ) this . resourceProperties . get ( JOB_START_TIME ) ;
2015-05-29 16:08:21 +02:00
return timestampStringToCalendar ( millis ) ;
}
public void setJobStartTime ( Calendar jobStartTime ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( JOB_START_TIME , jobStartTime . getTimeInMillis ( ) ) ;
2015-05-29 16:08:21 +02:00
}
public Calendar getJobEndTime ( ) {
2015-06-09 14:54:14 +02:00
long millis = ( Long ) this . resourceProperties . get ( JOB_END_TIME ) ;
2015-05-29 16:08:21 +02:00
return timestampStringToCalendar ( millis ) ;
}
public void setJobEndTime ( Calendar jobEndTime ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( JOB_END_TIME , jobEndTime . getTimeInMillis ( ) ) ;
2015-05-29 16:08:21 +02:00
}
2015-06-10 12:42:39 +02:00
/ *
2015-06-05 15:55:22 +02:00
@Deprecated
2015-06-10 12:42:39 +02:00
protected JobStatus getJobStatus ( ) {
2015-06-09 14:54:14 +02:00
return JobStatus . values ( ) [ ( ( OperationResult ) this . resourceProperties . get ( OPERATION_RESULT ) ) . ordinal ( ) ] ;
2015-05-29 16:08:21 +02:00
}
2015-06-05 15:55:22 +02:00
@Deprecated
2015-06-10 12:42:39 +02:00
protected void setJobStatus ( JobStatus jobStatus ) throws InvalidValueException {
setResourceProperty ( OPERATION_RESULT , jobStatus ) ;
2015-05-29 16:08:21 +02:00
}
2015-06-10 12:42:39 +02:00
* /
2015-05-29 16:08:21 +02:00
public int getVmsUsed ( ) {
2015-06-09 14:54:14 +02:00
return ( Integer ) this . resourceProperties . get ( VMS_USED ) ;
2015-05-29 16:08:21 +02:00
}
public void setVmsUsed ( int vmsUsed ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( VMS_USED , vmsUsed ) ;
2015-05-29 16:08:21 +02:00
}
2015-06-09 17:36:38 +02:00
protected long calculateWallDuration ( ) throws InvalidValueException {
2015-06-05 15:55:22 +02:00
try {
2015-06-09 14:54:14 +02:00
long endTime = ( Long ) this . resourceProperties . get ( JOB_END_TIME ) ;
long startTime = ( Long ) this . resourceProperties . get ( JOB_START_TIME ) ;
2015-06-05 15:55:22 +02:00
long wallDuration = endTime - startTime ;
2015-06-09 14:54:14 +02:00
setResourceProperty ( WALL_DURATION , wallDuration ) ;
2015-06-09 17:36:38 +02:00
return wallDuration ;
2015-06-05 15:55:22 +02:00
} catch ( Exception e ) {
throw new InvalidValueException ( String . format ( " To calculate Wall Duration both %s and %s must be set " ,
START_TIME , END_TIME ) , e . getCause ( ) ) ;
}
2015-05-29 16:08:21 +02:00
}
2015-06-09 17:36:38 +02:00
public long getWallDuration ( ) {
2015-06-09 14:54:14 +02:00
Long wallDuration = ( Long ) this . resourceProperties . get ( WALL_DURATION ) ;
2015-06-05 15:55:22 +02:00
if ( wallDuration = = null ) {
2015-06-09 17:36:38 +02:00
try {
wallDuration = calculateWallDuration ( ) ;
} catch ( Exception e ) { }
2015-06-05 15:55:22 +02:00
}
return wallDuration ;
}
2015-05-29 16:08:21 +02:00
2015-05-28 13:19:25 +02:00
}