package metrics4Asterisk.metrics;
import metrics4Asterisk.parse.exception.NoQueueStartEventException;
import metrics4Asterisk.parse.exception.NoEndEventException;
import metrics4Asterisk.parse.exception.NoPauseStartEventException;
import java.text.DecimalFormat;
import metrics4Asterisk.util.Constants;
import org.apache.log4j.Logger;
/**
* This class represents an agent's performance.
* @author Lance Stine
*/
public class AgentMetric {
private static final Logger logger = Logger.getLogger(AgentMetric.class);
/**
* A field for the call count.
*/
private int callCount = 0;
/**
* A field for the total talk time for all calls. The value is in seconds.
*/
private int talkTime = 0;
/**
* A field for the maximum talk time for all calls. The value is in seconds.
*/
private int maxtalkTime = 0;
/**
* A field for the total wait time for all calls. The value is in seconds.
*/
private int waitTime = 0;
/**
* A field for the time logged into the queue. The value is in seconds.
*/
private int timeInQueue = 0;
/**
* A field for the time in pause while logged into the queue. The value is in seconds.
*/
private int timeInPause = 0;
/**
* A field for the time that was last logged into a queue. The value is in milliseconds.
*/
private long lastQueueAddTime = 0;
/**
* A field for the time that was last put in pause mode. The value is in milliseconds.
*/
private long lastQueuePauseTime = 0;
/**
* A field for this agent's extension.
*/
private String extension;
/**
* increment the call count
*/
public void addToCallCount() {
callCount++;
}
/**
* add the argument to the talk time
* @param talkTime
*/
public void addToTalkTime(int talkTime) {
changeMaxTalkTime(talkTime);
this.talkTime += talkTime;
}
/**
* Add the argument to the wait time field.
* @param waitTime
*/
public void addToWaitTime(int waitTime) {
this.waitTime += waitTime;
}
/**
* determine calls per hour
* @return 0 or callCount / timeInQueue converted to hours. The result is formated by Constants.PERCENT_FORMAT.
* @see metrics4Asterisk.util.Constants.PERCENT_FORMAT
*/
public String getCallsPerHour() {
if (timeInQueue == 0) {
return "0";
}
float callsPerHour = new Float(callCount).floatValue() / (new Float(timeInQueue).floatValue() / 3600000f);
return new DecimalFormat(Constants.PERCENT_FORMAT).format(callsPerHour);
}
/**
* This is used to allow queue login and pause times to be clipped to the initTime argument as a ending point. *
* @param initTime
* @throws NoEndEventException This will be thrown if the lastQueueAddTime is not 0.
*/
public void unInitTimes(long uninitTime, long origTime) throws NoEndEventException {
logger.debug("unInitTimes will stop at " + uninitTime + " if " + lastQueueAddTime + " != " + origTime);
if (lastQueueAddTime != 0L) {
throw new NoEndEventException();
}
}
/**
* Add (newTime - lastTime) arguments to the timeInQueue field.
* @param lastTime
* @param newTime
*/
private void addToQueueTime(long lastTime, long newTime) {
logger.debug("add queueTiem is " + timeInQueue);
timeInQueue += (newTime - lastTime);
logger.debug("queueTiem is " + timeInQueue);
}
/**
* Set the lastQueueAddTime field to the newTiem argument. This will also set lastQueuePauseTime to 0.
* @param newTime
*/
public void startQueueTime(long newTime) {
lastQueueAddTime = newTime;
logger.debug("starting queuetime: " + newTime);
lastQueuePauseTime = 0l;
}
/**
* Stop and total the timeInQueue field.
* @param newTime
* @throws NoQueueStartEventException This is thrown if the lastQueueAddTime is 0. This occurs when an ADDMEMBER event occurs before a REMOVEMEMBER event.
*/
public void stopQueueTime(long newTime) throws NoQueueStartEventException {
//prevent pause and logout events from totalling before a startQueuTime
long lastTime = lastQueueAddTime;
lastQueueAddTime = 0l;
if (lastTime != 0l) {
logger.debug("stopping queuetime: " + newTime);
addToQueueTime(lastTime, newTime);
//also try to add to pause time in case they were in paused before loggin out
stopPauseTimeWithQueue(newTime);
} else {
logger.debug("wrong call lastQueueAddTime=0 ");
throw new NoQueueStartEventException();
}
}
/**
* Add (newTime - lastTime) arguments to the timeInPause field.
* @param lastTime
* @param newTime
*/
private void addToPauseTime(long lastTime, long newTime) {
logger.debug("add PauseTime is " + timeInPause);
timeInPause += (newTime - lastTime);
logger.debug("PauseTime is " + timeInPause);
}
/**
* Set the lastQueuePauseTime to the newTime argument.
* @param newTime
*/
public void startPauseTime(long newTime) {
lastQueuePauseTime = newTime;
logger.debug("starting PauseTime: " + newTime);
}
/**
* Stop and total the time in pause.
* @param newTime
* @throws NoPauseStartEventException This is thrown if the lastQueuePauseTime is 0. This occurs when an UNPAUSE event occurs before a PAUSE event.
*/
public void stopPauseTime(long newTime) throws NoPauseStartEventException {
//prevent pause and logout events from totalling before a startQueuTime
long lastTime = lastQueuePauseTime;
lastQueuePauseTime = 0l;
if (lastTime != 0l) {
logger.debug("stopping PauseTime: " + newTime);
addToPauseTime(lastTime, newTime);
} else {
logger.debug("wrong call lastQueuePauseTime=0 ");
throw new NoPauseStartEventException();
}
}
/**
* Stop and total the time in pause but don't throw a NoPauseStartEventException if the lastQueuePauseTime is 0.
* This is called when by stopQueueTime to keep pause time totals correct if an agent leaves the queue while in pause.
* @param newTime
*/
private void stopPauseTimeWithQueue(long newTime) {
//prevent pause and logout events from totalling before a startQueuTime
long lastTime = lastQueuePauseTime;
lastQueuePauseTime = 0l;
if (lastTime != 0l) {
logger.debug("stopping PauseTime: " + newTime);
addToPauseTime(lastTime, newTime);
}
}
/**
*
* @return "0" or timeInPause / timeInQueue * 100
*/
public String getPercentPaused() {
if (timeInQueue == 0) {
return "0";
}
float percent = new Float(timeInPause).floatValue() / new Float(timeInQueue).floatValue() * 100f;
return new DecimalFormat(Constants.PERCENT_FORMAT).format(percent);
}
/**
*
* @return 0 or talkTime / callCount
*/
public int getAverageTalkTime() {
if (callCount == 0) {
return 0;
}
return talkTime / callCount;
}
/**
*
* @return 0 or waitTime / callCount
*/
public int getAverageWaitTime() {
if (callCount == 0) {
return 0;
}
return waitTime / callCount;
}
/**
* Change the maxtalkTime field if the talkTime argument is greater than it.
* @param talkTime
*/
public void changeMaxTalkTime(int talkTime) {
if (talkTime > this.maxtalkTime) {
this.maxtalkTime = talkTime;
}
}
/**
* @return the extension field's hash code
*/
@Override
public int hashCode() {
if (extension != null) {
return extension.hashCode();
}
return 0;
}
/**
* @param object
* @return true if the object is an instance of AgentMetric class and its extension field matches
*/
@Override
public boolean equals(Object object) {
if (!(object instanceof AgentMetric)) {
return false;
}
AgentMetric other = (AgentMetric) object;
return extension.hashCode() == other.extension.hashCode();
}
/**
* dump all of the fields to a string
* @return
*/
@Override
public String toString() {
StringBuilder buff = new StringBuilder();
buff.append("AgentMetric extension: ").append(extension).append("\n");
buff.append("AgentMetric getCallCount() ").append(getCallCount()).append("\n");
buff.append("AgentMetric getWaitTime() ").append(getWaitTime()).append("\n");
buff.append("AgentMetric getTalkTime() ").append(getTalkTime()).append("\n");
buff.append("AgentMetric averageWaitTime() ").append(getAverageWaitTime()).append("\n");
buff.append("AgentMetric averageTalkTime() ").append(getAverageTalkTime()).append("\n");
buff.append("AgentMetric getTimeInQueue() ").append(getTimeInQueue()).append("\n");
buff.append("AgentMetric getTimeInPause() ").append(getTimeInPause()).append("\n");
buff.append("AgentMetric percentPaused() ").append(getPercentPaused()).append("\n");
buff.append("AgentMetric callsPerHour() ").append(getCallsPerHour()).append("\n");
return buff.toString();
}
public int getCallCount() {
return callCount;
}
public void setCallCount(int callCount) {
this.callCount = callCount;
}
public int getTalkTime() {
return talkTime;
}
public void setTalkTime(int talkTime) {
this.talkTime = talkTime;
}
public int getWaitTime() {
return waitTime;
}
public void setWaitTime(int waitTime) {
this.waitTime = waitTime;
}
public int getMaxtalkTime() {
return maxtalkTime;
}
public void setMaxtalkTime(int maxtalkTime) {
this.maxtalkTime = maxtalkTime;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public int getTimeInQueue() {
return timeInQueue;
}
public void setTimeInQueue(int timeInQueue) {
this.timeInQueue = timeInQueue;
}
public int getTimeInPause() {
return timeInPause;
}
public void setTimeInPause(int timeInPause) {
this.timeInPause = timeInPause;
}
}