2021-03-15 16:58:07 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.accounting.analytics.persistence.postgresql;
|
|
|
|
|
2021-03-16 15:33:34 +01:00
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.Statement;
|
|
|
|
import java.time.OffsetDateTime;
|
2021-03-15 16:58:07 +01:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.SortedMap;
|
|
|
|
import java.util.SortedSet;
|
2021-03-16 15:33:34 +01:00
|
|
|
import java.util.TreeMap;
|
2021-03-15 16:58:07 +01:00
|
|
|
|
|
|
|
import org.gcube.accounting.analytics.Filter;
|
|
|
|
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.datamodel.aggregation.AggregatedServiceUsageRecord;
|
|
|
|
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
|
|
|
import org.gcube.accounting.persistence.AccountingPersistenceConfiguration;
|
|
|
|
import org.gcube.documentstore.records.AggregatedRecord;
|
|
|
|
import org.gcube.documentstore.records.RecordUtility;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class AccountingPersistenceQueryPostgreSQL implements AccountingPersistenceBackendQuery {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AccountingPersistenceQueryPostgreSQL.class);
|
|
|
|
|
2021-03-16 10:37:52 +01:00
|
|
|
public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS Z";
|
|
|
|
|
2021-03-15 16:58:07 +01:00
|
|
|
public static final String URL_PROPERTY_KEY = AccountingPersistenceConfiguration.URL_PROPERTY_KEY;
|
|
|
|
public static final String PASSWORD_PROPERTY_KEY = AccountingPersistenceConfiguration.PASSWORD_PROPERTY_KEY;
|
|
|
|
|
|
|
|
protected AccountingPersistenceBackendQueryConfiguration configuration;
|
|
|
|
|
|
|
|
protected Map<String,String> connectionMap;
|
|
|
|
|
|
|
|
static {
|
|
|
|
// One Record per package is enough
|
|
|
|
RecordUtility.addRecordPackage(ServiceUsageRecord.class.getPackage());
|
|
|
|
RecordUtility.addRecordPackage(AggregatedServiceUsageRecord.class.getPackage());
|
2021-03-16 15:33:34 +01:00
|
|
|
Map<String, Class<? extends AggregatedRecord<?,?>>> aggregatedRecords = RecordUtility.getAggregatedRecordClassesFound();
|
|
|
|
for(String typeName : aggregatedRecords.keySet()) {
|
|
|
|
try {
|
|
|
|
Class<? extends AggregatedRecord<?,?>> clz = aggregatedRecords.get(typeName);
|
|
|
|
UsageRecordToDBMapping.getRecordToDB(clz);
|
|
|
|
} catch (Exception e) {
|
|
|
|
new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 16:58:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void prepareConnection(AccountingPersistenceBackendQueryConfiguration configuration) throws Exception {
|
|
|
|
logger.trace("prepareConnection");
|
|
|
|
// String url = configuration.getProperty(URL_PROPERTY_KEY);
|
|
|
|
this.configuration = configuration;
|
|
|
|
this.connectionMap = new HashMap<>();
|
|
|
|
}
|
2021-03-16 10:37:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-15 16:58:07 +01:00
|
|
|
@Override
|
|
|
|
public SortedMap<Calendar, Info> getTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters)
|
|
|
|
throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
2021-03-16 10:37:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-16 15:33:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
protected Calendar getCalendar(OffsetDateTime offsetDateTime) {
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
long epochMillis = offsetDateTime.toInstant().toEpochMilli();
|
|
|
|
calendar.setTimeInMillis(epochMillis);
|
|
|
|
return calendar;
|
2021-03-16 10:37:52 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 15:33:34 +01:00
|
|
|
@Override
|
|
|
|
public SortedMap<Calendar, Info> getNoContextTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
2021-03-16 10:37:52 +01:00
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters)
|
|
|
|
throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
2021-03-16 15:33:34 +01:00
|
|
|
Connection connection = null;
|
|
|
|
Statement statement = connection.createStatement();
|
2021-03-16 10:37:52 +01:00
|
|
|
|
2021-03-16 15:33:34 +01:00
|
|
|
SortedMap<Calendar, Info> result = new TreeMap<>();
|
2021-03-16 10:37:52 +01:00
|
|
|
|
2021-03-16 15:33:34 +01:00
|
|
|
Query query = new Query(clz);
|
|
|
|
query.setTemporalConstraint(temporalConstraint);
|
|
|
|
query.setFilters(filters);
|
|
|
|
String sql = query.getNoContextTimeSeriesQuery();
|
2021-03-16 10:37:52 +01:00
|
|
|
|
2021-03-16 15:33:34 +01:00
|
|
|
List<String> requestedTableField = query.getRequestedTableField();
|
|
|
|
ResultSet resultSet = statement.executeQuery(sql);
|
2021-03-16 10:37:52 +01:00
|
|
|
|
2021-03-16 15:33:34 +01:00
|
|
|
while (resultSet.next()) {
|
|
|
|
|
|
|
|
OffsetDateTime offsetDateTime = resultSet.getObject(Query.DATE_OF_TIMESERIES_AS_FIELD, OffsetDateTime.class);
|
|
|
|
Calendar calendar = getCalendar(offsetDateTime);
|
|
|
|
|
|
|
|
|
2021-03-16 10:37:52 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-15 16:58:07 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SortedSet<NumberedFilter> getNextPossibleValues(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, String key, String orderingProperty)
|
|
|
|
throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SortedSet<NumberedFilter> getNextPossibleValuesWithMap(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, String key, String orderingProperty)
|
|
|
|
throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SortedSet<NumberedFilter> getFilterValues(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, String key) throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SortedSet<NumberedFilter> getFilterValues(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, String key, Integer limit) throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JSONObject getUsageValue(Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint,
|
|
|
|
Filter applicant) throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<UsageValue> getUsageValueQuotaTotal(List<UsageValue> listUsage) throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SortedMap<Filter, SortedMap<Calendar, Info>> getContextTimeSeries(
|
|
|
|
Class<? extends AggregatedRecord<?, ?>> clz, TemporalConstraint temporalConstraint, List<Filter> filters,
|
|
|
|
List<String> contexts) throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getRecord(String recordId, String type) throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SortedSet<String> getSpaceProvidersIds() throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SortedMap<Filter, SortedMap<Calendar, Long>> getSpaceTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
|
|
|
TemporalConstraint temporalConstraint, List<Filter> filters, List<String> providersId) throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isConnectionActive() throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|