118 lines
3.7 KiB
Java
118 lines
3.7 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.accounting.datamodel.implementations.aggregated;
|
|
|
|
import java.io.Serializable;
|
|
import java.util.Map;
|
|
|
|
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
|
|
import org.gcube.accounting.datamodel.decorators.AggregatedField;
|
|
import org.gcube.accounting.datamodel.decorators.RequiredField;
|
|
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
|
|
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
|
|
import org.gcube.accounting.exception.InvalidValueException;
|
|
|
|
/**
|
|
* This Class is for library internal use only
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
*/
|
|
public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implementations.ServiceUsageRecord implements AggregatedUsageRecord<ServiceUsageRecord, org.gcube.accounting.datamodel.implementations.ServiceUsageRecord> {
|
|
|
|
/**
|
|
* Generated Serial Version UID
|
|
*/
|
|
private static final long serialVersionUID = 6387584974618335623L;
|
|
|
|
@ValidInteger @AggregatedField @RequiredField
|
|
protected static final String INVOCATION_COUNT = "invocationCount";
|
|
@ValidLong @AggregatedField @RequiredField
|
|
protected static final String MAX_INVOCATION_TIME = "maxInvocationTime";
|
|
@ValidLong @AggregatedField @RequiredField
|
|
protected static final String MIN_INVOCATION_TIME = "minInvocationTime";
|
|
|
|
public ServiceUsageRecord(){
|
|
super();
|
|
this.resourceProperties.put(AGGREGATED, true);
|
|
}
|
|
|
|
public ServiceUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
|
|
super(properties);
|
|
this.resourceProperties.put(AGGREGATED, true);
|
|
}
|
|
|
|
protected ServiceUsageRecord(org.gcube.accounting.datamodel.implementations.ServiceUsageRecord record) throws InvalidValueException{
|
|
super(record.getResourceProperties());
|
|
this.setInvocationCount(1);
|
|
long duration = record.getDuration();
|
|
this.setMinInvocationTime(duration);
|
|
this.setMaxInvocationTime(duration);
|
|
this.resourceProperties.put(AGGREGATED, true);
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getInvocationCount() {
|
|
return (Integer) this.resourceProperties.get(INVOCATION_COUNT);
|
|
}
|
|
|
|
public void setInvocationCount(int invocationCount) throws InvalidValueException {
|
|
setResourceProperty(INVOCATION_COUNT, invocationCount);
|
|
}
|
|
|
|
public long getMaxInvocationTime() {
|
|
return (Long) this.resourceProperties.get(MAX_INVOCATION_TIME);
|
|
}
|
|
|
|
public void setMaxInvocationTime(long maxInvocationTime) throws InvalidValueException {
|
|
setResourceProperty(MAX_INVOCATION_TIME, maxInvocationTime);
|
|
}
|
|
|
|
public long getMinInvocationTime() {
|
|
return (Long) this.resourceProperties.get(MIN_INVOCATION_TIME);
|
|
}
|
|
|
|
public void setMinInvocationTime(long minInvocationTime) throws InvalidValueException {
|
|
setResourceProperty(MIN_INVOCATION_TIME, minInvocationTime);
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public void aggregate(ServiceUsageRecord record) {
|
|
try {
|
|
this.setInvocationCount(this.getInvocationCount() + record.getInvocationCount());
|
|
this.setDuration((this.getDuration() + record.getDuration()) / 2);
|
|
|
|
long max = ((ServiceUsageRecord) record).getMaxInvocationTime();
|
|
if(max > this.getMaxInvocationTime()){
|
|
this.setMaxInvocationTime(max);
|
|
}
|
|
|
|
long min = ((ServiceUsageRecord) record).getMinInvocationTime();
|
|
if(min < this.getMinInvocationTime()){
|
|
this.setMinInvocationTime(min);
|
|
}
|
|
|
|
}catch(Exception e){
|
|
throw new UnsupportedOperationException(e.getCause());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public ServiceUsageRecord getAggregatedUsageRecord(
|
|
org.gcube.accounting.datamodel.implementations.ServiceUsageRecord record)
|
|
throws InvalidValueException {
|
|
if(record instanceof ServiceUsageRecord){
|
|
return (ServiceUsageRecord) record;
|
|
}
|
|
return new ServiceUsageRecord(record);
|
|
}
|
|
|
|
}
|