accounting-dashboard-harves.../src/main/java/org/gcube/dataharvest/AccountingDataHarvesterPlug...

176 lines
5.6 KiB
Java
Raw Normal View History

package org.gcube.dataharvest;
import java.util.Date;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import org.gcube.dataharvest.DataHarvestPluginDeclaration;
import org.gcube.vremanagement.executor.exception.InputsNullException;
import org.gcube.vremanagement.executor.exception.InvalidInputsException;
import org.gcube.vremanagement.executor.plugin.Plugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.gcube.dataharvest.dao.*;
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;
public class AccountingDataHarvesterPlugin extends Plugin<DataHarvestPluginDeclaration> {
private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterPlugin.class);
public static final String PARAMETER_FROM = "from";
public static final String PARAMETER_TO = "to";
public static final String TEST = "test";
private boolean testMode = false, updateFlag = false;
private Date dateFrom, dateTo;
public AccountingDataHarvesterPlugin(DataHarvestPluginDeclaration pluginDeclaration) {
super(pluginDeclaration);
logger.debug("DataHarvestPlugin: constructor");
}
/** {@inheritDoc} */
@Override
public void launch(Map<String, Object> inputs) throws Exception {
logger.debug("DataHarvestPlugin: launch()");
String message = "";
DatabaseManager dbaseManager = new DatabaseManager(testMode);
if (inputs == null) {
message = "Invalid input parameter (null)";
logger.error(message);
throw new Exception(message, null);
}
Object from = inputs.get(PARAMETER_FROM);
if (!checkDate(from)) {
message = "Missing or invalid input parameter: " + PARAMETER_FROM;
logger.error(message);
throw new Exception(message, null);
}
dateFrom = (Date)from;
Object to = inputs.get(PARAMETER_TO);
if (!checkDate(to)) {
message = "Missing or invalid input parameter: " + PARAMETER_TO;
logger.error(message);
throw new Exception(message, null);
}
dateTo = (Date)to;
testMode = (inputs.get(TEST) == null) ? false : true;
logger.debug("TEST mode is " + testMode);
setTimePeriod();
logger.debug("Time period: " + dateFrom.toString() + " -> " + dateTo.toString());
try {
// collecting info on VRE users
VreUsersHarvester vreUsersHarvester = new VreUsersHarvester(dateFrom, dateTo);
List<Harvest> users = vreUsersHarvester.getData();
dbaseManager.insertMonthlyData((Date) dateFrom, (Date) dateTo, users, updateFlag);
} catch (Exception x) {
logger.error(x.getLocalizedMessage());
}
try {
// collecting info on Res. Catalogue (Dataset, Application, Deliverables, Methods)
ResourceCatalogueHarvester resourceCatalogueHarvester = new ResourceCatalogueHarvester(dateFrom, dateTo);
List<Harvest> res = resourceCatalogueHarvester.getData();
dbaseManager.insertMonthlyData((Date) dateFrom, (Date) dateTo, res, updateFlag);
} catch (Exception x) {
logger.error(x.getLocalizedMessage());
}
try {
// collecting info on Data/Method download
DataMethodDownloadHarvester dataMethodDownloadHarvester = new DataMethodDownloadHarvester(dateFrom, dateTo);
List<Harvest> res = dataMethodDownloadHarvester.getData();
dbaseManager.insertMonthlyData((Date) dateFrom, (Date) dateTo, res, updateFlag);
} catch (Exception x) {
logger.error(x.getLocalizedMessage());
}
try {
// collecting info on social (posts, replies and likes)
SocialHarvester socialHarvester = new SocialHarvester(dateFrom, dateTo);
List<Harvest> res = socialHarvester.getData();
dbaseManager.insertMonthlyData((Date) dateFrom, (Date) dateTo, res, updateFlag);
} catch (Exception x) {
logger.error(x.getLocalizedMessage());
}
try {
// collecting info on method invocation
MethodInvocationHarvester methodInvocationHarvester = new MethodInvocationHarvester(dateFrom, dateTo);
List<Harvest> res = methodInvocationHarvester.getData();
//insertMonthlyData((Date) dateFrom, (Date) dateTo, res);
} catch (Exception x) {
logger.error(x.getLocalizedMessage());
}
}
/** {@inheritDoc} */
@Override
protected void onStop() throws Exception {
logger.debug("DataHarvestPlugin: onStop()");
}
private boolean checkDate(Object date) {
if (date == null)
return false;
return date instanceof java.util.Date;
}
// private void insertMonthlyData(Date from, Date to, List<Harvest> data) {
// Dao dao = null;
// try {
// dao = dbConnect();
// dao.insertMonthlyMeasure(data, from, to, false);
// } catch (Exception e) {
// logger.error(e.getLocalizedMessage());
// } finally {
// if (dao != null) {
// try {
// dao.disconnect();
// } catch (DaoException e) {
// logger.error(e.getLocalizedMessage());
// }
// }
// }
// }
//
// private Dao dbConnect() throws DaoException {
// DatabaseDataExplorer dde = new DatabaseDataExplorer();
// dde.setTestMode(testMode);
// DatabaseConnectionData dcd = dde.retrieveDatabaseInfo();
// Dao dao = new Dao();
// dao.init();
// dao.connect(dcd.getUrl(), dcd.getUser(), dcd.getPassword());
// return dao;
// }
private void setTimePeriod() {
Calendar from = Calendar.getInstance();
from.setTime(dateFrom);
from.set(Calendar.HOUR_OF_DAY, 0);
from.set(Calendar.MINUTE, 0);
from.set(Calendar.SECOND, 0);
dateFrom = from.getTime();
Calendar to = Calendar.getInstance();
to.setTime(dateTo);
to.set(Calendar.HOUR_OF_DAY, 23);
to.set(Calendar.MINUTE, 59);
to.set(Calendar.SECOND, 59);
dateTo = to.getTime();
}
}