|
|
|
@ -23,23 +23,20 @@ import org.gcube.accounting.analytics.Info;
|
|
|
|
|
import org.gcube.accounting.analytics.NumberedFilter;
|
|
|
|
|
import org.gcube.accounting.analytics.TemporalConstraint;
|
|
|
|
|
import org.gcube.accounting.analytics.UsageValue;
|
|
|
|
|
import org.gcube.accounting.analytics.exception.DuplicatedKeyFilterException;
|
|
|
|
|
import org.gcube.accounting.analytics.exception.KeyException;
|
|
|
|
|
import org.gcube.accounting.analytics.exception.ValueException;
|
|
|
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQuery;
|
|
|
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQueryConfiguration;
|
|
|
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
|
|
|
|
|
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
|
|
|
|
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageStatusRecord;
|
|
|
|
|
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
|
|
|
|
import org.gcube.accounting.persistence.AccountingPersistenceConfiguration;
|
|
|
|
|
import org.gcube.accounting.utility.postgresql.RecordToDBConnection;
|
|
|
|
|
import org.gcube.accounting.utility.postgresql.RecordToDBFields;
|
|
|
|
|
import org.gcube.accounting.utility.postgresql.RecordToDBMapping;
|
|
|
|
|
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
|
import org.gcube.documentstore.records.AggregatedRecord;
|
|
|
|
|
import org.gcube.documentstore.records.Record;
|
|
|
|
|
import org.gcube.documentstore.records.RecordUtility;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
@ -58,6 +55,39 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|
|
|
|
|
|
|
|
|
protected AccountingPersistenceBackendQueryConfiguration configuration;
|
|
|
|
|
|
|
|
|
|
protected ObjectMapper objectMapper;
|
|
|
|
|
|
|
|
|
|
protected Class<? extends AggregatedRecord<?, ?>> clz;
|
|
|
|
|
protected TemporalConstraint temporalConstraint;
|
|
|
|
|
|
|
|
|
|
protected Set<String> contexts;
|
|
|
|
|
protected Set<Filter> filters;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public AccountingPersistenceQueryPostgreSQL() {
|
|
|
|
|
objectMapper = new ObjectMapper();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setRequestedRecords(Class<? extends AggregatedRecord<?, ?>> clz) {
|
|
|
|
|
this.clz = clz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setTemporalConstraint(TemporalConstraint temporalConstraint) {
|
|
|
|
|
this.temporalConstraint = temporalConstraint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setContexts(Set<String> contexts) {
|
|
|
|
|
this.contexts = contexts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setFilters(Set<Filter> filters) {
|
|
|
|
|
this.filters = filters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
// One Record per package is enough
|
|
|
|
|
RecordUtility.addRecordPackage(ServiceUsageRecord.class.getPackage());
|
|
|
|
@ -87,9 +117,24 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected SortedMap<Calendar, Info> getTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, Set<String> contexts)
|
|
|
|
|
throws Exception {
|
|
|
|
|
protected void addProperty(ObjectNode objectNode, String key, Object value) {
|
|
|
|
|
if(value instanceof Number) {
|
|
|
|
|
|
|
|
|
|
if(value instanceof Integer) {
|
|
|
|
|
objectNode.put(key, (int) value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Long longValue = Long.valueOf(value.toString());
|
|
|
|
|
objectNode.put(key, longValue);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
objectNode.put(key, (String) value.toString());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected SortedMap<Calendar, Info> getTimeSeries(Set<String> contexts) throws Exception {
|
|
|
|
|
Connection connection = getConnection(clz);
|
|
|
|
|
try {
|
|
|
|
|
Statement statement = connection.createStatement();
|
|
|
|
@ -114,15 +159,15 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|
|
|
|
OffsetDateTime offsetDateTime = resultSet.getObject(tableFieldName, OffsetDateTime.class);
|
|
|
|
|
Calendar calendar = getCalendar(offsetDateTime);
|
|
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
|
ObjectNode objectNode = objectMapper.createObjectNode();
|
|
|
|
|
|
|
|
|
|
for(String tableField : requestedTableField) {
|
|
|
|
|
String usageRecordField = recordToDBMapper.getRecordField(tableField);
|
|
|
|
|
Object object = resultSet.getObject(tableField);
|
|
|
|
|
jsonObject.put(usageRecordField, object);
|
|
|
|
|
Object obj = resultSet.getObject(tableField);
|
|
|
|
|
addProperty(objectNode, usageRecordField, obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Info info = new Info(calendar, jsonObject);
|
|
|
|
|
Info info = new Info(calendar, objectNode);
|
|
|
|
|
result.put(calendar, info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -133,13 +178,8 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public SortedMap<Calendar, Info> getTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters)
|
|
|
|
|
throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
|
|
|
|
String context = AccountingPersistenceBackendQuery.getScopeToQuery();
|
|
|
|
|
Set<String> contexts = new HashSet<>();
|
|
|
|
|
contexts.add(context);
|
|
|
|
|
return getTimeSeries(clz, temporalConstraint, filters, contexts);
|
|
|
|
|
public SortedMap<Calendar, Info> getTimeSeries() throws Exception {
|
|
|
|
|
return getTimeSeries(contexts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Calendar getCalendar(OffsetDateTime offsetDateTime) {
|
|
|
|
@ -148,25 +188,17 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|
|
|
|
calendar.setTimeInMillis(epochMillis);
|
|
|
|
|
return calendar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public SortedMap<Calendar, Info> getNoContextTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters)
|
|
|
|
|
throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
|
|
|
|
return getTimeSeries(clz, temporalConstraint, filters, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public SortedMap<Filter, SortedMap<Calendar, Info>> getContextTimeSeries(
|
|
|
|
|
Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters,
|
|
|
|
|
List<String> contexts) throws Exception {
|
|
|
|
|
public SortedMap<Filter, SortedMap<Calendar, Info>> getContextTimeSeries() throws Exception {
|
|
|
|
|
|
|
|
|
|
SortedMap<Filter,SortedMap<Calendar,Info>> ret = new TreeMap<>();
|
|
|
|
|
for(String context : contexts) {
|
|
|
|
|
Filter contextFilter = new Filter("scope", context);
|
|
|
|
|
Set<String> timeSeriesContexts = new HashSet<>();
|
|
|
|
|
timeSeriesContexts.add(context);
|
|
|
|
|
SortedMap<Calendar, Info> timeSeries = getTimeSeries(clz, temporalConstraint, filters, timeSeriesContexts);
|
|
|
|
|
Set<String> ctxs = new HashSet<>();
|
|
|
|
|
ctxs.add(context);
|
|
|
|
|
|
|
|
|
|
SortedMap<Calendar, Info> timeSeries = getTimeSeries(ctxs);
|
|
|
|
|
if(!timeSeries.isEmpty()) {
|
|
|
|
|
ret.put(contextFilter, timeSeries);
|
|
|
|
|
}
|
|
|
|
@ -174,9 +206,7 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected SortedSet<NumberedFilter> getNumberedValues(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, String key,
|
|
|
|
|
String orderingProperty, Integer limit) throws Exception {
|
|
|
|
|
protected SortedSet<NumberedFilter> getNumberedValues(String key, String orderingProperty, Integer limit) throws Exception {
|
|
|
|
|
Connection connection = getConnection(clz);
|
|
|
|
|
try {
|
|
|
|
|
Statement statement = connection.createStatement();
|
|
|
|
@ -191,10 +221,6 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|
|
|
|
|
|
|
|
|
SortedSet<NumberedFilter> result = new TreeSet<>();
|
|
|
|
|
|
|
|
|
|
String context = AccountingPersistenceBackendQuery.getScopeToQuery();
|
|
|
|
|
Set<String> contexts = new HashSet<>();
|
|
|
|
|
contexts.add(context);
|
|
|
|
|
|
|
|
|
|
Query query = new Query(clz);
|
|
|
|
|
query.setTemporalConstraint(temporalConstraint);
|
|
|
|
|
query.setFilters(filters);
|
|
|
|
@ -229,34 +255,26 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public SortedSet<NumberedFilter> getFilterValues(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, String key) throws Exception {
|
|
|
|
|
return getNumberedValues(clz, temporalConstraint, filters, key, null, null);
|
|
|
|
|
public SortedSet<NumberedFilter> getFilterValues(String key) throws Exception {
|
|
|
|
|
return getNumberedValues(key, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public SortedSet<NumberedFilter> getFilterValues(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, String key, Integer limit) throws Exception {
|
|
|
|
|
return getNumberedValues(clz, temporalConstraint, filters, key, null, limit);
|
|
|
|
|
public SortedSet<NumberedFilter> getFilterValues(String key, Integer limit) throws Exception {
|
|
|
|
|
return getNumberedValues(key, null, limit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(
|
|
|
|
|
Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters,
|
|
|
|
|
String topKey, String orderingProperty)
|
|
|
|
|
throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
|
|
|
|
|
|
|
|
|
String context = AccountingPersistenceBackendQuery.getScopeToQuery();
|
|
|
|
|
Set<String> contexts = new HashSet<>();
|
|
|
|
|
contexts.add(context);
|
|
|
|
|
public SortedMap<NumberedFilter, SortedMap<Calendar, Info>> getTopValues(String topKey, String orderingProperty)
|
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
|
|
SortedMap<NumberedFilter,SortedMap<Calendar,Info>> ret = new TreeMap<>();
|
|
|
|
|
|
|
|
|
|
SortedSet<NumberedFilter> top = getNumberedValues(clz, temporalConstraint, filters, topKey, orderingProperty, 10);
|
|
|
|
|
SortedSet<NumberedFilter> top = getNumberedValues(topKey, orderingProperty, 10);
|
|
|
|
|
|
|
|
|
|
for(NumberedFilter numberedFilter : top) {
|
|
|
|
|
filters.add(numberedFilter);
|
|
|
|
|
SortedMap<Calendar,Info> map = getTimeSeries(clz, temporalConstraint, filters, contexts);
|
|
|
|
|
SortedMap<Calendar,Info> map = getTimeSeries();
|
|
|
|
|
ret.put(numberedFilter, map);
|
|
|
|
|
filters.remove(numberedFilter);
|
|
|
|
|
}
|
|
|
|
@ -309,43 +327,33 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public SortedSet<String> getSpaceProvidersIds() throws Exception {
|
|
|
|
|
Class<? extends AggregatedRecord<?, ?>> aggregatedRecordClass = AggregatedStorageStatusRecord.class;
|
|
|
|
|
Connection connection = getConnection(aggregatedRecordClass);
|
|
|
|
|
try {
|
|
|
|
|
Statement statement = connection.createStatement();
|
|
|
|
|
|
|
|
|
|
Query query = new Query(aggregatedRecordClass);
|
|
|
|
|
query.setTableFieldToRequest(AggregatedStorageStatusRecord.PROVIDER_URI);
|
|
|
|
|
String sql = query.getDinstinctValuesQuery();
|
|
|
|
|
|
|
|
|
|
SortedSet<String> providersIds = new TreeSet<>();
|
|
|
|
|
|
|
|
|
|
logger.trace("Going to request the following query: {}", sql);
|
|
|
|
|
ResultSet resultSet = statement.executeQuery(sql);
|
|
|
|
|
|
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
String id = resultSet.getString(1);
|
|
|
|
|
providersIds.add(id);
|
|
|
|
|
public SortedMap<Filter, SortedMap<Calendar, Info>> getSpaceTimeSeries(Set<String> dataTypes) throws Exception {
|
|
|
|
|
/*
|
|
|
|
|
SortedMap<Filter, SortedMap<Calendar, Info>> sortedMap = new TreeMap<>();
|
|
|
|
|
setRequestedRecords(AggregatedStorageStatusRecord.class);
|
|
|
|
|
for(String dataType : dataTypes) {
|
|
|
|
|
Filter filter = new Filter(StorageStatusRecord.DATA_TYPE, dataType);
|
|
|
|
|
if(filters == null) {
|
|
|
|
|
filters = new HashSet<>();
|
|
|
|
|
}
|
|
|
|
|
filters.add(filter);
|
|
|
|
|
|
|
|
|
|
return providersIds;
|
|
|
|
|
}finally {
|
|
|
|
|
connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<UsageValue> getUsageValueQuotaTotal(List<UsageValue> listUsage) throws Exception {
|
|
|
|
|
SortedMap<Calendar, Info> timeSeries = getTimeSeries();
|
|
|
|
|
sortedMap.put(filter, timeSeries);
|
|
|
|
|
|
|
|
|
|
filters.remove(filter);
|
|
|
|
|
}
|
|
|
|
|
return sortedMap;
|
|
|
|
|
*/
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public SortedMap<Filter, SortedMap<Calendar, Long>> getSpaceTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, List<String> providersId) throws Exception {
|
|
|
|
|
public List<UsageValue> getUsageValueQuotaTotal(List<UsageValue> listUsage) throws Exception {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void close() throws Exception {
|
|
|
|
|
// OK
|
|
|
|
|