This commit is contained in:
Alessandro Pieve 2016-06-28 13:00:55 +00:00
parent a6bb27420e
commit 6413cd9cf6
7 changed files with 188 additions and 83 deletions

View File

@ -20,6 +20,7 @@
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">

View File

@ -5,6 +5,11 @@
<projects> <projects>
</projects> </projects>
<buildSpec> <buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand> <buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name> <name>org.eclipse.jdt.core.javabuilder</name>
<arguments> <arguments>
@ -15,9 +20,17 @@
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature> <nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

View File

@ -1,5 +1,8 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7 org.eclipse.jdt.core.compiler.source=1.7

View File

@ -20,7 +20,8 @@ public class NoUsableAccountingPersistenceQueryFound extends Exception {
* call to {@link #initCause}. * call to {@link #initCause}.
*/ */
public NoUsableAccountingPersistenceQueryFound() { public NoUsableAccountingPersistenceQueryFound() {
super(); super("No Usable Accounting Persistence Query Found");
} }
/** /**

View File

@ -5,6 +5,7 @@ package org.gcube.accounting.analytics.persistence;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.SortedSet; import java.util.SortedSet;
@ -16,6 +17,7 @@ import org.gcube.accounting.analytics.exception.DuplicatedKeyFilterException;
import org.gcube.accounting.analytics.exception.KeyException; import org.gcube.accounting.analytics.exception.KeyException;
import org.gcube.accounting.analytics.exception.ValueException; import org.gcube.accounting.analytics.exception.ValueException;
import org.gcube.documentstore.records.AggregatedRecord; import org.gcube.documentstore.records.AggregatedRecord;
import org.json.JSONObject;
/** /**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
@ -26,7 +28,7 @@ public interface AccountingPersistenceBackendQuery {
public void prepareConnection( public void prepareConnection(
AccountingPersistenceBackendQueryConfiguration configuration) AccountingPersistenceBackendQueryConfiguration configuration)
throws Exception; throws Exception;
/** /**
* Query the persistence obtaining a Map where the date is the key and the * Query the persistence obtaining a Map where the date is the key and the
@ -53,9 +55,12 @@ public interface AccountingPersistenceBackendQuery {
*/ */
public SortedMap<Calendar, Info> getTimeSeries( public SortedMap<Calendar, Info> getTimeSeries(
Class<? extends AggregatedRecord<?, ?>> clz, Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters) TemporalConstraint temporalConstraint, List<Filter> filters)
throws DuplicatedKeyFilterException, KeyException, ValueException, throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception; Exception;
/** /**
* Return a SortedMap containing the TimeSeries for top values for a certain * Return a SortedMap containing the TimeSeries for top values for a certain
@ -85,11 +90,11 @@ public interface AccountingPersistenceBackendQuery {
* @throws Exception * @throws Exception
*/ */
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> public SortedMap<NumberedFilter, SortedMap<Calendar, Info>>
getTopValues(Class<? extends AggregatedRecord<?,?>> clz, getTopValues(Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters, TemporalConstraint temporalConstraint, List<Filter> filters,
String topKey, String orderingProperty) String topKey, String orderingProperty)
throws DuplicatedKeyFilterException, KeyException, ValueException, throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception; Exception;
/** /**
* *
@ -106,10 +111,10 @@ public interface AccountingPersistenceBackendQuery {
*/ */
public SortedSet<NumberedFilter> getNextPossibleValues( public SortedSet<NumberedFilter> getNextPossibleValues(
Class<? extends AggregatedRecord<?, ?>> clz, Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters, TemporalConstraint temporalConstraint, List<Filter> filters,
String key, String orderingProperty) throws String key, String orderingProperty) throws
DuplicatedKeyFilterException, KeyException, ValueException, DuplicatedKeyFilterException, KeyException, ValueException,
Exception; Exception;
/** /**
* Close the connection to persistence * Close the connection to persistence
@ -119,4 +124,48 @@ public interface AccountingPersistenceBackendQuery {
*/ */
public void close() throws Exception; public void close() throws Exception;
/**
* Return a sortedSet filter value
*
*
* @param clz
* @param temporalConstraint
* @param filters
* @param key
* @param orderingProperty
* @return
* @throws DuplicatedKeyFilterException
* @throws KeyException
* @throws ValueException
* @throws Exception
*/
public SortedSet<NumberedFilter> getFilterValues(
Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,
String key) throws Exception;
/**
* Return a JsonObject with value
* e.g.for StorageUsageRecord {"dataVolume":1860328,"operationCount":4115}
* e.g. for ServiceUsageRcord {"operationCount":1651624}
*
* @param clz
* the Usage Record Class of interest
* @param temporalConstraint
* the TemporalConstraint (interval and aggregation)
* @param applicant
* the type field and value
* @return
* @throws Exception
*/
public JSONObject getUsageValue(Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, Filter applicant)
throws Exception;
} }

View File

@ -46,8 +46,9 @@ public abstract class AccountingPersistenceBackendQueryFactory {
try { try {
ServiceLoader<AccountingPersistenceBackendQuery> serviceLoader = ServiceLoader.load(AccountingPersistenceBackendQuery.class); ServiceLoader<AccountingPersistenceBackendQuery> serviceLoader = ServiceLoader.load(AccountingPersistenceBackendQuery.class);
for (AccountingPersistenceBackendQuery found : serviceLoader) { for (AccountingPersistenceBackendQuery found : serviceLoader) {
Class<? extends AccountingPersistenceBackendQuery> foundClass = found.getClass(); Class<? extends AccountingPersistenceBackendQuery> foundClass=null;
try { try {
foundClass = found.getClass();
String foundClassName = foundClass.getSimpleName(); String foundClassName = foundClass.getSimpleName();
logger.debug("Testing {}", foundClassName); logger.debug("Testing {}", foundClassName);
AccountingPersistenceBackendQueryConfiguration configuration = new AccountingPersistenceBackendQueryConfiguration(foundClass); AccountingPersistenceBackendQueryConfiguration configuration = new AccountingPersistenceBackendQueryConfiguration(foundClass);
@ -55,14 +56,17 @@ public abstract class AccountingPersistenceBackendQueryFactory {
accountingPersistenceQuery = found; accountingPersistenceQuery = found;
break; break;
} catch (Exception e) { } catch (Exception e) {
logger.debug(String.format("%s not initialized correctly. It will not be used", foundClass.getSimpleName())); logger.error(String.format("%s not initialized correctly. It will not be used", foundClass.getSimpleName()));
} }
} }
} catch(Exception e){ } catch(Exception e){
throw new NoUsableAccountingPersistenceQueryFound(); logger.error(String.format("service loader or not initialized correctly."));
throw new NoUsableAccountingPersistenceQueryFound(e.getLocalizedMessage());
} }
if(accountingPersistenceQuery==null){ if(accountingPersistenceQuery==null){
logger.error(String.format("accountingPersistenceQuery null"));
throw new NoUsableAccountingPersistenceQueryFound(); throw new NoUsableAccountingPersistenceQueryFound();
} }

View File

@ -10,6 +10,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet; import java.util.TreeSet;
import javax.activity.InvalidActivityException; import javax.activity.InvalidActivityException;
@ -37,7 +38,7 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
private static final AccountingPersistenceQuery accountingPersistenceQuery; private static final AccountingPersistenceQuery accountingPersistenceQuery;
public static final int DEFAULT_LIMIT_RESULT_NUMBER = 5; public static final int DEFAULT_LIMIT_RESULT_NUMBER = 5;
private AccountingPersistenceQuery() { private AccountingPersistenceQuery() {
} }
@ -51,7 +52,7 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
public static SortedSet<String> getQuerableKeys( public static SortedSet<String> getQuerableKeys(
@SuppressWarnings("rawtypes") AggregatedRecord instance) @SuppressWarnings("rawtypes") AggregatedRecord instance)
throws Exception { throws Exception {
SortedSet<String> properties = new TreeSet<>( SortedSet<String> properties = new TreeSet<>(
instance.getRequiredFields()); instance.getRequiredFields());
@ -67,11 +68,11 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
public static SortedSet<String> getQuerableKeys( public static SortedSet<String> getQuerableKeys(
Class<? extends AggregatedRecord<?,?>> clz) Class<? extends AggregatedRecord<?,?>> clz)
throws Exception { throws Exception {
AggregatedRecord<?,?> instance = clz.newInstance(); AggregatedRecord<?,?> instance = clz.newInstance();
return getQuerableKeys(instance); return getQuerableKeys(instance);
} }
public static String getDefaultOrderingProperties( public static String getDefaultOrderingProperties(
Class<? extends AggregatedRecord<?, ?>> clz){ Class<? extends AggregatedRecord<?, ?>> clz){
if(clz.isAssignableFrom(AggregatedStorageUsageRecord.class)){ if(clz.isAssignableFrom(AggregatedStorageUsageRecord.class)){
@ -79,23 +80,26 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
} }
return AggregatedRecord.OPERATION_COUNT; return AggregatedRecord.OPERATION_COUNT;
} }
protected static JSONObject getPaddingJSONObject( protected static JSONObject getPaddingJSONObject(
Map<Calendar, Info> unpaddedResults) throws JSONException { Map<Calendar, Info> unpaddedResults) throws JSONException {
Info auxInfo = new ArrayList<Info>(unpaddedResults.values()).get(0);
JSONObject auxJsonObject = auxInfo.getValue();
@SuppressWarnings("unchecked")
Iterator<String> keys = auxJsonObject.keys();
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
while (keys.hasNext()) { //verify data consistency
String key = keys.next(); if (unpaddedResults.size()!=0){
jsonObject.put(key, 0); Info auxInfo = new ArrayList<Info>(unpaddedResults.values()).get(0);
JSONObject auxJsonObject = auxInfo.getValue();
@SuppressWarnings("unchecked")
Iterator<String> keys = auxJsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
jsonObject.put(key, 0);
}
} }
return jsonObject; return jsonObject;
} }
/** /**
* Pad the data * Pad the data
* *
@ -111,7 +115,7 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
public static SortedMap<Calendar, Info> padMap( public static SortedMap<Calendar, Info> padMap(
SortedMap<Calendar, Info> unpaddedData, SortedMap<Calendar, Info> unpaddedData,
TemporalConstraint temporalConstraint) throws Exception { TemporalConstraint temporalConstraint) throws Exception {
JSONObject jsonObject = getPaddingJSONObject(unpaddedData); JSONObject jsonObject = getPaddingJSONObject(unpaddedData);
SortedSet<Calendar> sequence = temporalConstraint.getCalendarSequence(); SortedSet<Calendar> sequence = temporalConstraint.getCalendarSequence();
for (Calendar progressTime : sequence) { for (Calendar progressTime : sequence) {
@ -123,58 +127,66 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
} }
return unpaddedData; return unpaddedData;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public SortedMap<Calendar, Info> getTimeSeries( public SortedMap<Calendar, Info> getTimeSeries(
Class<? extends AggregatedRecord<?,?>> clz, Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters) TemporalConstraint temporalConstraint, List<Filter> filters)
throws DuplicatedKeyFilterException, KeyException, ValueException, throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception { Exception {
return this.getTimeSeries(clz, temporalConstraint, filters, false); return this.getTimeSeries(clz, temporalConstraint, filters, false);
} }
public SortedMap<Calendar, Info> getTimeSeries( public SortedMap<Calendar, Info> getTimeSeries(
Class<? extends AggregatedRecord<?,?>> clz, Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters, TemporalConstraint temporalConstraint, List<Filter> filters,
boolean pad) throws DuplicatedKeyFilterException, KeyException, boolean pad) throws DuplicatedKeyFilterException, KeyException,
ValueException, Exception { ValueException, Exception {
SortedMap<Calendar, Info> ret = SortedMap<Calendar, Info> ret =
AccountingPersistenceBackendQueryFactory.getInstance() AccountingPersistenceBackendQueryFactory.getInstance()
.getTimeSeries(clz, temporalConstraint, .getTimeSeries(clz, temporalConstraint,
filters); filters);
if(ret==null){
ret = new TreeMap<>();
}
if(pad){ if(pad){
ret = padMap(ret, temporalConstraint); ret = padMap(ret, temporalConstraint);
} }
return ret; return ret;
} }
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues( public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?, ?>> clz, Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters, TemporalConstraint temporalConstraint, List<Filter> filters,
String topKey, String orderingProperty, boolean pad, int limit) String topKey, String orderingProperty, boolean pad, int limit)
throws DuplicatedKeyFilterException, KeyException, ValueException, throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception { Exception {
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> got; SortedMap<NumberedFilter, SortedMap<Calendar, Info>> got;
if(orderingProperty==null){ if(orderingProperty==null){
orderingProperty = getDefaultOrderingProperties(clz); orderingProperty = getDefaultOrderingProperties(clz);
} }
got = AccountingPersistenceBackendQueryFactory.getInstance() got = AccountingPersistenceBackendQueryFactory.getInstance()
.getTopValues(clz, temporalConstraint, filters, topKey, .getTopValues(clz, temporalConstraint, filters, topKey,
orderingProperty); orderingProperty);
int count = got.size() > limit ? limit : got.size(); int count = got.size() > limit ? limit : got.size();
NumberedFilter firstRemovalKey = null; NumberedFilter firstRemovalKey = null;
for(NumberedFilter nf : got.keySet()){ for(NumberedFilter nf : got.keySet()){
if(--count>=0 || limit<=0){ if(--count>=0 || limit<=0){
if(pad){ if(pad){
@ -188,70 +200,73 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
} }
} }
} }
if(firstRemovalKey!=null){ if(firstRemovalKey!=null){
return got.subMap(got.firstKey(), firstRemovalKey); return got.subMap(got.firstKey(), firstRemovalKey);
} }
return got; return got;
} }
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues( public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?,?>> clz, Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters, TemporalConstraint temporalConstraint, List<Filter> filters,
String topKey) throws DuplicatedKeyFilterException, String topKey) throws DuplicatedKeyFilterException,
KeyException, ValueException, Exception { KeyException, ValueException, Exception {
String orderingProperty = AccountingPersistenceQuery String orderingProperty = AccountingPersistenceQuery
.getDefaultOrderingProperties(clz); .getDefaultOrderingProperties(clz);
return this.getTopValues(clz, temporalConstraint, filters, topKey, return this.getTopValues(clz, temporalConstraint, filters, topKey,
orderingProperty, false, 0); orderingProperty, false, 0);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues( public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?,?>> clz, Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters, TemporalConstraint temporalConstraint, List<Filter> filters,
String topKey, String orderingProperty) throws String topKey, String orderingProperty) throws
DuplicatedKeyFilterException, KeyException, ValueException, DuplicatedKeyFilterException, KeyException, ValueException,
Exception { Exception {
return this.getTopValues(clz, temporalConstraint, filters, topKey, return this.getTopValues(clz, temporalConstraint, filters, topKey,
orderingProperty, false, 0); orderingProperty, false, 0);
} }
public SortedSet<NumberedFilter> getNextPossibleValues( public SortedSet<NumberedFilter> getNextPossibleValues(
Class<? extends AggregatedRecord<?,?>> clz, Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters, TemporalConstraint temporalConstraint, List<Filter> filters,
String key) throws DuplicatedKeyFilterException, KeyException, String key) throws DuplicatedKeyFilterException, KeyException,
ValueException, Exception { ValueException, Exception {
String orderingProperty = AccountingPersistenceQuery String orderingProperty = AccountingPersistenceQuery
.getDefaultOrderingProperties(clz); .getDefaultOrderingProperties(clz);
return this.getNextPossibleValues(clz, temporalConstraint, filters, return this.getNextPossibleValues(clz, temporalConstraint, filters,
key, orderingProperty); key, orderingProperty);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public SortedSet<NumberedFilter> getNextPossibleValues( public SortedSet<NumberedFilter> getNextPossibleValues(
Class<? extends AggregatedRecord<?, ?>> clz, Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters, TemporalConstraint temporalConstraint, List<Filter> filters,
String key, String orderingProperty) throws String key, String orderingProperty) throws
DuplicatedKeyFilterException, KeyException, ValueException, DuplicatedKeyFilterException, KeyException, ValueException,
Exception { Exception {
return AccountingPersistenceBackendQueryFactory.getInstance() return AccountingPersistenceBackendQueryFactory.getInstance()
.getNextPossibleValues(clz, temporalConstraint, filters, .getNextPossibleValues(clz, temporalConstraint, filters,
key, orderingProperty); key, orderingProperty);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -266,8 +281,27 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
@Override @Override
public void prepareConnection( public void prepareConnection(
AccountingPersistenceBackendQueryConfiguration configuration) AccountingPersistenceBackendQueryConfiguration configuration)
throws Exception { throws Exception {
throw new InvalidActivityException(); throw new InvalidActivityException();
} }
@Override
public SortedSet<NumberedFilter> getFilterValues(
Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,
String key) throws Exception {
// TODO Auto-generated method stub
return AccountingPersistenceBackendQueryFactory.getInstance()
.getFilterValues(clz, temporalConstraint, filters,
key);
}
@Override
public JSONObject getUsageValue(Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, Filter applicant)
throws Exception {
// TODO Auto-generated method stub
return null;
}
} }