Added Test
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/infrastructure-tests@124503 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
5e07a8aaf3
commit
ccb566d5d2
|
@ -0,0 +1,195 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.accounting.analytics.persistence.couchdb;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.codehaus.jackson.JsonNode;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.node.ArrayNode;
|
||||
import org.ektorp.DocumentNotFoundException;
|
||||
import org.ektorp.ViewQuery;
|
||||
import org.ektorp.ViewResult;
|
||||
import org.gcube.accounting.analytics.Filter;
|
||||
import org.gcube.accounting.analytics.Info;
|
||||
import org.gcube.accounting.analytics.TemporalConstraint;
|
||||
import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode;
|
||||
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQueryConfiguration;
|
||||
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.documentstore.records.AggregatedRecord;
|
||||
import org.gcube.testutility.ScopedTest;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
public class AccountingPersistenceQueryCouchDBTest extends ScopedTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccountingPersistenceQueryCouchDBTest.class);
|
||||
|
||||
protected AccountingPersistenceQueryCouchDB accountingPersistenceQueryCouchDB;
|
||||
|
||||
public class ExtendedInfo extends Info {
|
||||
|
||||
protected String key;
|
||||
|
||||
/**
|
||||
* @return the key
|
||||
*/
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key the key to set
|
||||
*/
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public ExtendedInfo(String key, Calendar calendar, JSONObject value){
|
||||
super(calendar, value);
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
String info = String .format("Key : %s, %s ", key, super.toString());
|
||||
return info;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Before
|
||||
public void before() throws Exception{
|
||||
super.before();
|
||||
ScopeProvider.instance.set("/gcube");
|
||||
AccountingPersistenceBackendQueryConfiguration configuration = new
|
||||
AccountingPersistenceBackendQueryConfiguration(AccountingPersistenceQueryCouchDB.class);
|
||||
configuration.addProperty(AccountingPersistenceQueryCouchDB.DB_NAME, "accounting-my-dev-test");
|
||||
accountingPersistenceQueryCouchDB = new AccountingPersistenceQueryCouchDB();
|
||||
|
||||
accountingPersistenceQueryCouchDB.prepareConnection(configuration);
|
||||
}
|
||||
|
||||
protected Map<Calendar, Info> myNewQuery(
|
||||
@SuppressWarnings("rawtypes") Class<? extends AggregatedRecord> recordClass,
|
||||
TemporalConstraint temporalConstraint, List<Filter> filters)
|
||||
throws Exception {
|
||||
|
||||
//String currentScope = BasicUsageRecord.getScopeFromToken();
|
||||
String currentScope = ScopeProvider.instance.get();
|
||||
String designDocId = accountingPersistenceQueryCouchDB.getDesignDocId(recordClass) + "TopValues";
|
||||
|
||||
ViewQuery query = new ViewQuery().designDocId(designDocId);
|
||||
|
||||
query.group(true);
|
||||
int groupLevel = temporalConstraint.getAggregationMode().ordinal()+2;
|
||||
if(filters!=null){
|
||||
groupLevel += filters.size()*2;
|
||||
}
|
||||
query.groupLevel(groupLevel);
|
||||
|
||||
ArrayNode startKey = new ObjectMapper().createArrayNode();
|
||||
startKey.add(currentScope);
|
||||
ArrayNode endKey = new ObjectMapper().createArrayNode();
|
||||
endKey.add(currentScope);
|
||||
|
||||
String viewName = AccountingPersistenceQueryCouchDB.MAP_REDUCE_ALL;
|
||||
query = query.viewName(viewName);
|
||||
|
||||
startKey = accountingPersistenceQueryCouchDB.getRangeKey(startKey, temporalConstraint.getStartTime(), temporalConstraint.getAggregationMode(), false);
|
||||
endKey = accountingPersistenceQueryCouchDB.getRangeKey(endKey, temporalConstraint.getEndTime(), temporalConstraint.getAggregationMode(), false);
|
||||
|
||||
if(filters!=null && filters.size()!=0){
|
||||
for(Filter filter : filters){
|
||||
/*
|
||||
ObjectNode objectNode = new ObjectMapper().createObjectNode();
|
||||
objectNode.put(filter.getKey(), filter.getValue());
|
||||
startKey.add(objectNode);
|
||||
endKey.add(objectNode);
|
||||
*/
|
||||
startKey.add(filter.getKey());
|
||||
endKey.add(filter.getKey());
|
||||
|
||||
if(filter.getValue()!=null && filters.size()==1){
|
||||
startKey.add(filter.getValue());
|
||||
endKey.add(filter.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//endKey.add("{}");
|
||||
|
||||
query.startKey(startKey);
|
||||
query.endKey(endKey);
|
||||
query.descending(true);
|
||||
|
||||
logger.trace("\nDesign Doc ID : {}, View Name : {}, Group Level : {} \nStart Key : {} \nEnd Key : {}",
|
||||
designDocId, viewName, groupLevel, startKey, endKey);
|
||||
|
||||
Map<Calendar, Info> infos = new HashMap<Calendar, Info>();
|
||||
|
||||
ViewResult viewResult;
|
||||
try {
|
||||
viewResult = accountingPersistenceQueryCouchDB.query(query);
|
||||
} catch (DocumentNotFoundException e) {
|
||||
// Install VIEW if valid and does not exist
|
||||
throw e;
|
||||
}
|
||||
|
||||
for (ViewResult.Row row : viewResult) {
|
||||
JsonNode key = row.getKeyAsNode();
|
||||
JSONArray array = new JSONArray(key.toString());
|
||||
Calendar calendar = accountingPersistenceQueryCouchDB.getCalendarFromArray(array);
|
||||
|
||||
JsonNode value = row.getValueAsNode();
|
||||
JSONObject obj = new JSONObject(value.toString());
|
||||
|
||||
ExtendedInfo extendedInfo = new ExtendedInfo(key.toString(), calendar, obj);
|
||||
|
||||
infos.put(calendar, extendedInfo);
|
||||
}
|
||||
|
||||
return infos;
|
||||
}
|
||||
|
||||
public void printMap(Map<Calendar, Info> map){
|
||||
for(Info info : map.values()){
|
||||
logger.debug("{}", info);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
Calendar startTime = Calendar.getInstance();
|
||||
startTime.set(2015, Calendar.OCTOBER, 9);
|
||||
Calendar endTime = Calendar.getInstance();
|
||||
endTime.set(2015, Calendar.OCTOBER, 16);
|
||||
|
||||
List<Filter> filters = new ArrayList<Filter>();
|
||||
Filter filter = new Filter("consumerId", "gianpaolo.coro");
|
||||
filters.add(filter);
|
||||
TemporalConstraint temporalConstraint = new TemporalConstraint(startTime.getTimeInMillis(), endTime.getTimeInMillis(), AggregationMode.DAILY);
|
||||
Map<Calendar, Info> oldQuery = accountingPersistenceQueryCouchDB.reallyQuery(AggregatedServiceUsageRecord.class, temporalConstraint, filters);
|
||||
logger.debug("Old Query Results :");
|
||||
printMap(oldQuery);
|
||||
|
||||
Map<Calendar, Info> newQuery = myNewQuery(AggregatedServiceUsageRecord.class, temporalConstraint, filters);
|
||||
logger.debug("New Query Results :");
|
||||
printMap(newQuery);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue