implementing methods
This commit is contained in:
parent
7ed62332b4
commit
dc27ed4af4
|
@ -9,8 +9,10 @@ import java.sql.Statement;
|
|||
import java.time.OffsetDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeMap;
|
||||
|
@ -84,26 +86,11 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|||
this.usageRecordDB = new HashMap<>();
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
protected Calendar getCalendar(OffsetDateTime offsetDateTime) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
long epochMillis = offsetDateTime.toInstant().toEpochMilli();
|
||||
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 {
|
||||
TemporalConstraint temporalConstraint, List<Filter> filters, Set<String> contexts)
|
||||
throws Exception {
|
||||
Connection connection = getConnection(clz);
|
||||
try {
|
||||
Statement statement = connection.createStatement();
|
||||
|
||||
SortedMap<Calendar, Info> result = new TreeMap<>();
|
||||
|
@ -111,7 +98,9 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|||
Query query = new Query(clz);
|
||||
query.setTemporalConstraint(temporalConstraint);
|
||||
query.setFilters(filters);
|
||||
String sql = query.getNoContextTimeSeriesQuery();
|
||||
query.setContexts(contexts);
|
||||
|
||||
String sql = query.getTimeSeriesQuery();
|
||||
|
||||
List<String> requestedTableField = query.getRequestedTableField();
|
||||
RecordToDBMapper recordToDBMapper = query.getRecordToDBMapper();
|
||||
|
@ -135,6 +124,33 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|||
}
|
||||
|
||||
return result;
|
||||
}finally {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SortedMap<Calendar, Info> getTimeSeries(Class<? extends AggregatedRecord<?, ?>> clz,
|
||||
TemporalConstraint temporalConstraint, List<Filter> filters)
|
||||
throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
||||
String currentScope = AccountingPersistenceBackendQuery.getScopeToQuery();
|
||||
Set<String> contexts = new HashSet<>();
|
||||
contexts.add(currentScope);
|
||||
return getTimeSeries(clz, temporalConstraint, filters, contexts);
|
||||
}
|
||||
|
||||
protected Calendar getCalendar(OffsetDateTime offsetDateTime) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
long epochMillis = offsetDateTime.toInstant().toEpochMilli();
|
||||
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
|
||||
|
@ -146,6 +162,13 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|||
return null;
|
||||
}
|
||||
|
||||
@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> getNextPossibleValues(Class<? extends AggregatedRecord<?, ?>> clz,
|
||||
TemporalConstraint temporalConstraint, List<Filter> filters, String key, String orderingProperty)
|
||||
|
@ -164,15 +187,7 @@ public class AccountingPersistenceQueryPostgreSQL implements AccountingPersisten
|
|||
|
||||
@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;
|
||||
// OK
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.io.Serializable;
|
|||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -11,6 +12,7 @@ import org.gcube.accounting.analytics.Filter;
|
|||
import org.gcube.accounting.analytics.TemporalConstraint;
|
||||
import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode;
|
||||
import org.gcube.accounting.analytics.TemporalConstraint.CalendarEnum;
|
||||
import org.gcube.accounting.datamodel.UsageRecord;
|
||||
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
||||
import org.gcube.documentstore.records.AggregatedRecord;
|
||||
|
||||
|
@ -45,10 +47,19 @@ public class Query {
|
|||
public void setFilters(List<Filter> filters) {
|
||||
this.filters = filters;
|
||||
}
|
||||
|
||||
public void setContexts(Set<String> contexts) {
|
||||
this.contexts = contexts;
|
||||
}
|
||||
|
||||
public void addContext(String context) {
|
||||
if(contexts == null) {
|
||||
contexts = new HashSet<>();
|
||||
}
|
||||
contexts.add(context);
|
||||
}
|
||||
|
||||
|
||||
public RecordToDBMapper getRecordToDBMapper() {
|
||||
return recordToDBMapper;
|
||||
}
|
||||
|
@ -113,6 +124,26 @@ public class Query {
|
|||
}
|
||||
}
|
||||
|
||||
protected void addContextFilter() {
|
||||
if(contexts!=null && contexts.size()>0) {
|
||||
// The first filter if the time_bucket
|
||||
stringBuffer.append(" AND (");
|
||||
boolean first = true;
|
||||
for(String context : contexts) {
|
||||
if(first) {
|
||||
first = false;
|
||||
}else {
|
||||
stringBuffer.append(" OR ");
|
||||
}
|
||||
appendTableField(UsageRecord.SCOPE);
|
||||
stringBuffer.append("=");
|
||||
appendValue(context);
|
||||
}
|
||||
stringBuffer.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void addEmittedFields() throws Exception {
|
||||
Set<String> aggregatedField = clz.newInstance().getAggregatedFields();
|
||||
for(String fieldName : aggregatedField) {
|
||||
|
@ -209,7 +240,7 @@ public class Query {
|
|||
stringBuffer.append(" ASC");
|
||||
}
|
||||
|
||||
public String getNoContextTimeSeriesQuery() throws Exception {
|
||||
public String getTimeSeriesQuery() throws Exception {
|
||||
newQuery();
|
||||
|
||||
addTimeBucket();
|
||||
|
@ -222,10 +253,12 @@ public class Query {
|
|||
addTemporalConstraintToQuery();
|
||||
|
||||
addFilters();
|
||||
addContextFilter();
|
||||
|
||||
addGropuBy();
|
||||
addOrderBy();
|
||||
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ public class AccountingPersistenceQueryPostgreSQLTest extends ContextTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testGetNoContextTimeSeriesQuery() throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
||||
public void testGetNoContextTimeSeries() throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
||||
Calendar startTimeCalendar = Calendar.getInstance();
|
||||
startTimeCalendar.set(Calendar.YEAR, 2020);
|
||||
startTimeCalendar.set(Calendar.MONTH, Calendar.MARCH);
|
||||
|
@ -68,4 +68,32 @@ public class AccountingPersistenceQueryPostgreSQLTest extends ContextTest {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testTimeSeries() throws DuplicatedKeyFilterException, KeyException, ValueException, Exception {
|
||||
Calendar startTimeCalendar = Calendar.getInstance();
|
||||
startTimeCalendar.set(Calendar.YEAR, 2020);
|
||||
startTimeCalendar.set(Calendar.MONTH, Calendar.MARCH);
|
||||
startTimeCalendar.set(Calendar.DAY_OF_MONTH, 1);
|
||||
startTimeCalendar.set(Calendar.HOUR_OF_DAY, 0);
|
||||
startTimeCalendar.set(Calendar.MINUTE, 0);
|
||||
|
||||
Calendar entTimeCalendar = Calendar.getInstance();
|
||||
entTimeCalendar.set(Calendar.MONTH, Calendar.MARCH);
|
||||
entTimeCalendar.set(Calendar.DAY_OF_MONTH, 15);
|
||||
entTimeCalendar.set(Calendar.HOUR_OF_DAY, 16);
|
||||
entTimeCalendar.set(Calendar.MINUTE, 17);
|
||||
|
||||
List<Filter> filters = new ArrayList<>();
|
||||
Filter filter = new Filter(UsageRecord.CONSUMER_ID, QueryTest.getRandomUser());
|
||||
filters.add(filter);
|
||||
|
||||
TemporalConstraint temporalConstraint = new TemporalConstraint(startTimeCalendar.getTimeInMillis(), entTimeCalendar.getTimeInMillis(), AggregationMode.MINUTELY);
|
||||
SortedMap<Calendar, Info> timeseries = accountingPersistenceQueryPostgreSQL.getTimeSeries(AggregatedServiceUsageRecord.class, temporalConstraint, filters);
|
||||
for(Calendar c : timeseries.keySet()) {
|
||||
Info info = timeseries.get(c);
|
||||
logger.debug("{}", info);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -81,7 +81,7 @@ public class ContextTest {
|
|||
setContext(token);
|
||||
}
|
||||
|
||||
private static void setContext(String token) throws ObjectNotFound, Exception {
|
||||
public static void setContext(String token) throws ObjectNotFound, Exception {
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
||||
|
|
|
@ -5,12 +5,15 @@ package org.gcube.accounting.analytics.persistence.postgresql;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.accounting.analytics.Filter;
|
||||
import org.gcube.accounting.analytics.TemporalConstraint;
|
||||
import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode;
|
||||
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQuery;
|
||||
import org.gcube.accounting.datamodel.UsageRecord;
|
||||
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
||||
import org.junit.Test;
|
||||
|
@ -60,7 +63,14 @@ public class QueryTest extends ContextTest {
|
|||
Query query = new Query(AggregatedServiceUsageRecord.class);
|
||||
query.setTemporalConstraint(temporalConstraint);
|
||||
query.setFilters(filters);
|
||||
String ret = query.getNoContextTimeSeriesQuery();
|
||||
String ret = query.getTimeSeriesQuery();
|
||||
logger.debug(ret);
|
||||
|
||||
String currentScope = AccountingPersistenceBackendQuery.getScopeToQuery();
|
||||
Set<String> contexts = new HashSet<>();
|
||||
contexts.add(currentScope);
|
||||
query.setContexts(contexts);
|
||||
ret = query.getTimeSeriesQuery();
|
||||
logger.debug(ret);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue