Extend by operator statistics

This commit is contained in:
Dmitriy Simushev 2013-05-28 10:55:58 +00:00
parent cef80ad20b
commit 734c68a022
7 changed files with 169 additions and 21 deletions

View File

@ -136,7 +136,11 @@ $dbtables = array(
"operatorid" => "int NOT NULL", "operatorid" => "int NOT NULL",
"threads" => "int NOT NULL DEFAULT 0", "threads" => "int NOT NULL DEFAULT 0",
"messages" => "int NOT NULL DEFAULT 0", "messages" => "int NOT NULL DEFAULT 0",
"averagelength" => "FLOAT(10, 1) NOT NULL DEFAULT 0" "averagelength" => "FLOAT(10, 1) NOT NULL DEFAULT 0",
"sentinvitations" => "int NOT NULL DEFAULT 0",
"acceptedinvitations" => "int NOT NULL DEFAULT 0",
"rejectedinvitations" => "int NOT NULL DEFAULT 0",
"ignoredinvitations" => "int NOT NULL DEFAULT 0"
), ),
"${mysqlprefix}chatrevision" => array( "${mysqlprefix}chatrevision" => array(
@ -242,7 +246,7 @@ $dbtables_can_update = array(
"${mysqlprefix}chatmessage" => array("agentId"), "${mysqlprefix}chatmessage" => array("agentId"),
"${mysqlprefix}indexedchatmessage" => array(), "${mysqlprefix}indexedchatmessage" => array(),
"${mysqlprefix}chatoperator" => array("vcavatar", "vcjabbername", "iperm", "istatus", "idisabled", "vcemail", "dtmrestore", "vcrestoretoken"), "${mysqlprefix}chatoperator" => array("vcavatar", "vcjabbername", "iperm", "istatus", "idisabled", "vcemail", "dtmrestore", "vcrestoretoken"),
"${mysqlprefix}chatoperatorstatistics" => array(), "${mysqlprefix}chatoperatorstatistics" => array("sentinvitations", "acceptedinvitations", "rejectedinvitations", "ignoredinvitations"),
"${mysqlprefix}chatban" => array(), "${mysqlprefix}chatban" => array(),
"${mysqlprefix}chatgroup" => array("vcemail", "iweight", "parent", "vctitle", "vcchattitle", "vclogo", "vchosturl"), "${mysqlprefix}chatgroup" => array("vcemail", "iweight", "parent", "vctitle", "vcchattitle", "vclogo", "vchosturl"),
"${mysqlprefix}chatgroupoperator" => array(), "${mysqlprefix}chatgroupoperator" => array(),

View File

@ -184,6 +184,22 @@ if ($act == "silentcreateall") {
runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcrestoretoken varchar(64)", $link); runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcrestoretoken varchar(64)", $link);
} }
if (in_array("${mysqlprefix}chatoperatorstatistics.sentinvitations", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperatorstatistics ADD sentinvitations int NOT NULL DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatoperatorstatistics.acceptedinvitations", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperatorstatistics ADD acceptedinvitations int NOT NULL DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatoperatorstatistics.rejectedinvitations", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperatorstatistics ADD rejectedinvitations int NOT NULL DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatoperatorstatistics.ignoredinvitations", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatoperatorstatistics ADD ignoredinvitations int NOT NULL DEFAULT 0", $link);
}
if (in_array("${mysqlprefix}chatresponses.vctitle", $absent_columns)) { if (in_array("${mysqlprefix}chatresponses.vctitle", $absent_columns)) {
runsql("ALTER TABLE ${mysqlprefix}chatresponses ADD vctitle varchar(100) NOT NULL DEFAULT '' AFTER groupid", $link); runsql("ALTER TABLE ${mysqlprefix}chatresponses ADD vctitle varchar(100) NOT NULL DEFAULT '' AFTER groupid", $link);
} }

View File

@ -270,28 +270,121 @@ function calculate_operator_statistics() {
$start = empty($result['start']) ? 0 : $result['start']; $start = empty($result['start']) ? 0 : $result['start'];
$today = floor(time() / (24*60*60)) * 24*60*60; $today = floor(time() / (24*60*60)) * 24*60*60;
$statistics = array();
// Caclculate statistics // Caclculate statistics
$db->query( // Get base operator's info
"INSERT INTO {chatoperatorstatistics} ( " . $db_results = $db->query(
"date, operatorid, threads, messages, averagelength" . "SELECT (FLOOR(m.dtmcreated / (24*60*60)) * 24*60*60) AS date, " .
") SELECT (FLOOR(m.dtmcreated / (24*60*60)) * 24*60*60) AS date, " . "m.agentId AS operator_id, " .
"o.operatorid AS opid, " .
"COUNT(distinct m.threadid) AS threads, " . "COUNT(distinct m.threadid) AS threads, " .
"SUM(m.ikind = :kind_agent) AS msgs, " . "COUNT(m.messageid) AS messages, " .
"AVG(CHAR_LENGTH(m.tmessage)) AS avglen " . "AVG(CHAR_LENGTH(m.tmessage)) AS avg_msg_length " .
"FROM {indexedchatmessage} m, {chatoperator} o " . // Use {indexedchatmessage} as base table because of one thread can
"WHERE m.agentId = o.operatorid " . // be related with more than one operator (they can change each
// other during conversation).
"FROM {indexedchatmessage} m, {chatthread} t " .
"WHERE m.ikind = :kind_agent " .
"AND m.threadid = t.threadid " .
"AND (m.dtmcreated - :start) > 24*60*60 " . "AND (m.dtmcreated - :start) > 24*60*60 " .
// Calculate statistics only for messages that older one day // Calculate statistics only for messages that older one day
"AND (:today - m.dtmcreated) > 24*60*60 " . "AND (:today - m.dtmcreated) > 24*60*60 " .
"GROUP BY date " . // Ignore not accepted invitations
"ORDER BY date", "AND (t.invitationstate = :not_invited " .
"OR t.invitationstate = :invitation_accepted) " .
"GROUP BY date, operator_id",
array( array(
':kind_agent' => Thread::KIND_AGENT,
':start' => $start, ':start' => $start,
':today' => $today ':today' => $today,
) ':not_invited' => Thread::INVITATION_NOT_INVITED,
':invitation_accepted' => Thread::INVITATION_ACCEPTED,
':kind_agent' => Thread::KIND_AGENT
),
array('return_rows' => Database::RETURN_ALL_ROWS)
); );
// Store retrieved data as statistics info
foreach($db_results as $result) {
// Cast types of some fields
$result['avg_msg_length'] = (float)$result['avg_msg_length'];
$result['messages'] = (int)$result['messages'];
$statistics[$result['date'] . '_' . $result['operator_id']] = $result;
}
// Get info about invitations
$db_results = $db->query(
"SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " .
"agentId as operator_id, " .
"COUNT(threadid) AS invitation_sent, " .
"SUM(invitationstate = :invitation_accepted) AS invitation_accepted, " .
"SUM(invitationstate = :invitation_rejected) AS invitation_rejected, " .
"SUM(invitationstate = :invitation_ignored) AS invitation_ignored " .
"FROM {chatthread} " .
"WHERE (dtmcreated - :start) > 24*60*60 " .
// Calculate statistics only for threads that older than one day
"AND (:today - dtmcreated) > 24*60*60 " .
// Check if thread has related operator
"AND agentId != 0 " .
// Ignore not accepted invitations
"AND (invitationstate = :invitation_accepted " .
"OR invitationstate = :invitation_rejected " .
"OR invitationstate = :invitation_ignored) " .
"GROUP BY date, operator_id",
array(
':start' => $start,
':today' => $today,
':invitation_accepted' => Thread::INVITATION_ACCEPTED,
':invitation_rejected' => Thread::INVITATION_REJECTED,
':invitation_ignored' => Thread::INVITATION_IGNORED
),
array('return_rows' => Database::RETURN_ALL_ROWS)
);
// Store retrieved data as statistics info
foreach($db_results as $result) {
$key = $result['date'] . '_' . $result['operator_id'];
if (empty($statistics[$key])) {
$statistics[$key] = array();
}
$statistics[$key] += $result;
}
// Sort statistics by date before save it in the database
ksort($statistics);
foreach($statistics as $row) {
// Set default values
$row += array(
'threads' => 0,
'messages' => 0,
'avg_msg_length' => 0,
'invitation_sent' => 0,
'invitation_accepted' => 0,
'invitation_rejected' => 0,
'invitation_ignored' => 0
);
// Prepare data for insert
$insert_data = array();
foreach($row as $field_name => $field_value) {
$insert_data[':' . $field_name] = $field_value;
}
$db->query(
"INSERT INTO {chatoperatorstatistics} (" .
"date, operatorid, threads, messages, averagelength, " .
"sentinvitations, acceptedinvitations, " .
"rejectedinvitations, ignoredinvitations " .
") VALUES (".
":date, :operator_id, :threads, :messages, " .
":avg_msg_length, :invitation_sent, " .
":invitation_accepted, :invitation_rejected, " .
":invitation_ignored " .
")",
$insert_data
);
}
} catch(Exception $e) { } catch(Exception $e) {
// Something went wrong: warn and rollback transaction. // Something went wrong: warn and rollback transaction.
trigger_error( trigger_error(

View File

@ -462,6 +462,10 @@ report.byoperator.1=Operator
report.byoperator.2=Chat Threads report.byoperator.2=Chat Threads
report.byoperator.3=Messages report.byoperator.3=Messages
report.byoperator.4=Average message length (in chars) report.byoperator.4=Average message length (in chars)
report.byoperator.5=Invitations sent
report.byoperator.6=Invitations accepted
report.byoperator.7=Invitations rejected
report.byoperator.8=Invitations ignored
report.byoperator.title=Threads by operator report.byoperator.title=Threads by operator
report.bypage.1=Page report.bypage.1=Page
report.bypage.2=View times report.bypage.2=View times

View File

@ -461,6 +461,10 @@ report.byoperator.1=
report.byoperator.2=Диалогов report.byoperator.2=Диалогов
report.byoperator.3=Сообщений report.byoperator.3=Сообщений
report.byoperator.4=Средняя длина сообщения (в символах) report.byoperator.4=Средняя длина сообщения (в символах)
report.byoperator.5=Приглашений отправлено
report.byoperator.6=Приглашений принято
report.byoperator.7=Приглашений отклонено
report.byoperator.8=Приглашений проигнорировано
report.byoperator.title=Статистика по операторам report.byoperator.title=Статистика по операторам
report.bypage.1=Страница report.bypage.1=Страница
report.bypage.2=Просмотров report.bypage.2=Просмотров

View File

@ -129,9 +129,15 @@ if ($statisticstype == 'bydate') {
} elseif($statisticstype == 'byagent') { } elseif($statisticstype == 'byagent') {
$page['reportByAgent'] = $db->query( $page['reportByAgent'] = $db->query(
"SELECT o.vclocalename AS name, " . "SELECT o.vclocalename AS name, " .
"s.threads AS threads, " . "SUM(s.threads) AS threads, " .
"s.messages AS msgs, " . "SUM(s.messages) AS msgs, " .
"s.averagelength AS avglen " . "ROUND( " .
"SUM(s.averagelength * s.messages) / SUM(s.messages), " .
"1) AS avglen, " .
"SUM(sentinvitations) AS sentinvitations, " .
"SUM(acceptedinvitations) AS acceptedinvitations, " .
"SUM(rejectedinvitations) AS rejectedinvitations, " .
"SUM(ignoredinvitations) AS ignoredinvitations " .
"FROM {chatoperatorstatistics} s, {chatoperator} o " . "FROM {chatoperatorstatistics} s, {chatoperator} o " .
"WHERE s.operatorid = o.operatorid " . "WHERE s.operatorid = o.operatorid " .
"AND s.date >= :start " . "AND s.date >= :start " .

View File

@ -164,7 +164,22 @@ require_once('inc_errors.php');
<?php echo getlocal("report.byoperator.3") ?> <?php echo getlocal("report.byoperator.3") ?>
</th><th> </th><th>
<?php echo getlocal("report.byoperator.4") ?> <?php echo getlocal("report.byoperator.4") ?>
</th></tr> </th>
<?php if ($page['show_invitations_info']) { ?>
<th>
<?php echo getlocal("report.byoperator.5") ?>
</th>
<th>
<?php echo getlocal("report.byoperator.6") ?>
</th>
<th>
<?php echo getlocal("report.byoperator.7") ?>
</th>
<th>
<?php echo getlocal("report.byoperator.8") ?>
</th>
<?php } ?>
</tr>
</thead> </thead>
<tbody> <tbody>
<?php if( $page['reportByAgent'] ) { ?> <?php if( $page['reportByAgent'] ) { ?>
@ -174,11 +189,17 @@ require_once('inc_errors.php');
<td><?php echo $row['threads'] ?></td> <td><?php echo $row['threads'] ?></td>
<td><?php echo $row['msgs'] ?></td> <td><?php echo $row['msgs'] ?></td>
<td><?php echo $row['avglen'] ?></td> <td><?php echo $row['avglen'] ?></td>
<?php if ($page['show_invitations_info']) { ?>
<td><?php echo $row['sentinvitations'] ?></td>
<td><?php echo $row['acceptedinvitations'] ?></td>
<td><?php echo $row['rejectedinvitations'] ?></td>
<td><?php echo $row['ignoredinvitations'] ?></td>
<?php } ?>
</tr> </tr>
<?php } ?> <?php } ?>
<?php } else { ?> <?php } else { ?>
<tr> <tr>
<td colspan="4"> <td colspan="<?php echo($page['show_invitations_info'] ? 8 : 4); ?>">
<?php echo getlocal("report.no_items") ?> <?php echo getlocal("report.no_items") ?>
</td> </td>
</tr> </tr>