Fixing query

This commit is contained in:
Luca Frosini 2021-03-30 17:52:19 +02:00
parent 1dea95a0f3
commit 50ecd09a8c
2 changed files with 39 additions and 63 deletions

View File

@ -3,6 +3,7 @@ package org.gcube.accounting.analytics.persistence.postgresql;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -13,7 +14,6 @@ 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.accounting.datamodel.aggregation.AggregatedStorageStatusRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
import org.gcube.accounting.utility.postgresql.PostgreSQLQuery;
import org.gcube.accounting.utility.postgresql.RecordToDBFields;
@ -39,7 +39,23 @@ public class Query extends PostgreSQLQuery {
private String recordId;
protected Set<String> providersId;
protected String orConditionKey;
protected Set<String> orConditionValues;
public void setOrConditionKey(String orConditionKey) {
this.orConditionKey = orConditionKey;
}
public void setOrConditionValues(Collection<String> orConditionValues) {
this.orConditionValues = new HashSet<>(orConditionValues);
}
public void addOrConditionValue(String conditionValue) {
if(this.orConditionValues == null) {
this.orConditionValues = new HashSet<>();
}
this.orConditionValues.add(conditionValue);
}
public Query(Class<? extends AggregatedRecord<?, ?>> clz) throws Exception {
this.clz = clz;
@ -85,17 +101,6 @@ public class Query extends PostgreSQLQuery {
this.recordId = recordId;
}
public void addProvidersId(String providerId) {
if(providersId == null) {
providersId = new HashSet<>();
}
providersId.add(providerId);
}
public void setProvidersId(List<String> providersId) {
this.providersId = new HashSet<>(providersId);
}
public RecordToDBFields getRecordToDBMapper() {
return recordToDBFields;
}
@ -132,42 +137,32 @@ public class Query extends PostgreSQLQuery {
}
}
protected void addContextFilter() {
if(contexts!=null && contexts.size()>0) {
protected void addOrConditions(String key, Set<String> values) {
if(values!=null && values.size()>0) {
// The first filter if the time_bucket
stringBuffer.append(" AND (");
boolean first = true;
for(String context : contexts) {
for(String value : values) {
if(first) {
first = false;
}else {
stringBuffer.append(" OR ");
}
appendTableField(UsageRecord.SCOPE);
appendTableField(key);
stringBuffer.append("=");
appendValue(context);
appendValue(value);
}
stringBuffer.append(")");
}
}
protected void addProvidersIdFilter() {
if(providersId!=null && providersId.size()>0) {
// The first filter if the time_bucket
stringBuffer.append(" AND (");
boolean first = true;
for(String providerId : providersId) {
if(first) {
first = false;
}else {
stringBuffer.append(" OR ");
}
appendTableField(AggregatedStorageStatusRecord.PROVIDER_ID);
stringBuffer.append("=");
appendValue(providerId);
}
stringBuffer.append(")");
}
protected void addContextFilter() {
addOrConditions(UsageRecord.SCOPE, contexts);
}
protected void addOrConditionFilter() {
addOrConditions(orConditionKey, orConditionValues);
}
protected void addEmittedFields() throws Exception {
@ -237,8 +232,9 @@ public class Query extends PostgreSQLQuery {
stringBuffer.append("', ");
appendTableField(AggregatedRecord.START_TIME);
stringBuffer.append(") ");
stringBuffer.append(DATE_OF_TIMESERIES_AS_FIELD);
requestedTableField.add(DATE_OF_TIMESERIES_AS_FIELD);
String tableField = getTableField(DATE_OF_TIMESERIES_AS_FIELD);
stringBuffer.append(tableField);
requestedTableField.add(tableField);
}
private void newQuery() {
@ -263,12 +259,12 @@ public class Query extends PostgreSQLQuery {
protected void addDateGropuBy() {
stringBuffer.append(" GROUP BY ");
stringBuffer.append(DATE_OF_TIMESERIES_AS_FIELD);
appendTableField(DATE_OF_TIMESERIES_AS_FIELD);
}
protected void addDateOrderBy() {
stringBuffer.append(" ORDER BY ");
stringBuffer.append(DATE_OF_TIMESERIES_AS_FIELD);
appendTableField(DATE_OF_TIMESERIES_AS_FIELD);
stringBuffer.append(" ASC");
}
@ -277,7 +273,8 @@ public class Query extends PostgreSQLQuery {
String dbField = getTableField(tableFieldToRequest);
stringBuffer.append(dbField);
stringBuffer.append(" ORDER BY ");
stringBuffer.append(orderByField);
dbField = getTableField(orderByField);
stringBuffer.append(dbField);
stringBuffer.append(" DESC");
}
@ -299,8 +296,7 @@ public class Query extends PostgreSQLQuery {
stringBuffer.append(dbField);
requestedTableField.add(dbField);
stringBuffer.append(") AS ");
stringBuffer.append(orderByField);
stringBuffer.append(dbField);
}
public String getTimeSeriesQuery() throws Exception {
@ -324,25 +320,6 @@ public class Query extends PostgreSQLQuery {
return stringBuffer.toString();
}
public String getSpaceTimeSeries() {
newQuery();
addRequestedDate();
stringBuffer.append(" FROM ");
stringBuffer.append(recordToDBFields.getTableName());
addTemporalConstraintToQuery();
addFilters();
addProvidersIdFilter();
addDateGropuBy();
addDateOrderBy();
return stringBuffer.toString();
}
public String getNextPossibleValueQuery() throws Exception {
newQuery();
@ -361,7 +338,7 @@ public class Query extends PostgreSQLQuery {
return stringBuffer.toString();
}
public String getRecordQuery(){
newQuery();
stringBuffer.append("* ");

View File

@ -21,7 +21,6 @@ import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQu
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageStatusRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
import org.gcube.accounting.utility.postgresql.RecordToDBMapping;
import org.gcube.documentstore.records.Record;