Alessandro Pieve 8 years ago
parent dc6d7844e1
commit 4acf7a9ac7

@ -16,12 +16,12 @@
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>

@ -8,7 +8,7 @@
</parent>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-analytics-persistence-couchbase</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<name>Accounting Analytics Persistence CouchBase</name>
<description>Accounting Analytics Persistence CouchBase Implementation</description>
@ -81,6 +81,33 @@
<version>1.0.13</version>
<scope>test</scope>
</dependency>
<!-- use for Junit Test -->
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-client</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-authorization</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<!-- End Use for Junit Test -->
</dependencies>
<build>

@ -0,0 +1,213 @@
package org.gcube.accounting.analytics.persistence.couchbase;
/**
*
*/
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
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.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.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Alessandro Pieve (ISTI - CNR)
*
*/
public class AccountingPersistenceQueryCouchBaseTest {
private static Logger logger = LoggerFactory.getLogger(AccountingPersistenceQueryCouchBaseTest.class);
protected AccountingPersistenceQueryCouchBase accountingPersistenceQueryCouchBase;
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{
ScopeProvider.instance.set("/gcube/devNext");
AccountingPersistenceBackendQueryConfiguration configuration = new
AccountingPersistenceBackendQueryConfiguration(AccountingPersistenceQueryCouchBase.class);
accountingPersistenceQueryCouchBase = new AccountingPersistenceQueryCouchBase();
accountingPersistenceQueryCouchBase.prepareConnection(configuration);
}
@After
public void after() throws Exception{
//SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset();
}
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(2016, Calendar.AUGUST, 29);
Calendar endTime = Calendar.getInstance();
endTime.set(2016, Calendar.AUGUST, 29,23,59);
List<Filter> filters = new ArrayList<Filter>();
TemporalConstraint temporalConstraint =
new TemporalConstraint(startTime.getTimeInMillis(),
endTime.getTimeInMillis(), AggregationMode.DAILY);
Class<AggregatedServiceUsageRecord> clz =
AggregatedServiceUsageRecord.class;
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> set =
accountingPersistenceQueryCouchBase.getTopValues(
clz, temporalConstraint, filters,
AggregatedServiceUsageRecord.CALLED_METHOD, null);
logger.debug("Result final{}", set);
}
public static SortedMap<Calendar, Info> padMap(
SortedMap<Calendar, Info> unpaddedData,
TemporalConstraint temporalConstraint) throws Exception {
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;
}
public static JSONObject getPaddingJSONObject(
Map<Calendar, Info> unpaddedResults) throws JSONException {
JSONObject jsonObject = new JSONObject();
//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);
}
}
return jsonObject;
}
@Test
public void testFull() throws Exception {
logger.debug("test full ");
try{
Calendar startTime = Calendar.getInstance();
//startTime.set(2016, Calendar.AUGUST, 20, 00, 00);
startTime.set(2016, Calendar.AUGUST, 29,00,00);
Calendar endTime = Calendar.getInstance();
//endTime.set(2016, Calendar.AUGUST, 29, 23, 59);
endTime.set(2016, Calendar.AUGUST, 31,23,59);
List<Filter> filters = new ArrayList<Filter>();
TemporalConstraint temporalConstraint =
new TemporalConstraint(startTime.getTimeInMillis(),
endTime.getTimeInMillis(), AggregationMode.DAILY);
Class<AggregatedServiceUsageRecord> clz =
AggregatedServiceUsageRecord.class;
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> set =
accountingPersistenceQueryCouchBase.getTopValues(
clz, temporalConstraint, filters,
AggregatedServiceUsageRecord.CALLED_METHOD, null);
/**pad*/
int limit=0;
boolean pad=true;
int count = set.size() > limit ? limit : set.size();
NumberedFilter firstRemovalKey = null;
logger.debug("set completo"+set.toString());
for(NumberedFilter nf : set.keySet()){
if(--count>=0 || limit<=0){
if(pad){
padMap(set.get(nf), temporalConstraint);
}
}else{
if(firstRemovalKey==null){
firstRemovalKey = nf;
}else{
break;
}
}
}
if(firstRemovalKey!=null){
logger.debug("First removal key set:"+set.subMap(set.firstKey(), firstRemovalKey));
}
logger.debug("set: "+set);
}catch(Exception e){
e.printStackTrace();
}
}
}