2015-07-08 15:24:32 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.accounting.analytics;
|
|
|
|
|
2015-07-21 16:33:36 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
2015-09-09 15:22:39 +02:00
|
|
|
import java.util.Collections;
|
2015-07-09 16:39:25 +02:00
|
|
|
import java.util.HashMap;
|
2015-07-21 16:33:36 +02:00
|
|
|
import java.util.Iterator;
|
2015-07-08 15:24:32 +02:00
|
|
|
import java.util.List;
|
2015-07-09 16:39:25 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2015-07-08 15:24:32 +02:00
|
|
|
|
2015-07-13 15:34:52 +02:00
|
|
|
import org.gcube.accounting.analytics.exception.NoAvailableScopeException;
|
|
|
|
import org.gcube.accounting.analytics.exception.NoUsableAccountingPersistenceQueryFound;
|
2015-10-14 15:10:06 +02:00
|
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQuery;
|
|
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQueryFactory;
|
2015-07-30 15:09:54 +02:00
|
|
|
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
|
2015-07-09 16:39:25 +02:00
|
|
|
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
|
|
|
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
2015-07-13 15:34:52 +02:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2015-07-21 16:33:36 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2015-07-09 16:39:25 +02:00
|
|
|
import org.reflections.Reflections;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-07-08 15:24:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
|
|
*/
|
|
|
|
public class ResourceRecordQuery {
|
2015-07-09 16:39:25 +02:00
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ResourceRecordQuery.class);
|
|
|
|
|
|
|
|
protected static Map<Class<? extends SingleUsageRecord>, Set<String>> resourceRecords = null;
|
|
|
|
|
2015-09-07 16:36:08 +02:00
|
|
|
/**
|
|
|
|
* Return a Map containing a set of required fields for each Resource
|
|
|
|
* Records Types
|
|
|
|
* @return the Map
|
|
|
|
*/
|
2015-10-01 16:24:50 +02:00
|
|
|
public static synchronized Map<Class<? extends SingleUsageRecord>, Set<String>> getResourceRecordsTypes() {
|
2015-07-09 16:39:25 +02:00
|
|
|
if(resourceRecords==null){
|
|
|
|
resourceRecords = new HashMap<Class<? extends SingleUsageRecord>, Set<String>>();
|
|
|
|
Package usageRecordPackage = ServiceUsageRecord.class.getPackage();
|
|
|
|
Reflections reflections = new Reflections(usageRecordPackage.getName());
|
|
|
|
Set<Class<? extends SingleUsageRecord>> resourceRecordsTypes = reflections.getSubTypesOf(SingleUsageRecord.class);
|
|
|
|
for(Class<? extends SingleUsageRecord> resourceRecordsType : resourceRecordsTypes){
|
|
|
|
try {
|
|
|
|
SingleUsageRecord singleUsageRecord = resourceRecordsType.newInstance();
|
|
|
|
resourceRecords.put(resourceRecordsType, singleUsageRecord.getRequiredFields());
|
|
|
|
} catch (InstantiationException | IllegalAccessException e) {
|
2015-07-22 17:29:06 +02:00
|
|
|
logger.error(String.format("Unable to correctly istantiate %s", resourceRecordsType.getSimpleName()), e);
|
2015-07-09 16:39:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return resourceRecords;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-14 15:10:06 +02:00
|
|
|
protected AccountingPersistenceBackendQuery accountingPersistenceQuery;
|
2015-07-08 15:24:32 +02:00
|
|
|
|
2015-09-07 16:36:08 +02:00
|
|
|
/**
|
|
|
|
* Instantiate the ResourceRecord for the current scope
|
|
|
|
* @throws NoAvailableScopeException if there is not possible to query in
|
|
|
|
* the current scope
|
|
|
|
* @throws NoUsableAccountingPersistenceQueryFound if there is no available
|
|
|
|
* instance which can query in that scope
|
|
|
|
*/
|
2015-07-13 15:34:52 +02:00
|
|
|
public ResourceRecordQuery() throws NoAvailableScopeException, NoUsableAccountingPersistenceQueryFound {
|
2015-10-14 15:10:06 +02:00
|
|
|
this.accountingPersistenceQuery = AccountingPersistenceBackendQueryFactory.getInstance();
|
2015-07-13 15:34:52 +02:00
|
|
|
}
|
|
|
|
|
2015-09-07 16:36:08 +02:00
|
|
|
/**
|
|
|
|
* Instantiate the ResourceRecord for the provided scope
|
|
|
|
* @param scope the scope
|
|
|
|
* @throws NoAvailableScopeException if there is not possible to query in
|
|
|
|
* that scope
|
|
|
|
* @throws NoUsableAccountingPersistenceQueryFound if there is no available
|
|
|
|
* instance which can query in that scope
|
|
|
|
*/
|
2015-07-13 15:34:52 +02:00
|
|
|
public ResourceRecordQuery(String scope) throws NoAvailableScopeException, NoUsableAccountingPersistenceQueryFound {
|
|
|
|
ScopeProvider.instance.set(scope);
|
2015-10-14 15:10:06 +02:00
|
|
|
this.accountingPersistenceQuery = AccountingPersistenceBackendQueryFactory.getInstance();
|
2015-07-08 15:24:32 +02:00
|
|
|
}
|
|
|
|
|
2015-07-21 16:33:36 +02:00
|
|
|
protected static JSONObject getPaddingJSONObject(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();
|
|
|
|
while(keys.hasNext()){
|
|
|
|
String key = keys.next();
|
|
|
|
jsonObject.put(key, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonObject;
|
2015-07-08 15:24:32 +02:00
|
|
|
}
|
|
|
|
|
2015-09-07 16:36:08 +02: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 List<Info> getPaddedResults(Map<Calendar, Info> unpaddedData,
|
|
|
|
TemporalConstraint temporalConstraint) throws Exception {
|
|
|
|
JSONObject jsonObject = getPaddingJSONObject(unpaddedData);
|
2015-07-21 16:33:36 +02:00
|
|
|
|
|
|
|
List<Info> paddedResults = new ArrayList<Info>();
|
|
|
|
List<Calendar> sequence = temporalConstraint.getCalendarSequence();
|
|
|
|
|
|
|
|
for(Calendar progressTime : sequence){
|
2015-09-07 16:36:08 +02:00
|
|
|
if(unpaddedData.get(progressTime)!=null){
|
|
|
|
paddedResults.add(unpaddedData.get(progressTime));
|
2015-07-21 16:33:36 +02:00
|
|
|
}else{
|
2015-09-08 11:49:47 +02:00
|
|
|
Info info = new Info(progressTime, jsonObject);
|
2015-07-21 16:33:36 +02:00
|
|
|
paddedResults.add(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paddedResults;
|
|
|
|
}
|
2015-07-13 15:34:52 +02:00
|
|
|
|
2015-07-21 16:33:36 +02:00
|
|
|
/**
|
|
|
|
* Return results with padding if pad is set to true.
|
2015-09-07 16:36:08 +02:00
|
|
|
* @param usageRecordType the UsageRecord type to query
|
|
|
|
* @param temporalConstraint the temporal interval and the granularity
|
|
|
|
* @param filters the list keys to filter (in AND)
|
|
|
|
* @param pad indicate is the results have to be padded with zeros when
|
|
|
|
* there is no data available at certain data points of sequence
|
|
|
|
* @return the requested list of Info
|
|
|
|
* @throws Exception if fails
|
2015-07-21 16:33:36 +02:00
|
|
|
*/
|
2015-07-30 15:09:54 +02:00
|
|
|
public List<Info> getInfo(@SuppressWarnings("rawtypes") Class<? extends AggregatedUsageRecord> usageRecordType,
|
2015-07-21 16:33:36 +02:00
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, boolean pad) throws Exception {
|
|
|
|
Map<Calendar, Info> unpaddedResults = accountingPersistenceQuery.query(usageRecordType, temporalConstraint, filters);
|
|
|
|
if(!pad){
|
|
|
|
return new ArrayList<Info>(unpaddedResults.values());
|
|
|
|
}
|
|
|
|
return getPaddedResults(unpaddedResults, temporalConstraint);
|
|
|
|
}
|
2015-07-13 15:34:52 +02:00
|
|
|
|
2015-07-21 16:33:36 +02:00
|
|
|
/**
|
2015-09-07 16:36:08 +02:00
|
|
|
* Return unpadded results
|
|
|
|
* @param usageRecordType the UsageRecord type to query
|
|
|
|
* @param temporalConstraint the temporal interval and the granularity
|
|
|
|
* @param filters the list keys to filter (in AND)
|
|
|
|
* @return the requested list of Info
|
|
|
|
* @throws Exception if fails
|
2015-07-21 16:33:36 +02:00
|
|
|
*/
|
2015-07-30 15:09:54 +02:00
|
|
|
public List<Info> getInfo(@SuppressWarnings("rawtypes") Class<? extends AggregatedUsageRecord> usageRecordType,
|
2015-07-21 16:33:36 +02:00
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters) throws Exception{
|
|
|
|
return getInfo(usageRecordType, temporalConstraint, filters, false);
|
|
|
|
}
|
2015-07-13 15:34:52 +02:00
|
|
|
|
2015-09-08 17:04:56 +02:00
|
|
|
/**
|
|
|
|
* Return the list of key valid for queries a certain usage record type
|
|
|
|
* @param usageRecordType the usage record type
|
|
|
|
* @return a set containing the list of key
|
|
|
|
* @throws Exception if fails
|
|
|
|
*/
|
2015-09-09 15:22:39 +02:00
|
|
|
public List<String> getKeys(@SuppressWarnings("rawtypes") Class<? extends AggregatedUsageRecord> usageRecordType) throws Exception{
|
|
|
|
Set<String> keys = accountingPersistenceQuery.getKeys(usageRecordType);
|
|
|
|
List<String> toSort = new ArrayList<String>(keys);
|
|
|
|
Collections.sort(toSort);
|
|
|
|
return toSort;
|
2015-09-08 17:04:56 +02:00
|
|
|
}
|
|
|
|
|
2015-10-01 17:35:09 +02:00
|
|
|
public List<String> getPossibleValuesForKey(@SuppressWarnings("rawtypes") Class<? extends AggregatedUsageRecord> usageRecordType, String key) throws Exception {
|
|
|
|
Set<String> keys = accountingPersistenceQuery.getPossibleValuesForKey(usageRecordType, key);
|
|
|
|
List<String> toSort = new ArrayList<String>(keys);
|
|
|
|
Collections.sort(toSort);
|
|
|
|
return toSort;
|
|
|
|
}
|
|
|
|
|
2015-07-08 15:24:32 +02:00
|
|
|
}
|