2015-06-09 14:03:59 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2015-12-18 17:09:05 +01:00
|
|
|
package org.gcube.accounting.datamodel.aggregation;
|
2015-06-09 14:03:59 +02:00
|
|
|
|
2015-06-09 15:53:21 +02:00
|
|
|
import java.io.Serializable;
|
2015-11-30 18:34:48 +01:00
|
|
|
import java.util.Calendar;
|
2015-06-09 15:53:21 +02:00
|
|
|
import java.util.Map;
|
2015-06-09 14:03:59 +02:00
|
|
|
|
|
|
|
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
|
2015-07-29 14:41:28 +02:00
|
|
|
import org.gcube.accounting.datamodel.basetypes.AbstractJobUsageRecord;
|
2017-09-14 14:21:23 +02:00
|
|
|
import org.gcube.accounting.datamodel.basetypes.AbstractServiceUsageRecord;
|
2015-07-29 14:41:28 +02:00
|
|
|
import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord;
|
2015-12-18 17:09:05 +01:00
|
|
|
import org.gcube.documentstore.exception.InvalidValueException;
|
|
|
|
import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions;
|
2016-01-13 16:18:59 +01:00
|
|
|
import org.gcube.documentstore.records.aggregation.AggregationUtility;
|
2017-09-14 14:21:23 +02:00
|
|
|
import org.gcube.documentstore.records.implementation.AggregatedField;
|
|
|
|
import org.gcube.documentstore.records.implementation.RequiredField;
|
|
|
|
import org.gcube.documentstore.records.implementation.validations.annotations.ValidLong;
|
2015-06-09 14:03:59 +02:00
|
|
|
|
2017-09-18 15:08:46 +02:00
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
2017-05-11 16:38:38 +02:00
|
|
|
import com.fasterxml.jackson.annotation.JsonTypeName;
|
|
|
|
|
2015-06-09 14:03:59 +02:00
|
|
|
/**
|
2015-06-23 17:47:38 +02:00
|
|
|
* This Class is for library internal use only
|
2016-10-12 14:15:28 +02:00
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
2015-06-09 14:03:59 +02:00
|
|
|
*/
|
2017-05-11 16:38:38 +02:00
|
|
|
@JsonTypeName(value="JobUsageRecord")
|
2015-07-29 14:41:28 +02:00
|
|
|
public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements AggregatedUsageRecord<AggregatedJobUsageRecord, JobUsageRecord> {
|
2015-06-09 14:03:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generated Serial Version UID
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = -3376423316219914682L;
|
2017-09-06 11:47:13 +02:00
|
|
|
|
2017-09-14 14:21:23 +02:00
|
|
|
@AggregatedField
|
|
|
|
public static final String DURATION = AbstractServiceUsageRecord.DURATION;
|
|
|
|
|
2016-09-19 11:06:46 +02:00
|
|
|
@RequiredField @ValidLong @AggregatedField
|
|
|
|
public static final String MAX_INVOCATION_TIME = "maxInvocationTime";
|
|
|
|
@RequiredField @ValidLong @AggregatedField
|
|
|
|
public static final String MIN_INVOCATION_TIME = "minInvocationTime";
|
2017-09-06 11:47:13 +02:00
|
|
|
|
2015-07-29 14:41:28 +02:00
|
|
|
public AggregatedJobUsageRecord(){
|
2015-06-09 14:03:59 +02:00
|
|
|
super();
|
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2016-01-28 18:29:11 +01:00
|
|
|
public AggregatedJobUsageRecord(Map<String, ? extends Serializable> properties) throws InvalidValueException{
|
2015-06-09 15:53:21 +02:00
|
|
|
super(properties);
|
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2017-05-11 16:38:38 +02:00
|
|
|
// TODO
|
2016-09-19 11:06:46 +02:00
|
|
|
public AggregatedJobUsageRecord(JobUsageRecord record) throws InvalidValueException{
|
|
|
|
super(record.getResourceProperties());
|
|
|
|
this.setOperationCount(1);
|
|
|
|
long duration = record.getDuration();
|
|
|
|
this.setMinInvocationTime(duration);
|
|
|
|
this.setMaxInvocationTime(duration);
|
|
|
|
Calendar creationTime = record.getCreationTime();
|
|
|
|
this.setCreationTime(Calendar.getInstance());
|
|
|
|
this.setStartTime(creationTime);
|
|
|
|
this.setEndTime(creationTime);
|
2015-12-18 17:09:05 +01:00
|
|
|
}
|
2017-09-14 14:21:23 +02:00
|
|
|
|
2015-11-19 16:19:51 +01:00
|
|
|
@Override
|
|
|
|
public int getOperationCount() {
|
|
|
|
return super.getOperationCount();
|
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2015-11-19 16:19:51 +01:00
|
|
|
@Override
|
|
|
|
public void setOperationCount(int operationCount) throws InvalidValueException {
|
|
|
|
super.setOperationCount(operationCount);
|
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2017-09-18 15:08:46 +02:00
|
|
|
@JsonIgnore
|
2016-09-19 11:06:46 +02:00
|
|
|
public long getMaxInvocationTime() {
|
|
|
|
return (Long) this.resourceProperties.get(MAX_INVOCATION_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMaxInvocationTime(long maxInvocationTime) throws InvalidValueException {
|
|
|
|
super.setResourceProperty(MAX_INVOCATION_TIME, maxInvocationTime);
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:08:46 +02:00
|
|
|
@JsonIgnore
|
2016-09-19 11:06:46 +02:00
|
|
|
public long getMinInvocationTime() {
|
|
|
|
return (Long) this.resourceProperties.get(MIN_INVOCATION_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMinInvocationTime(long minInvocationTime) throws InvalidValueException {
|
|
|
|
setResourceProperty(MIN_INVOCATION_TIME, minInvocationTime);
|
|
|
|
}
|
2017-09-06 11:47:13 +02:00
|
|
|
|
2015-11-30 18:34:48 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Calendar getStartTime() {
|
2015-12-02 11:08:14 +01:00
|
|
|
return super.getStartTimeAsCalendar();
|
2015-11-30 18:34:48 +01:00
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2015-11-30 18:34:48 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setStartTime(Calendar startTime) throws InvalidValueException {
|
|
|
|
super.setStartTime(startTime);
|
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2015-11-30 18:34:48 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
2015-12-02 11:08:14 +01:00
|
|
|
public Calendar getEndTime() {
|
|
|
|
return super.getEndTimeAsCalendar();
|
2015-11-30 18:34:48 +01:00
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2015-11-30 18:34:48 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setEndTime(Calendar endTime) throws InvalidValueException {
|
|
|
|
super.setEndTime(endTime);
|
|
|
|
}
|
|
|
|
|
2017-05-11 16:38:38 +02:00
|
|
|
@Override
|
2017-09-14 14:21:23 +02:00
|
|
|
public void setAggregated(Boolean aggregate) throws InvalidValueException {
|
|
|
|
super.setAggregated(aggregate);
|
2017-05-11 16:38:38 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2017-09-18 15:08:46 +02:00
|
|
|
@Override
|
2017-09-14 14:21:23 +02:00
|
|
|
public Boolean isAggregated() {
|
|
|
|
return super.isAggregated();
|
2017-05-11 16:38:38 +02:00
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2017-09-14 14:21:23 +02:00
|
|
|
|
2016-09-19 11:06:46 +02:00
|
|
|
protected long durationWeightedAverage(AggregatedJobUsageRecord record) throws InvalidValueException{
|
|
|
|
long thisDuration = this.getDuration() * this.getOperationCount();
|
|
|
|
long recordDuration = record.getDuration() * record.getOperationCount();
|
|
|
|
long totalOperationCount = this.getOperationCount() + record.getOperationCount();
|
|
|
|
return (thisDuration + recordDuration) / totalOperationCount;
|
|
|
|
}
|
2017-09-14 14:21:23 +02:00
|
|
|
|
2015-12-18 17:09:05 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public AggregatedJobUsageRecord aggregate(AggregatedJobUsageRecord record)
|
|
|
|
throws NotAggregatableRecordsExceptions {
|
2016-01-13 16:18:59 +01:00
|
|
|
try {
|
2017-09-14 14:21:23 +02:00
|
|
|
/*
|
|
|
|
* It is also checked in aggregationUtility.aggregate(record)
|
|
|
|
* but has to be also checked before modifying values
|
|
|
|
*/
|
|
|
|
if(!isAggregable(record)){
|
|
|
|
throw new NotAggregatableRecordsExceptions("The Record provided as argument has different values for field wich must be common to be aggregatable");
|
|
|
|
}
|
|
|
|
|
2016-01-13 16:18:59 +01:00
|
|
|
AggregationUtility<AggregatedJobUsageRecord> aggregationUtility = new AggregationUtility<AggregatedJobUsageRecord>(this);
|
2017-09-14 14:21:23 +02:00
|
|
|
|
2016-09-19 11:06:46 +02:00
|
|
|
setDuration(durationWeightedAverage(record));
|
2017-09-14 14:21:23 +02:00
|
|
|
|
2016-09-19 11:06:46 +02:00
|
|
|
long max = record.getMaxInvocationTime();
|
|
|
|
if(max > this.getMaxInvocationTime()){
|
|
|
|
this.setMaxInvocationTime(max);
|
|
|
|
}
|
2017-09-14 14:21:23 +02:00
|
|
|
|
2016-09-19 11:06:46 +02:00
|
|
|
long min = record.getMinInvocationTime();
|
|
|
|
if(min < this.getMinInvocationTime()){
|
|
|
|
this.setMinInvocationTime(min);
|
|
|
|
}
|
2017-09-14 14:21:23 +02:00
|
|
|
|
|
|
|
// This statement is at the end because the aggregate method
|
|
|
|
// sum operation counts. If this statement is moved at the
|
|
|
|
// beginning the weighted average is not calculated correctly
|
2016-01-13 16:18:59 +01:00
|
|
|
aggregationUtility.aggregate(record);
|
2017-09-14 14:21:23 +02:00
|
|
|
|
2016-02-19 11:49:07 +01:00
|
|
|
} catch(NotAggregatableRecordsExceptions e){
|
2017-09-14 14:21:23 +02:00
|
|
|
throw e;
|
2016-02-19 11:49:07 +01:00
|
|
|
} catch(Exception ex){
|
|
|
|
throw new NotAggregatableRecordsExceptions(ex);
|
2016-01-13 16:18:59 +01:00
|
|
|
}
|
2017-09-14 14:21:23 +02:00
|
|
|
|
2016-09-19 11:06:46 +02:00
|
|
|
return this;
|
2015-12-18 17:09:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public AggregatedJobUsageRecord aggregate(JobUsageRecord record)
|
|
|
|
throws NotAggregatableRecordsExceptions {
|
2016-01-13 16:18:59 +01:00
|
|
|
try {
|
|
|
|
return aggregate(new AggregatedJobUsageRecord(record));
|
2016-02-19 11:49:07 +01:00
|
|
|
} catch(NotAggregatableRecordsExceptions e){
|
|
|
|
throw e;
|
|
|
|
} catch(Exception ex){
|
|
|
|
throw new NotAggregatableRecordsExceptions(ex);
|
2016-01-13 16:18:59 +01:00
|
|
|
}
|
2015-12-18 17:09:05 +01:00
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2016-01-13 16:18:59 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean isAggregable(AggregatedJobUsageRecord record) throws NotAggregatableRecordsExceptions {
|
|
|
|
AggregationUtility<AggregatedJobUsageRecord> aggregationUtility = new AggregationUtility<AggregatedJobUsageRecord>(this);
|
|
|
|
return aggregationUtility.isAggregable(record);
|
2015-12-18 17:09:05 +01:00
|
|
|
|
2016-01-13 16:18:59 +01:00
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2016-01-13 16:18:59 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean isAggregable(JobUsageRecord record) throws NotAggregatableRecordsExceptions {
|
|
|
|
try {
|
|
|
|
return isAggregable(new AggregatedJobUsageRecord(record));
|
2016-02-19 11:49:07 +01:00
|
|
|
} catch(NotAggregatableRecordsExceptions e){
|
|
|
|
throw e;
|
|
|
|
} catch(Exception ex){
|
|
|
|
throw new NotAggregatableRecordsExceptions(ex);
|
2016-01-13 16:18:59 +01:00
|
|
|
}
|
|
|
|
}
|
2016-09-19 11:06:46 +02:00
|
|
|
|
2015-12-18 17:09:05 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
2017-05-11 16:38:38 +02:00
|
|
|
*/
|
2015-12-18 17:09:05 +01:00
|
|
|
@Override
|
|
|
|
public Class<JobUsageRecord> getAggregable() {
|
|
|
|
return JobUsageRecord.class;
|
|
|
|
}
|
|
|
|
|
2017-09-14 14:21:23 +02:00
|
|
|
|
2015-06-09 14:03:59 +02:00
|
|
|
}
|