refs #200: Create accouting-lib library

https://support.d4science.org/issues/200
Implementing aggregation facilities 

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115503 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-06-24 11:00:35 +00:00
parent 28525346d1
commit 47ade73adc
7 changed files with 37 additions and 59 deletions

View File

@ -4,20 +4,13 @@
package org.gcube.accounting.datamodel;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.exception.NotAggregatableRecordsExceptions;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/
public interface AggregatedUsageRecord<T extends B, B extends SingleUsageRecord> extends SingleUsageRecord {
public interface AggregatedUsageRecord<T extends AggregatedUsageRecord<T, B>, B extends SingleUsageRecord> extends SingleUsageRecord {
public T getAggregatedUsageRecord(B b) throws InvalidValueException ;
/**
* Aggregate the Record provided as parameter with this record if Aggregatable
* @throws NotAggregatableRecordsExceptions
*/
public void aggregate(T record) throws NotAggregatableRecordsExceptions;
}

View File

@ -15,7 +15,7 @@ import org.gcube.accounting.exception.NotAggregatableRecordsExceptions;
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public abstract class AggregationStrategy<T extends B, B extends SingleUsageRecord> {
public abstract class AggregationStrategy<T extends AggregatedUsageRecord<T, B>, B extends SingleUsageRecord> {
protected T t;
protected Set<String> aggregationField;
@ -58,17 +58,18 @@ public abstract class AggregationStrategy<T extends B, B extends SingleUsageReco
return true;
}
public void aggregate(B record) throws NotAggregatableRecordsExceptions {
protected abstract T reallyAggregate(T t) throws NotAggregatableRecordsExceptions;
public T aggregate(B record) throws NotAggregatableRecordsExceptions {
try{
if(isAggregable(record)){
throw new NotAggregatableRecordsExceptions("The Record provided as argument has different values for field wich must be common to be aggragatable");
}
@SuppressWarnings("unchecked")
AggregatedUsageRecord<T, B> aggregatedUsageRecord = ((AggregatedUsageRecord<T, B>) t);
aggregatedUsageRecord.aggregate(aggregatedUsageRecord.getAggregatedUsageRecord(record));
t.getAggregatedUsageRecord(record);
t = reallyAggregate(t);
throw new NotAggregatableRecordsExceptions("");
return t;
}catch(NotAggregatableRecordsExceptions e){
throw e;
}catch(Exception ex){

View File

@ -8,7 +8,6 @@ import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.exception.NotAggregatableRecordsExceptions;
/**
* This Class is for library internal use only
@ -42,16 +41,5 @@ public class JobUsageRecord extends org.gcube.accounting.datamodel.implementatio
return null;
}
/**
* {@inheritDoc}
*/
@Override
public void aggregate(JobUsageRecord record)
throws NotAggregatableRecordsExceptions {
// TODO Auto-generated method stub
}
}

View File

@ -8,7 +8,6 @@ import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.exception.NotAggregatableRecordsExceptions;
/**
* This Class is for library internal use only
@ -43,16 +42,4 @@ public class PortletUsageRecord extends org.gcube.accounting.datamodel.implement
return null;
}
/**
* {@inheritDoc}
*/
@Override
public void aggregate(PortletUsageRecord record)
throws NotAggregatableRecordsExceptions {
// TODO Auto-generated method stub
}
}

View File

@ -49,9 +49,6 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement
this.setMaxInvocationTime(duration);
this.resourceProperties.put(AGGREGATED, true);
}
public int getInvocationCount() {
return (Integer) this.resourceProperties.get(INVOCATION_COUNT);
@ -77,10 +74,8 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement
setResourceProperty(MIN_INVOCATION_TIME, minInvocationTime);
}
/**
* {@inheritDoc}
*/
@Override
/*
public void aggregate(ServiceUsageRecord record) {
try {
this.setInvocationCount(this.getInvocationCount() + record.getInvocationCount());
@ -100,7 +95,8 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement
throw new UnsupportedOperationException(e.getCause());
}
}
*/
/**
* {@inheritDoc}
*/

View File

@ -8,7 +8,6 @@ import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.exception.NotAggregatableRecordsExceptions;
/**
* This Class is for library internal use only
@ -41,16 +40,5 @@ public class StorageUsageRecord extends org.gcube.accounting.datamodel.implement
// TODO Auto-generated method stub
return null;
}
/**
* {@inheritDoc}
*/
@Override
public void aggregate(StorageUsageRecord record)
throws NotAggregatableRecordsExceptions {
// TODO Auto-generated method stub
}
}

View File

@ -5,6 +5,7 @@ package org.gcube.accounting.datamodel.implementations.aggregationstrategy;
import org.gcube.accounting.datamodel.AggregationStrategy;
import org.gcube.accounting.datamodel.implementations.aggregated.ServiceUsageRecord;
import org.gcube.accounting.exception.NotAggregatableRecordsExceptions;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
@ -24,4 +25,28 @@ public class ServiceUsageRecordAggregationStrategy extends AggregationStrategy<S
this.aggregationField.add(ServiceUsageRecord.SERVICE_NAME);
}
protected ServiceUsageRecord reallyAggregate(ServiceUsageRecord record)
throws NotAggregatableRecordsExceptions {
try {
t.setInvocationCount(t.getInvocationCount() + record.getInvocationCount());
t.setDuration((t.getDuration() + record.getDuration()) / 2);
long max = record.getMaxInvocationTime();
if(max > t.getMaxInvocationTime()){
t.setMaxInvocationTime(max);
}
long min = record.getMinInvocationTime();
if(min < t.getMinInvocationTime()){
t.setMinInvocationTime(min);
}
}catch(Exception e){
throw new UnsupportedOperationException(e.getCause());
}
return t;
}
}