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.QueueSummary;
import metrics4Asterisk.display.WeekDistribution;
import metrics4Asterisk.parse.AgentInQueuePerformanceMapper;
import metrics4Asterisk.metrics.AgentMetric;
import metrics4Asterisk.parse.CallMapper;
import metrics4Asterisk.metrics.CallMetric;
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 MetricsManager {
private final Logger logger = Logger.getLogger(MetricsManager.class);
Calendar cfromDate;
Calendar ctoDate;
private QueueSummary queueSummary;
private WeekDistribution weeks;
private Map<String, CallMetric> callMap;
private Map<String, AgentMetric> agentMap;
private String fromDate = "";
private String toDate = "";
public void parseForCalls(InputStream ip, Set<String> queues) {
//check the date range for validity and change
LogParser<LogMapper<CallMetric>> queueLogParser = new LogParser<LogMapper<CallMetric>>();
CallMapper mapperc = new CallMapper(queues);
queueLogParser.setLogMapper(mapperc);
BufferedReader in;
CsvReader reader;
InputStreamReader ins = new InputStreamReader(ip);
in = new BufferedReader(ins);
reader = new CsvReader(in, '|');
try {
try {
queueLogParser.parse(cfromDate, ctoDate, reader);
this.callMap = mapperc.getMap();
setQueueSummary(new QueueSummary());
getQueueSummary().makeSummary(queues, callMap);
setWeeks(new WeekDistribution());
getWeeks().init(cfromDate, ctoDate);
getWeeks().setQueueNames(queues);
getWeeks().makeSummary(callMap);
} 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);
}
}
}
public void parseForAgenInQueuetPerformance(InputStream ip, String queueName, Set<String> agentNames) {
//TODO hook this into the one queue mapper
LogParser<LogMapper<AgentMetric>> agentParser = new LogParser<LogMapper<AgentMetric>>();
AgentInQueuePerformanceMapper agentPerformanceMapper = new AgentInQueuePerformanceMapper(agentNames, queueName);
agentParser.setLogMapper(agentPerformanceMapper);
BufferedReader in;
CsvReader reader;
InputStreamReader ins = new InputStreamReader(ip);
in = new BufferedReader(ins);
reader = new CsvReader(in, '|');
try {
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);
}
}
LogMapper<AgentMetric> agentLoginMap = agentParser.getLogMapper();
this.agentMap = agentLoginMap.getMap();
}
public Map<String, CallMetric> getCallMap() {
return callMap;
}
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 queueSummary
*/
public QueueSummary getQueueSummary() {
return queueSummary;
}
/**
* @param queueSummary the queueSummary to set
*/
public void setQueueSummary(QueueSummary queueSummary) {
this.queueSummary = queueSummary;
}
/**
* @return the weeks
*/
public WeekDistribution getWeeks() {
return weeks;
}
/**
* @param weeks the weeks to set
*/
public void setWeeks(WeekDistribution weeks) {
this.weeks = weeks;
}
}