package metrics4Asterisk.webdemo;
import com.csvreader.CsvReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Map;
import java.util.Set;
import metrics4Asterisk.display.AgentSummary;
import metrics4Asterisk.metrics.AgentMetric;
import metrics4Asterisk.parse.AgentPerformanceMapper;
import metrics4Asterisk.parse.LogMapper;
import metrics4Asterisk.parse.LogParser;
import org.apache.log4j.Logger;
/**
*
* @author Lance Stine
* This class is used to parse the Asterisk log file, cache the results, and gather metrics.
*/
public class AgentMetricsManager {
private final Logger logger = Logger.getLogger(MetricsManager.class);
Calendar cfromDate;
Calendar ctoDate;
private AgentSummary agentSummary;
private Map<String, AgentMetric> agentMap;
private String fromDate = "";
private String toDate = "";
public void parseForAgentPerformance(InputStream ip, Set<String> agentNames) {
LogParser<LogMapper<AgentMetric>> agentParser = new LogParser<LogMapper<AgentMetric>>();
AgentPerformanceMapper agentPerformanceMapper = new AgentPerformanceMapper(agentNames);
agentParser.setLogMapper(agentPerformanceMapper);
BufferedReader in;
CsvReader reader;
InputStreamReader ins = new InputStreamReader(ip);
in = new BufferedReader(ins);
reader = new CsvReader(in, '|');
try {
agentParser.parse(cfromDate, ctoDate, reader);
} catch (Exception ex) {
logger.error("parse", ex);
} finally {
try {
reader.close();
try {
in.close();
} catch (IOException ex) {
logger.error(null, ex);
}
try {
ins.close();
} catch (IOException ex) {
logger.error(null, ex);
}
ip.close();
} catch (IOException ex) {
logger.error(null, ex);
}
}
this.agentMap = agentPerformanceMapper.getMap();
}
public Map<String, AgentMetric> getAgentMap() {
return agentMap;
}
public String getFromDate() {
return fromDate;
}
/**
* Setting the fromDate field will also set the encapsulted cFromDate Calendar field.
* If an exception occurs then the errrorMessage field will contain the exception's message.
* @param toDate
*/
public void setFromDate(String fromDate) throws Exception {
if (!this.fromDate.equals(fromDate)) {
try {
this.cfromDate = DateMaker.makeFromString(fromDate);
this.fromDate = fromDate;
} catch (Exception e) {
throw new Exception("Error setting date. Is " + fromDate + " in mm/dd/yyyy format? " + e.getMessage());
}
}
}
public String getToDate() {
return toDate;
}
/**
* Setting the toDate field will also set the encapsulted cToDate Calendar field.
* The cToDate field will be made excelusive by subtracting one second from it.
* If an exception occurs then the errrorMessage field will contain the exception's message.
* @param toDate
*/
public void setToDate(String toDate) throws Exception {
if (!this.toDate.equals(toDate)) {
try {
this.ctoDate = DateMaker.makeFromString(toDate);
this.toDate = toDate;
ctoDate.add(Calendar.SECOND, -1); //make end date exclusive
} catch (Exception e) {
throw new Exception("Error setting date. Is " + toDate + " in mm/dd/yyyy format? " + e.getMessage());
}
}
}
/**
* @return the agentSummary
*/
public AgentSummary getAgentSummary() {
return agentSummary;
}
/**
* @param agentSummary the agentSummary to set
*/
public void setAgentSummary(AgentSummary agentSummary) {
this.agentSummary = agentSummary;
}
}