From 392c09eaaee0dc4950686f7b9e17876b2af1cbdd Mon Sep 17 00:00:00 2001 From: Alessandro Pieve Date: Mon, 19 Sep 2016 09:06:46 +0000 Subject: [PATCH] git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@131473 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../aggregation/AggregatedJobUsageRecord.java | 94 +++++++++++++++---- .../basetypes/AbstractJobUsageRecord.java | 15 +-- .../CalculateJobWallDurationAction.java | 4 +- .../usagerecords/JobUsageRecordTest.java | 4 +- .../gcube/testutility/TestUsageRecord.java | 8 +- 5 files changed, 91 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/gcube/accounting/datamodel/aggregation/AggregatedJobUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/aggregation/AggregatedJobUsageRecord.java index e1ae95b..785e7d1 100644 --- a/src/main/java/org/gcube/accounting/datamodel/aggregation/AggregatedJobUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/aggregation/AggregatedJobUsageRecord.java @@ -13,6 +13,9 @@ import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord; import org.gcube.documentstore.exception.InvalidValueException; import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions; import org.gcube.documentstore.records.aggregation.AggregationUtility; +import org.gcube.documentstore.records.implementation.AggregatedField; +import org.gcube.documentstore.records.implementation.RequiredField; +import org.gcube.documentstore.records.implementation.validations.annotations.ValidLong; /** * This Class is for library internal use only @@ -20,34 +23,66 @@ import org.gcube.documentstore.records.aggregation.AggregationUtility; */ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements AggregatedUsageRecord { + + /** * Generated Serial Version UID */ private static final long serialVersionUID = -3376423316219914682L; - + /*add a field max e min invocation*/ + @RequiredField @ValidLong @AggregatedField + public static final String MAX_INVOCATION_TIME = "maxInvocationTime"; + @RequiredField @ValidLong @AggregatedField + public static final String MIN_INVOCATION_TIME = "minInvocationTime"; + public AggregatedJobUsageRecord(){ super(); } - + public AggregatedJobUsageRecord(Map properties) throws InvalidValueException{ super(properties); } - + // TODO - public AggregatedJobUsageRecord(JobUsageRecord jobUsageRecord) throws InvalidValueException{ - throw new UnsupportedOperationException(); + public AggregatedJobUsageRecord(JobUsageRecord record) throws InvalidValueException{ + //throw new UnsupportedOperationException(); + 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); + } - @Override public int getOperationCount() { return super.getOperationCount(); } - + @Override public void setOperationCount(int operationCount) throws InvalidValueException { super.setOperationCount(operationCount); } - + + public long getMaxInvocationTime() { + return (Long) this.resourceProperties.get(MAX_INVOCATION_TIME); + } + + public void setMaxInvocationTime(long maxInvocationTime) throws InvalidValueException { + super.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} */ @@ -55,7 +90,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements public Calendar getStartTime() { return super.getStartTimeAsCalendar(); } - + /** * {@inheritDoc} */ @@ -63,7 +98,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements public void setStartTime(Calendar startTime) throws InvalidValueException { super.setStartTime(startTime); } - + /** * {@inheritDoc} */ @@ -71,7 +106,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements public Calendar getEndTime() { return super.getEndTimeAsCalendar(); } - + /** * {@inheritDoc} */ @@ -80,6 +115,14 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements super.setEndTime(endTime); } + + 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; + } + /** * {@inheritDoc} */ @@ -87,17 +130,26 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements public AggregatedJobUsageRecord aggregate(AggregatedJobUsageRecord record) throws NotAggregatableRecordsExceptions { try { - /* TODO + AggregationUtility aggregationUtility = new AggregationUtility(this); + setDuration(durationWeightedAverage(record)); + long max = record.getMaxInvocationTime(); + if(max > this.getMaxInvocationTime()){ + this.setMaxInvocationTime(max); + } + + long min = record.getMinInvocationTime(); + if(min < this.getMinInvocationTime()){ + this.setMinInvocationTime(min); + } aggregationUtility.aggregate(record); } catch(NotAggregatableRecordsExceptions e){ - throw e; */ + throw e; } catch(Exception ex){ throw new NotAggregatableRecordsExceptions(ex); } - //return this; - - throw new UnsupportedOperationException(); + return this; + //throw new UnsupportedOperationException(); } /** @@ -114,7 +166,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements throw new NotAggregatableRecordsExceptions(ex); } } - + /** * {@inheritDoc} */ @@ -124,7 +176,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements return aggregationUtility.isAggregable(record); } - + /** * {@inheritDoc} */ @@ -138,7 +190,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements throw new NotAggregatableRecordsExceptions(ex); } } - + /** * {@inheritDoc} */ @@ -147,4 +199,8 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements return JobUsageRecord.class; } + @Override + protected void cleanExtraFields(){ + return; + } } diff --git a/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractJobUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractJobUsageRecord.java index 79a6131..812b033 100644 --- a/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractJobUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractJobUsageRecord.java @@ -34,13 +34,13 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord { @NotEmptyIfNotNull public static final String JOB_QUALIFIER = "jobQualifier"; - @ValidLong + @RequiredField @ValidLong public static final String JOB_START_TIME = "jobStartTime"; - @ValidLong + @RequiredField @ValidLong public static final String JOB_END_TIME = "jobEndTime"; @RequiredField @ComputedField(action=CalculateJobWallDurationAction.class) @ValidLong - public static final String WALL_DURATION = "wallDuration"; + public static final String DURATION = "duration"; public AbstractJobUsageRecord(){ super(); @@ -114,7 +114,7 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord { long endTime = (Long) this.resourceProperties.get(JOB_END_TIME); long startTime = (Long) this.resourceProperties.get(JOB_START_TIME); long wallDuration = endTime - startTime; - setResourceProperty(AbstractJobUsageRecord.WALL_DURATION, wallDuration); + setResourceProperty(AbstractJobUsageRecord.DURATION, wallDuration); return wallDuration; }catch(Exception e){ throw new InvalidValueException(String.format("To calculate Wall Duration both %s and %s must be set", @@ -122,8 +122,8 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord { } } - public long getWallDuration() throws InvalidValueException { - Long wallDuration = (Long) this.resourceProperties.get(WALL_DURATION); + public long getDuration() throws InvalidValueException { + Long wallDuration = (Long) this.resourceProperties.get(DURATION); if(wallDuration == null){ try { wallDuration = calculateWallDuration(); @@ -133,5 +133,8 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord { } return wallDuration; } + public void setDuration(Long duration) throws InvalidValueException { + setResourceProperty(DURATION, duration); + } } diff --git a/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateJobWallDurationAction.java b/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateJobWallDurationAction.java index 7cc46f8..d7dbf38 100644 --- a/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateJobWallDurationAction.java +++ b/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateJobWallDurationAction.java @@ -19,9 +19,9 @@ public class CalculateJobWallDurationAction implements FieldAction { public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException { try { long wallDuration = ((AbstractJobUsageRecord) record).calculateWallDuration(); - if(key.compareTo(AbstractJobUsageRecord.WALL_DURATION)==0){ + if(key.compareTo(AbstractJobUsageRecord.DURATION)==0){ logger.warn("{} is automatically computed using {} and {}. This invocation has the only effect of recalculating the value. Any provided value is ignored.", - AbstractJobUsageRecord.WALL_DURATION, AbstractJobUsageRecord.JOB_START_TIME, AbstractJobUsageRecord.JOB_END_TIME); + AbstractJobUsageRecord.DURATION, AbstractJobUsageRecord.JOB_START_TIME, AbstractJobUsageRecord.JOB_END_TIME); value = wallDuration; } }catch(InvalidValueException e){ } diff --git a/src/test/java/org/gcube/accounting/datamodel/usagerecords/JobUsageRecordTest.java b/src/test/java/org/gcube/accounting/datamodel/usagerecords/JobUsageRecordTest.java index 9895afb..5d82c6d 100644 --- a/src/test/java/org/gcube/accounting/datamodel/usagerecords/JobUsageRecordTest.java +++ b/src/test/java/org/gcube/accounting/datamodel/usagerecords/JobUsageRecordTest.java @@ -34,7 +34,9 @@ public class JobUsageRecordTest extends ScopedTest { expectedRequiredFields.add(UsageRecord.SCOPE); expectedRequiredFields.add(UsageRecord.OPERATION_RESULT); expectedRequiredFields.add(AbstractJobUsageRecord.JOB_ID); - expectedRequiredFields.add(AbstractJobUsageRecord.WALL_DURATION); + expectedRequiredFields.add(AbstractJobUsageRecord.DURATION); + expectedRequiredFields.add(AbstractJobUsageRecord.JOB_END_TIME); + expectedRequiredFields.add(AbstractJobUsageRecord.JOB_START_TIME); return expectedRequiredFields; } diff --git a/src/test/java/org/gcube/testutility/TestUsageRecord.java b/src/test/java/org/gcube/testutility/TestUsageRecord.java index 354a260..78dfaef 100644 --- a/src/test/java/org/gcube/testutility/TestUsageRecord.java +++ b/src/test/java/org/gcube/testutility/TestUsageRecord.java @@ -37,6 +37,7 @@ public class TestUsageRecord { public final static String TEST_SERVICE_CLASS = "TestServiceClass"; public final static String TEST_SERVICE_NAME = "TestServiceName"; public final static String TEST_CALLED_METHOD = "TestCalledMethod"; + public final static String TEST_CALLER_QUALIFIER = "TestCallerQualifier"; public final static String TEST_CALLER_HOST = "remotehost"; public final static String TEST_HOST = "localhost"; @@ -85,7 +86,7 @@ public class TestUsageRecord { usageRecord.setCallerHost(TEST_CALLER_HOST); usageRecord.setHost(TEST_HOST); - + usageRecord.setCallerQualifier(TEST_CALLER_QUALIFIER); usageRecord.setServiceClass(TEST_SERVICE_CLASS); usageRecord.setServiceName(TEST_SERVICE_NAME); usageRecord.setCalledMethod(TEST_CALLED_METHOD); @@ -151,11 +152,7 @@ public class TestUsageRecord { usageRecord.setOperationResult(TEST_OPERATION_RESULT); usageRecord.setJobId(TEST_JOB_ID); - usageRecord.setJobName(TEST_JOB_NAME); - usageRecord.setJobQualifier(TEST_JOB_QUALIFIER); - - Calendar startTime = Calendar.getInstance(); Calendar endTime = Calendar.getInstance(); endTime.setTimeInMillis(startTime.getTimeInMillis() + HALF_DURATION); @@ -164,7 +161,6 @@ public class TestUsageRecord { usageRecord.setJobStartTime(startTime); usageRecord.setJobEndTime(endTime); - } catch (InvalidValueException e) { logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e); }