diff --git a/src/main/java/org/gcube/accounting/analytics/NumberedFilter.java b/src/main/java/org/gcube/accounting/analytics/NumberedFilter.java index acabc52..17afb9a 100644 --- a/src/main/java/org/gcube/accounting/analytics/NumberedFilter.java +++ b/src/main/java/org/gcube/accounting/analytics/NumberedFilter.java @@ -6,7 +6,6 @@ package org.gcube.accounting.analytics; import java.util.Calendar; import java.util.Map; -import org.json.JSONException; import org.json.JSONObject; /** @@ -16,45 +15,63 @@ import org.json.JSONObject; public class NumberedFilter extends Filter { protected Double d; + protected String orderingProperty; - public NumberedFilter(String key, String value, Number n) { + public NumberedFilter(String key, String value, Number n, String orderingProperty) { super(key, value); this.d = n.doubleValue(); + this.orderingProperty = orderingProperty; } - public NumberedFilter(Filter filter, Number n) { - this(filter.key, filter.value, n); + public NumberedFilter(Filter filter, Number n, String orderingProperty) { + this(filter.key, filter.value, n, orderingProperty); } - public NumberedFilter(Filter filter, Map timeSeries, String orderingProperty) throws JSONException { + public NumberedFilter(Filter filter, Map timeSeries, String orderingProperty) throws Exception { super(filter.key, filter.value); + this.d = new Double(0); + this.orderingProperty = orderingProperty; + for(Info info : timeSeries.values()){ JSONObject value = info.getValue(); - - if(d == null){ - d = value.getDouble(orderingProperty); + if(this.d == null){ + this.d = value.getDouble(orderingProperty); }else{ - d = d + value.getDouble(orderingProperty); + this.d = this.d + value.getDouble(orderingProperty); } } } /** - * @return the number + * @return the d */ - public Number getNumber() { + public Double getDouble() { return d; } /** - * @param number the number to set + * @param d the d to set */ - public void setNumber(Number n) { - this.d = n.doubleValue(); + public void setDouble(Double d) { + this.d = d; } - + + /** + * @return the orderingProperty + */ + public String getOrderingProperty() { + return orderingProperty; + } + + /** + * @param orderingProperty the orderingProperty to set + */ + public void setOrderingProperty(String orderingProperty) { + this.orderingProperty = orderingProperty; + } + /** {@inheritDoc} */ public int compareTo(NumberedFilter numberedFilter) { int compareResult = this.d.compareTo(numberedFilter.d); diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java index 5509b51..99c19f8 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java @@ -5,12 +5,12 @@ package org.gcube.accounting.analytics.persistence; import java.util.Calendar; import java.util.List; -import java.util.Map; import java.util.SortedMap; import java.util.SortedSet; 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.documentstore.records.AggregatedRecord; @@ -18,91 +18,125 @@ import org.gcube.documentstore.records.AggregatedRecord; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ */ public abstract class AccountingPersistenceBackendQuery { - + public static final int KEY_VALUES_LIMIT = 25; - - protected abstract void prepareConnection(AccountingPersistenceBackendQueryConfiguration configuration) throws Exception; - + + protected abstract void prepareConnection( + AccountingPersistenceBackendQueryConfiguration configuration) + throws Exception; + /** - * Query the persistence obtaining a Map where the date is the key and - * the #Info is the value. The result is relative to an Usage Record Type, + * Query the persistence obtaining a Map where the date is the key and the + * #Info is the value. The result is relative to an Usage Record Type, * respect a TemporalConstraint and can be applied one or more filters. - * @param aggregatedRecordClass the 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 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 aggregatedRecordClass + * the 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 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. * @return the Map containing for each date in the required interval the - * requested data - * @throws Exception if fails + * requested data + * @throws Exception + * if fails */ public abstract SortedMap getTimeSeries( - Class> aggregatedRecordClass, - TemporalConstraint temporalConstraint, - List filters) throws Exception; - - - /** - * Return a SortedMap containing the TimeSeries for top values for a - * certain key taking in account all Filters. The 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 aggregatedRecordClass 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 set containing the list of possible values - * @throws Exception if fails - */ - public abstract SortedMap> getTopValues( - Class> recordClass, + Class> aggregatedRecordClass, TemporalConstraint temporalConstraint, List filters) throws 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 aggregatedRecordClass 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 set containing the list of possible values - * @throws Exception if fails + * Return a SortedMap containing the TimeSeries for top values for a certain + * key taking in account all Filters. The 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 aggregatedRecordClass + * 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 SortedMap + * @throws Exception + * if fails */ - public abstract SortedSet getNextPossibleValues( - Class> aggregatedRecordClass, - TemporalConstraint temporalConstraint, - List filters) throws Exception; - + public SortedMap> getTopValues( + Class> aggregatedRecordClass, + TemporalConstraint temporalConstraint, List filters) + throws Exception { + String orderingProperty = AccountingPersistenceQuery + .getDefaultOrderingProperties(aggregatedRecordClass); + return getTopValues(aggregatedRecordClass, temporalConstraint, filters, + orderingProperty); + } + + public abstract SortedMap> getTopValues( + Class> aggregatedRecordClass, + TemporalConstraint temporalConstraint, List filters, + String orderingProperty) throws 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 aggregatedRecordClass + * 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 + * @throws Exception + * if fails + */ + public SortedSet getNextPossibleValues( + Class> aggregatedRecordClass, + TemporalConstraint temporalConstraint, List filters) + throws Exception { + String orderingProperty = AccountingPersistenceQuery + .getDefaultOrderingProperties(aggregatedRecordClass); + return getNextPossibleValues(aggregatedRecordClass, temporalConstraint, + filters, orderingProperty); + } + + public abstract SortedSet getNextPossibleValues( + Class> aggregatedRecordClass, + TemporalConstraint temporalConstraint, List filters, + String orderingProperty) throws Exception; + /** * Close the connection to persistence - * @throws Exception if the close fails + * + * @throws Exception + * if the close fails */ public abstract void close() throws Exception; - + } diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java index eae42bc..9323a0b 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java @@ -5,15 +5,16 @@ package org.gcube.accounting.analytics.persistence; import java.util.Calendar; import java.util.List; -import java.util.Map; import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeSet; 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.datamodel.UsageRecord; +import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord; import org.gcube.documentstore.records.AggregatedRecord; import org.gcube.documentstore.records.Record; @@ -59,7 +60,7 @@ public class AccountingPersistenceQuery { return getQuerableKeys(instance); } - public Map getTimeSeries( + public SortedMap getTimeSeries( Class> aggregatedRecordClass, TemporalConstraint temporalConstraint, List filters) throws Exception { @@ -68,7 +69,17 @@ public class AccountingPersistenceQuery { filters); } - public static SortedMap> getTopValues( + public static String getDefaultOrderingProperties(Class> recordClass){ + if(recordClass.isAssignableFrom(AggregatedStorageUsageRecord.class)){ + return AggregatedStorageUsageRecord.DATA_VOLUME; + } + return AggregatedRecord.OPERATION_COUNT; + } + + + + + public static SortedMap> getTopValues( Class> aggregatedRecordClass, TemporalConstraint temporalConstraint, List filters) throws Exception { @@ -77,7 +88,7 @@ public class AccountingPersistenceQuery { filters); } - public static SortedSet getNextPossibleValues( + public static SortedSet getNextPossibleValues( Class> aggregatedRecordClass, TemporalConstraint temporalConstraint, List filters) throws Exception {