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-06-24 15:24:57 +02: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.AbstractServiceUsageRecord;
|
|
|
|
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
2015-12-18 17:09:05 +01:00
|
|
|
import org.gcube.documentstore.exception.InvalidValueException;
|
|
|
|
import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions;
|
2015-12-21 11:50:09 +01: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
|
|
|
|
|
|
|
/**
|
2015-06-23 17:47:38 +02:00
|
|
|
* This Class is for library internal use only
|
2015-06-09 14:03:59 +02:00
|
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
|
|
*/
|
2015-07-29 14:41:28 +02:00
|
|
|
public class AggregatedServiceUsageRecord extends AbstractServiceUsageRecord implements AggregatedUsageRecord<AggregatedServiceUsageRecord, ServiceUsageRecord> {
|
2015-06-09 14:03:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generated Serial Version UID
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 6387584974618335623L;
|
|
|
|
|
2015-06-24 17:51:46 +02:00
|
|
|
// Redefining DURATION to Set @AggregatedField
|
|
|
|
@RequiredField @ValidLong @AggregatedField
|
2015-12-18 17:09:05 +01:00
|
|
|
public static final String DURATION = AbstractServiceUsageRecord.DURATION;
|
2015-06-24 17:51:46 +02:00
|
|
|
|
2015-06-24 15:24:57 +02:00
|
|
|
@RequiredField @ValidLong @AggregatedField
|
2015-07-17 10:54:14 +02:00
|
|
|
public static final String MAX_INVOCATION_TIME = "maxInvocationTime";
|
2015-06-24 15:24:57 +02:00
|
|
|
@RequiredField @ValidLong @AggregatedField
|
2015-07-17 10:54:14 +02:00
|
|
|
public static final String MIN_INVOCATION_TIME = "minInvocationTime";
|
2015-12-18 17:09:05 +01:00
|
|
|
|
2015-07-29 14:41:28 +02:00
|
|
|
public AggregatedServiceUsageRecord(){
|
2015-06-09 14:03:59 +02:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:49:18 +01:00
|
|
|
public AggregatedServiceUsageRecord(Map<String, Comparable<? extends Serializable>> properties) throws InvalidValueException{
|
2015-06-09 15:53:21 +02:00
|
|
|
super(properties);
|
|
|
|
}
|
2015-06-23 17:47:38 +02:00
|
|
|
|
2015-12-18 17:09:05 +01:00
|
|
|
public AggregatedServiceUsageRecord(ServiceUsageRecord record) throws InvalidValueException{
|
2015-06-23 17:47:38 +02:00
|
|
|
super(record.getResourceProperties());
|
2015-06-26 15:50:39 +02:00
|
|
|
this.setOperationCount(1);
|
2015-06-23 17:47:38 +02:00
|
|
|
long duration = record.getDuration();
|
|
|
|
this.setMinInvocationTime(duration);
|
|
|
|
this.setMaxInvocationTime(duration);
|
2015-06-24 15:24:57 +02:00
|
|
|
Calendar creationTime = record.getCreationTime();
|
2015-06-26 15:50:39 +02:00
|
|
|
this.setCreationTime(Calendar.getInstance());
|
2015-06-24 15:24:57 +02:00
|
|
|
this.setStartTime(creationTime);
|
|
|
|
this.setEndTime(creationTime);
|
2015-06-23 17:47:38 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:50:39 +02:00
|
|
|
@Override
|
|
|
|
public int getOperationCount() {
|
|
|
|
return super.getOperationCount();
|
2015-06-09 14:03:59 +02:00
|
|
|
}
|
2015-06-26 15:50:39 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setOperationCount(int operationCount) throws InvalidValueException {
|
|
|
|
super.setOperationCount(operationCount);
|
2015-06-09 14:03:59 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 17:47:38 +02:00
|
|
|
public long getMaxInvocationTime() {
|
|
|
|
return (Long) this.resourceProperties.get(MAX_INVOCATION_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMaxInvocationTime(long maxInvocationTime) throws InvalidValueException {
|
2015-12-02 11:08:14 +01:00
|
|
|
super.setResourceProperty(MAX_INVOCATION_TIME, maxInvocationTime);
|
2015-06-09 14:03:59 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 17:47:38 +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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setStartTime(Calendar startTime) throws InvalidValueException {
|
|
|
|
super.setStartTime(startTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
2015-12-02 11:08:14 +01:00
|
|
|
public Calendar getEndTime() {
|
|
|
|
return super.getEndTimeAsCalendar();
|
2015-11-30 18:34:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setEndTime(Calendar endTime) throws InvalidValueException {
|
|
|
|
super.setEndTime(endTime);
|
|
|
|
}
|
|
|
|
|
2015-12-18 17:09:05 +01:00
|
|
|
protected long durationWeightedAverage(AggregatedServiceUsageRecord record){
|
|
|
|
long thisDuration = this.getDuration() * this.getOperationCount();
|
|
|
|
long recordDuration = record.getDuration() * record.getOperationCount();
|
|
|
|
long totalOperationCount = this.getOperationCount() + record.getOperationCount();
|
|
|
|
return (thisDuration + recordDuration) / totalOperationCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public AggregatedServiceUsageRecord aggregate(AggregatedServiceUsageRecord record)
|
|
|
|
throws NotAggregatableRecordsExceptions {
|
|
|
|
try {
|
|
|
|
setDuration(durationWeightedAverage(record));
|
|
|
|
setOperationCount(this.getOperationCount() + record.getOperationCount());
|
|
|
|
|
|
|
|
long max = record.getMaxInvocationTime();
|
|
|
|
if(max > this.getMaxInvocationTime()){
|
|
|
|
this.setMaxInvocationTime(max);
|
|
|
|
}
|
|
|
|
|
|
|
|
long min = record.getMinInvocationTime();
|
|
|
|
if(min < this.getMinInvocationTime()){
|
|
|
|
this.setMinInvocationTime(min);
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch(Exception e){
|
|
|
|
throw new UnsupportedOperationException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public AggregatedServiceUsageRecord aggregate(ServiceUsageRecord record)
|
|
|
|
throws NotAggregatableRecordsExceptions {
|
|
|
|
try {
|
|
|
|
return aggregate(new AggregatedServiceUsageRecord(record));
|
|
|
|
} catch (InvalidValueException e) {
|
|
|
|
throw new NotAggregatableRecordsExceptions(e.getCause());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Class<ServiceUsageRecord> getAggregable() {
|
|
|
|
return ServiceUsageRecord.class;
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:03:59 +02:00
|
|
|
}
|