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-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-11 17:50:37 +02:00
|
|
|
|
2018-05-16 15:08:25 +02:00
|
|
|
import org.gcube.dataharvest.dao.DatabaseManager;
|
2018-05-11 17:50:37 +02:00
|
|
|
import org.gcube.dataharvest.datamodel.Harvest;
|
|
|
|
import org.gcube.dataharvest.harvester.DataMethodDownloadHarvester;
|
|
|
|
import org.gcube.dataharvest.harvester.MethodInvocationHarvester;
|
|
|
|
import org.gcube.dataharvest.harvester.ResourceCatalogueHarvester;
|
|
|
|
import org.gcube.dataharvest.harvester.SocialHarvester;
|
|
|
|
import org.gcube.dataharvest.harvester.VreUsersHarvester;
|
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-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
|
|
|
|
|
|
|
public class AccountingDataHarvesterPlugin extends Plugin<DataHarvestPluginDeclaration> {
|
2018-05-17 16:04:22 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterPlugin.class);
|
2018-05-16 15:08:25 +02:00
|
|
|
|
2018-05-17 12:52:17 +02:00
|
|
|
private static final String PROPERTY_FILENAME = "config.properties";
|
|
|
|
|
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";
|
|
|
|
public static final String DRY_RUN_INPUT_PARAMETER = "dryRun";
|
2018-05-16 15:08:25 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
protected Date start;
|
|
|
|
protected Date end;
|
|
|
|
|
|
|
|
private static final InheritableThreadLocal<Properties> properties = new InheritableThreadLocal<Properties>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Properties initialValue() {
|
|
|
|
return new Properties();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
public static InheritableThreadLocal<Properties> getProperties() {
|
|
|
|
return properties;
|
|
|
|
}
|
2018-05-17 12:52:17 +02:00
|
|
|
|
|
|
|
private void getConfigParameters() throws IOException {
|
2018-05-17 16:04:22 +02:00
|
|
|
try {
|
|
|
|
Properties properties = new Properties();
|
|
|
|
InputStream input = AccountingDataHarvesterPlugin.class.getClassLoader()
|
|
|
|
.getResourceAsStream(PROPERTY_FILENAME);
|
|
|
|
properties.load(input);
|
|
|
|
getProperties().set(properties);
|
|
|
|
} catch(Exception e) {
|
|
|
|
logger.warn(
|
|
|
|
"Unable to load {} file containing configuration properties. AccountingDataHarvesterPlugin will use defaults",
|
|
|
|
PROPERTY_FILENAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final InheritableThreadLocal<ContextAuthorization> contextAuthorization = new InheritableThreadLocal<ContextAuthorization>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ContextAuthorization initialValue() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
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-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-17 12:52:17 +02:00
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
MeasureType measureType = MeasureType.valueOf((String) inputs.get(MEASURE_TYPE_INPUT_PARAMETER));
|
2018-05-17 12:52:17 +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-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-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-17 16:04:22 +02:00
|
|
|
end = DateUtils.getEndDateFromStartDate(measureType, start, 1);
|
|
|
|
|
|
|
|
logger.debug("Harvesting from {} to {}", DateUtils.LAUNCH_DATE_FORMAT.format(start),
|
|
|
|
DateUtils.LAUNCH_DATE_FORMAT.format(end));
|
|
|
|
|
2018-05-17 17:21:08 +02:00
|
|
|
getConfigParameters();
|
|
|
|
|
|
|
|
ContextAuthorization contextAuthorization = new ContextAuthorization();
|
|
|
|
|
2018-05-17 16:04:22 +02:00
|
|
|
DatabaseManager dbaseManager = new DatabaseManager();
|
2018-05-11 17:50:37 +02:00
|
|
|
|
|
|
|
|
2018-05-17 17:21:08 +02:00
|
|
|
for() {
|
2018-05-11 17:50:37 +02:00
|
|
|
|
2018-05-17 17:21:08 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// collecting info on VRE users
|
|
|
|
VreUsersHarvester vreUsersHarvester = new VreUsersHarvester(start, end);
|
|
|
|
List<Harvest> users = vreUsersHarvester.getData();
|
|
|
|
if(!dryRun) {
|
|
|
|
dbaseManager.insertMonthlyData(start, end, users, reRun);
|
|
|
|
}
|
|
|
|
} catch(Exception x) {
|
|
|
|
logger.error(x.getLocalizedMessage());
|
2018-05-16 15:08:25 +02:00
|
|
|
}
|
2018-05-17 17:21:08 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// collecting info on Res. Catalogue (Dataset, Application, Deliverables, Methods)
|
|
|
|
ResourceCatalogueHarvester resourceCatalogueHarvester = new ResourceCatalogueHarvester(start, end);
|
|
|
|
List<Harvest> res = resourceCatalogueHarvester.getData();
|
|
|
|
if(!dryRun) {
|
|
|
|
dbaseManager.insertMonthlyData(start, end, res, reRun);
|
|
|
|
}
|
|
|
|
} catch(Exception x) {
|
|
|
|
logger.error(x.getLocalizedMessage());
|
2018-05-16 15:08:25 +02:00
|
|
|
}
|
2018-05-17 17:21:08 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// collecting info on Data/Method download
|
|
|
|
DataMethodDownloadHarvester dataMethodDownloadHarvester = new DataMethodDownloadHarvester(start, end);
|
|
|
|
List<Harvest> res = dataMethodDownloadHarvester.getData();
|
|
|
|
if(!dryRun) {
|
|
|
|
dbaseManager.insertMonthlyData(start, end, res, reRun);
|
|
|
|
}
|
|
|
|
} catch(Exception x) {
|
|
|
|
logger.error(x.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// collecting info on social (posts, replies and likes)
|
|
|
|
SocialHarvester socialHarvester = new SocialHarvester(start, end);
|
|
|
|
List<Harvest> res = socialHarvester.getData();
|
|
|
|
if(!dryRun) {
|
|
|
|
dbaseManager.insertMonthlyData(start, end, res, reRun);
|
|
|
|
}
|
|
|
|
} catch(Exception x) {
|
|
|
|
logger.error(x.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// collecting info on method invocation
|
|
|
|
MethodInvocationHarvester methodInvocationHarvester = new MethodInvocationHarvester(start, end);
|
|
|
|
List<Harvest> res = methodInvocationHarvester.getData();
|
|
|
|
if(!dryRun) {
|
|
|
|
dbaseManager.insertMonthlyData(start, end, res, reRun);
|
|
|
|
}
|
|
|
|
} catch(Exception x) {
|
|
|
|
logger.error(x.getLocalizedMessage());
|
2018-05-16 15:08:25 +02:00
|
|
|
}
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-17 16:04:22 +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
|
|
|
}
|
|
|
|
|
|
|
|
}
|