Alessandro Pieve 2017-01-31 14:17:07 +00:00
parent 4f09feec06
commit ed86d266ff
2 changed files with 62 additions and 43 deletions

View File

@ -98,12 +98,13 @@ AccountingPersistenceBackendQuery {
public static final String DESIGN_DOC_ID_LIST_USAGE="ListUsage"; public static final String DESIGN_DOC_ID_LIST_USAGE="ListUsage";
public static final long ENV_TIME_OUT=240000; public static final long ENV_TIME_OUT=240000;
//public static final long ENV_TIME_OUT_VIEW=30000;
/* The environment configuration */ /* The environment configuration */
protected static final CouchbaseEnvironment ENV = DefaultCouchbaseEnvironment protected static final CouchbaseEnvironment ENV = DefaultCouchbaseEnvironment
.builder() .builder()
.connectTimeout(ENV_TIME_OUT) .connectTimeout(ENV_TIME_OUT)
.maxRequestLifetime(ENV_TIME_OUT) .maxRequestLifetime(ENV_TIME_OUT)
.viewTimeout(ENV_TIME_OUT) //.viewTimeout(ENV_TIME_OUT_VIEW)
.queryTimeout(ENV_TIME_OUT).build(); .queryTimeout(ENV_TIME_OUT).build();
@ -550,9 +551,7 @@ AccountingPersistenceBackendQuery {
//infos.put(info); //infos.put(info);
} }
//TODO DA Completare per la richiesta utente e servizi/altro utilizzati nel periodo //TODO complete the request from user/service usage into period
return null; return null;
@ -561,7 +560,7 @@ AccountingPersistenceBackendQuery {
protected SortedMap<Calendar, Info> mapReduceQuery( protected SortedMap<Calendar, Info> mapReduceQuery(
Class<? extends AggregatedRecord<?, ?>> clz, Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters,String context) TemporalConstraint temporalConstraint, List<Filter> filters,String context,Boolean valueEmpty)
throws Exception { throws Exception {
String currentScope=null; String currentScope=null;
if (context==null) if (context==null)
@ -680,41 +679,49 @@ AccountingPersistenceBackendQuery {
infos.put(calendar, info); infos.put(calendar, info);
} }
logger.trace("valueEmpty not permitted:{}",valueEmpty);
if (infos.isEmpty()){ //infos not empity is permitted only for getTimeSeries and Top
query = ViewQuery.from(designDocId, viewName); if (valueEmpty){
query.groupLevel(groupLevel); if (infos.isEmpty()){
query.descending(false); logger.trace("infos is empity");
try { query = ViewQuery.from(designDocId, viewName);
//execute query in a specify bucket query.groupLevel(groupLevel);
viewResult = connectionMap.get(clz.getSimpleName()).query(query); query.descending(false);
try {
} catch (Exception e) { //execute query in a specify bucket
logger.error(e.getLocalizedMessage()); viewResult = connectionMap.get(clz.getSimpleName()).query(query);
throw e; } catch (Exception e) {
logger.warn("not execute query",e.getLocalizedMessage());
//throw e;
}
try {
if(viewResult.totalRows()!=0){
ViewRow row=viewResult.allRows().get(0);
JsonArray array = getRangeKey(
temporalConstraint.getStartTime(),
aggregationMode, false, false);
Calendar calendar = getCalendarFromArray(array);
JsonObject value = (JsonObject) row.value();
JSONObject objJson= new JSONObject(value.toString());
JSONObject objJsontemplate=new JSONObject();
Iterator<?> iterateJson = objJson.keys();
while( iterateJson.hasNext() ) {
String key = (String)iterateJson.next();
objJsontemplate.put(key, 0);
}
//generate an example object for json
Info info = new Info(calendar, objJsontemplate);
infos.put(calendar, info);
}
} catch (Exception e) {
logger.warn("error :{}",e.getLocalizedMessage());
}
} }
ViewRow row=viewResult.allRows().get(0);
JsonArray array = getRangeKey(
temporalConstraint.getStartTime(),
aggregationMode, false, false);
Calendar calendar = getCalendarFromArray(array);
JsonObject value = (JsonObject) row.value();
JSONObject objJson = new JSONObject(value.toString());
JSONObject objJsontemplate = new JSONObject();
Iterator<?> iterateJson = objJson.keys();
while( iterateJson.hasNext() ) {
String key = (String)iterateJson.next();
objJsontemplate.put(key, 0);
}
//generate an example object for json
Info info = new Info(calendar, objJsontemplate);
infos.put(calendar, info);
//break;
} }
logger.trace("infos:{}",infos.toString());
return infos; return infos;
} }
@ -728,7 +735,7 @@ AccountingPersistenceBackendQuery {
TemporalConstraint temporalConstraint, List<Filter> filters) TemporalConstraint temporalConstraint, List<Filter> filters)
throws Exception { throws Exception {
SortedMap<Calendar, Info> map = mapReduceQuery(clz, temporalConstraint, filters,null); SortedMap<Calendar, Info> map = mapReduceQuery(clz, temporalConstraint, filters,null,true);
return map; return map;
} }
@ -780,7 +787,7 @@ AccountingPersistenceBackendQuery {
for(NumberedFilter nf : top){ for(NumberedFilter nf : top){
filters.add(nf); filters.add(nf);
SortedMap<Calendar, Info> map = SortedMap<Calendar, Info> map =
mapReduceQuery(clz, temporalConstraint, filters,null); mapReduceQuery(clz, temporalConstraint, filters,null,true);
ret.put(nf, map); ret.put(nf, map);
filters.remove(nf); filters.remove(nf);
} }
@ -1207,8 +1214,11 @@ AccountingPersistenceBackendQuery {
for(Filter nf : listContexts){ for(Filter nf : listContexts){
logger.debug("detail time series :{}",nf.toString()); logger.debug("detail time series :{}",nf.toString());
SortedMap<Calendar, Info> map = SortedMap<Calendar, Info> map =
mapReduceQuery(clz, temporalConstraint, filters,nf.getValue()); mapReduceQuery(clz, temporalConstraint, filters,nf.getValue(),false);
ret.put(nf, map); if (!map.isEmpty()){
ret.put(nf, map);
}
filters.remove(nf); filters.remove(nf);
} }
return ret; return ret;

View File

@ -537,6 +537,7 @@ public class AccountingPersistenceQueryCouchBaseTest {
Calendar endTime = Calendar.getInstance(); Calendar endTime = Calendar.getInstance();
endTime.set(2016, Calendar.OCTOBER, 20,23,59); endTime.set(2016, Calendar.OCTOBER, 20,23,59);
List<Filter> filters = new ArrayList<Filter>(); List<Filter> filters = new ArrayList<Filter>();
filters.add(new Filter(AggregatedServiceUsageRecord.CALLED_METHOD, "WebProcessingService"));
//filters.add(new Filter(AggregatedServiceUsageRecord.CONSUMER_ID, "alessandro.pieve")); //filters.add(new Filter(AggregatedServiceUsageRecord.CONSUMER_ID, "alessandro.pieve"));
TemporalConstraint temporalConstraint = TemporalConstraint temporalConstraint =
new TemporalConstraint(startTime.getTimeInMillis(), new TemporalConstraint(startTime.getTimeInMillis(),
@ -546,10 +547,18 @@ public class AccountingPersistenceQueryCouchBaseTest {
AggregatedServiceUsageRecord.class; AggregatedServiceUsageRecord.class;
List<String> context=new ArrayList<String>(); List<String> context=new ArrayList<String>();
// , /gcube/devsec/TestCreation12, /gcube/devsec/TestCreation13, /gcube/devsec/TestCreation14, /gcube/devsec/TestCreation15, /gcube/devsec/TestCreation16, /gcube/devsec/TestCreation17, /gcube/devsec/TestCreation7, /gcube/devsec/TestCreation8, /gcube/devsec/TestCreation9, /gcube/devsec/TestLucio2]]
context.add("/gcube"); context.add("/gcube");
context.add("/gcube/devNext"); context.add("/gcube/devNext");
context.add("/gcube/devNext/nextNext"); context.add("/gcube/devNext/Luciotest");
context.add("/gcube/devNext/NextNext");
context.add("/gcube/devsec"); context.add("/gcube/devsec");
context.add("/gcube/devsec/SmartCamera");
context.add("/gcube/devsec/statVRE");
context.add("/gcube/devsec/TestAddLast");
context.add("/gcube/devsec/devVRE"); context.add("/gcube/devsec/devVRE");
context.add("/gcube/devsec/preVRE"); context.add("/gcube/devsec/preVRE");
context.add("/gcube/preprod/preVRE"); context.add("/gcube/preprod/preVRE");