Implementing library

This commit is contained in:
Luca Frosini 2021-03-16 18:06:38 +01:00
parent f8c8605523
commit 63b8fde61c
1 changed files with 31 additions and 15 deletions

View File

@ -4,11 +4,11 @@
package org.gcube.accounting.analytics.persistence.postgresql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.OffsetDateTime;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
@ -44,11 +44,16 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS Z";
public static final String URL_PROPERTY_KEY = AccountingPersistenceConfiguration.URL_PROPERTY_KEY;
public static final String USERNAME_PROPERTY_KEY = AccountingPersistenceConfiguration.USERNAME_PROPERTY_KEY;
public static final String PASSWORD_PROPERTY_KEY = AccountingPersistenceConfiguration.PASSWORD_PROPERTY_KEY;
private String url;
private String username;
private String password;
protected AccountingPersistenceBackendQueryConfiguration configuration;
protected Map<String,String> connectionMap;
protected Map<String, String> typeNameToConnectionURL;
static {
// One Record per package is enough
@ -65,16 +70,24 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
}
}
protected Connection getConnection(Class<? extends AggregatedRecord<?, ?>> clz) throws Exception {
// String typeName = UsageRecordToDBMapping.getRecordToDB(clz).getTypeName();
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection(url, username, password);
logger.trace("Database opened successfully");
connection.setAutoCommit(false);
return connection;
}
@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<>();
this.url = configuration.getProperty(URL_PROPERTY_KEY);
this.username = configuration.getProperty(USERNAME_PROPERTY_KEY);
this.password = configuration.getProperty(PASSWORD_PROPERTY_KEY);
}
@Override
public SortedMap<Calendar, Info> getTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters)
@ -83,10 +96,6 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
return null;
}
protected Calendar getCalendar(OffsetDateTime offsetDateTime) {
Calendar calendar = Calendar.getInstance();
long epochMillis = offsetDateTime.toInstant().toEpochMilli();
@ -98,7 +107,7 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
public SortedMap<Calendar, Info> getNoContextTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
TemporalConstraint temporalConstraint, List<Filter> filters)
throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
Connection connection = null;
Connection connection = getConnection(clz);
Statement statement = connection.createStatement();
SortedMap<Calendar, Info> result = new TreeMap<>();
@ -109,20 +118,27 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
String sql = query.getNoContextTimeSeriesQuery();
List<String> requestedTableField = query.getRequestedTableField();
RecordToDBMapper recordToDBMapper = query.getRecordToDBMapper();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
OffsetDateTime offsetDateTime = resultSet.getObject(Query.DATE_OF_TIMESERIES_AS_FIELD, OffsetDateTime.class);
Calendar calendar = getCalendar(offsetDateTime);
JSONObject jsonObject = new JSONObject();
for(String tableField : requestedTableField) {
String usageRecordField = recordToDBMapper.getUsageRecordField(tableField);
Object object = resultSet.getObject(tableField);
jsonObject.put(usageRecordField, object);
}
Info info = new Info(calendar, jsonObject);
result.put(calendar, info);
}
return null;
return result;
}
@Override