128 lines
5.1 KiB
Java
128 lines
5.1 KiB
Java
package org.gcube.portlets.admin.accountingmanager.server.amservice.command;
|
|
|
|
import java.util.Calendar;
|
|
import java.util.HashSet;
|
|
import java.util.SortedMap;
|
|
|
|
import org.gcube.accounting.analytics.Info;
|
|
import org.gcube.accounting.analytics.NumberedFilter;
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQueryFactory;
|
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.query.AccountingQueryTop;
|
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponse4JobTop;
|
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponse4PortletTop;
|
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponse4ServiceTop;
|
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponse4StorageTop;
|
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponse4TaskTop;
|
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponseBuilder;
|
|
import org.gcube.portlets.admin.accountingmanager.server.amservice.response.SeriesResponseDirector;
|
|
import org.gcube.portlets.admin.accountingmanager.shared.data.AccountingType;
|
|
import org.gcube.portlets.admin.accountingmanager.shared.data.response.SeriesResponse;
|
|
import org.gcube.portlets.admin.accountingmanager.shared.exception.ServiceException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
*
|
|
* @author Giancarlo Panichi
|
|
*
|
|
*
|
|
*/
|
|
public class AccountingCommandTop implements AccountingCommand<SeriesResponse> {
|
|
private static final Logger logger = LoggerFactory.getLogger(AccountingCommandTop.class);
|
|
|
|
private AccountingQueryTop accountingQueryTop;
|
|
private AccountingType accountingType;
|
|
|
|
public AccountingCommandTop(AccountingQueryTop accountingQueryTop, AccountingType accountingType) {
|
|
this.accountingQueryTop = accountingQueryTop;
|
|
this.accountingType = accountingType;
|
|
}
|
|
|
|
@Override
|
|
public SeriesResponse execute() throws ServiceException {
|
|
try {
|
|
|
|
// if (accountingQueryTop.getScope() != null &&
|
|
// !accountingQueryTop.getScope().isEmpty()) {
|
|
// AccountingPersistenceQueryFactory.getForcedQueryScope().set(accountingQueryTop.getScope());
|
|
// }
|
|
|
|
AccountingPersistenceQuery apq = AccountingPersistenceQueryFactory.getInstance();
|
|
|
|
logger.debug("Query TopValues: " + accountingQueryTop.getFilterKey().getKey());
|
|
|
|
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> topSM;
|
|
|
|
logger.debug("Execute Top()");
|
|
apq.setRequestedRecords(accountingQueryTop.getType());
|
|
|
|
HashSet<String> ctx = null;
|
|
if (accountingQueryTop.getContext() != null && accountingQueryTop.getContext().getContexts() != null
|
|
&& !accountingQueryTop.getContext().getContexts().isEmpty()) {
|
|
ctx = new HashSet<String>(accountingQueryTop.getContext().getContexts());
|
|
}
|
|
apq.setContexts(ctx);
|
|
apq.setTemporalConstraint(accountingQueryTop.getTemporalConstraint());
|
|
apq.setFilters(accountingQueryTop.getFilters());
|
|
|
|
topSM = apq.getTopValues(accountingQueryTop.getFilterKey().getKey(), null, true,
|
|
accountingQueryTop.getTopNumber());
|
|
|
|
// if (accountingQueryTop.getScope() != null &&
|
|
// !accountingQueryTop.getScope().isEmpty()) {
|
|
// AccountingPersistenceQueryFactory.getForcedQueryScope().remove();
|
|
// }
|
|
|
|
if (topSM == null) {
|
|
throw new ServiceException("Error retrieving info for top: sorted map is null!");
|
|
}
|
|
|
|
logger.debug("TopSM: " + topSM);
|
|
|
|
SeriesResponseBuilder seriesResponseBuilder = getSeriesResponseBuilder(accountingType, topSM);
|
|
|
|
SeriesResponseDirector seriesResponseDirector = new SeriesResponseDirector();
|
|
seriesResponseDirector.setSeriesResponseBuilder(seriesResponseBuilder);
|
|
seriesResponseDirector.constructSeriesResponse();
|
|
SeriesResponse seriesResponse = seriesResponseDirector.getSeriesResponse();
|
|
|
|
if (seriesResponse == null) {
|
|
throw new ServiceException("Error creating series response!");
|
|
}
|
|
logger.debug("SeriesResponse Created: " + seriesResponse);
|
|
return seriesResponse;
|
|
|
|
} catch (Throwable e) {
|
|
logger.error("Error in AccountingCommandTop(): " + e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new ServiceException("No data available!");
|
|
|
|
}
|
|
}
|
|
|
|
private SeriesResponseBuilder getSeriesResponseBuilder(AccountingType accountingType,
|
|
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> topSM) throws ServiceException {
|
|
if (accountingType == null) {
|
|
throw new ServiceException("Error accounting type is null");
|
|
}
|
|
|
|
switch (accountingType) {
|
|
case JOB:
|
|
return new SeriesResponse4JobTop(accountingQueryTop.getTopNumber(), topSM);
|
|
case PORTLET:
|
|
return new SeriesResponse4PortletTop(accountingQueryTop.getTopNumber(), topSM);
|
|
case SERVICE:
|
|
return new SeriesResponse4ServiceTop(accountingQueryTop.getTopNumber(), topSM);
|
|
case STORAGE:
|
|
return new SeriesResponse4StorageTop(accountingQueryTop.getTopNumber(), topSM);
|
|
case TASK:
|
|
return new SeriesResponse4TaskTop(accountingQueryTop.getTopNumber(), topSM);
|
|
default:
|
|
throw new ServiceException("Error request type is unknow!");
|
|
|
|
}
|
|
}
|
|
|
|
}
|