Fixing query
This commit is contained in:
parent
1dea95a0f3
commit
50ecd09a8c
|
@ -3,6 +3,7 @@ package org.gcube.accounting.analytics.persistence.postgresql;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
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.analytics.TemporalConstraint.CalendarEnum;
|
||||||
import org.gcube.accounting.datamodel.UsageRecord;
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
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.datamodel.aggregation.AggregatedStorageUsageRecord;
|
||||||
import org.gcube.accounting.utility.postgresql.PostgreSQLQuery;
|
import org.gcube.accounting.utility.postgresql.PostgreSQLQuery;
|
||||||
import org.gcube.accounting.utility.postgresql.RecordToDBFields;
|
import org.gcube.accounting.utility.postgresql.RecordToDBFields;
|
||||||
|
@ -39,7 +39,23 @@ public class Query extends PostgreSQLQuery {
|
||||||
|
|
||||||
private String recordId;
|
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 {
|
public Query(Class<? extends AggregatedRecord<?, ?>> clz) throws Exception {
|
||||||
this.clz = clz;
|
this.clz = clz;
|
||||||
|
@ -85,17 +101,6 @@ public class Query extends PostgreSQLQuery {
|
||||||
this.recordId = recordId;
|
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() {
|
public RecordToDBFields getRecordToDBMapper() {
|
||||||
return recordToDBFields;
|
return recordToDBFields;
|
||||||
}
|
}
|
||||||
|
@ -132,42 +137,32 @@ public class Query extends PostgreSQLQuery {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addContextFilter() {
|
protected void addOrConditions(String key, Set<String> values) {
|
||||||
if(contexts!=null && contexts.size()>0) {
|
if(values!=null && values.size()>0) {
|
||||||
// The first filter if the time_bucket
|
// The first filter if the time_bucket
|
||||||
stringBuffer.append(" AND (");
|
stringBuffer.append(" AND (");
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
for(String context : contexts) {
|
for(String value : values) {
|
||||||
if(first) {
|
if(first) {
|
||||||
first = false;
|
first = false;
|
||||||
}else {
|
}else {
|
||||||
stringBuffer.append(" OR ");
|
stringBuffer.append(" OR ");
|
||||||
}
|
}
|
||||||
appendTableField(UsageRecord.SCOPE);
|
appendTableField(key);
|
||||||
stringBuffer.append("=");
|
stringBuffer.append("=");
|
||||||
appendValue(context);
|
appendValue(value);
|
||||||
}
|
}
|
||||||
stringBuffer.append(")");
|
stringBuffer.append(")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addProvidersIdFilter() {
|
|
||||||
if(providersId!=null && providersId.size()>0) {
|
protected void addContextFilter() {
|
||||||
// The first filter if the time_bucket
|
addOrConditions(UsageRecord.SCOPE, contexts);
|
||||||
stringBuffer.append(" AND (");
|
}
|
||||||
boolean first = true;
|
|
||||||
for(String providerId : providersId) {
|
protected void addOrConditionFilter() {
|
||||||
if(first) {
|
addOrConditions(orConditionKey, orConditionValues);
|
||||||
first = false;
|
|
||||||
}else {
|
|
||||||
stringBuffer.append(" OR ");
|
|
||||||
}
|
|
||||||
appendTableField(AggregatedStorageStatusRecord.PROVIDER_ID);
|
|
||||||
stringBuffer.append("=");
|
|
||||||
appendValue(providerId);
|
|
||||||
}
|
|
||||||
stringBuffer.append(")");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addEmittedFields() throws Exception {
|
protected void addEmittedFields() throws Exception {
|
||||||
|
@ -237,8 +232,9 @@ public class Query extends PostgreSQLQuery {
|
||||||
stringBuffer.append("', ");
|
stringBuffer.append("', ");
|
||||||
appendTableField(AggregatedRecord.START_TIME);
|
appendTableField(AggregatedRecord.START_TIME);
|
||||||
stringBuffer.append(") ");
|
stringBuffer.append(") ");
|
||||||
stringBuffer.append(DATE_OF_TIMESERIES_AS_FIELD);
|
String tableField = getTableField(DATE_OF_TIMESERIES_AS_FIELD);
|
||||||
requestedTableField.add(DATE_OF_TIMESERIES_AS_FIELD);
|
stringBuffer.append(tableField);
|
||||||
|
requestedTableField.add(tableField);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void newQuery() {
|
private void newQuery() {
|
||||||
|
@ -263,12 +259,12 @@ public class Query extends PostgreSQLQuery {
|
||||||
|
|
||||||
protected void addDateGropuBy() {
|
protected void addDateGropuBy() {
|
||||||
stringBuffer.append(" GROUP BY ");
|
stringBuffer.append(" GROUP BY ");
|
||||||
stringBuffer.append(DATE_OF_TIMESERIES_AS_FIELD);
|
appendTableField(DATE_OF_TIMESERIES_AS_FIELD);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addDateOrderBy() {
|
protected void addDateOrderBy() {
|
||||||
stringBuffer.append(" ORDER BY ");
|
stringBuffer.append(" ORDER BY ");
|
||||||
stringBuffer.append(DATE_OF_TIMESERIES_AS_FIELD);
|
appendTableField(DATE_OF_TIMESERIES_AS_FIELD);
|
||||||
stringBuffer.append(" ASC");
|
stringBuffer.append(" ASC");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,7 +273,8 @@ public class Query extends PostgreSQLQuery {
|
||||||
String dbField = getTableField(tableFieldToRequest);
|
String dbField = getTableField(tableFieldToRequest);
|
||||||
stringBuffer.append(dbField);
|
stringBuffer.append(dbField);
|
||||||
stringBuffer.append(" ORDER BY ");
|
stringBuffer.append(" ORDER BY ");
|
||||||
stringBuffer.append(orderByField);
|
dbField = getTableField(orderByField);
|
||||||
|
stringBuffer.append(dbField);
|
||||||
stringBuffer.append(" DESC");
|
stringBuffer.append(" DESC");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,8 +296,7 @@ public class Query extends PostgreSQLQuery {
|
||||||
stringBuffer.append(dbField);
|
stringBuffer.append(dbField);
|
||||||
requestedTableField.add(dbField);
|
requestedTableField.add(dbField);
|
||||||
stringBuffer.append(") AS ");
|
stringBuffer.append(") AS ");
|
||||||
stringBuffer.append(orderByField);
|
stringBuffer.append(dbField);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTimeSeriesQuery() throws Exception {
|
public String getTimeSeriesQuery() throws Exception {
|
||||||
|
@ -324,25 +320,6 @@ public class Query extends PostgreSQLQuery {
|
||||||
return stringBuffer.toString();
|
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 {
|
public String getNextPossibleValueQuery() throws Exception {
|
||||||
newQuery();
|
newQuery();
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,6 @@ import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQu
|
||||||
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
|
||||||
import org.gcube.accounting.datamodel.UsageRecord;
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
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.datamodel.aggregation.AggregatedStorageUsageRecord;
|
||||||
import org.gcube.accounting.utility.postgresql.RecordToDBMapping;
|
import org.gcube.accounting.utility.postgresql.RecordToDBMapping;
|
||||||
import org.gcube.documentstore.records.Record;
|
import org.gcube.documentstore.records.Record;
|
||||||
|
|
Loading…
Reference in New Issue