storage-manager-trigger/src/main/java/org/gcube/contentmanager/storageserver/accounting/ReportAccountingImpl.java

113 lines
4.3 KiB
Java

package org.gcube.contentmanager.storageserver.accounting;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.messaging.ResourceAccounting;
import org.gcube.accounting.messaging.ResourceAccountingFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ReportAccountingImpl implements Report {
final Logger logger = LoggerFactory.getLogger(ReportAccountingImpl.class);
public RawUsageRecord ssr;
public ResourceAccounting raFactory;
@Override
public void init(){
raFactory = null;
try {
raFactory = ResourceAccountingFactory.getResourceAccountingInstance();
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public RawUsageRecord setGenericProperties(String resourceType, String consumerId, String resourceScope, String creationTime, String lastAccess, String owner) {
logger.trace("set accounting generic properties: resourceType: "+resourceType+" consumerId "+consumerId+" scope: "+resourceScope+ " creationTime "+creationTime+" lastAccess "+lastAccess+" owner "+ owner);
if(raFactory==null) init();
RawUsageRecord sr = new RawUsageRecord();
sr.setResourceType(resourceType);
if(consumerId!=null) sr.setConsumerId(consumerId);
if(resourceScope !=null) sr.setResourceScope(resourceScope);
//set creation time
if(creationTime!=null){
SimpleDateFormat formatter = new SimpleDateFormat("dd MM yyyy 'at' hh:mm:ss z");
Date date=null;
try {
date = formatter.parse(creationTime);
} catch (ParseException e) {
logger.error("Error in parsing date: "+creationTime+" exc msg: "+e.getMessage());
}
sr.setCreateTime(date);
try {
date = new SimpleDateFormat("dd MM yyyy 'at' hh:mm:ss z").parse(lastAccess);
sr.setStartTime(date);
sr.setEndTime(date);
} catch (Exception e) {
e.printStackTrace();
}
//specific properties
if(owner != null) sr.setResourceOwner(owner);
// end mandatory files
}
logger.debug("generic fields completed ");
return sr;
}
@Override
public RawUsageRecord setSpecificProperties( RawUsageRecord sur, String operation, String size, String filePath, String callerIP, String dataType, String dataCount) {
logger.trace("set accounting properties: operation: "+operation+" size: "+size+ " remotePath: "+filePath+" callerIP "+callerIP+" dataType "+dataType+" dataCount "+dataCount);
if(sur==null) sur = new RawUsageRecord();
if (operation!=null) sur.setResourceSpecificProperty("operationType",operation);
if(size!= null) sur.setResourceSpecificProperty("dataVolume", size);
if(filePath != null) sur.setResourceSpecificProperty("remotePath", filePath);
if(callerIP!=null) sur.setResourceSpecificProperty("callerIP", callerIP);
sur.setResourceSpecificProperty("dataType",dataType);
sur.setResourceSpecificProperty("dataCount", dataCount);
return sur;
}
public void printRecord(RawUsageRecord record){
logger.info(" accounting properties: " +
"\n\t owner: "+record.getResourceOwner()+
"\n\t scope "+record.getResourceScope()+
"\n\t type "+record.getResourceType()+
"\n\t consumer "+record.getConsumerId()+
"\n\t startTime "+record.getStartTime()+
"\n\t endTime "+record.getEndTime()+
"\n\t file "+record.getResourceSpecificProperty("remotePath")+
"\n\t size "+record.getResourceSpecificProperty("dataVolume")+
"\n\t caller "+record.getResourceSpecificProperty("callerIP")+
"\n\t dataType "+record.getResourceSpecificProperty("dataType")+
"\n\t dataCount "+record.getResourceSpecificProperty("dataCount")+
"\n\t operation "+record.getResourceSpecificProperty("operationType"));
}
@Override
public void send(RawUsageRecord sur) {
try {
raFactory = ResourceAccountingFactory.getResourceAccountingInstance();
} catch (Exception e) {
e.printStackTrace();
}
if(raFactory!=null){
logger.debug("report sending...");
raFactory.sendAccountingMessage(sur);
logger.info(" report send: \n\t"+sur);
}else
logger.error("Problem on building accounting record: Factory Object is null ");
}
}