def agent_unregistration(request):
    """ Unregisters an active agent for the call center. """
    log.info("agent_unregistration. retrieved agent ANI:" + request.POST['agentANI'])
    agentGateway = AgentGateway()
    response = agentGateway.unregisterAgentForServiceToday(request.POST['agentANI'])
    if response:
        return HttpResponse('unregistration.successful')    
    else:
        return HttpResponse('unregistration.unsuccessful')
class CallGatewayTest(unittest.TestCase):
    """ Provides function coverage for CallGateway. """

    def setUp(self):
        self.AgentGateway = AgentGateway()
        self.CallGateway = CallGateway()
        self.agentToInsert = None
        self.ANI = "212555555" + str(random.randrange(9))
        self.createdCall = None
        self.activeCalls = []

    def tearDown(self):
        if self.agentToInsert:
            self.agentToInsert.delete()
        if self.createdCall:
            self.createdCall.delete()
        if len(self.activeCalls) > 0:
            for call in self.activeCalls:
                call.delete()

    def test_addCall(self):
        # calls need an agent
        self.agentToInsert = self.AgentGateway.registerAgentForServiceToday(self.ANI)

        self.createdCall = self.CallGateway.addCall(self.agentToInsert)

        self.assertEqual(self.createdCall.agent.ani, self.agentToInsert.ani)
        self.assertNotEqual(self.createdCall.call_start, None)

    def test_getLastCallForAgent(self):
        # create some calls for an agent
        self.agentToInsert = self.AgentGateway.registerAgentForServiceToday(self.ANI)
        now = datetime.datetime.now()
        self.activeCalls.append(
            self.CallGateway.addCall(self.agentToInsert, callEnd=(now + datetime.timedelta(seconds=50)))
        )  # [0]
        self.activeCalls.append(
            self.CallGateway.addCall(self.agentToInsert, callEnd=(now + datetime.timedelta(seconds=60)))
        )  # [1]

        lastCall = self.CallGateway.getLastCallForAgent(self.agentToInsert)

        # should return [1]
        self.assertEqual(lastCall.call_id, self.activeCalls[1].call_id)

    def test_endCall(self):
        self.agentToInsert = self.AgentGateway.registerAgentForServiceToday(self.ANI)
        self.createdCall = self.CallGateway.addCall(self.agentToInsert)

        self.assertEqual(self.createdCall.call_end, None)

        # end call
        endedCall = self.CallGateway.endCall(self.createdCall.call_id)

        self.assertEqual(self.createdCall.call_id, endedCall.call_id)
        self.assertNotEqual(endedCall.call_end, None)
 def setUp(self):
     self.AgentGateway = AgentGateway()
     self.CallGateway = CallGateway()
     self.agentToInsert = None
     self.ANI = "212555555" + str(random.randrange(9))
     self.createdCall = None
     self.activeCalls = []
 def __init__(self):
     from cc_vector_manager.routing_algorithms import UCD_MIA_Alogrithm
     from cc_vector_webservices.services.gateway import AgentGateway, CallGateway
     self.algo = UCD_MIA_Alogrithm()
     #create gateway instances     
     self.agentGateway = AgentGateway()
     self.callGateway = CallGateway()
 def setUp(self):
     self.ANI = "212555555" + str(random.randrange(9))
     self.AgentGateway = AgentGateway()
     self.CallGateway = CallGateway()
     self.today = datetime.datetime.today()
     self.agentToInsert = None
     self.activeAgents = []
     self.activeCalls = []
def agent_registration(request):
    """ Registers an active agent for the call center. """
    try:
        log.info("retrieved agent ANI:" + request.POST['agentANI'])
        assert(request.POST['agentANI'])        
        agentGateway = AgentGateway()
        #check if agent is already active
        if(agentGateway.isANICurrentlyActive(request.POST['agentANI'])):
            #agent already active
            log.info("agent_registration: agent already active ani: " + request.POST['agentANI'])
            return HttpResponse('registration.agent_already_active')
        else:
            #INSERT INTO DB
            log.info("about to call registerAgentForServiceToday()")
            agentGateway.registerAgentForServiceToday(request.POST['agentANI'])
            log.info("registered agent")      
            return HttpResponse('registration.successful')                
    except Exception as e:
        log.error("Module: views.agent_registration. Error: " + e)
        return HttpResponse('registration.unsuccessful')
class AgentGatewayTest(unittest.TestCase):
    """ Provides function coverage for AgentGateway. """

    def setUp(self):
        self.ANI = "212555555" + str(random.randrange(9))
        self.AgentGateway = AgentGateway()
        self.CallGateway = CallGateway()
        self.today = datetime.datetime.today()
        self.agentToInsert = None
        self.activeAgents = []
        self.activeCalls = []

    def tearDown(self):
        if self.agentToInsert:
            self.agentToInsert.delete()
        if len(self.activeAgents) > 0:
            for agent in self.activeAgents:
                agent.delete()
        if len(self.activeCalls) > 0:
            for call in self.activeCalls:
                call.delete()

    def test_registerAgent(self):
        self.agentToInsert = self.AgentGateway.registerAgentForServiceToday(self.ANI)

        # verify agent was inserted

        insertedAgent = Agents.objects.get(ani=self.ANI)

        self.assertTrue(insertedAgent)
        self.assertEqual(insertedAgent.ani, self.agentToInsert.ani)
        self.assertEqual(insertedAgent.connection_status, 1)
        self.assertEqual(insertedAgent.phone_status, 0)
        self.assertEqual(insertedAgent.added, self.agentToInsert.added)

    def test_getAvailableAgentsByIdleTime(self):
        # insert a bunch of agents
        self.activeAgents.append(
            self.AgentGateway.registerAgentForServiceToday("2125555560", connection=1, phone=0)
        )  # [0]
        self.activeAgents.append(
            self.AgentGateway.registerAgentForServiceToday("2125555561", connection=0, phone=0)
        )  # [1]
        self.activeAgents.append(
            self.AgentGateway.registerAgentForServiceToday("2125555562", connection=1, phone=1)
        )  # [2]
        self.activeAgents.append(
            self.AgentGateway.registerAgentForServiceToday("2125555563", connection=1, phone=1)
        )  # [3]
        self.activeAgents.append(
            self.AgentGateway.registerAgentForServiceToday("2125555564", connection=1, phone=0)
        )  # [4]
        self.activeAgents.append(
            self.AgentGateway.registerAgentForServiceToday("2125555565", connection=1, phone=0)
        )  # [5]
        # insert some calls
        now = datetime.datetime.now()
        self.activeCalls.append(
            self.CallGateway.addCall(agent=self.activeAgents[0], callEnd=(now + datetime.timedelta(seconds=50)))
        )  # call ended
        self.activeCalls.append(self.CallGateway.addCall(agent=self.activeAgents[2]))  # still on call
        self.activeCalls.append(self.CallGateway.addCall(agent=self.activeAgents[3]))  # still on call
        self.activeCalls.append(
            self.CallGateway.addCall(agent=self.activeAgents[4], callEnd=(now + datetime.timedelta(seconds=30)))
        )  # call ended
        self.activeCalls.append(
            self.CallGateway.addCall(agent=self.activeAgents[5], callEnd=(now + datetime.timedelta(seconds=60)))
        )  # call ended

        # get list of aviable agents
        # this returns unsorted list of hash containing agent ani and idle time
        # should have the following agents: 0,4,5
        avialableAgents = self.AgentGateway.getAvailableAgentsByIdleTime()
        self.assertEqual(len(avialableAgents), 3)

    def test_updateAgentPhoneStatus(self):
        self.agentToInsert = self.AgentGateway.registerAgentForServiceToday(self.ANI)
        self.assertEqual(self.agentToInsert.ani, self.ANI)
        # default phone status is 0
        self.assertEqual(self.agentToInsert.phone_status, 0)

        # change status
        modifiedAgent = self.AgentGateway.updateAgentPhoneStatus(self.agentToInsert, 1)

        self.assertEqual(modifiedAgent.ani, self.agentToInsert.ani)
        self.assertEqual(modifiedAgent.phone_status, 1)

    def test_unregisterAgentForServiceToday(self):
        self.agentToInsert = self.AgentGateway.registerAgentForServiceToday(self.ANI)
        # make sure agent is registered for service
        self.assertEqual(self.agentToInsert.ani, self.ANI)
        self.assertEqual(self.agentToInsert.connection_status, 1)

        # unregister
        unregedAgent = self.AgentGateway.unregisterAgentForServiceToday(self.ANI)

        self.assertEqual(self.agentToInsert.ani, unregedAgent.ani)
        self.assertEqual(unregedAgent.connection_status, 0)

    def test_isANICurrentlyActive(self):
        self.agentToInsert = self.AgentGateway.registerAgentForServiceToday(self.ANI)

        self.assertEqual(self.agentToInsert.ani, self.ANI)
        self.assertEqual(self.agentToInsert.connection_status, 1)

        self.assertEqual(self.AgentGateway.isANICurrentlyActive(self.ANI), True)
class CallCenterVectorManager(object):
    """ Handles selection of agents based on different routing algorithms.
        Currently implementing UCD-MIA.
    """
    
    
    def __init__(self):
        from cc_vector_manager.routing_algorithms import UCD_MIA_Alogrithm
        from cc_vector_webservices.services.gateway import AgentGateway, CallGateway
        self.algo = UCD_MIA_Alogrithm()
        #create gateway instances     
        self.agentGateway = AgentGateway()
        self.callGateway = CallGateway()
        
    def getNextAgent(self,msg):
        from urllib import urlencode
        from urllib2 import urlopen, Request
        from config import IVRSettings as SETTINGS
        #get next available agent 
        log.info("in CallCenterVectorManager.getNextAgent()")
        #get message contents
        loadedMessage = simplejson.loads(msg.body)
        ccxmlSessionID = loadedMessage['session']
        ccxmlUniqueID = loadedMessage['id']              
        
        #get available agents
        #returns <Agent Instance>
        agents = self.agentGateway.getAvailableAgentsByIdleTime()
        
        #use our selected UCD_MIA algorithm to select the next agent
        agentWithMostIdleTime = self.algo.getNextAgent(agents)
        
        if agentWithMostIdleTime:            
            #create call with agent
            call = self.callGateway.addCall(agent=agentWithMostIdleTime)
            #update agent phone status
            agentWithMostIdleTime=self.agentGateway.updateAgentPhoneStatus(agentWithMostIdleTime,1)
            #inject event into ccxml session
            data = urlencode({SETTINGS.IVR_SESSION_KEY:ccxmlSessionID, SETTINGS.IVR_EVENT_KEY:SETTINGS.IVR_EVENT_VALUE, \
                              SETTINGS.IVR_UNIQUE_ID_KEY:ccxmlUniqueID, SETTINGS.IVR_AGENT_ANI_KEY:agentWithMostIdleTime.ani, \
                              SETTINGS.IVR_DESTINATION_TYPE_KEY: agentWithMostIdleTime.determineANIType(), \
                              SETTINGS.IVR_CALL_ID_KEY: call.call_id})
            url = SETTINGS.IVR_URL
            request = Request(url, data)
            log.info("ccxml url: " + request.get_full_url() + request.get_data())
            response = urlopen(request)
            if response:
                log.info("Agent assigned to call sucessfully")
            else:
                log.info("Agent assigned to call unsucessfully")
        else:
            #No agent was found
            #send call back to queue
            publisher_args = {MQConfig.DEFAULT_ROUTING_KEY_KEY: MQConfig.DEFAULT_ROUTING_KEY_VALUE,
                          MQConfig.DEFAULT_DELIVERY_MODE_KEY: MQConfig.DEFAULT_DELIVERY_MODE,
                          MQConfig.DEFAULT_EXCHANGE_VALUE_KEY: MQConfig.DEFAULT_EXCHANGE_VALUE
                         }        
            publisher = CallCenterVectorPublisher(**publisher_args)
        
            #setup message
            msg = simplejson.dumps({'session': ccxmlSessionID , 'id': ccxmlUniqueID})
            log.info("message dumped as json")
            publisher.publish_to_queue(msg)
            log.info("message pushed to queue")
            publisher.close()