refs #2197: Remove the use of ScopeProvider if any from accounting-analytics-persistence-couchbase

https://support.d4science.org/issues/2197

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-analytics-persistence-couchbase@124015 82a268e6-3cf1-43bd-a215-b396298e98cf
feature/19115
Luca Frosini 8 years ago
parent 6700f27202
commit c8a656514f

@ -39,7 +39,7 @@
<dependency>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-analytics</artifactId>
<version>[1.1.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<version>[1.2.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.data.publishing</groupId>

@ -3,29 +3,42 @@
*/
package org.gcube.accounting.analytics.persistence.couchbase;
import static com.couchbase.client.java.query.Select.select;
import static com.couchbase.client.java.query.dsl.Expression.s;
import static com.couchbase.client.java.query.dsl.Expression.x;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Set;
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.AccountingPersistenceBackendQuery;
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQueryConfiguration;
import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.documentstore.records.AggregatedRecord;
import org.gcube.documentstore.records.Record;
import org.gcube.documentstore.records.RecordUtility;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import com.couchbase.client.java.query.N1qlQueryResult;
import com.couchbase.client.java.query.N1qlQueryRow;
import com.couchbase.client.java.query.Select;
import com.couchbase.client.java.query.dsl.Expression;
import com.couchbase.client.java.query.dsl.path.GroupByPath;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
@ -72,22 +85,77 @@ public class AccountingPersistenceQueryCouchBase extends AccountingPersistenceBa
cluster.disconnect();
}
protected Calendar getCalendar(JSONObject obj, AggregationMode aggregationMode) throws NumberFormatException, JSONException{
long millis;
if(obj.has(AggregatedRecord.START_TIME)){
millis = new Long(obj.getString(AggregatedRecord.START_TIME));
logger.trace("The result {} was from an aggregated record. Using {}", obj.toString(), AggregatedRecord.START_TIME);
}else{
millis = new Long(obj.getString(UsageRecord.CREATION_TIME));
logger.trace("The result {} was from single record. Using {}", obj.toString(), UsageRecord.CREATION_TIME);
}
Calendar calendar = TemporalConstraint.getAlignedCalendar(millis, aggregationMode);
logger.trace("{} has been aligned to {}", millis, calendar.getTimeInMillis());
return calendar;
}
/**
* {@inheritDoc}
*/
@Override
protected Map<Calendar, Info> reallyQuery(
protected Map<Calendar, Info> reallyQuery(
@SuppressWarnings("rawtypes") Class<? extends AggregatedRecord> recordClass,
TemporalConstraint temporalConstraint, List<Filter> filters)
throws Exception {
N1qlQueryResult result = bucket.query(Select.select("*").from(bucketName).limit(10));
String currentScope = BasicUsageRecord.getScopeFromToken();
String recordType = recordClass.newInstance().getRecordType();
Expression expression = x(BasicUsageRecord.SCOPE).eq(s(currentScope));
expression = expression.and(x(BasicUsageRecord.RECORD_TYPE).eq(s(recordType)));
long startTime = temporalConstraint.getAlignedStartTime().getTimeInMillis();
expression = expression.and(
x(AggregatedRecord.START_TIME).gt(startTime).
or(x(AggregatedRecord.CREATION_TIME).gt(startTime)));
long endTime = temporalConstraint.getAlignedEndTime().getTimeInMillis();
expression = expression.and(
x(AggregatedRecord.END_TIME).lt(endTime)).
or(x(AggregatedRecord.CREATION_TIME).lt(endTime));
AggregationMode aggregationMode = temporalConstraint.getAggregationMode();
if(filters!=null){
for(Filter filter : filters){
expression = expression.and(x(filter.getKey()).eq(s(filter.getValue())));
}
}
GroupByPath groupByPath = select("*").from(bucketName).where(expression);
N1qlQueryResult result = bucket.query(groupByPath);
List<N1qlQueryRow> rows = result.allRows();
Map<Calendar, Info> map = new HashMap<Calendar, Info>();
for(N1qlQueryRow row : rows){
logger.debug(row.toString());
JsonObject jsonObject = row.value().getObject(bucketName);
Record record = RecordUtility.getRecord(jsonObject.toMap().toString());
JSONObject obj = new JSONObject(jsonObject.toString());
Calendar calendar = getCalendar(obj, aggregationMode);
if(map.containsKey(calendar)){
Info info = map.get(calendar);
JSONObject value = info.getValue();
jsonObject.toMap();
}else{
map.put(calendar, new Info(calendar, obj));
}
}
return null;
return map;
}
@ -110,6 +178,18 @@ public class AccountingPersistenceQueryCouchBase extends AccountingPersistenceBa
Class<? extends AggregatedRecord> recordClass, String key)
throws Exception {
// TODO Auto-generated method stub
// SELECT DISTINCT
return null;
}
/* (non-Javadoc)
* @see org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQuery#getPossibleValuesForKey(java.lang.Class, java.lang.String, int)
*/
@Override
public Set<String> getPossibleValuesForKey(
Class<? extends AggregatedRecord> recordClass, String key, int limit)
throws Exception {
// TODO Auto-generated method stub
return null;
}

@ -4,7 +4,8 @@
package org.gcube.accounting.analytics.persistence;
import org.gcube.accounting.analytics.persistence.couchbase.AccountingPersistenceQueryCouchBase;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -17,9 +18,13 @@ public class AccountingPersistenceQueryConfigurationTest {
private static Logger logger = LoggerFactory.getLogger(AccountingPersistenceQueryConfigurationTest.class);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set("");
}
@Test
public void test() throws Exception {
ScopeProvider.instance.set("/gcube/devsec/devVRE");
AccountingPersistenceBackendQueryConfiguration acbqc = new AccountingPersistenceBackendQueryConfiguration(AccountingPersistenceQueryCouchBase.class);
logger.debug("{}", acbqc);
}

@ -15,15 +15,13 @@ import org.gcube.accounting.analytics.Info;
import org.gcube.accounting.analytics.ResourceRecordQuery;
import org.gcube.accounting.analytics.TemporalConstraint;
import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode;
import org.gcube.accounting.analytics.exception.NoAvailableScopeException;
import org.gcube.accounting.analytics.exception.NoUsableAccountingPersistenceQueryFound;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@ -42,14 +40,14 @@ public class AccountingPersistenceQueryFactoryTest {
protected AccountingPersistenceBackendQuery apq;
@Before
public void before() throws NoAvailableScopeException, NoUsableAccountingPersistenceQueryFound{
ScopeProvider.instance.set("/gcube/devNext");
public void before() throws Exception{
SecurityTokenProvider.instance.set("");
apq = AccountingPersistenceBackendQueryFactory.getInstance();
}
@After
public void after(){
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
apq = null;
}
@ -57,6 +55,9 @@ public class AccountingPersistenceQueryFactoryTest {
public void testNullFilters() throws Exception {
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(startTime.getTimeInMillis()-(1000*60*60*24*3));
//startTime.setTimeInMillis(new Long("1449829933665"));
startTime.set(Calendar.YEAR, 2015);
startTime.set(Calendar.MONTH, 1);
Calendar endTime = Calendar.getInstance();
TemporalConstraint temporalConstraint = new TemporalConstraint(startTime.getTimeInMillis(), endTime.getTimeInMillis(), AggregationMode.SECONDLY);
Map<Calendar, Info> infos = apq.query(AggregatedServiceUsageRecord.class, temporalConstraint, null);