Refactoring

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-analytics@126269 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-03-24 17:59:34 +00:00
parent 9f3397483b
commit effe6b280a
5 changed files with 342 additions and 84 deletions

View File

@ -0,0 +1,70 @@
/**
*
*/
package org.gcube.accounting.analytics.exception;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class DuplicatedKeyFilterException extends Exception {
/**
* Generated serial Version UID
*/
private static final long serialVersionUID = -327144230654860518L;
/**
* Constructs a new exception with {@code null} as its detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*/
public DuplicatedKeyFilterException() {
super();
}
/**
* Constructs a new exception with the specified detail message. The cause
* is not initialized, and may subsequently be initialized by a call to
* initCause.
* @param message the detail message. The detail message is saved for later
* retrieval by the getMessage() method.
*/
public DuplicatedKeyFilterException(String message) {
super(message);
}
/**
* Constructs a new exception with the specified cause and a detail
* message of <tt>(cause==null ? null : cause.toString())</tt> (which
* typically contains the class and detail message of <tt>cause</tt>).
* This constructor is useful for exceptions that are little more than
* wrappers for other throwables (for example, {@link
* java.security.PrivilegedActionException}).
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public DuplicatedKeyFilterException(Throwable cause) {
super(cause);
}
/**
* Constructs a new exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public DuplicatedKeyFilterException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,70 @@
/**
*
*/
package org.gcube.accounting.analytics.exception;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class KeyException extends Exception {
/**
* Generated serial Version UID
*/
private static final long serialVersionUID = -327144230654860518L;
/**
* Constructs a new exception with {@code null} as its detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*/
public KeyException() {
super();
}
/**
* Constructs a new exception with the specified detail message. The cause
* is not initialized, and may subsequently be initialized by a call to
* initCause.
* @param message the detail message. The detail message is saved for later
* retrieval by the getMessage() method.
*/
public KeyException(String message) {
super(message);
}
/**
* Constructs a new exception with the specified cause and a detail
* message of <tt>(cause==null ? null : cause.toString())</tt> (which
* typically contains the class and detail message of <tt>cause</tt>).
* This constructor is useful for exceptions that are little more than
* wrappers for other throwables (for example, {@link
* java.security.PrivilegedActionException}).
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public KeyException(Throwable cause) {
super(cause);
}
/**
* Constructs a new exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public KeyException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,70 @@
/**
*
*/
package org.gcube.accounting.analytics.exception;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class ValueException extends Exception {
/**
* Generated serial Version UID
*/
private static final long serialVersionUID = -327144230654860518L;
/**
* Constructs a new exception with {@code null} as its detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*/
public ValueException() {
super();
}
/**
* Constructs a new exception with the specified detail message. The cause
* is not initialized, and may subsequently be initialized by a call to
* initCause.
* @param message the detail message. The detail message is saved for later
* retrieval by the getMessage() method.
*/
public ValueException(String message) {
super(message);
}
/**
* Constructs a new exception with the specified cause and a detail
* message of <tt>(cause==null ? null : cause.toString())</tt> (which
* typically contains the class and detail message of <tt>cause</tt>).
* This constructor is useful for exceptions that are little more than
* wrappers for other throwables (for example, {@link
* java.security.PrivilegedActionException}).
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public ValueException(Throwable cause) {
super(cause);
}
/**
* Constructs a new exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public ValueException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -12,16 +12,19 @@ import org.gcube.accounting.analytics.Filter;
import org.gcube.accounting.analytics.Info;
import org.gcube.accounting.analytics.NumberedFilter;
import org.gcube.accounting.analytics.TemporalConstraint;
import org.gcube.accounting.analytics.exception.DuplicatedKeyFilterException;
import org.gcube.accounting.analytics.exception.KeyException;
import org.gcube.accounting.analytics.exception.ValueException;
import org.gcube.documentstore.records.AggregatedRecord;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/
public abstract class AccountingPersistenceBackendQuery {
public interface AccountingPersistenceBackendQuery {
public static final int KEY_VALUES_LIMIT = 25;
protected abstract void prepareConnection(
public void prepareConnection(
AccountingPersistenceBackendQueryConfiguration configuration)
throws Exception;
@ -43,13 +46,16 @@ public abstract class AccountingPersistenceBackendQuery {
* than one filter with the same key an Exception is thrown.
* @return the Map containing for each date in the required interval the
* requested data
* @throws DuplicatedKeyFilterException
* @throws KeyException
* @throws ValueException
* @throws Exception
* if fails
*/
public abstract SortedMap<Calendar, Info> getTimeSeries(
Class<? extends AggregatedRecord<?,?>> clz,
public SortedMap<Calendar, Info> getTimeSeries(
Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters)
throws Exception;
throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception;
/**
* Return a SortedMap containing the TimeSeries for top values for a certain
@ -63,73 +69,47 @@ public abstract class AccountingPersistenceBackendQuery {
* @param temporalConstraint
* the TemporalConstraint (interval and aggregation)
* @param filters
* list of filter to obtain the time series. If null or empty
* list get all data for the interested Record Class with the
* applying temporal constraint. All Filter (except one) must
* have not null and not empty key and value. One Filter must
* have not null and not empty key and a null value. The filters
* are must be related to different keys and are in AND. If the
* list contains more than one filter with the same key an
* Exception is thrown. If the list contains more than one filter
* with null value an Exception is thrown.
* list of filter to obtain the time series of top values. If
* null or empty list get all data for the interested Record
* Class with the applying temporal constraint. All Filter must
* have not null and not empty key and value. The filters are
* must be related to different keys and are in AND. If the list
* contains more than one filter with the same key an Exception
* is thrown.
* @param topKey
* @param orderingProperty
* @return a SortedMap
* @throws DuplicatedKeyFilterException
* @throws KeyException
* @throws ValueException
* @throws Exception
* if fails
*/
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters)
throws Exception {
String orderingProperty = AccountingPersistenceQuery
.getDefaultOrderingProperties(clz);
return getTopValues(clz, temporalConstraint, filters,
orderingProperty);
}
public abstract SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,
String orderingProperty) throws Exception;
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>>
getTopValues(Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,
String topKey, String orderingProperty)
throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception;
/**
* Return the list of possible values for a key for a certain usageRecord
* taking in account all Filters. The value for a certain key is identified
* adding a Filter with a null value. Only one Filter with null value is
* allowed otherwise an Exception is thrown. The values are ordered from the
* most occurred value.
*
* @param clz
* the Usage Record Class of interest
* @param temporalConstraint
* the TemporalConstraint (interval and aggregation)
* @param filters
* list of filter to obtain the time series. If null or empty
* list get all data for the interested Record Class with the
* applying temporal constraint. All Filter (except one) must
* have not null and not empty key and value. One Filter must
* have not null and not empty key and a null value. The filters
* are must be related to different keys and are in AND. If the
* list contains more than one filter with the same key an
* Exception is thrown. If the list contains more than one filter
* with null value an Exception is thrown.
* @return a SortedSet containing the list of possible values
* @param topKey
* @param orderingProperty
* @return
* @throws DuplicatedKeyFilterException
* @throws KeyException
* @throws ValueException
* @throws Exception
* if fails
*/
public SortedSet<NumberedFilter> getNextPossibleValues(
Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters)
throws Exception {
String orderingProperty = AccountingPersistenceQuery
.getDefaultOrderingProperties(clz);
return getNextPossibleValues(clz, temporalConstraint,
filters, orderingProperty);
}
public abstract SortedSet<NumberedFilter> getNextPossibleValues(
Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,
String orderingProperty) throws Exception;
String topKey, String orderingProperty) throws
DuplicatedKeyFilterException, KeyException, ValueException,
Exception;
/**
* Close the connection to persistence
@ -137,6 +117,6 @@ public abstract class AccountingPersistenceBackendQuery {
* @throws Exception
* if the close fails
*/
public abstract void close() throws Exception;
public void close() throws Exception;
}

View File

@ -13,10 +13,15 @@ import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.activity.InvalidActivityException;
import org.gcube.accounting.analytics.Filter;
import org.gcube.accounting.analytics.Info;
import org.gcube.accounting.analytics.NumberedFilter;
import org.gcube.accounting.analytics.TemporalConstraint;
import org.gcube.accounting.analytics.exception.DuplicatedKeyFilterException;
import org.gcube.accounting.analytics.exception.KeyException;
import org.gcube.accounting.analytics.exception.ValueException;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
import org.gcube.documentstore.records.AggregatedRecord;
@ -28,7 +33,7 @@ import org.json.JSONObject;
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class AccountingPersistenceQuery {
public class AccountingPersistenceQuery implements AccountingPersistenceBackendQuery {
private static final AccountingPersistenceQuery accountingPersistenceQuery;
@ -68,7 +73,8 @@ public class AccountingPersistenceQuery {
return getQuerableKeys(instance);
}
public static String getDefaultOrderingProperties(Class<? extends AggregatedRecord<?, ?>> clz){
public static String getDefaultOrderingProperties(
Class<? extends AggregatedRecord<?, ?>> clz){
if(clz.isAssignableFrom(AggregatedStorageUsageRecord.class)){
return AggregatedStorageUsageRecord.DATA_VOLUME;
}
@ -118,11 +124,24 @@ public class AccountingPersistenceQuery {
}
return unpaddedData;
}
/**
* {@inheritDoc}
*/
public SortedMap<Calendar, Info> getTimeSeries(
Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters)
throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception {
return this.getTimeSeries(clz, temporalConstraint, filters, false);
}
public SortedMap<Calendar, Info> getTimeSeries(
Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,
boolean pad) throws Exception {
boolean pad) throws DuplicatedKeyFilterException, KeyException,
ValueException, Exception {
SortedMap<Calendar, Info> ret =
AccountingPersistenceBackendQueryFactory.getInstance()
.getTimeSeries(clz, temporalConstraint,
@ -136,9 +155,11 @@ public class AccountingPersistenceQuery {
}
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,
String orderingProperty, boolean pad, int limit) throws Exception {
Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,
String topKey, String orderingProperty, boolean pad, int limit)
throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception {
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> got;
@ -148,15 +169,17 @@ public class AccountingPersistenceQuery {
got = AccountingPersistenceBackendQueryFactory.getInstance()
.getTopValues(clz, temporalConstraint,
filters, orderingProperty);
filters, topKey, orderingProperty);
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> ret = new TreeMap<NumberedFilter, SortedMap<Calendar,Info>>();
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> ret =
new TreeMap<NumberedFilter, SortedMap<Calendar,Info>>();
if(pad){
int count = got.size() > limit ? limit : got.size();
while(--count >= 0){
for(NumberedFilter nf : got.keySet()){
SortedMap<Calendar, Info> m = padMap(got.get(nf), temporalConstraint);
SortedMap<Calendar, Info> m =
padMap(got.get(nf), temporalConstraint);
ret.put(nf, m);
}
}
@ -167,31 +190,76 @@ public class AccountingPersistenceQuery {
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters)
throws Exception {
return AccountingPersistenceBackendQueryFactory.getInstance()
.getTopValues(clz, temporalConstraint,
filters);
TemporalConstraint temporalConstraint, List<Filter> filters,
String topKey) throws DuplicatedKeyFilterException,
KeyException, ValueException, Exception {
String orderingProperty = AccountingPersistenceQuery
.getDefaultOrderingProperties(clz);
return this.getTopValues(clz, temporalConstraint, filters, topKey,
orderingProperty, false, 0);
}
/**
* {@inheritDoc}
*/
@Override
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,
String topKey, String orderingProperty) throws
DuplicatedKeyFilterException, KeyException, ValueException,
Exception {
return this.getTopValues(clz, temporalConstraint, filters, topKey,
orderingProperty, false, 0);
}
public SortedSet<NumberedFilter> getNextPossibleValues(
Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters)
throws Exception {
return AccountingPersistenceBackendQueryFactory.getInstance()
.getNextPossibleValues(clz,
temporalConstraint, filters);
TemporalConstraint temporalConstraint, List<Filter> filters,
String topKey) throws DuplicatedKeyFilterException, KeyException,
ValueException, Exception {
String orderingProperty = AccountingPersistenceQuery
.getDefaultOrderingProperties(clz);
return this.getNextPossibleValues(clz, temporalConstraint, filters,
topKey, orderingProperty);
}
/**
* {@inheritDoc}
*/
@Override
public SortedSet<NumberedFilter> getNextPossibleValues(
Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,
String topKey, String orderingProperty) throws
DuplicatedKeyFilterException, KeyException, ValueException,
Exception {
return AccountingPersistenceBackendQueryFactory.getInstance()
.getNextPossibleValues(clz, temporalConstraint, filters,
topKey, orderingProperty);
}
/**
* Close the connection to persistence
*
* @throws Exception
* if the close fails
* {@inheritDoc}
*/
@Override
public void close() throws Exception {
AccountingPersistenceBackendQueryFactory.getInstance().close();
}
/**
* {@inheritDoc}
*/
@Override
public void prepareConnection(
AccountingPersistenceBackendQueryConfiguration configuration)
throws Exception {
throw new InvalidActivityException();
}
}