reformatted code

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-analytics@154378 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-09-21 12:54:33 +00:00
parent 390136c155
commit 7c45a1d29a
1 changed files with 131 additions and 218 deletions

View File

@ -51,46 +51,39 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
} }
@Deprecated @Deprecated
public static SortedSet<String> getQuerableKeys( public static SortedSet<String> getQuerableKeys(@SuppressWarnings("rawtypes") AggregatedRecord instance)
@SuppressWarnings("rawtypes") AggregatedRecord instance) throws Exception {
throws Exception {
return instance.getQuerableKeys(); return instance.getQuerableKeys();
} }
public static SortedSet<String> getQuerableKeys( public static SortedSet<String> getQuerableKeys(Class<? extends AggregatedRecord<?, ?>> clz) throws Exception {
Class<? extends AggregatedRecord<?,?>> clz) AggregatedRecord<?, ?> instance = clz.newInstance();
throws Exception {
AggregatedRecord<?,?> instance = clz.newInstance(); // limit filter key for accounting storage status (used from portlet
// accounting for tad space)
//limit filter key for accounting storage status (used from portlet accounting for tad space) if (clz.equals(AggregatedStorageStatusRecord.class)) {
if (clz.equals(AggregatedStorageStatusRecord.class)){ SortedSet<String> storageStatus = new TreeSet<>();
SortedSet<String> storageStatus= new TreeSet<>();
storageStatus.add(AggregatedStorageStatusRecord.CONSUMER_ID); storageStatus.add(AggregatedStorageStatusRecord.CONSUMER_ID);
storageStatus.add(AggregatedStorageStatusRecord.DATA_SERVICEID); storageStatus.add(AggregatedStorageStatusRecord.DATA_SERVICEID);
return storageStatus; return storageStatus;
} else {
return instance.getQuerableKeys();
} }
else{
return instance.getQuerableKeys();
}
} }
public static String getDefaultOrderingProperties( public static String getDefaultOrderingProperties(Class<? extends AggregatedRecord<?, ?>> clz) {
Class<? extends AggregatedRecord<?, ?>> clz){ if (clz.isAssignableFrom(AggregatedStorageUsageRecord.class)) {
if(clz.isAssignableFrom(AggregatedStorageUsageRecord.class)){
return AggregatedStorageUsageRecord.DATA_VOLUME; return AggregatedStorageUsageRecord.DATA_VOLUME;
} }
return AggregatedRecord.OPERATION_COUNT; return AggregatedRecord.OPERATION_COUNT;
} }
protected static JSONObject getPaddingJSONObject( protected static JSONObject getPaddingJSONObject(Map<Calendar, Info> unpaddedResults) throws JSONException {
Map<Calendar, Info> unpaddedResults) throws JSONException {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
//verify data consistency // verify data consistency
if (unpaddedResults.size()!=0){ if (unpaddedResults.size() != 0) {
Info auxInfo = new ArrayList<Info>(unpaddedResults.values()).get(0); Info auxInfo = new ArrayList<Info>(unpaddedResults.values()).get(0);
JSONObject auxJsonObject = auxInfo.getValue(); JSONObject auxJsonObject = auxInfo.getValue();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -115,8 +108,7 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
* @throws Exception * @throws Exception
* if fails * if fails
*/ */
public static SortedMap<Calendar, Info> padMap( public static SortedMap<Calendar, Info> padMap(SortedMap<Calendar, Info> unpaddedData,
SortedMap<Calendar, Info> unpaddedData,
TemporalConstraint temporalConstraint) throws Exception { TemporalConstraint temporalConstraint) throws Exception {
JSONObject jsonObject = getPaddingJSONObject(unpaddedData); JSONObject jsonObject = getPaddingJSONObject(unpaddedData);
@ -134,124 +126,95 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public SortedMap<Calendar, Info> getTimeSeries( public SortedMap<Calendar, Info> getTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
Class<? extends AggregatedRecord<?,?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters)
TemporalConstraint temporalConstraint, List<Filter> filters) throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception {
return this.getTimeSeries(clz, temporalConstraint, filters, false); return this.getTimeSeries(clz, temporalConstraint, filters, false);
} }
public SortedMap<Calendar, Info> getTimeSeries( public SortedMap<Calendar, Info> getTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
Class<? extends AggregatedRecord<?,?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters, boolean pad)
TemporalConstraint temporalConstraint, List<Filter> filters, throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
boolean pad) throws DuplicatedKeyFilterException, KeyException, SortedMap<Calendar, Info> ret = AccountingPersistenceBackendQueryFactory.getInstance().getTimeSeries(clz,
ValueException, Exception { temporalConstraint, filters);
SortedMap<Calendar, Info> ret =
AccountingPersistenceBackendQueryFactory.getInstance()
.getTimeSeries(clz, temporalConstraint,
filters);
if(ret==null){ if (ret == null) {
ret = new TreeMap<>(); ret = new TreeMap<>();
} }
if (pad) {
if(pad){
ret = padMap(ret, temporalConstraint); ret = padMap(ret, temporalConstraint);
} }
return ret; return ret;
} }
public SortedMap<Calendar, Info> getNoContextTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters)
public SortedMap<Calendar, Info> getNoContextTimeSeries( throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
Class<? extends AggregatedRecord<?,?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters)
throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception {
return this.getNoContextTimeSeries(clz, temporalConstraint, filters, false); return this.getNoContextTimeSeries(clz, temporalConstraint, filters, false);
} }
public SortedMap<Calendar, Info> getNoContextTimeSeries( public SortedMap<Calendar, Info> getNoContextTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
Class<? extends AggregatedRecord<?,?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters, boolean pad)
TemporalConstraint temporalConstraint, List<Filter> filters, throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
boolean pad) throws DuplicatedKeyFilterException, KeyException, SortedMap<Calendar, Info> ret = AccountingPersistenceBackendQueryFactory.getInstance()
ValueException, Exception { .getNoContextTimeSeries(clz, temporalConstraint, filters);
SortedMap<Calendar, Info> ret =
AccountingPersistenceBackendQueryFactory.getInstance()
.getNoContextTimeSeries(clz, temporalConstraint,
filters);
if(ret==null){ if (ret == null) {
ret = new TreeMap<>(); ret = new TreeMap<>();
} }
if (pad) {
if(pad){
ret = padMap(ret, temporalConstraint); ret = padMap(ret, temporalConstraint);
} }
return ret; return ret;
} }
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues( public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?, ?>> clz, Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters,
TemporalConstraint temporalConstraint, List<Filter> filters, String topKey, String orderingProperty, boolean pad, int limit)
String topKey, String orderingProperty, boolean pad, int limit) throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
throws DuplicatedKeyFilterException, KeyException, ValueException,
Exception {
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> got; SortedMap<NumberedFilter, SortedMap<Calendar, Info>> got;
if(orderingProperty==null){ if (orderingProperty == null) {
orderingProperty = getDefaultOrderingProperties(clz); orderingProperty = getDefaultOrderingProperties(clz);
} }
got = AccountingPersistenceBackendQueryFactory.getInstance() got = AccountingPersistenceBackendQueryFactory.getInstance().getTopValues(clz, temporalConstraint, filters,
.getTopValues(clz, temporalConstraint, filters, topKey, topKey, orderingProperty);
orderingProperty);
int count = got.size() > limit ? limit : got.size(); int count = got.size() > limit ? limit : got.size();
NumberedFilter firstRemovalKey = null; NumberedFilter firstRemovalKey = null;
for(NumberedFilter nf : got.keySet()){ for (NumberedFilter nf : got.keySet()) {
if(--count>=0 || limit<=0){ if (--count >= 0 || limit <= 0) {
if(pad){ if (pad) {
padMap(got.get(nf), temporalConstraint); padMap(got.get(nf), temporalConstraint);
} }
}else{ } else {
if(firstRemovalKey==null){ if (firstRemovalKey == null) {
firstRemovalKey = nf; firstRemovalKey = nf;
}else{ } else {
break; break;
} }
} }
} }
if(firstRemovalKey!=null){ if (firstRemovalKey != null) {
return got.subMap(got.firstKey(), firstRemovalKey); return got.subMap(got.firstKey(), firstRemovalKey);
} }
return got; return got;
} }
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues( public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?,?>> clz, Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters,
TemporalConstraint temporalConstraint, List<Filter> filters, String topKey) throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
String topKey) throws DuplicatedKeyFilterException, String orderingProperty = AccountingPersistenceQuery.getDefaultOrderingProperties(clz);
KeyException, ValueException, Exception {
String orderingProperty = AccountingPersistenceQuery
.getDefaultOrderingProperties(clz);
return this.getTopValues(clz, temporalConstraint, filters, topKey, orderingProperty, false, 0);
return this.getTopValues(clz, temporalConstraint, filters, topKey,
orderingProperty, false, 0);
} }
/** /**
@ -259,56 +222,42 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
*/ */
@Override @Override
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues( public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
Class<? extends AggregatedRecord<?,?>> clz, Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters,
TemporalConstraint temporalConstraint, List<Filter> filters, String topKey, String orderingProperty)
String topKey, String orderingProperty) throws throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
DuplicatedKeyFilterException, KeyException, ValueException, return this.getTopValues(clz, temporalConstraint, filters, topKey, orderingProperty, false, 0);
Exception {
return this.getTopValues(clz, temporalConstraint, filters, topKey,
orderingProperty, false, 0);
} }
public SortedSet<NumberedFilter> getNextPossibleValues( public SortedSet<NumberedFilter> getNextPossibleValues(Class<? extends AggregatedRecord<?, ?>> clz,
Class<? extends AggregatedRecord<?,?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters, String key)
TemporalConstraint temporalConstraint, List<Filter> filters, throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
String key) throws DuplicatedKeyFilterException, KeyException,
ValueException, Exception {
String orderingProperty = AccountingPersistenceQuery String orderingProperty = AccountingPersistenceQuery.getDefaultOrderingProperties(clz);
.getDefaultOrderingProperties(clz);
return this.getNextPossibleValues(clz, temporalConstraint, filters, return this.getNextPossibleValues(clz, temporalConstraint, filters, key, orderingProperty);
key, orderingProperty);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public SortedSet<NumberedFilter> getNextPossibleValues( public SortedSet<NumberedFilter> getNextPossibleValues(Class<? extends AggregatedRecord<?, ?>> clz,
Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters, String key, String orderingProperty)
TemporalConstraint temporalConstraint, List<Filter> filters, throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
String key, String orderingProperty) throws
DuplicatedKeyFilterException, KeyException, ValueException,
Exception {
return AccountingPersistenceBackendQueryFactory.getInstance() return AccountingPersistenceBackendQueryFactory.getInstance().getNextPossibleValues(clz, temporalConstraint,
.getNextPossibleValues(clz, temporalConstraint, filters, filters, key, orderingProperty);
key, orderingProperty);
} }
@Override @Override
public SortedSet<NumberedFilter> getNextPossibleValuesWithMap( public SortedSet<NumberedFilter> getNextPossibleValuesWithMap(Class<? extends AggregatedRecord<?, ?>> clz,
Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters, String key, String orderingProperty)
TemporalConstraint temporalConstraint, List<Filter> filters, throws Exception {
String key, String orderingProperty) throws Exception { return AccountingPersistenceBackendQueryFactory.getInstance().getNextPossibleValuesWithMap(clz,
return AccountingPersistenceBackendQueryFactory.getInstance() temporalConstraint, filters, key, orderingProperty);
.getNextPossibleValuesWithMap(clz, temporalConstraint, filters,
key, orderingProperty);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -321,154 +270,121 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void prepareConnection( public void prepareConnection(AccountingPersistenceBackendQueryConfiguration configuration) throws Exception {
AccountingPersistenceBackendQueryConfiguration configuration)
throws Exception {
throw new InvalidActivityException(); throw new InvalidActivityException();
} }
@Override @Override
public SortedSet<NumberedFilter> getFilterValues( public SortedSet<NumberedFilter> getFilterValues(Class<? extends AggregatedRecord<?, ?>> clz,
Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters, String key) throws Exception {
TemporalConstraint temporalConstraint, List<Filter> filters,
String key) throws Exception { return AccountingPersistenceBackendQueryFactory.getInstance().getFilterValues(clz, temporalConstraint, filters,
key);
return AccountingPersistenceBackendQueryFactory.getInstance()
.getFilterValues(clz, temporalConstraint, filters,
key);
} }
@Override @Override
public JSONObject getUsageValue(Class<? extends AggregatedRecord<?, ?>> clz, public JSONObject getUsageValue(Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint,
TemporalConstraint temporalConstraint, Filter applicant) Filter applicant) throws Exception {
throws Exception {
return AccountingPersistenceBackendQueryFactory.getInstance()
.getUsageValue(clz, temporalConstraint, applicant);
}
return AccountingPersistenceBackendQueryFactory.getInstance().getUsageValue(clz, temporalConstraint, applicant);
}
@Override @Override
public List<UsageValue> getUsageValueQuotaTotal( List<UsageValue> listUsage) public List<UsageValue> getUsageValueQuotaTotal(List<UsageValue> listUsage) throws Exception {
throws Exception {
return AccountingPersistenceBackendQueryFactory.getInstance().getUsageValueQuotaTotal(listUsage);
return AccountingPersistenceBackendQueryFactory.getInstance()
.getUsageValueQuotaTotal(listUsage);
} }
@Override @Override
public SortedMap<Filter, SortedMap<Calendar, Info>> getContextTimeSeries( public SortedMap<Filter, SortedMap<Calendar, Info>> getContextTimeSeries(
Class<? extends AggregatedRecord<?, ?>> clz, Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters,
TemporalConstraint temporalConstraint, List<Filter> filters,List<String> contexts) List<String> contexts) throws Exception {
throws Exception {
return AccountingPersistenceBackendQueryFactory.getInstance().getContextTimeSeries(clz, temporalConstraint,
return AccountingPersistenceBackendQueryFactory.getInstance() filters, contexts);
.getContextTimeSeries(clz, temporalConstraint, filters, contexts);
} }
public SortedMap<Filter, SortedMap<Calendar, Info>> getContextTimeSeries( public SortedMap<Filter, SortedMap<Calendar, Info>> getContextTimeSeries(
Class<? extends AggregatedRecord<?, ?>> clz, Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters,
TemporalConstraint temporalConstraint, List<Filter> filters, List<String> contexts, boolean pad) throws DuplicatedKeyFilterException, Exception {
List<String> contexts, boolean pad)
throws DuplicatedKeyFilterException,
Exception {
SortedMap<Filter, SortedMap<Calendar, Info>> got; SortedMap<Filter, SortedMap<Calendar, Info>> got;
got = AccountingPersistenceBackendQueryFactory.getInstance() got = AccountingPersistenceBackendQueryFactory.getInstance().getContextTimeSeries(clz, temporalConstraint,
.getContextTimeSeries(clz, temporalConstraint, filters, contexts); filters, contexts);
int count = got.size(); int count = got.size();
Filter firstRemovalKey = null; Filter firstRemovalKey = null;
for(Filter nf : got.keySet()){ for (Filter nf : got.keySet()) {
if(--count>=0){ if (--count >= 0) {
if(pad){ if (pad) {
padMap(got.get(nf), temporalConstraint); padMap(got.get(nf), temporalConstraint);
} }
}else{ } else {
if(firstRemovalKey==null){ if (firstRemovalKey == null) {
firstRemovalKey = nf; firstRemovalKey = nf;
}else{ } else {
break; break;
} }
} }
} }
if(firstRemovalKey!=null){ if (firstRemovalKey != null) {
return got.subMap(got.firstKey(), firstRemovalKey); return got.subMap(got.firstKey(), firstRemovalKey);
} }
return got; return got;
} }
public String getRecord(String recordId, String type) throws Exception {
String record = AccountingPersistenceBackendQueryFactory.getInstance().getRecord(recordId, type);
public String getRecord(String recordId, String type ) throws Exception {
String record= AccountingPersistenceBackendQueryFactory.getInstance().getRecord(recordId,type);
return record; return record;
} }
@Override @Override
public SortedSet<String> getSpaceProvidersIds() throws Exception{ public SortedSet<String> getSpaceProvidersIds() throws Exception {
SortedSet<String> providersId= AccountingPersistenceBackendQueryFactory.getInstance().getSpaceProvidersIds(); SortedSet<String> providersId = AccountingPersistenceBackendQueryFactory.getInstance().getSpaceProvidersIds();
return providersId; return providersId;
} }
@Override @Override
public SortedMap<Filter, SortedMap<Calendar, Long>> getSpaceTimeSeries( public SortedMap<Filter, SortedMap<Calendar, Long>> getSpaceTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters, List<String> providersId) throws Exception {
TemporalConstraint temporalConstraint, List<Filter> filters,
List<String> providersId) SortedMap<Filter, SortedMap<Calendar, Long>> spaceTimeSeries = AccountingPersistenceBackendQueryFactory
throws Exception { .getInstance().getSpaceTimeSeries(clz, temporalConstraint, filters, providersId);
SortedMap<Filter, SortedMap<Calendar, Long>> spaceTimeSeries=
AccountingPersistenceBackendQueryFactory.getInstance().getSpaceTimeSeries(clz, temporalConstraint, filters, providersId);
int count = spaceTimeSeries.size(); int count = spaceTimeSeries.size();
Filter firstRemovalKey = null; Filter firstRemovalKey = null;
for(Filter nf : spaceTimeSeries.keySet()){ for (Filter nf : spaceTimeSeries.keySet()) {
if(--count>=0){ if (--count >= 0) {
padMapStorage(spaceTimeSeries.get(nf), temporalConstraint); padMapStorage(spaceTimeSeries.get(nf), temporalConstraint);
}else{ } else {
if(firstRemovalKey==null){ if (firstRemovalKey == null) {
firstRemovalKey = nf; firstRemovalKey = nf;
}else{ } else {
break; break;
} }
} }
} }
if(firstRemovalKey!=null){ if (firstRemovalKey != null) {
return spaceTimeSeries.subMap(spaceTimeSeries.firstKey(), firstRemovalKey); return spaceTimeSeries.subMap(spaceTimeSeries.firstKey(), firstRemovalKey);
} }
return spaceTimeSeries; return spaceTimeSeries;
} }
public SortedMap<Calendar, Long> padMapStorage(SortedMap<Calendar, Long> unpaddedData,
public SortedMap<Calendar, Long> padMapStorage(
SortedMap<Calendar, Long> unpaddedData,
TemporalConstraint temporalConstraint) throws Exception { TemporalConstraint temporalConstraint) throws Exception {
SortedSet<Calendar> sequence = temporalConstraint.getCalendarSequence(); SortedSet<Calendar> sequence = temporalConstraint.getCalendarSequence();
Long longValuePre = null; Long longValuePre = null;
for (Calendar progressTime : sequence) { for (Calendar progressTime : sequence) {
Long longValue = unpaddedData.get(progressTime); Long longValue = unpaddedData.get(progressTime);
if (longValue == null) { if (longValue == null) {
unpaddedData.put(progressTime, longValuePre); unpaddedData.put(progressTime, longValuePre);
}else{ } else {
longValuePre=longValue; longValuePre = longValue;
} }
} }
return unpaddedData; return unpaddedData;
} }
@ -478,7 +394,4 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ
return AccountingPersistenceBackendQueryFactory.getInstance().isConnectionActive(); return AccountingPersistenceBackendQueryFactory.getInstance().isConnectionActive();
} }
} }