Demo entry 6324476

PsuedoService

   

Submitted by anonymous on Nov 18, 2016 at 17:08
Language: Python. Code size: 13.2 kB.

agentListTimer_Tick(){

	#Check if background worker has stopped runnning and needs to be restarted.
	if(agentListBackgroundWorker is busy){
		LogMessage("Agent list is still running", TraceLevel.Info, "AgentList-");
	}
	else{
		start agentRelationBackgroundWorker
	}
	agentListTimer.Stop();
}

agentListBackgroundWorker_DoWork(){

	if(agentRelationBackgroundWorker has a cancellation request){
		
		#Calls function to stop running background workers, logs to the logfilename passed in.
		cancelAysnc("AgentList-");
	
	}
	else {
	
		Create Cisco DB Connection;
		Create Inner Cisco DB Connection;
		Create Intelevue DB Connection;
		Create Inner Intelevue DB Connection;
		
		try{
			CiscoConnection.Open();
			IntelevueConnection.Open();
			InnerCiscoConnection.Open();
			InnerIntelevueConnection.Open();
		
			string fields = "", values = "", whereClause = "", aSourceID = "";
			
			if(UCType == UCCX){
				
				"""			
				Agent List Query:
					Selects a distinct agentID for each active agent.
				"""
				ciscoQuery = "
						SELECT DISTINCT agentID 
						FROM 
							(SELECT agentID 
							 FROM AgentStateDetail asd 
							 INNER JOIN Resource r ON asd.agentID = r.resourceID 
							 WHERE active = 't'
							 )
						";
				run ciscoQuery with CiscoConnection;
				
				List<OdbcDataReader> agentList = new List<OdbcDataReader>();
				
				while(ciscoQuery has results){
					
					if(agentRelationBackgroundWorker has a cancellation request){		
						#Calls function to stop running background workers, logs to the logfilename passed in.
						cancelAysnc("AgentList-");
					
					}
					"""			
					Agent Loop Query:
						Selects the single most recent agent state information including:
							ID, login, first/last name, extension, eventype, reason code, eventdatetime for all active agents.
					"""
					
					string loopCiscoQuery = "
								SELECT DISTINCT agentID, resourceLoginID, resourceFirstName, resourceLastName, extension, eventType, reasonCode, eventDateTime 
                                FROM (
									  SELECT agentID, resourceLoginID, resourceFirstName, resourceLastName, extension, eventType, reasonCode, eventDateTime
                                      FROM AgentStateDetail asd 
									  INNER JOIN Resource r ON asd.agentID = r.resourceID
									  )
								WHERE agentID = '{Current agentID}'
								ORDER BY eventDateTime DESC
								";
					run loopCiscoQuery with CiscoConnection;
					
					agentList.Add(loopCiscoQuery OdbcDataReader);
				
				}
				
				for (int iterator = 0; iterator < agentList.Count; iterator++){
				
					read sqlreader from agentList for current agent;
					try {
					
						for(i=0; i < current agentList sqlreader's fields){
						
							if(agentRelationBackgroundWorker has a cancellation request){		
								#Calls function to stop running background workers, logs to the logfilename passed in.
								cancelAysnc("AgentList-");							
							}
							#Agent State
							if(i == 5)
							{
								"""
								Switch statement swaps the int value (as pulled from the Cisco Database) for the string value of agent state.
								This switch handles agent states:
									Log In, Not Ready, Ready, Talking, Work, and Logout 
									All other states will default to the int value as pulled from Cisco query
								"""
								switch()
									case "1":
										values += "'" + "Log In" + "',";
										break
									case "2":
										values += "'" + "Not Ready" + "',";
										break
									case "3-7":
										values += "'{agent state string}'"								
									default: 
										values += "'" + agentList[iterator].GetString(5) + "',"; 
										break
								
							}
							#Last data update, makes sure it doesn't remove date formatting as done in else 
							else if(i == 6){
								values += "'" + agentList[iterator].GetString(i) + "',";
							}
							#Seting aSource ID variable and add aSourceID into whereClause
							else if(i == 0 ){
								whereClause += agentList[iterator].GetString(i) + "'";
								aSourceID = "'" + agentList[iterator].GetString(i) + "'";
							}
							#Setting formatting for all other items in value list
							else {
								values += "'" + agentList[iterator].GetString(i).Replace("'","%27") + "',";
							}
							#Calculate and append the last values into values string
							agentStateTime = DateTime.Parse(agentList[iterator].GetString(7));
							values += "'" + agentList[iterator].GetString(7));
							values += "" + agentStateTime.ToLocalTime() + "'";
							
							agentTimeInState = DateTime.Now - agentStateTime;
							
							"""
							Field list is a comma delimited string of the Intelevue DB Table columns that correspond with the values pulled from Cisco Database.
							This field list is used when inserting and updating the Intelevue table and is in the same order to match Cisco Query results to make
							code for update/insert queries easier by inserting variables within the query structure.
							"""
                            fields = "aLogin, fName, lName, extension, state, reasonCode, lastDataUpdate, timeInState, lastDataSync";
                            
							#Update function adds cisco record into intelevue database
                            updateAgent(ivConn, e, fields, values, whereClause, aSourceID); #e is the event argument being passed for cancellation requests
						}
					catch
					{
						if (e.ToString().Contains("No data exists for the row/column."))
						{
							LogMessage("Warning: " + ex, TraceLevel.Warning, "AgentList-");
						}
						else
						{
							LogMessage("Error: \n exception: " + ex, TraceLevel.Error, "AgentList-");
						}
					}
				}
					
			}
			#UCCE
			else {
				"""
				Agent List Query:
					Selects: ID, login, first/last name, extension, state, reason code, and last state change time for each active agent.
				"""
			
				ciscoQuery = "
						SELECT Agent_Real_Time.SkillTargetID, Person.LoginName, Person.FirstName, Person.LastName, Agent_Real_Time.Extension, 
							   Agent_Real_Time.AgentState, Agent_Real_Time.ReasonCode, Agent_Real_Time.DateTimeLastStateChange
                        FROM Person, Agent_Real_Time, Agent
                        WHERE Agent.Deleted = 'N' 
						AND Agent_Real_Time.SkillTargetID = Agent.SkillTargetID 
						AND Agent.PersonID = Person.PersonID 
                        ORDER BY SkillTargetID
						";
				run ciscoQuery with CiscoConnection;
				
				"""
				ProcessedSkillGroupIDs string is used to keep track of the agents within Cisco's Database (as it only consists of logged in agents)
				This is used to update all other agents found within the Intelevue Database as logged out.
				"""
				string processedSkillGroupIDs = "0,";
				
				if(ciscoQueryResult.HasRows){
				
					while(ciscoQueryResult.Read()){
					
						processedSkillGroupIDs += "'" + ciscoQueryResult + "',";

                        try
                        {
                            DateTime agentStateTime;
                            values = "";
                            whereClause = "WHERE aSourceID = '";
                            for (i = 0; i < ciscoQueryResult.FieldCount - 1; i++)
                            {
                                #Check cancellation request
                                if (backWorkAgentList.CancellationPending)
                                {
                                    cancelAsync("AgentList-");
                                    e.Cancel = true;
                                }
								#Agent States
                                if (i == 5)
                                {
									"""
									Switch statement swaps the int value (as pulled from the Cisco Database) for the string value of agent state.
									This switch handles agent states:
										Log Out, Logged On, Not Ready, Ready, Talking, Work Not Ready, Work Ready, Busy Other, Reserved, Calls on Hold
										Active, Paused, Interrupted, and Not Active. 
										All other states will default to Other.
									"""
                                    switch (ciscoQueryResult.GetString(i))
                                    {
                                        case "0":
                                            values += "'" + "Log Out" + "',";
                                            break;
                                        case "1":
                                            values += "'" + "Logged On" + "',";
                                            break;
                                        case "2-14":
											values += "'{agent state string}'"
                                         default:
                                            values += "'" + "Other" + "',";
                                            break;
                                    }
                                }
								#Last data update, makes sure it doesn't remove date formatting as done in else 
                                else if (i == 6)
                                {
                                    values += "'" + ciscoQueryResult.GetString(i) + "',";
                                }
								#Adds aSourceID into whereClause and sets the aSource variable with the value from query.
                                else if (i == 0)
                                {
                                    whereClause += ciscoQueryResult.GetString(i) + "'";
                                    aSourceID = "'" + ciscoQueryResult.GetString(i) + "'";
                                }
								#Appends all other query values into correct format to create singled quoted values in a comma delimited value string
                                else
                                {
                                    values += "'" + ciscoQueryResult.GetString(i).Replace("'", "%27") + "',";
                                }
								#Calculate and append the last values into values string
								agentStateTime = DateTime.Parse(ciscoQueryResult.GetString(7));
                                values += "'" + agentStateTime + "'";
								values += ",'" + agentTimeInState + "','" + DateTime.Now + "'";

                                agentTimeInState = DateTime.Now - agentStateTime;
								
								"""
								Field list is a comma delimited string of the Intelevue DB Table columns that correspond with the values pulled from Cisco Database.
								This field list is used when inserting and updating the Intelevue table and is in the same order to match Cisco Query results to make
								code for update/insert queries easier by inserting variables within the query structure.
								"""
                                fields = "aLogin, fName, lName, extension, state, reasonCode, lastDataUpdate, timeInState, lastDataSync";
                                
								#Update function adds cisco record into intelevue database
                                updateAgent(ivConn, e, fields, values, whereClause, aSourceID); #e is the event argument being passed for cancellation requests
					}
					catch (Exception exe)
                    {
                        if (e.ToString().Contains("No data exists for the row/column."))
                        {
                            LogMessage("Warning: " + exe, TraceLevel.Warning, "AgentList-");
                        }
                        else
                        {
                            LogMessage("Error: \n exception: " + exe, TraceLevel.Error, "AgentList-");
                        }
                    }
				}
			}
			IntelevueConnection.Close();
			IntelevueConnection.Open();
			"""
			Update Logged Out Agents Query:
				Sets the state for all agents unaccounted for within the system to 'Log Out' (As Cisco's tables only consists on logged in agents)
			"""
			updateQuery = "
						  UPDATE rtAgentInfo 
						  SET state = 'Log Out', lastDataUpdate = GETDATE() 
						  WHERE aSourceID 
						  NOT IN ('{processedSkillGroupIDs.Remove(processedSkillGroupIDs.Length - 1)}') 
						  AND state <> 'Log Out'
						  "
			run ciscoQuery with IntelevueConnection;
			
			"""
			Update Logged Out Agents' Time in state:
				Updates all Logged Out agents's time in state to consist of the correct duration. 
			"""
			updateQuery = "
						  UPDATE rtAgentInfo 
						  SET lastDataSync = GETDATE(), timeInState = DATEDIFF(SECOND, lastDataUpdate, GETDATE()) 
						  WHERE state = 'Log Out'
						  "
			run ciscoQuery with IntelevueConnection;
		
		
		}
			
		CiscoConnection.Close();
		IntelevueConnection.Close();
		InnerCiscoConnection.Close();
		InnerIntelevueConnection.Close();
		}
		catch {
		
            LogMessage("Agent List Error: " + exc.Message.ToString(), TraceLevel.Error, "AgentList-");
            LogMessage("Agent List Error Detail: " + exc.ToString(), TraceLevel.XVerbose, "AgentList-");
		}
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).