|
|
|
@ -30,6 +30,7 @@ import org.gcube.accounting.analytics.NumberedFilter;
|
|
|
|
|
import org.gcube.accounting.analytics.TemporalConstraint;
|
|
|
|
|
import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode;
|
|
|
|
|
import org.gcube.accounting.analytics.TemporalConstraint.CalendarEnum;
|
|
|
|
|
import org.gcube.accounting.analytics.TotalFilters;
|
|
|
|
|
import org.gcube.accounting.analytics.exception.DuplicatedKeyFilterException;
|
|
|
|
|
import org.gcube.accounting.analytics.exception.ValueException;
|
|
|
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQuery;
|
|
|
|
@ -42,7 +43,6 @@ import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
|
import org.gcube.documentstore.records.AggregatedRecord;
|
|
|
|
|
import org.gcube.documentstore.records.Record;
|
|
|
|
|
import org.gcube.documentstore.records.RecordUtility;
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
@ -1061,4 +1061,157 @@ AccountingPersistenceBackendQuery {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<TotalFilters> getUsageValueQuotaTotal(
|
|
|
|
|
Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
|
TemporalConstraint temporalConstraint, List<TotalFilters> listUsage)
|
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String currentScope = ScopeProvider.instance.get();
|
|
|
|
|
AggregationMode aggregationMode = temporalConstraint
|
|
|
|
|
.getAggregationMode();
|
|
|
|
|
|
|
|
|
|
JsonArray temporalStartKey = getRangeKey(
|
|
|
|
|
temporalConstraint.getStartTime(),
|
|
|
|
|
aggregationMode, false, false);
|
|
|
|
|
|
|
|
|
|
JsonArray temporalEndKey = getRangeKey(
|
|
|
|
|
temporalConstraint.getEndTime(),
|
|
|
|
|
aggregationMode, false, false);
|
|
|
|
|
|
|
|
|
|
String designDocId = getDesignDocId(clz);
|
|
|
|
|
for (TotalFilters totalfilters:listUsage){
|
|
|
|
|
|
|
|
|
|
Double totalQuota=0.00;
|
|
|
|
|
|
|
|
|
|
for (Filters singleFilter:totalfilters.getTotalFilters()){
|
|
|
|
|
String viewNameTmp=null;
|
|
|
|
|
|
|
|
|
|
JsonArray startKeyTmp=JsonArray.create();
|
|
|
|
|
startKeyTmp.add(currentScope);
|
|
|
|
|
|
|
|
|
|
JsonArray endKeyTmp=JsonArray.create();
|
|
|
|
|
endKeyTmp.add(currentScope);
|
|
|
|
|
|
|
|
|
|
int groupLevelTmp= 2;
|
|
|
|
|
|
|
|
|
|
for (Filter filter:singleFilter.getFilters()){
|
|
|
|
|
if (groupLevelTmp==2)
|
|
|
|
|
viewNameTmp=filter.getKey();
|
|
|
|
|
else
|
|
|
|
|
viewNameTmp=viewNameTmp+"__"+filter.getKey();
|
|
|
|
|
startKeyTmp.add(filter.getValue());
|
|
|
|
|
endKeyTmp.add(filter.getValue());
|
|
|
|
|
groupLevelTmp++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Object temporal: temporalStartKey.toList()){
|
|
|
|
|
if (!temporal.toString().isEmpty())
|
|
|
|
|
startKeyTmp.add(temporal);
|
|
|
|
|
}
|
|
|
|
|
int count =1;
|
|
|
|
|
for (Object temporal: temporalEndKey.toList()){
|
|
|
|
|
if (!temporal.toString().isEmpty()){
|
|
|
|
|
//couchbase exclude last value
|
|
|
|
|
if (count==temporalEndKey.size())
|
|
|
|
|
temporal=(int)temporal+1;
|
|
|
|
|
endKeyTmp.add(temporal);
|
|
|
|
|
}
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ViewQuery query = ViewQuery.from(designDocId, viewNameTmp);
|
|
|
|
|
query.inclusiveEnd();
|
|
|
|
|
query.groupLevel(groupLevelTmp);
|
|
|
|
|
query.startKey(startKeyTmp);
|
|
|
|
|
query.endKey(endKeyTmp);
|
|
|
|
|
query.descending(false);
|
|
|
|
|
|
|
|
|
|
logger.trace("Bucket :{}, Design Doc ID : {}, View Name : {}, "
|
|
|
|
|
+ "Group Level : {}, Start Key : {}, End Key : {},"
|
|
|
|
|
+ "temporalStartKey :{}, temporalEndKey :{}",
|
|
|
|
|
clz.getSimpleName(),designDocId, viewNameTmp, groupLevelTmp, startKeyTmp, endKeyTmp,temporalStartKey.toString(), temporalEndKey.toString());
|
|
|
|
|
|
|
|
|
|
ViewResult viewResult;
|
|
|
|
|
try {
|
|
|
|
|
//execute query in a specify bucket
|
|
|
|
|
viewResult = connectionMap.get(clz.getSimpleName()).query(query);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error(e.getLocalizedMessage());
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, Float> map = new HashMap<String, Float>();
|
|
|
|
|
|
|
|
|
|
for (ViewRow row : viewResult) {
|
|
|
|
|
|
|
|
|
|
JsonObject jsnobject = (JsonObject) row.value();
|
|
|
|
|
JSONObject objJson = new JSONObject(jsnobject.toString());
|
|
|
|
|
|
|
|
|
|
Iterator<?> iterateJosn = objJson.keys();
|
|
|
|
|
while( iterateJosn.hasNext() ) {
|
|
|
|
|
String key = (String)iterateJosn.next();
|
|
|
|
|
Float valuetmp=Float.parseFloat(objJson.get(key).toString());
|
|
|
|
|
|
|
|
|
|
if (key.equals("operationCount") || key.equals("dataVolume")){
|
|
|
|
|
if (map.containsKey(key)) {
|
|
|
|
|
map.put(key, valuetmp + map.get(key));
|
|
|
|
|
|
|
|
|
|
// TODO verify a better method
|
|
|
|
|
if (designDocId.equals("StorageUsageRecord")){
|
|
|
|
|
if (key.equals("dataVolume")){
|
|
|
|
|
singleFilter.setOrderingProperty(key);
|
|
|
|
|
singleFilter.setD(singleFilter.getD()+valuetmp.doubleValue());
|
|
|
|
|
totalQuota+=singleFilter.getD()+valuetmp.doubleValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
singleFilter.setOrderingProperty(key);
|
|
|
|
|
singleFilter.setD(singleFilter.getD()+valuetmp.doubleValue());
|
|
|
|
|
totalQuota+=singleFilter.getD()+valuetmp.doubleValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
map.put(key, valuetmp);
|
|
|
|
|
|
|
|
|
|
// TODO verify a better method
|
|
|
|
|
if (designDocId.equals("StorageUsageRecord")){
|
|
|
|
|
if (key.equals("dataVolume")){
|
|
|
|
|
singleFilter.setOrderingProperty(key);
|
|
|
|
|
singleFilter.setD(valuetmp.doubleValue());
|
|
|
|
|
totalQuota+=valuetmp.doubleValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
singleFilter.setOrderingProperty(key);
|
|
|
|
|
singleFilter.setD(valuetmp.doubleValue());
|
|
|
|
|
totalQuota+=valuetmp.doubleValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
totalfilters.setD(totalQuota);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return listUsage;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|