FOP2墙板插件

时间:2017-02-21 10:22:56

标签: php mysql plugins asterisk

我正在使用FOP2 Full Wallboard插件,它为队列墙板显示添加了三个不同的小部件:代理列表,等待呼叫列表,队列信息/统计信息框。墙板可以显示从Asternic CCStats PRO获取的统计信息。通过使用dustjs模板,可以使用html,css和javascript完全自定义小部件。这是完整的墙板documentation

在后端加载一个名为 getstatsfromasternic.php 的脚本

我打算在我的队列模板中显示总传出,但问题是它只接受文档中声明的Wallboard Queue变量。

getstatsfromasternic 脚本代码:

<?php
if (php_sapi_name() !='cli') exit;
require_once(dirname(__FILE__)."/../../dblib.php");
require_once(dirname(__FILE__)."/../../asmanager.php");

$ini_array       = parse_ini_file('fullwallboard.ini', true);
$asternic_dir    = $ini_array['asternic_install_directory'];
if($asternic_dir == '') {
    $asternic_dir = '/var/www/html/stats';
}

require_once("$asternic_dir/config.php");
$SLA_ANSWERED = Array();

$DEBUG            = 0;
$SLA_ANSWERED[''] = 20;

$loggedInAgents = array();
$condagent ='';
if(isset($argv[1])) {
    $agente=base64_decode($argv[1]);
    $condagent = " AND agent IN ($agente) ";
    $partes = preg_split("/,/",$agente);
    foreach($partes as $valor) {
        $loggedInAgents[]=substr($valor,1,-1);
    }
}

if($DEBUG & 1) {
    $fp = fopen("/tmp/wallboard_debug.log","a");
    fputs($fp,"GET WALLBOARD STATS ($condagent)\n\n");
    if(count($loggedInAgents)>0) {
        fputs($fp,"Agents logged:\n");
        fputs($fp,print_r($loggedInAgents,1)."\n");
    }
}

$astman = new AsteriskManager();

if($DEBUG & 2) {
    fputs($fp,"MySQL host: $DBHOST, user: $DBUSER, pass: $DBPASS, database name: $DBNAME\n");
}

// Connect to MySQL
$db = new dbcon($DBHOST, $DBUSER, $DBPASS, $DBNAME, false);

if (!function_exists('json_encode')) {
    function json_encode($content) {
        require_once dirname(__FILE__).'/../../../JSON.php';
        $json = new Services_JSON;
        return $json->encode($content);
    }
}

$myqevents       = array();
$last_stop       = array();
$last_stop_pause = array();
$agent_stats     = array();
$queue_stats     = array();
$outbound        = array();

$abr['COMPLETECALLER']   = 'CC';
$abr['COMPLETEAGENT']    = 'CA';
$abr['COMPLETEOUTBOUND'] = 'CO';
$abr['RINGNOANSWER']     = 'RA';
$abr['COMPLETED']        = 'COMPLETED';
$abr['COMPLETEDSLA']     = 'COMPLETEDSLA';
$abr['SERVICELEVEL']     = 'SERVICELEVEL';
$abr['ABANDONED']        = 'ABANDONED';
$abr['TALKTIME']         = 'TALKTIME';
$abr['WAITTIME']         = 'WAITTIME';

function parse_conf($filename) {

    $file = file($filename);

    foreach ($file AS $line) {
        if (preg_match("/^\s*([\w]+)\s*=\s*\"?([\w\/\:\.\*\%!=\+\#@&\\$-]*)\"?\s*([;].*)?/",$line,$matches)) {
            $conf[ $matches[1] ] = $matches[2];
        }
    }

    if(!isset($conf['manager_port'])) { $conf['manager_port']='0000';      }
    if(!isset($conf['manager_host'])) { $conf['manager_host']='0.0.0.0'; }

    return $conf;
}

if(is_file("/usr/local/fop2/fop2.cfg") || is_file("/etc/asterisk/fop2/fop2.cfg")) {
    if(is_file("/usr/local/fop2/fop2.cfg")) {
        $fop2conf = parse_conf("/usr/local/fop2/fop2.cfg");
    }
}

$query = "SELECT * FROM qevent ORDER BY null";
$res = $db->consulta($query);
while($row=$db->fetch_assoc($res)) {
    $myqevents[$row['event']] = $row['event_id'];
}

$query = "SELECT * FROM setup WHERE keyword='call_flow' OR keyword='sla_answered'";
$res = $db->consulta($query);
while($row=$db->fetch_assoc($res)) {
    if($row['value']=='outbound') {
        $outbound[]="\"".$row['parameter']."\"";
    }
    if($row['keyword']=='sla_answered') {
        $SLA_ANSWERED[$row['parameter']]=intval($row['value']);
    }
}

if(count($outbound)>0) {
    $outboundqueues = "AND queue NOT IN (".implode(",",$outbound).") ";
    $outboundquery  = "AND queue IN (".implode(",",$outbound).") ";
} else {
    $outboundqueues = '';
    $outboundquery  = '';
}

if($DEBUG & 1) {
    fputs($fp,"----- Session Times Start ----- \n\n");
}

$query = "SELECT @start:=NULL, @stop:=NULL, @qagent:=NULL";
$res = $db->consulta($query);  

$query = "SELECT queue_stats_id, agent, UNIX_TIMESTAMP(start) AS start, UNIX_TIMESTAMP(stop) AS stop, ";
$query.= "TIMESTAMPDIFF(SECOND, start, stop) AS duration ";
$query.= "FROM ( SELECT queue_stats_id, datetime, qevent, (@qagent <> qagent) AS new_qagent, ";
$query.= "@start AS start, @start := IF(qevent = '".$myqevents['ADDMEMBER']."', datetime, NULL) AS prev_start, ";
$query.= "@stop  := IF(qevent = '".$myqevents['REMOVEMEMBER']."', datetime, NULL) AS stop, @qagent := qagent AS qagent ";
$query.= "FROM queue_stats WHERE qevent IN (".$myqevents['ADDMEMBER'].",".$myqevents['REMOVEMEMBER'].") ORDER BY qagent, datetime ) AS tmp ";
$query.= "LEFT JOIN qagent ON qagent = qagent.agent_id, (SELECT @start:=NULL, @stop:=NULL, @qagent:=NULL) AS vars ";
$query.= "WHERE new_qagent = 0 AND start IS NOT NULL AND stop IS NOT NULL and start>=CURDATE() $condagent";

$query = "SELECT queue_stats_id, agent, start, stop, (stop - start) AS duration ";
$query.= "FROM ( SELECT queue_stats_id,datetime,unix_timestamp(datetime),(@qagent <> qagent) AS new_qagent, ";
$query.= "agent,event, @prev_start := @start as start, ";
$query.= "@start := if(qevent='".$myqevents['ADDMEMBER']."',if(@start=0,unix_timestamp(datetime),if(@qagent<>qagent,unix_timestamp(datetime),@start)),0) as pstart, ";
$query.= "@stop  := IF(qevent = '".$myqevents['REMOVEMEMBER']."', unix_timestamp(datetime), NULL) AS stop, ";
$query.= "@qagent := qagent FROM queue_stats LEFT JOIN qevent ON qevent=event_id ";
$query.= "LEFT JOIN qagent ON qagent=agent_id, ( select @start := 0, @prev_start :=0, @qagent :='' ) sqlvars ";
$query.= "WHERE datetime>=CURDATE() AND qevent in (".$myqevents['ADDMEMBER'].",".$myqevents['REMOVEMEMBER'].") ORDER BY qagent,datetime) AS tmp ";
$query.= "WHERE start > 0 AND stop > 0 AND stop > start $condagent";

$res = $db->consulta($query);

if($DEBUG & 2) { 
    fputs($fp,"\nClosed sessions MySQL query:\n\n$query\n\n");
}

while($row=$db->fetch_assoc($res)) {
    $last_stop[$row['agent']]=$row['stop']; // Save last stop date to use in open session query below
    if(!isset($agent_stats[$row['agent']]['ST'])) { $agent_stats[$row['agent']]['ST']=0; }
    if(!isset($agent_stats[$row['agent']]['WT'])) { $agent_stats[$row['agent']]['WT']=0; }
    if(!isset($agent_stats[$row['agent']]['HT'])) { $agent_stats[$row['agent']]['HT']=0; }
    if(!isset($agent_stats[$row['agent']]['TT'])) { $agent_stats[$row['agent']]['TT']=0; }
    if(!isset($agent_stats[$row['agent']]['PT'])) { $agent_stats[$row['agent']]['PT']=0; }
    $agent_stats[$row['agent']]['ST']+=$row['duration'];
    if($DEBUG & 4) {
        fputs($fp,"Agent closed session: ".$row['agent'].", sesion += ".$row['duration']." = ".$agent_stats[$row['agent']]['ST']."\n");
    }
}

// Perform actual query for open sessions, (session with no closing event)
$query = "SELECT queue_stats_id,agent,UNIX_TIMESTAMP(datetime) AS start,UNIX_TIMESTAMP(now()) AS stop, ";
$query.= "TIMESTAMPDIFF(SECOND,datetime,now()) AS duration FROM queue_stats ";
$query.= "LEFT JOIN qagent ON qagent = qagent.agent_id WHERE qevent=".$myqevents['ADDMEMBER']." AND datetime>=CURDATE() $condagent";

$res = $db->consulta($query);
if($DEBUG & 2) {
    fputs($fp,"\nOpen sessions MySQL query:\n\n$query\n\n");
}
while($row=$db->fetch_assoc($res)) {
    if(!isset($last_stop[$row['agent']])) { $last_stop[$row['agent']]=$row['start']; } // We did not have a last stop from closed session so use the first start instead
    if($row['start']>=$last_stop[$row['agent']]) {
        if(!isset($agent_stats[$row['agent']])) { $agent_stats[$row['agent']]=array();}
        if(!isset($agent_stats[$row['agent']]['ST'])) { $agent_stats[$row['agent']]['ST']=0;}
        if(!isset($agent_stats[$row['agent']]['WT'])) { $agent_stats[$row['agent']]['WT']=0; }
        if(!isset($agent_stats[$row['agent']]['HT'])) { $agent_stats[$row['agent']]['HT']=0; }
        if(!isset($agent_stats[$row['agent']]['TT'])) { $agent_stats[$row['agent']]['TT']=0; }
        if(!isset($agent_stats[$row['agent']]['PT'])) { $agent_stats[$row['agent']]['PT']=0; }
        $agent_stats[$row['agent']]['ST']+=$row['duration'];
        $last_stop[$row['agent']]=time();  // If there is a match, last stop is current time
        if($DEBUG & 4) {
            fputs($fp,"Agent open session: ".$row['agent']." sesion += ".$row['duration']." = ".$agent_stats[$row['agent']]['ST']."\n");
        }
    }
}

// Now, for agents with no start or stop sessions, lets just compute a session time from todays 00 hours
$query = "SELECT agent,UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(CURDATE()) as ST FROM qagent WHERE 1=1";

if($DEBUG & 2) {
    fputs($fp,"\nComputed sessions MySQL query:\n\n$query\n\n");
}
$res = $db->consulta($query);
while($row=$db->fetch_assoc($res)) {
    if(!in_array($row['agent'],$loggedInAgents)) {
        if($DEBUG & 4) { 
            fputs($fp,"Agent ".$row['agent']." is not logged, we can safely skip it.\n");
        }
    } else {
        if(!isset($agent_stats[$row['agent']]['ST'])) {
            if($DEBUG & 1) {
                fputs($fp,"Agent computed session: ".$row['agent']." ST to ".$row['ST']."\n");
            }
            $agent_stats[$row['agent']]['ST']=$row['ST'];
            if(!isset($agent_stats[$row['agent']]['WT'])) { $agent_stats[$row['agent']]['WT']=0; }
            if(!isset($agent_stats[$row['agent']]['HT'])) { $agent_stats[$row['agent']]['HT']=0; }
            if(!isset($agent_stats[$row['agent']]['TT'])) { $agent_stats[$row['agent']]['TT']=0; }
            if(!isset($agent_stats[$row['agent']]['PT'])) { $agent_stats[$row['agent']]['PT']=0; }
        }
    }
}

if($DEBUG & 1) {
    fputs($fp,"----- Session Times End ----- \n\n");
}

// Call and Pause Stats for AGENTS
if($DEBUG & 1) {
    fputs($fp,"----- Agents Call Stats Start ----- \n");
}

// Total Pause duration for today, excluding pauses with reasons Hold, Login and Wrapup. Only counts "closed" pauses
$query = "SELECT @start:=NULL, @stop:=NULL, @qagent:=NULL";
$res = $db->consulta($query);  

// Perform actual query
$query = "SELECT queue_stats_id, agent, unix_timestamp(start) AS start, unix_timestamp(stop) AS stop, ";
$query.= "TIMESTAMPDIFF(SECOND, start, stop) AS duration FROM ( SELECT queue_stats_id, datetime, qevent, ";
$query.= "(@qagent <> qagent) AS new_qagent, @start AS start, ";
$query.= "@start := IF(qevent = '".$myqevents['PAUSE']."' or qevent='".$myqevents['PAUSEALL']."', datetime, NULL) AS prev_start, ";
$query.= "@stop := IF(qevent = '".$myqevents['UNPAUSE']."' or qevent='".$myqevents['UNPAUSEALL']."', datetime, NULL) AS stop, ";
$query.= "@qagent := qagent AS qagent FROM queue_stats WHERE qevent IN (".$myqevents['PAUSE'].",".$myqevents['PAUSEALL'].",".$myqevents['UNPAUSE'].",".$myqevents['UNPAUSEALL'].") ";
$query.= "AND info1<>'Hold' AND info1<>'Login' AND info1<>'Wrapup' AND datetime>=CURDATE() ORDER BY qagent, datetime ) AS tmp ";
$query.= "LEFT JOIN qagent ON qagent = qagent.agent_id, (SELECT @start:=NULL, @stop:=NULL, @qagent:=NULL) AS vars ";
$query.= "WHERE new_qagent = 0 AND start IS NOT NULL AND stop IS NOT NULL $condagent";

if($DEBUG & 2) {
    fputs($fp,"\nPauses (excluding hold and wrapup) MySQL query:\n\n$query\n\n");
}
$res = $db->consulta($query); 

while($row=$db->fetch_assoc($res)) {
    $last_stop_pause[$row['agent']]=$row['stop'];
    if(!isset($agent_stats[$row['agent']]['PT'])) { $agent_stats[$row['agent']]['PT']=0; }
    $agent_stats[$row['agent']]['PT']+=$row['duration'];
    if($DEBUG & 4) {
        fputs($fp,"Regular Pause agent ".$row['agent']." += ".$row['duration'].", sums up to ".$agent_stats[$row['agent']]['PT']."\n");
    }
}

// Total Pause duration with reason Hold, when using the Hold report plugin
$query = "SELECT @start:=NULL, @stop:=NULL, @qagent:=NULL, @reason:=NULL";
$res = $db->consulta($query);  

// Perform actual query
$query = "SELECT queue_stats_id, agent, unix_timestamp(start) AS start, unix_timestamp(stop) AS stop, ";
$query.= "TIMESTAMPDIFF(SECOND, start, stop) AS duration, reason from (SELECT queue_stats_id, datetime, qevent, ";
$query.= "(@qagent <> qagent) AS new_qagent, @start AS start, ";
$query.= "@start := IF(qevent = '".$myqevents['PAUSE']."' or qevent='".$myqevents['PAUSEALL']."', datetime, NULL) AS prev_start, ";
$query.= "@stop := IF(qevent = '".$myqevents['UNPAUSE']."' or qevent='".$myqevents['UNPAUSEALL']."', datetime, NULL) AS stop, ";
$query.= "@qagent := qagent AS qagent, @reason AS reason, ";
$query.= "@reason := IF(qevent = '".$myqevents['PAUSE']."' or qevent='".$myqevents['PAUSEALL']."', info1, NULL) as prev_reason  ";
$query.= "FROM queue_stats WHERE qevent IN (".$myqevents['PAUSE'].",".$myqevents['PAUSEALL'].",".$myqevents['UNPAUSE'].",".$myqevents['UNPAUSEALL'].") ";
$query.= "AND datetime>=CURDATE() ORDER BY qagent, datetime) tmp LEFT JOIN qagent ON qagent = qagent.agent_id, ";
$query.= "(SELECT @start:=NULL, @stop:=NULL, @qagent:=NULL,@reason:=NULL) AS vars ";
$query.= "WHERE start IS NOT NULL AND stop IS NOT NULL AND new_qagent=0 AND start>=CURDATE() AND reason='Hold' $condagent";

if($DEBUG & 2) {
    fputs($fp,"\nPauses (Hold) MySQL query:\n\n$query\n\n");
}
$res = $db->consulta($query); 

while($row=$db->fetch_assoc($res)) {
    $last_stop_pause[$row['agent']]=$row['stop'];
    if(!isset($agent_stats[$row['agent']]['HT'])) { $agent_stats[$row['agent']]['HT']=0; }
    $agent_stats[$row['agent']]['HT']+=$row['duration'];
    if($DEBUG & 4) {
        fputs($fp,"Hold Pause agent ".$row['agent']." += ".$row['duration'].", sums up to ".$agent_stats[$row['agent']]['HT']."\n");
    }
}

// Total Pause duration with reason Wrapup, when using the auto wrapup plugin with reason Wrapup 
$query = "SELECT @start:=NULL, @stop:=NULL, @qagent:=NULL, @reason:=NULL";
$res = $db->consulta($query);  

// Perform actual query
$query = "SELECT queue_stats_id, agent, unix_timestamp(start) AS start, unix_timestamp(stop) AS stop, ";
$query.= "TIMESTAMPDIFF(SECOND, start, stop) AS duration, reason from (SELECT queue_stats_id, datetime, qevent, ";
$query.= "(@qagent <> qagent) AS new_qagent, @start AS start, ";
$query.= "@start := IF(qevent = '".$myqevents['PAUSE']."' or qevent='".$myqevents['PAUSEALL']."', datetime, NULL) AS prev_start, ";
$query.= "@stop := IF(qevent = '".$myqevents['UNPAUSE']."' or qevent='".$myqevents['UNPAUSEALL']."', datetime, NULL) AS stop, ";
$query.= "@qagent := qagent AS qagent, @reason AS reason, ";
$query.= "@reason := IF(qevent = '".$myqevents['PAUSE']."' or qevent='".$myqevents['PAUSEALL']."', info1, NULL) as prev_reason  ";
$query.= "FROM queue_stats WHERE qevent IN (".$myqevents['PAUSE'].",".$myqevents['PAUSEALL'].",".$myqevents['UNPAUSE'].",".$myqevents['UNPAUSEALL'].") ";
$query.= "AND datetime>=CURDATE() ORDER BY qagent, datetime) tmp LEFT JOIN qagent ON qagent = qagent.agent_id, ";
$query.= "(SELECT @start:=NULL, @stop:=NULL, @qagent:=NULL,@reason:=NULL) AS vars ";
$query.= "WHERE start IS NOT NULL AND stop IS NOT NULL AND new_qagent=0 AND reason='Wrapup' $condagent";

if($DEBUG & 2) {
    fputs($fp,"\nPauses (Wrapup) MySQL query:\n\n$query\n\n");
}
$res = $db->consulta($query); 

while($row=$db->fetch_assoc($res)) {
    $last_stop_pause[$row['agent']]=$row['stop'];
    if(!isset($agent_stats[$row['agent']]['WT'])) { $agent_stats[$row['agent']]['WT']=0; }
    $agent_stats[$row['agent']]['WT']+=$row['duration'];
    if($DEBUG & 4) {
        fputs($fp,"Wrapup Pause agent ".$row['agent']." += ".$row['duration'].", sums up to ".$agent_stats[$row['agent']]['WT']."\n");
    }
}

// check for Open Pauses now, open pauses with no unpause, discarding hold and wrapup
$query = "SELECT queue_stats_id,agent,unix_timestamp(datetime) AS start, ";
$query.= "unix_timestamp(now()) AS stop, TIMESTAMPDIFF(SECOND,datetime,now()) AS duration ";
$query.= "FROM queue_stats LEFT JOIN qagent ON qagent = qagent.agent_id ";
$query.= "WHERE qevent IN (".$myqevents['PAUSE'].",".$myqevents['PAUSEALL'].") AND datetime>=CURDATE() AND (info1<>'Hold' AND info1<>'Wrapup') $condagent";

if($DEBUG & 2) {
    fputs($fp,"\nOpen Pauses MySQL query:\n\n$query\n\n");
}

$res = $db->consulta($query);

while($row=$db->fetch_assoc($res)) {
    if(!isset($last_stop_pause[$row['agent']])) { $last_stop_pause[$row['agent']]=$row['start']; }
    if($row['start']>=$last_stop_pause[$row['agent']]) {
        if(!isset($agent_stats[$row['agent']]['PT'])) { $agent_stats[$row['agent']]['PT']=0; }
        $agent_stats[$row['agent']]['PT']+=$row['duration'];
        if($DEBUG & 4) {
            fputs($fp,"Open pause agent ".$row['agent']." += ".$row['duration'].", sums up to ".$agent_stats[$row['agent']]['PT']."\n");
        }
        $last_stop_pause[$row['agent']]=time();  
    }
}

// TALKTIME for today, per agent
$query = "SELECT agent,sum(info2) AS talktime ";
$query.= "FROM queue_stats LEFT JOIN qagent ON qagent=qagent.agent_id ";
$query.= "LEFT JOIN qevent ON qevent=qevent.event_id ";
$query.= "LEFT JOIN qname ON qname=qname.queue_id ";
$query.= "WHERE qevent IN(".$myqevents['COMPLETECALLER'].",".$myqevents['COMPLETEAGENT'].") $outboundqueues ";
$query.= "AND datetime >= CURDATE() $condagent GROUP BY qagent";

if($DEBUG & 2) {
    fputs($fp,"\nTalk time MySQL query:\n\n$query\n\n");
}

$res = $db->consulta($query);
while($row=$db->fetch_assoc($res)) {
    $agent_stats[$row['agent']]['TT']=$row['talktime'];
    if($DEBUG & 4) {
        fputs($fp,"Talk time agent ".$row['agent']." = ".$row['talktime']."\n");
    }
}

// RING NO ANSWER y COMPLETE  for today, per agent
$query = "SELECT agent,event,count(qevent) AS count FROM queue_stats ";
$query.= "LEFT JOIN qagent ON qagent=qagent.agent_id ";
$query.= "LEFT JOIN qevent ON qevent=qevent.event_id ";
$query.= "LEFT JOIN qname ON qname=qname.queue_id ";
$query.=" WHERE qevent IN(".$myqevents['COMPLETECALLER'].",".$myqevents['COMPLETEAGENT'].",".$myqevents['RINGNOANSWER'].") $outboundqueues ";
$query.= "AND datetime >= CURDATE() $condagent GROUP BY qagent,qevent";

if($DEBUG & 2) {
    fputs($fp,"\nRingNoAnswer and Complete* MySQL query:\n\n$query\n\n");
}

$res = $db->consulta($query);
while($row=$db->fetch_assoc($res)) {
    $agent_stats[$row['agent']][$abr[$row['event']]]=$row['count'];
    if($DEBUG & 4) {
        fputs($fp,"Completed agent ".$row['agent'].", event ".$row['event']." = ".$row['count']."\n");
    }
}

// OUTBOUND for today using Asternic call_flow setup. Requires a licensed Asternicy
if($outboundquery<>'') {

    $query = "SELECT agent,event,count(qevent) AS count FROM queue_stats ";
    $query.= "LEFT JOIN qagent ON qagent=qagent.agent_id LEFT JOIN qevent ON qevent=qevent.event_id ";
    $query.= "LEFT JOIN qname ON qname=qname.queue_id WHERE qevent ";
    $query.= "IN(".$myqevents['COMPLETECALLER'].",".$myqevents['COMPLETEAGENT'].") $outboundquery AND datetime >= CURDATE() $condagent ";
    $query.= "GROUP BY qagent,qevent";

    if($DEBUG & 2) {
        fputs($fp,"\nOutbound query via  Asternic call_flow queues MySQL query:\n\n$query\n\n");
    }

    $res = $db->consulta($query);
    while($row=$db->fetch_assoc($res)) {
        $agent_stats[$row['agent']][$abr['COMPLETEOUTBOUND']]=$row['count'];
        if($DEBUG & 4) {
            fputs($fp,"Outbound asternic agent ".$row['agent']." = ".$row['count']."\n");
        }
    }

}

// Use CDR table for outbound count in a FreePBX System. For this to work 
// we must be sure the Asternic user is able to access asteriskcdrdb.cdr 
// table

/* Custom query */
$query = "SELECT agent,count(*) as count,data2 as talktime ";
$query.= "FROM queuemetrics.queue_log ";
$query.= "WHERE partition = 'P001' ";
$query.= "AND time_id >= UNIX_TIMESTAMP(CURDATE()) ";
$query.= "AND verb IN ('COMPLETECALLER', 'COMPLETEAGENT') AND data3 = '' $condagent ";
$query.= "GROUP BY agent ";

if($DEBUG & 2) {
    fputs($fp,"\nOutbound query via FreePBX CDR MySQL query:\n\n$query\n\n");
}

$res = $db->consulta($query);
while($row=$db->fetch_assoc($res)) {
    $agent_stats[$row['agent']][$abr['COMPLETEOUTBOUND']]=$row['count'];
    if(!isset($agent_stats[$row['agent']]['TT'])) { $agent_stats[$row['agent']]['TT']=0; }
    $agent_stats[$row['agent']]['TT']+=$row['talktime'];
    if($DEBUG & 4) {
        fputs($fp,"Outbound cdr agent ".$row['agent']." = ".$row['count']."\n");
        fputs($fp,"Outbound cdr agent talktime ".$row['agent']." = ".$agent_stats[$row['agent']]['TT']."\n");
    }
}

foreach($agent_stats AS $agent=>$nada) {
    if(!isset($agent_stats[$agent]['PT'])) { $agent_stats[$agent]['PT']=0; }
    if(!isset($agent_stats[$agent]['ST'])) { $agent_stats[$agent]['ST']=0; }
    if(!isset($agent_stats[$agent]['CC'])) { $agent_stats[$agent]['CC']=0; }
    if(!isset($agent_stats[$agent]['CA'])) { $agent_stats[$agent]['CA']=0; }
    if(!isset($agent_stats[$agent]['CO'])) { $agent_stats[$agent]['CO']=0; }
    if(!isset($agent_stats[$agent]['RA'])) { $agent_stats[$agent]['RA']=0; }
    if(!isset($agent_stats[$agent]['TT'])) { $agent_stats[$agent]['TT']=0; }

    if(preg_match("/Agent\//",$agent)) {
        $numeric_agent = preg_replace("/Agent\//","",$agent);
        $agent_stats[$numeric_agent]=$agent_stats[$agent];
    }

}

if($DEBUG & 1) {
    fputs($fp,print_r($agent_stats,1));
    fputs($fp,"----- Agents Call Stats End ----- \n\n");
}

if($DEBUG & 1) {
    fputs($fp,"----- Queue Stats Start ----- \n");
}

// Queue Stats Queries

$query = "SELECT queue FROM qname WHERE queue NOT IN ('All','None')";
if($DEBUG & 2) {
    fputs($fp,"\nQueues filler MySQL query:\n\n$query\n\n");
}

$res = $db->consulta($query);
while($row=$db->fetch_assoc($res)) {
    if(!isset($queue_stats[$row['queue']][$abr['SERVICELEVEL']])) { 
        $queue_stats[$row['queue']][$abr['SERVICELEVEL']]=isset($SLA_ANSWERED[$row['queue']])?$SLA_ANSWERED[$row['queue']]:$SLA_ANSWERED[''];
    }
    if(!isset($queue_stats[$row['queue']][$abr['COMPLETEDSLA']])) { $queue_stats[$row['queue']][$abr['COMPLETEDSLA']]=0; }
    if(!isset($queue_stats[$row['queue']][$abr['COMPLETED']])) { $queue_stats[$row['queue']][$abr['COMPLETED']]=0; }
    if(!isset($queue_stats[$row['queue']][$abr['TALKTIME']])) { $queue_stats[$row['queue']][$abr['TALKTIME']]=0; }
    if(!isset($queue_stats[$row['queue']][$abr['WAITTIME']])) { $queue_stats[$row['queue']][$abr['WAITTIME']]=0; }
    if(!isset($queue_stats[$row['queue']][$abr['ABANDONED']])) { $queue_stats[$row['queue']][$abr['ABANDONED']]=0; }
}

// total completed caller and agent
$query = "SELECT count(*) AS count,queue,sum(info1) as waittime, sum(info2) as talktime ";
$query.= "FROM queue_stats LEFT JOIN qevent ON qevent=qevent.event_id ";
$query.= "LEFT JOIN qname ON qname=qname.queue_id ";
$query.= "WHERE datetime>=CURDATE() AND event IN ('COMPLETECALLER','COMPLETEAGENT','TRANSFER') GROUP BY qname ORDER BY null";

if($DEBUG & 2) {
    fputs($fp,"\nQueues Completed MySQL query:\n\n$query\n\n");
}

$res = $db->consulta($query);
while($row=$db->fetch_assoc($res)) {
    $queue_stats[$row['queue']][$abr['COMPLETED']]=intval($row['count']);
    $queue_stats[$row['queue']][$abr['COMPLETEDSLA']]=0;
    $queue_stats[$row['queue']][$abr['TALKTIME']]=intval($row['talktime']);
    $queue_stats[$row['queue']][$abr['WAITTIME']]=intval($row['waittime']);

    if($DEBUG & 4) {
        fputs($fp,"Queue ".$row['queue'].", completed: ".$row['count'].", TalkTime: ".$row['talktime'].", Wait Time: ".$row['waittime']."\n");
    }
}

// completed under sla 
$query = "SELECT queue,info1  ";
$query.= "FROM queue_stats LEFT JOIN qevent ON qevent=qevent.event_id ";
$query.= "LEFT JOIN qname ON qname=qname.queue_id ";
$query.= "WHERE datetime>=CURDATE() AND event IN ('COMPLETECALLER','COMPLETEAGENT','TRANSFER') $outboundqueues ORDER BY null";

if($DEBUG & 2) {
    fputs($fp,"\nQueues Completed SLA MySQL query:\n\n$query\n\n");
}

$res = $db->consulta($query);
while($row=$db->fetch_assoc($res)) {
    $sla = (isset($SLA_ANSWERED[$row['queue']]))?$SLA_ANSWERED[$row['queue']]:$SLA_ANSWERED[''];
    $waittime = $row['info1'];
    if($waittime<=$sla) {
        $queue_stats[$row['queue']][$abr['COMPLETEDSLA']]++;
    }
}

// exit and abandon
$query = "SELECT count(*) AS count,queue,sum(info3) as waittime FROM queue_stats ";
$query.= "LEFT JOIN qevent ON qevent=qevent.event_id ";
$query.= "LEFT JOIN qname ON qname=qname.queue_id ";
$query.= "WHERE datetime>=CURDATE() AND event IN ('ABANDON','EXITWITHKEY','EXITWITHTIMEOUT','EXITEMPTY') GROUP BY qname ORDER BY null";

if($DEBUG & 2) {
    fputs($fp,"\nQueues no answer/abandon MySQL query:\n\n$query\n\n");
}
$res = $db->consulta($query);
while($row=$db->fetch_assoc($res)) {
    if(!isset($queue_stats[$row['queue']][$abr['COMPLETED']])) { $queue_stats[$row['queue']][$abr['COMPLETED']]=0; }
    if(!isset($queue_stats[$row['queue']][$abr['TALKTIME']])) { $queue_stats[$row['queue']][$abr['TALKTIME']]=0; }
    if(!isset($queue_stats[$row['queue']][$abr['WAITTIME']])) { $queue_stats[$row['queue']][$abr['WAITTIME']]=0; }
    if(!isset($queue_stats[$row['queue']][$abr['ABANDONED']])) { $queue_stats[$row['queue']][$abr['ABANDONED']]=0; }
    $queue_stats[$row['queue']][$abr['ABANDONED']]=intval($row['count']);
    $queue_stats[$row['queue']][$abr['WAITTIME']]+=intval($row['waittime']);
    if($DEBUG & 4) {
        fputs($fp,"Queue ".$row['queue'].", abandoned: ".$row['count'].", Wait Time: ".$row['waittime']."\n");
    }
}

if($DEBUG & 1) {
    fputs($fp,print_r($queue_stats,1));
    fputs($fp,"----- Queue Stats End ----- \n\n");
}

// Connect to AMI and fire events for every piece of stats we have
if(!$res = $astman->connect($fop2conf['manager_host'].':'.$fop2conf['manager_port'], $fop2conf['manager_user'] , $fop2conf['manager_secret'], 'off')) {
    unset($astman);
}

if ($astman) {

    foreach ($agent_stats as $key => $nada) {
        // we have to send individual events as there is a limit in AMI events header size
        $pepe_stats = Array();
        $pepe_stats[$key] = $agent_stats[$key];
        $json_data = json_encode($pepe_stats);
        $res = $astman->UserEvent('ASTERNICAGENTSTATS',array('Channel'=>'GLOBAL_FULLWALLBOARD','Family'=>'ASTERNICAGENTSTATS','Value'=>base64_encode($json_data)));
    }

    $res = $astman->UserEvent('ASTERNICQUEUESTATSRESET',array('Channel'=>'GLOBAL_FULLWALLBOARD','Family'=>'ASTERNICQUEUESTATSRESET','Value'=>''));
    foreach ($queue_stats as $key => $nada) {
        $pepe_stats = Array();
        $pepe_stats[$key] = $queue_stats[$key];
        $json_data = json_encode($pepe_stats);
        $res = $astman->UserEvent('ASTERNICQUEUESTATS',array('Channel'=>'GLOBAL_FULLWALLBOARD','Family'=>'ASTERNICQUEUESTATS','Value'=>base64_encode($json_data)));
    }
    $res = $astman->UserEvent('ASTERNICQUEUESTATSEND',array('Channel'=>'GLOBAL_FULLWALLBOARD','Family'=>'ASTERNICQUEUESTATSEND','Value'=>''));
}

if($DEBUG & 1) {
    fclose($fp);
}

请注意,我没有安装Asternic密钥而没有许可证,因为它不是强制运行插件。

0 个答案:

没有答案