Implementing library
This commit is contained in:
parent
f8c8605523
commit
63b8fde61c
|
@ -4,11 +4,11 @@
|
||||||
package org.gcube.accounting.analytics.persistence.postgresql;
|
package org.gcube.accounting.analytics.persistence.postgresql;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.SortedMap;
|
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 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 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;
|
public static final String PASSWORD_PROPERTY_KEY = AccountingPersistenceConfiguration.PASSWORD_PROPERTY_KEY;
|
||||||
|
|
||||||
|
private String url;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
protected AccountingPersistenceBackendQueryConfiguration configuration;
|
protected AccountingPersistenceBackendQueryConfiguration configuration;
|
||||||
|
|
||||||
protected Map<String,String> connectionMap;
|
protected Map<String, String> typeNameToConnectionURL;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// One Record per package is enough
|
// 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
|
@Override
|
||||||
public void prepareConnection(AccountingPersistenceBackendQueryConfiguration configuration) throws Exception {
|
public void prepareConnection(AccountingPersistenceBackendQueryConfiguration configuration) throws Exception {
|
||||||
logger.trace("prepareConnection");
|
logger.trace("prepareConnection");
|
||||||
// String url = configuration.getProperty(URL_PROPERTY_KEY);
|
|
||||||
this.configuration = configuration;
|
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
|
@Override
|
||||||
public SortedMap<Calendar, Info> getTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
public SortedMap<Calendar, Info> getTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
||||||
TemporalConstraint temporalConstraint, List<Filter> filters)
|
TemporalConstraint temporalConstraint, List<Filter> filters)
|
||||||
|
@ -83,10 +96,6 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected Calendar getCalendar(OffsetDateTime offsetDateTime) {
|
protected Calendar getCalendar(OffsetDateTime offsetDateTime) {
|
||||||
Calendar calendar = Calendar.getInstance();
|
Calendar calendar = Calendar.getInstance();
|
||||||
long epochMillis = offsetDateTime.toInstant().toEpochMilli();
|
long epochMillis = offsetDateTime.toInstant().toEpochMilli();
|
||||||
|
@ -98,7 +107,7 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
||||||
public SortedMap<Calendar, Info> getNoContextTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
public SortedMap<Calendar, Info> getNoContextTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
||||||
TemporalConstraint temporalConstraint, List<Filter> filters)
|
TemporalConstraint temporalConstraint, List<Filter> filters)
|
||||||
throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
||||||
Connection connection = null;
|
Connection connection = getConnection(clz);
|
||||||
Statement statement = connection.createStatement();
|
Statement statement = connection.createStatement();
|
||||||
|
|
||||||
SortedMap<Calendar, Info> result = new TreeMap<>();
|
SortedMap<Calendar, Info> result = new TreeMap<>();
|
||||||
|
@ -109,20 +118,27 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
||||||
String sql = query.getNoContextTimeSeriesQuery();
|
String sql = query.getNoContextTimeSeriesQuery();
|
||||||
|
|
||||||
List<String> requestedTableField = query.getRequestedTableField();
|
List<String> requestedTableField = query.getRequestedTableField();
|
||||||
|
RecordToDBMapper recordToDBMapper = query.getRecordToDBMapper();
|
||||||
|
|
||||||
ResultSet resultSet = statement.executeQuery(sql);
|
ResultSet resultSet = statement.executeQuery(sql);
|
||||||
|
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
|
|
||||||
OffsetDateTime offsetDateTime = resultSet.getObject(Query.DATE_OF_TIMESERIES_AS_FIELD, OffsetDateTime.class);
|
OffsetDateTime offsetDateTime = resultSet.getObject(Query.DATE_OF_TIMESERIES_AS_FIELD, OffsetDateTime.class);
|
||||||
Calendar calendar = getCalendar(offsetDateTime);
|
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 result;
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue