accounting-dashboard-harves.../src/main/java/org/gcube/dataharvest/harvester/sobigdata/DataMethodDownloadHarvester...

187 lines
6.1 KiB
Java

package org.gcube.dataharvest.harvester.sobigdata;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.SortedSet;
import org.apache.commons.lang.Validate;
import org.gcube.common.homelibrary.home.Home;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.HomeManager;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.accounting.AccountingEntry;
import org.gcube.common.homelibrary.jcr.repository.JCRRepository;
import org.gcube.common.homelibrary.jcr.workspace.JCRWorkspace;
import org.gcube.common.homelibrary.jcr.workspace.JCRWorkspaceItem;
import org.gcube.dataharvest.datamodel.HarvestedData;
import org.gcube.dataharvest.datamodel.HarvestedDataKey;
import org.gcube.dataharvest.utils.DateUtils;
import org.gcube.dataharvest.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class DataMethodDownloadHarvester.
*
* @author Eric Perrone (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
* @author Francesco Mangiacrapa (ISTI - CNR)
*/
public class DataMethodDownloadHarvester extends SoBigDataHarvester {
private static Logger logger = LoggerFactory.getLogger(DataMethodDownloadHarvester.class);
private int count = 0;
/**
* Instantiates a new data method download harvester.
*
* @param start the start
* @param end the end
* @param catalogueContext the catalogue context
* @param contexts the contexts
* @throws ParseException the parse exception
*/
public DataMethodDownloadHarvester(Date start, Date end, SortedSet<String> contexts) throws Exception {
super(start, end, contexts);
}
/* (non-Javadoc)
* @see org.gcube.dataharvest.harvester.BasicHarvester#getData()
*/
@Override
public List<HarvestedData> getData() throws Exception {
String defaultContext = Utils.getCurrentContext();
logger.debug("The context is: "+defaultContext);
try {
String vreName = getVRENameToHL(defaultContext);
logger.debug("Getting VRE Name to HL from context/scope returns: "+vreName);
String user = vreName + "-Manager";
logger.debug("Using user '"+user+"' to getHome from HL");
//ISTANCING HL AND GETTING HOME FOR VRE MANAGER
HomeManager manager = HomeLibrary.getHomeManagerFactory().getHomeManager();
@SuppressWarnings("deprecation")
Home home = manager.getHome(user);
JCRWorkspace ws = (JCRWorkspace) home.getWorkspace();
//
String path = "/Workspace/MySpecialFolders/" + vreName;
logger.debug("Getting item by Path: "+path);
JCRWorkspaceItem item = (JCRWorkspaceItem) ws.getItemByPath(path);
//
logger.info("Analyzing " + defaultContext + " in the period [" + startDate.toString() + " to " + endDate.toString() +"] starting from root: "+item.getName());
HarvestedData defaultHarvesteData = new HarvestedData(HarvestedDataKey.DATA_METHOD_DOWNLOAD, defaultContext, count);
List<HarvestedData> data = new ArrayList<HarvestedData>();
for (WorkspaceItem children: item.getChildren()) {
count = 0; //resettings the counter
HarvestedData harvestedData;
//Getting statistics for folder
if(children.isFolder()){
logger.info("Getting statistics for folder: "+children.getName());
getStats(children, startDate, endDate);
String normalizedName = children.getName().replaceAll("[^A-Za-z0-9]","");
String scope = mapWsFolderNameToVRE.get(normalizedName);
//Checking if it is a VRE name to right accouning...
if(scope!=null && !scope.isEmpty()){
logger.info("Found scope '" + scope + "' matching with normalized VRE name: "+normalizedName);
harvestedData = new HarvestedData(HarvestedDataKey.DATA_METHOD_DOWNLOAD, scope, count);
data.add(harvestedData);
logger.info("Added data: "+harvestedData);
}else{
logger.info("No scope found matching the folder name: "+normalizedName +", accounting its stats in the default context: "+defaultContext);
//INCREASING THE DEFAULT CONTEXT COUNTER...
defaultHarvesteData.setMeasure(defaultHarvesteData.getMeasure()+count);
logger.info("Increased default context stats: "+defaultHarvesteData);
}
}
}
//ADDING DEFAULT ACCOUNTING
data.add(defaultHarvesteData);
logger.info("In the period [from {} to {} ] returning workspace accouting data {}", DateUtils.LAUNCH_DATE_FORMAT.format(startDate), DateUtils.LAUNCH_DATE_FORMAT.format(endDate), data);
return data;
} catch(Exception e) {
throw e;
}
}
/**
* Gets the stats.
*
* @param baseItem the base item
* @param start the start
* @param end the end
* @return the stats
* @throws InternalErrorException the internal error exception
*/
private void getStats(WorkspaceItem baseItem, Date start, Date end) throws InternalErrorException {
List<? extends WorkspaceItem> children;
if(baseItem.isFolder()) {
children = baseItem.getChildren();
for(WorkspaceItem child : children)
getStats(child, start, end);
} else {
try {
List<AccountingEntry> accounting = baseItem.getAccounting();
for(AccountingEntry entry : accounting) {
switch(entry.getEntryType()) {
case CREATE:
case UPDATE:
case READ:
Calendar calendar = entry.getDate();
if(calendar.after(DateUtils.dateToCalendar(start)) && calendar.before(DateUtils.dateToCalendar(end))) {
count++;
}
break;
default:
break;
}
}
} catch(Exception e) {
logger.error("DataMethodDownloadHarvester: " + e.getLocalizedMessage());
throw new InternalErrorException(e.getLocalizedMessage());
}
}
}
/**
* Gets the VRE name to HL.
*
* @param vre the vre
* @return the VRE name to HL
*/
private static String getVRENameToHL(String vre) {
Validate.notNull(vre, "scope must be not null");
String newName;
if(vre.startsWith(JCRRepository.PATH_SEPARATOR))
newName = vre.replace(JCRRepository.PATH_SEPARATOR, "-").substring(1);
else
newName = vre.replace(JCRRepository.PATH_SEPARATOR, "-");
return newName;
}
}