2015-07-08 15:24:32 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.accounting.analytics.persistence;
|
|
|
|
|
2016-03-16 17:30:26 +01:00
|
|
|
import java.util.ArrayList;
|
2015-07-21 16:33:36 +02:00
|
|
|
import java.util.Calendar;
|
2016-03-16 17:30:26 +01:00
|
|
|
import java.util.Iterator;
|
2015-07-08 17:28:33 +02:00
|
|
|
import java.util.List;
|
2016-03-16 17:30:26 +01:00
|
|
|
import java.util.Map;
|
2016-03-16 13:29:03 +01:00
|
|
|
import java.util.SortedMap;
|
|
|
|
import java.util.SortedSet;
|
2016-06-28 15:00:55 +02:00
|
|
|
import java.util.TreeMap;
|
2015-07-08 17:28:33 +02:00
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
import javax.activity.InvalidActivityException;
|
|
|
|
|
2015-07-08 17:28:33 +02:00
|
|
|
import org.gcube.accounting.analytics.Filter;
|
|
|
|
import org.gcube.accounting.analytics.Info;
|
2016-03-16 15:53:59 +01:00
|
|
|
import org.gcube.accounting.analytics.NumberedFilter;
|
2015-07-08 17:28:33 +02:00
|
|
|
import org.gcube.accounting.analytics.TemporalConstraint;
|
2016-03-24 18:59:34 +01:00
|
|
|
import org.gcube.accounting.analytics.exception.DuplicatedKeyFilterException;
|
|
|
|
import org.gcube.accounting.analytics.exception.KeyException;
|
|
|
|
import org.gcube.accounting.analytics.exception.ValueException;
|
2016-03-16 15:53:59 +01:00
|
|
|
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
|
2015-12-18 17:13:22 +01:00
|
|
|
import org.gcube.documentstore.records.AggregatedRecord;
|
2016-03-16 17:30:26 +01:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2015-07-08 17:28:33 +02:00
|
|
|
|
2015-07-08 15:24:32 +02:00
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
2016-03-16 13:29:03 +01:00
|
|
|
*
|
2015-07-08 15:24:32 +02:00
|
|
|
*/
|
2016-03-24 18:59:34 +01:00
|
|
|
public class AccountingPersistenceQuery implements AccountingPersistenceBackendQuery {
|
2015-10-14 15:10:06 +02:00
|
|
|
|
|
|
|
private static final AccountingPersistenceQuery accountingPersistenceQuery;
|
2016-03-16 13:29:03 +01:00
|
|
|
|
2016-03-16 17:30:26 +01:00
|
|
|
public static final int DEFAULT_LIMIT_RESULT_NUMBER = 5;
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-16 13:29:03 +01:00
|
|
|
private AccountingPersistenceQuery() {
|
|
|
|
}
|
|
|
|
|
2015-10-14 15:10:06 +02:00
|
|
|
static {
|
|
|
|
accountingPersistenceQuery = new AccountingPersistenceQuery();
|
|
|
|
}
|
2016-03-16 13:29:03 +01:00
|
|
|
|
|
|
|
protected static synchronized AccountingPersistenceQuery getInstance() {
|
2015-10-14 15:10:06 +02:00
|
|
|
return accountingPersistenceQuery;
|
|
|
|
}
|
2016-03-16 13:29:03 +01:00
|
|
|
|
2016-09-19 12:54:05 +02:00
|
|
|
@Deprecated
|
2016-03-16 13:29:03 +01:00
|
|
|
public static SortedSet<String> getQuerableKeys(
|
|
|
|
@SuppressWarnings("rawtypes") AggregatedRecord instance)
|
2016-06-28 15:00:55 +02:00
|
|
|
throws Exception {
|
2016-09-19 12:54:05 +02:00
|
|
|
return instance.getQuerableKeys();
|
2015-07-08 17:28:33 +02:00
|
|
|
}
|
2016-03-16 13:29:03 +01:00
|
|
|
|
|
|
|
public static SortedSet<String> getQuerableKeys(
|
2016-03-17 12:16:30 +01:00
|
|
|
Class<? extends AggregatedRecord<?,?>> clz)
|
2016-06-28 15:00:55 +02:00
|
|
|
throws Exception {
|
2016-03-17 12:16:30 +01:00
|
|
|
AggregatedRecord<?,?> instance = clz.newInstance();
|
2016-09-19 12:54:05 +02:00
|
|
|
return instance.getQuerableKeys();
|
2015-10-14 15:10:06 +02:00
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
public static String getDefaultOrderingProperties(
|
|
|
|
Class<? extends AggregatedRecord<?, ?>> clz){
|
2016-03-17 12:16:30 +01:00
|
|
|
if(clz.isAssignableFrom(AggregatedStorageUsageRecord.class)){
|
2016-03-16 15:53:59 +01:00
|
|
|
return AggregatedStorageUsageRecord.DATA_VOLUME;
|
|
|
|
}
|
|
|
|
return AggregatedRecord.OPERATION_COUNT;
|
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-16 17:30:26 +01:00
|
|
|
protected static JSONObject getPaddingJSONObject(
|
|
|
|
Map<Calendar, Info> unpaddedResults) throws JSONException {
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
2016-06-28 15:00:55 +02:00
|
|
|
//verify data consistency
|
|
|
|
if (unpaddedResults.size()!=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);
|
|
|
|
}
|
2016-03-16 17:30:26 +01:00
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-16 17:30:26 +01:00
|
|
|
return jsonObject;
|
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-16 17:30:26 +01:00
|
|
|
/**
|
|
|
|
* Pad the data
|
|
|
|
*
|
|
|
|
* @param unpaddedData
|
|
|
|
* the data to be pad
|
|
|
|
* @param temporalConstraint
|
|
|
|
* temporalConstraint the temporal interval and the granularity
|
|
|
|
* of the data to pad
|
|
|
|
* @return the data padded taking in account the TemporalConstraint
|
|
|
|
* @throws Exception
|
|
|
|
* if fails
|
|
|
|
*/
|
|
|
|
public static SortedMap<Calendar, Info> padMap(
|
|
|
|
SortedMap<Calendar, Info> unpaddedData,
|
|
|
|
TemporalConstraint temporalConstraint) throws Exception {
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-16 17:30:26 +01:00
|
|
|
JSONObject jsonObject = getPaddingJSONObject(unpaddedData);
|
|
|
|
SortedSet<Calendar> sequence = temporalConstraint.getCalendarSequence();
|
|
|
|
for (Calendar progressTime : sequence) {
|
|
|
|
Info info = unpaddedData.get(progressTime);
|
|
|
|
if (info == null) {
|
|
|
|
info = new Info(progressTime, jsonObject);
|
|
|
|
unpaddedData.put(progressTime, info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unpaddedData;
|
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public SortedMap<Calendar, Info> getTimeSeries(
|
|
|
|
Class<? extends AggregatedRecord<?,?>> clz,
|
2016-06-28 15:00:55 +02:00
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters)
|
|
|
|
throws DuplicatedKeyFilterException, KeyException, ValueException,
|
|
|
|
Exception {
|
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
return this.getTimeSeries(clz, temporalConstraint, filters, false);
|
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-17 12:05:17 +01:00
|
|
|
public SortedMap<Calendar, Info> getTimeSeries(
|
2016-03-17 12:16:30 +01:00
|
|
|
Class<? extends AggregatedRecord<?,?>> clz,
|
2016-06-28 15:00:55 +02:00
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters,
|
|
|
|
boolean pad) throws DuplicatedKeyFilterException, KeyException,
|
|
|
|
ValueException, Exception {
|
2016-03-17 12:05:17 +01:00
|
|
|
SortedMap<Calendar, Info> ret =
|
|
|
|
AccountingPersistenceBackendQueryFactory.getInstance()
|
2016-03-17 12:16:30 +01:00
|
|
|
.getTimeSeries(clz, temporalConstraint,
|
2016-03-17 12:05:17 +01:00
|
|
|
filters);
|
2016-06-28 15:00:55 +02:00
|
|
|
|
|
|
|
if(ret==null){
|
|
|
|
ret = new TreeMap<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-17 12:05:17 +01:00
|
|
|
if(pad){
|
|
|
|
ret = padMap(ret, temporalConstraint);
|
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-17 12:05:17 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
|
2016-03-24 18:59:34 +01:00
|
|
|
Class<? extends AggregatedRecord<?, ?>> clz,
|
2016-06-28 15:00:55 +02:00
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters,
|
|
|
|
String topKey, String orderingProperty, boolean pad, int limit)
|
|
|
|
throws DuplicatedKeyFilterException, KeyException, ValueException,
|
|
|
|
Exception {
|
2016-03-23 11:09:27 +01:00
|
|
|
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> got;
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-16 17:30:26 +01:00
|
|
|
if(orderingProperty==null){
|
2016-03-17 12:16:30 +01:00
|
|
|
orderingProperty = getDefaultOrderingProperties(clz);
|
2016-03-16 17:30:26 +01:00
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-23 11:09:27 +01:00
|
|
|
got = AccountingPersistenceBackendQueryFactory.getInstance()
|
2016-03-25 16:22:59 +01:00
|
|
|
.getTopValues(clz, temporalConstraint, filters, topKey,
|
2016-06-28 15:00:55 +02:00
|
|
|
orderingProperty);
|
|
|
|
|
2016-03-25 16:22:59 +01:00
|
|
|
int count = got.size() > limit ? limit : got.size();
|
|
|
|
NumberedFilter firstRemovalKey = null;
|
|
|
|
for(NumberedFilter nf : got.keySet()){
|
|
|
|
if(--count>=0 || limit<=0){
|
|
|
|
if(pad){
|
2016-09-08 10:46:05 +02:00
|
|
|
padMap(got.get(nf), temporalConstraint);
|
|
|
|
|
2016-03-25 16:22:59 +01:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(firstRemovalKey==null){
|
|
|
|
firstRemovalKey = nf;
|
|
|
|
}else{
|
|
|
|
break;
|
2016-03-16 17:30:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 16:22:59 +01:00
|
|
|
if(firstRemovalKey!=null){
|
|
|
|
return got.subMap(got.firstKey(), firstRemovalKey);
|
|
|
|
}
|
|
|
|
return got;
|
2016-03-16 17:30:26 +01:00
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-17 12:05:17 +01:00
|
|
|
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
|
2016-03-17 12:16:30 +01:00
|
|
|
Class<? extends AggregatedRecord<?,?>> clz,
|
2016-06-28 15:00:55 +02:00
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters,
|
|
|
|
String topKey) throws DuplicatedKeyFilterException,
|
|
|
|
KeyException, ValueException, Exception {
|
2016-03-24 18:59:34 +01:00
|
|
|
String orderingProperty = AccountingPersistenceQuery
|
|
|
|
.getDefaultOrderingProperties(clz);
|
2016-06-28 15:00:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
return this.getTopValues(clz, temporalConstraint, filters, topKey,
|
|
|
|
orderingProperty, false, 0);
|
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
|
|
|
|
Class<? extends AggregatedRecord<?,?>> clz,
|
2016-06-28 15:00:55 +02:00
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters,
|
|
|
|
String topKey, String orderingProperty) throws
|
|
|
|
DuplicatedKeyFilterException, KeyException, ValueException,
|
|
|
|
Exception {
|
2016-03-24 18:59:34 +01:00
|
|
|
return this.getTopValues(clz, temporalConstraint, filters, topKey,
|
2016-06-28 15:00:55 +02:00
|
|
|
orderingProperty, false, 0);
|
2016-03-16 13:29:03 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 12:05:17 +01:00
|
|
|
public SortedSet<NumberedFilter> getNextPossibleValues(
|
2016-03-17 12:16:30 +01:00
|
|
|
Class<? extends AggregatedRecord<?,?>> clz,
|
2016-06-28 15:00:55 +02:00
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters,
|
|
|
|
String key) throws DuplicatedKeyFilterException, KeyException,
|
|
|
|
ValueException, Exception {
|
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
String orderingProperty = AccountingPersistenceQuery
|
|
|
|
.getDefaultOrderingProperties(clz);
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
return this.getNextPossibleValues(clz, temporalConstraint, filters,
|
2016-06-28 15:00:55 +02:00
|
|
|
key, orderingProperty);
|
2016-03-16 13:29:03 +01:00
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public SortedSet<NumberedFilter> getNextPossibleValues(
|
|
|
|
Class<? extends AggregatedRecord<?, ?>> clz,
|
2016-06-28 15:00:55 +02:00
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters,
|
|
|
|
String key, String orderingProperty) throws
|
|
|
|
DuplicatedKeyFilterException, KeyException, ValueException,
|
|
|
|
Exception {
|
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
return AccountingPersistenceBackendQueryFactory.getInstance()
|
|
|
|
.getNextPossibleValues(clz, temporalConstraint, filters,
|
2016-03-30 09:42:48 +02:00
|
|
|
key, orderingProperty);
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
2015-09-29 16:25:58 +02:00
|
|
|
/**
|
2016-03-24 18:59:34 +01:00
|
|
|
* {@inheritDoc}
|
2015-09-29 16:25:58 +02:00
|
|
|
*/
|
2016-03-24 18:59:34 +01:00
|
|
|
@Override
|
2015-10-14 15:10:06 +02:00
|
|
|
public void close() throws Exception {
|
|
|
|
AccountingPersistenceBackendQueryFactory.getInstance().close();
|
|
|
|
}
|
2016-03-16 13:29:03 +01:00
|
|
|
|
2016-03-24 18:59:34 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void prepareConnection(
|
|
|
|
AccountingPersistenceBackendQueryConfiguration configuration)
|
2016-06-28 15:00:55 +02:00
|
|
|
throws Exception {
|
2016-03-24 18:59:34 +01:00
|
|
|
throw new InvalidActivityException();
|
|
|
|
}
|
2016-06-28 15:00:55 +02:00
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2015-07-08 15:24:32 +02:00
|
|
|
}
|