2018-05-18 18:33:28 +02:00
|
|
|
package org.gcube.dataharvest.harvester;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.SortedMap;
|
|
|
|
|
2018-09-04 11:13:16 +02:00
|
|
|
import org.gcube.accounting.accounting.summary.access.model.ScopeDescriptor;
|
|
|
|
import org.gcube.accounting.accounting.summary.access.model.internal.Dimension;
|
|
|
|
import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord;
|
2018-05-18 18:33:28 +02:00
|
|
|
import org.gcube.accounting.analytics.Filter;
|
|
|
|
import org.gcube.accounting.analytics.Info;
|
|
|
|
import org.gcube.accounting.analytics.TemporalConstraint;
|
|
|
|
import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode;
|
|
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
|
|
|
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQueryFactory;
|
|
|
|
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
|
2018-05-21 18:22:08 +02:00
|
|
|
import org.gcube.accounting.datamodel.aggregation.AggregatedJobUsageRecord;
|
2018-06-13 19:40:02 +02:00
|
|
|
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
|
|
|
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
2019-12-03 16:46:46 +01:00
|
|
|
import org.gcube.dataharvest.AccountingDashboardHarvesterPlugin;
|
2018-05-24 12:49:53 +02:00
|
|
|
import org.gcube.dataharvest.datamodel.HarvestedDataKey;
|
2018-05-18 18:33:28 +02:00
|
|
|
import org.gcube.dataharvest.utils.DateUtils;
|
|
|
|
import org.gcube.dataharvest.utils.Utils;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Eric Perrone (ISTI - CNR)
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class MethodInvocationHarvester extends BasicHarvester {
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(MethodInvocationHarvester.class);
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 19:12:46 +02:00
|
|
|
public static final String DATAMINER_SERVICE_NAME = "DataMiner";
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-09-04 11:13:16 +02:00
|
|
|
public MethodInvocationHarvester(Date start, Date end) throws Exception {
|
2018-05-18 18:33:28 +02:00
|
|
|
super(start, end);
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
@Override
|
2018-09-04 11:13:16 +02:00
|
|
|
public List<AccountingRecord> getAccountingRecords() throws Exception {
|
2018-05-18 18:33:28 +02:00
|
|
|
try {
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-09-04 11:13:16 +02:00
|
|
|
ArrayList<AccountingRecord> accountingRecords = new ArrayList<AccountingRecord>();
|
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
AccountingPersistenceQuery accountingPersistenceQuery = AccountingPersistenceQueryFactory.getInstance();
|
2018-05-28 15:22:59 +02:00
|
|
|
TemporalConstraint temporalConstraint = new TemporalConstraint(start.getTime(), end.getTime(),
|
2018-05-18 18:33:28 +02:00
|
|
|
AggregationMode.MONTHLY);
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
String context = Utils.getCurrentContext();
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
List<String> contexts = new ArrayList<>();
|
|
|
|
contexts.add(context);
|
2018-06-13 19:40:02 +02:00
|
|
|
|
|
|
|
SortedMap<Filter,SortedMap<Calendar,Info>> result = null;
|
|
|
|
List<Filter> filters = new ArrayList<>();
|
|
|
|
|
|
|
|
Date newMethodInvocationHarvesterStartDate = DateUtils.getStartCalendar(2017, Calendar.DECEMBER, 31).getTime();
|
|
|
|
|
|
|
|
if(start.after(newMethodInvocationHarvesterStartDate)) {
|
|
|
|
// From 01/01/2018 accounting Method Invocation using JobUsageRecord
|
|
|
|
result = accountingPersistenceQuery.getContextTimeSeries(
|
2018-05-21 18:22:08 +02:00
|
|
|
AggregatedJobUsageRecord.class, temporalConstraint, filters, contexts, true);
|
2018-06-13 19:40:02 +02:00
|
|
|
} else {
|
|
|
|
// Before 31/12/2017 accounting Method Invocation using ServiceUsageRecord
|
|
|
|
filters.add(new Filter(ServiceUsageRecord.SERVICE_NAME, DATAMINER_SERVICE_NAME));
|
|
|
|
result = accountingPersistenceQuery.getContextTimeSeries(
|
|
|
|
AggregatedServiceUsageRecord.class, temporalConstraint, filters, contexts, true);
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2019-12-03 16:46:46 +01:00
|
|
|
ScopeDescriptor scopeDescriptor = AccountingDashboardHarvesterPlugin.getScopeDescriptor();
|
2018-09-04 11:13:16 +02:00
|
|
|
Dimension dimension = getDimension(HarvestedDataKey.METHOD_INVOCATIONS);
|
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
if(result != null) {
|
|
|
|
for(Filter filter : result.keySet()) {
|
|
|
|
SortedMap<Calendar,Info> infoMap = result.get(filter);
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-28 15:22:59 +02:00
|
|
|
Calendar calendar = DateUtils.dateToCalendar(start);
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
Info info = infoMap.get(calendar);
|
2018-05-28 15:22:59 +02:00
|
|
|
logger.debug("{} : {}", DateUtils.format(calendar), info);
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
JSONObject jsonObject = info.getValue();
|
|
|
|
long numberOfInvocation = jsonObject.getLong(AggregatedUsageRecord.OPERATION_COUNT);
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-09-04 11:13:16 +02:00
|
|
|
AccountingRecord accountingRecord = new AccountingRecord(scopeDescriptor, instant, dimension, numberOfInvocation);
|
|
|
|
accountingRecords.add(accountingRecord);
|
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
} else {
|
|
|
|
logger.error("No data found.");
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-09-04 11:13:16 +02:00
|
|
|
return accountingRecords;
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
} catch(Exception e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
}
|