2018-05-11 17:50:37 +02:00
|
|
|
package org.gcube.dataharvest;
|
|
|
|
|
2018-05-17 12:52:17 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2018-05-18 12:00:10 +02:00
|
|
|
import java.util.ArrayList;
|
2018-05-16 15:08:25 +02:00
|
|
|
import java.util.Date;
|
2018-05-11 17:50:37 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2018-05-17 12:52:17 +02:00
|
|
|
import java.util.Properties;
|
2018-05-18 12:00:10 +02:00
|
|
|
import java.util.SortedSet;
|
2018-05-11 17:50:37 +02:00
|
|
|
|
2018-05-29 11:08:24 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2018-06-13 19:39:01 +02:00
|
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
|
|
|
import org.gcube.common.scope.impl.ScopeBean.Type;
|
2018-05-16 15:08:25 +02:00
|
|
|
import org.gcube.dataharvest.dao.DatabaseManager;
|
2018-05-18 12:00:35 +02:00
|
|
|
import org.gcube.dataharvest.datamodel.HarvestedData;
|
2018-05-18 18:33:28 +02:00
|
|
|
import org.gcube.dataharvest.harvester.MethodInvocationHarvester;
|
2018-05-21 12:18:06 +02:00
|
|
|
import org.gcube.dataharvest.harvester.SocialInteractionsHarvester;
|
2018-05-18 14:24:27 +02:00
|
|
|
import org.gcube.dataharvest.harvester.VREUsersHarvester;
|
2018-05-18 12:00:10 +02:00
|
|
|
import org.gcube.dataharvest.harvester.sobigdata.DataMethodDownloadHarvester;
|
|
|
|
import org.gcube.dataharvest.harvester.sobigdata.ResourceCatalogueHarvester;
|
2018-05-21 14:49:01 +02:00
|
|
|
import org.gcube.dataharvest.harvester.sobigdata.TagMeMethodInvocationHarvester;
|
2018-05-17 12:52:17 +02:00
|
|
|
import org.gcube.dataharvest.utils.ContextAuthorization;
|
2018-05-17 16:04:22 +02:00
|
|
|
import org.gcube.dataharvest.utils.DateUtils;
|
|
|
|
import org.gcube.dataharvest.utils.MeasureType;
|
2018-05-18 12:00:10 +02:00
|
|
|
import org.gcube.dataharvest.utils.Utils;
|
2018-05-16 15:08:25 +02:00
|
|
|
import org.gcube.vremanagement.executor.plugin.Plugin;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-05-11 17:50:37 +02:00
|
|
|
|
2018-05-18 14:17:54 +02:00
|
|
|
/**
|
|
|
|
* @author Eric Perrone (ISTI - CNR)
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2018-05-11 17:50:37 +02:00
|
|
|
public class AccountingDataHarvesterPlugin extends Plugin<DataHarvestPluginDeclaration> {
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterPlugin.class);
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 12:52:17 +02:00
|
|
|
private static final String PROPERTY_FILENAME = "config.properties";
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
public static final String START_DATE_INPUT_PARAMETER = "startDate";
|
|
|
|
public static final String MEASURE_TYPE_INPUT_PARAMETER = "measureType";
|
|
|
|
public static final String RERUN_INPUT_PARAMETER = "reRun";
|
2018-06-04 16:35:32 +02:00
|
|
|
public static final String GET_VRE_USERS_INPUT_PARAMETER = "getVREUsers";
|
2018-05-17 16:04:22 +02:00
|
|
|
public static final String DRY_RUN_INPUT_PARAMETER = "dryRun";
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-28 12:00:15 +02:00
|
|
|
public static final String SO_BIG_DATA_CATALOGUE_CONTEXT = "/d4science.research-infrastructures.eu/SoBigData/ResourceCatalogue";
|
2018-05-18 16:36:05 +02:00
|
|
|
public static final String TAGME_CONTEXT = "/d4science.research-infrastructures.eu/SoBigData/TagMe";
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
protected Date start;
|
|
|
|
protected Date end;
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 12:00:10 +02:00
|
|
|
public AccountingDataHarvesterPlugin(DataHarvestPluginDeclaration pluginDeclaration) {
|
|
|
|
super(pluginDeclaration);
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
private static final InheritableThreadLocal<Properties> properties = new InheritableThreadLocal<Properties>() {
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
@Override
|
|
|
|
protected Properties initialValue() {
|
|
|
|
return new Properties();
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
};
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
public static InheritableThreadLocal<Properties> getProperties() {
|
|
|
|
return properties;
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-29 13:05:55 +02:00
|
|
|
public Properties getConfigParameters() throws IOException {
|
|
|
|
Properties properties = new Properties();
|
2018-05-17 16:04:22 +02:00
|
|
|
try {
|
|
|
|
InputStream input = AccountingDataHarvesterPlugin.class.getClassLoader()
|
|
|
|
.getResourceAsStream(PROPERTY_FILENAME);
|
|
|
|
properties.load(input);
|
2018-05-29 13:05:55 +02:00
|
|
|
return properties;
|
2018-05-17 16:04:22 +02:00
|
|
|
} catch(Exception e) {
|
|
|
|
logger.warn(
|
|
|
|
"Unable to load {} file containing configuration properties. AccountingDataHarvesterPlugin will use defaults",
|
|
|
|
PROPERTY_FILENAME);
|
|
|
|
}
|
2018-05-29 13:05:55 +02:00
|
|
|
return properties;
|
2018-05-17 16:04:22 +02:00
|
|
|
}
|
2018-06-13 14:43:36 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
/** {@inheritDoc} */
|
|
|
|
@Override
|
2018-05-17 16:04:22 +02:00
|
|
|
public void launch(Map<String,Object> inputs) throws Exception {
|
|
|
|
logger.debug("{} is starting", this.getClass().getSimpleName());
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
if(inputs == null || inputs.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("The can only be launched providing valid input parameters");
|
2018-05-17 12:52:17 +02:00
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
if(!inputs.containsKey(MEASURE_TYPE_INPUT_PARAMETER)) {
|
|
|
|
throw new IllegalArgumentException("Please set required parameter '" + MEASURE_TYPE_INPUT_PARAMETER + "'");
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
MeasureType measureType = MeasureType.valueOf((String) inputs.get(MEASURE_TYPE_INPUT_PARAMETER));
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
boolean reRun = true;
|
|
|
|
if(inputs.containsKey(RERUN_INPUT_PARAMETER)) {
|
|
|
|
try {
|
|
|
|
reRun = (boolean) inputs.get(RERUN_INPUT_PARAMETER);
|
|
|
|
}catch (Exception e) {
|
|
|
|
throw new IllegalArgumentException("'" + RERUN_INPUT_PARAMETER + "' must be a boolean");
|
|
|
|
}
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-06-04 16:35:32 +02:00
|
|
|
boolean getVREUsers = true;
|
|
|
|
if(inputs.containsKey(GET_VRE_USERS_INPUT_PARAMETER)) {
|
|
|
|
try {
|
|
|
|
reRun = (boolean) inputs.get(GET_VRE_USERS_INPUT_PARAMETER);
|
|
|
|
}catch (Exception e) {
|
|
|
|
throw new IllegalArgumentException("'" + GET_VRE_USERS_INPUT_PARAMETER + "' must be a boolean");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
boolean dryRun = true;
|
|
|
|
if(inputs.containsKey(DRY_RUN_INPUT_PARAMETER)) {
|
|
|
|
try {
|
|
|
|
dryRun = (boolean) inputs.get(DRY_RUN_INPUT_PARAMETER);
|
|
|
|
}catch (Exception e) {
|
|
|
|
throw new IllegalArgumentException("'" + DRY_RUN_INPUT_PARAMETER + "' must be a boolean");
|
|
|
|
}
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
if(inputs.containsKey(START_DATE_INPUT_PARAMETER)) {
|
|
|
|
String startDateString = (String) inputs.get(START_DATE_INPUT_PARAMETER);
|
|
|
|
start = DateUtils.UTC_DATE_FORMAT.parse(startDateString + " " + DateUtils.UTC);
|
|
|
|
} else {
|
|
|
|
start = DateUtils.getPreviousPeriod(measureType).getTime();
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-28 15:22:59 +02:00
|
|
|
logger.debug("Harvesting from {} to {}", DateUtils.format(start),
|
|
|
|
DateUtils.format(end));
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-29 13:05:55 +02:00
|
|
|
Properties properties = getConfigParameters();
|
|
|
|
getProperties().set(properties);
|
|
|
|
|
2018-06-13 19:39:01 +02:00
|
|
|
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
DatabaseManager dbaseManager = new DatabaseManager();
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 12:00:10 +02:00
|
|
|
SortedSet<String> contexts = contextAuthorization.getContexts();
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 12:00:35 +02:00
|
|
|
ArrayList<HarvestedData> data = new ArrayList<HarvestedData>();
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-29 11:08:24 +02:00
|
|
|
String initialToken = SecurityTokenProvider.instance.get();
|
2018-06-13 19:39:01 +02:00
|
|
|
|
|
|
|
// GAnalytics gAnalytics = null
|
|
|
|
Object gAnalytics = null;
|
2018-05-29 11:08:24 +02:00
|
|
|
|
2018-05-18 12:00:10 +02:00
|
|
|
for(String context : contexts) {
|
|
|
|
// Setting the token for the context
|
|
|
|
Utils.setContext(contextAuthorization.getTokenForContext(context));
|
2018-06-13 19:39:01 +02:00
|
|
|
|
|
|
|
ScopeBean scopeBean = new ScopeBean(context);
|
|
|
|
|
|
|
|
if(gAnalytics == null) {
|
|
|
|
|
|
|
|
if(scopeBean.is(Type.INFRASTRUCTURE)) {
|
|
|
|
// gAnalytics = new GAnalytics();
|
|
|
|
}else {
|
|
|
|
// This code should be never used because the scopes are sorted by fullname
|
|
|
|
|
|
|
|
ScopeBean parent = scopeBean.enclosingScope();
|
|
|
|
while(!parent.is(Type.INFRASTRUCTURE)) {
|
|
|
|
parent = scopeBean.enclosingScope();
|
|
|
|
|
|
|
|
}
|
|
|
|
// Setting back token for the context
|
|
|
|
Utils.setContext(contextAuthorization.getTokenForContext(parent.toString()));
|
|
|
|
|
|
|
|
// gAnalytics = new GAnalytics();
|
|
|
|
|
|
|
|
// Setting back token for the context
|
|
|
|
Utils.setContext(contextAuthorization.getTokenForContext(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// Collecting Google Analytics Data for VREs Accesses
|
|
|
|
// List<HarvestedData> harvested = gAnalytics.getData();
|
|
|
|
// data.addAll(harvested);
|
|
|
|
} catch(Exception e) {
|
|
|
|
logger.error("Error harvesting Social Interactions for {}", context, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-17 17:21:08 +02:00
|
|
|
try {
|
2018-05-18 14:23:57 +02:00
|
|
|
// Collecting info on social (posts, replies and likes)
|
2018-05-21 12:18:06 +02:00
|
|
|
SocialInteractionsHarvester socialHarvester = new SocialInteractionsHarvester(start, end);
|
2018-05-18 14:23:57 +02:00
|
|
|
List<HarvestedData> harvested = socialHarvester.getData();
|
|
|
|
data.addAll(harvested);
|
|
|
|
} catch(Exception e) {
|
2018-05-21 12:18:06 +02:00
|
|
|
logger.error("Error harvesting Social Interactions for {}", context, e);
|
2018-05-18 14:23:57 +02:00
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 14:23:57 +02:00
|
|
|
try {
|
|
|
|
// Collecting info on VRE users
|
2018-06-04 16:35:32 +02:00
|
|
|
if(getVREUsers) {
|
2018-06-13 19:39:01 +02:00
|
|
|
// Harvesting Users only for VREs (not for VO and ROOT which is the sum of the children contexts)
|
|
|
|
if(scopeBean.is(Type.VRE)) {
|
|
|
|
VREUsersHarvester vreUsersHarvester = new VREUsersHarvester(start, end);
|
|
|
|
List<HarvestedData> harvested = vreUsersHarvester.getData();
|
|
|
|
data.addAll(harvested);
|
|
|
|
}
|
2018-05-29 11:08:24 +02:00
|
|
|
}
|
2018-05-18 14:23:57 +02:00
|
|
|
} catch(Exception e) {
|
|
|
|
logger.error("Error harvesting Context Users for {}", context, e);
|
2018-05-16 15:08:25 +02:00
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
|
|
|
|
2018-05-28 12:00:15 +02:00
|
|
|
if(context.startsWith(SO_BIG_DATA_CATALOGUE_CONTEXT)) {
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-21 12:18:06 +02:00
|
|
|
try {
|
|
|
|
// Collecting info on Resource Catalogue (Dataset, Application, Deliverables, Methods)
|
2018-05-28 12:00:15 +02:00
|
|
|
ResourceCatalogueHarvester resourceCatalogueHarvester = new ResourceCatalogueHarvester(start, end, contexts);
|
2018-05-21 12:18:06 +02:00
|
|
|
List<HarvestedData> harvested = resourceCatalogueHarvester.getData();
|
|
|
|
data.addAll(harvested);
|
|
|
|
} catch(Exception e) {
|
|
|
|
logger.error("Error harvesting Resource Catalogue Information for {}", context, e);
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 16:36:05 +02:00
|
|
|
try {
|
|
|
|
// Collecting info on Data/Method download
|
2018-05-28 12:00:15 +02:00
|
|
|
DataMethodDownloadHarvester dataMethodDownloadHarvester = new DataMethodDownloadHarvester(start, end, contexts);
|
2018-05-18 16:36:05 +02:00
|
|
|
List<HarvestedData> harvested = dataMethodDownloadHarvester.getData();
|
|
|
|
data.addAll(harvested);
|
|
|
|
} catch(Exception e) {
|
|
|
|
logger.error("Error harvesting Data Method Download for {}", context, e);
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
if(context.startsWith(TAGME_CONTEXT)) {
|
|
|
|
try {
|
|
|
|
// Collecting info on method invocation
|
|
|
|
TagMeMethodInvocationHarvester tagMeMethodInvocationHarvester = new TagMeMethodInvocationHarvester(start, end);
|
|
|
|
List<HarvestedData> harvested = tagMeMethodInvocationHarvester.getData();
|
|
|
|
data.addAll(harvested);
|
|
|
|
} catch(Exception e) {
|
|
|
|
logger.error("Error harvesting Method Invocations for {}", context, e);
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
try {
|
|
|
|
// Collecting info on method invocation
|
|
|
|
MethodInvocationHarvester methodInvocationHarvester = new MethodInvocationHarvester(start, end);
|
|
|
|
List<HarvestedData> harvested = methodInvocationHarvester.getData();
|
|
|
|
data.addAll(harvested);
|
|
|
|
} catch(Exception e) {
|
|
|
|
logger.error("Error harvesting Method Invocations for {}", context, e);
|
2018-05-18 16:36:05 +02:00
|
|
|
}
|
2018-05-16 15:08:25 +02:00
|
|
|
}
|
2018-06-04 16:35:32 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|
2018-05-29 11:08:24 +02:00
|
|
|
|
|
|
|
Utils.setContext(initialToken);
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-18 12:00:10 +02:00
|
|
|
if(!dryRun) {
|
|
|
|
dbaseManager.insertMonthlyData(start, end, data, reRun);
|
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
/** {@inheritDoc} */
|
|
|
|
@Override
|
|
|
|
protected void onStop() throws Exception {
|
2018-05-17 16:04:22 +02:00
|
|
|
logger.debug("{} is stopping", this.getClass().getSimpleName());
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|
2018-05-24 12:49:53 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|