diff --git a/.project b/.project index 664a398..9d560d4 100644 --- a/.project +++ b/.project @@ -16,12 +16,12 @@ - org.eclipse.m2e.core.maven2Builder + org.eclipse.wst.validation.validationbuilder - org.eclipse.wst.validation.validationbuilder + org.eclipse.m2e.core.maven2Builder diff --git a/pom.xml b/pom.xml index d06047e..2b39087 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.gcube.accounting accounting-analytics-persistence-couchbase - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT Accounting Analytics Persistence CouchBase Accounting Analytics Persistence CouchBase Implementation @@ -81,6 +81,33 @@ 1.0.13 test + + + org.gcube.resources + registry-publisher + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + test + + + org.gcube.core + common-encryption + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + test + + + org.gcube.common + authorization-client + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + provided + + + org.gcube.common + common-authorization + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + provided + + + diff --git a/src/test/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBaseTest.java b/src/test/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBaseTest.java new file mode 100644 index 0000000..9c11353 --- /dev/null +++ b/src/test/java/org/gcube/accounting/analytics/persistence/couchbase/AccountingPersistenceQueryCouchBaseTest.java @@ -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 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 filters = new ArrayList(); + + TemporalConstraint temporalConstraint = + new TemporalConstraint(startTime.getTimeInMillis(), + endTime.getTimeInMillis(), AggregationMode.DAILY); + + Class clz = + AggregatedServiceUsageRecord.class; + + SortedMap> set = + accountingPersistenceQueryCouchBase.getTopValues( + clz, temporalConstraint, filters, + AggregatedServiceUsageRecord.CALLED_METHOD, null); + + logger.debug("Result final{}", set); + + } + public static SortedMap padMap( + SortedMap unpaddedData, + TemporalConstraint temporalConstraint) throws Exception { + JSONObject jsonObject = getPaddingJSONObject(unpaddedData); + SortedSet 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 unpaddedResults) throws JSONException { + + JSONObject jsonObject = new JSONObject(); + //verify data consistency + + if (unpaddedResults.size()!=0){ + Info auxInfo = new ArrayList(unpaddedResults.values()).get(0); + JSONObject auxJsonObject = auxInfo.getValue(); + @SuppressWarnings("unchecked") + Iterator 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 filters = new ArrayList(); + + TemporalConstraint temporalConstraint = + new TemporalConstraint(startTime.getTimeInMillis(), + endTime.getTimeInMillis(), AggregationMode.DAILY); + + Class clz = + AggregatedServiceUsageRecord.class; + + SortedMap> 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(); + } + } +}