Clean up statistics code

This commit is contained in:
Dmitriy Simushev 2013-05-29 08:38:23 +00:00
parent 62a2176aaa
commit b89e838625

View File

@ -109,7 +109,11 @@ function calculate_thread_statistics() {
); );
// Store statistics data // Store statistics data
$statistics = extend_statistics_info(array(), $db_results); $statistics = extend_statistics_info(
array(),
$db_results,
array('date')
);
// Get info about missed threads // Get info about missed threads
$db_results = $db->query( $db_results = $db->query(
@ -132,14 +136,17 @@ function calculate_thread_statistics() {
array('return_rows' => Database::RETURN_ALL_ROWS) array('return_rows' => Database::RETURN_ALL_ROWS)
); );
// Add average waiting time to statistics data // Add info about missed threads to statistics data
$statistics = extend_statistics_info($statistics, $db_results); $statistics = extend_statistics_info(
$statistics,
$db_results,
array('date')
);
// Get info about average chat time and missed threads count. // Get info about average waiting time.
$db_results = $db->query( $db_results = $db->query(
"SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " . "SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " .
"ROUND(AVG(dtmchatstarted-dtmcreated),1) AS avg_waiting_time, "ROUND(AVG(dtmchatstarted-dtmcreated),1) AS avg_waiting_time " .
SUM(dtmchatstarted = 0) AS missed_threads " .
"FROM {chatthread} " . "FROM {chatthread} " .
"WHERE (dtmcreated - :start) > 24*60*60 " . "WHERE (dtmcreated - :start) > 24*60*60 " .
// Calculate statistics only for threads that older than one day // Calculate statistics only for threads that older than one day
@ -158,15 +165,19 @@ function calculate_thread_statistics() {
); );
// Add average waiting time to statistics data // Add average waiting time to statistics data
$statistics = extend_statistics_info($statistics, $db_results); $statistics = extend_statistics_info(
$statistics,
$db_results,
array('date')
);
// Get invitation info // Get invitation info
$db_results = $db->query( $db_results = $db->query(
"SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " . "SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " .
"COUNT(*) AS invitation_sent, " . "COUNT(*) AS invitations_sent, " .
"SUM(invitationstate = :invitation_accepted) AS invitation_accepted, " . "SUM(invitationstate = :invitation_accepted) AS invitations_accepted, " .
"SUM(invitationstate = :invitation_rejected) AS invitation_rejected, " . "SUM(invitationstate = :invitation_rejected) AS invitations_rejected, " .
"SUM(invitationstate = :invitation_ignored) AS invitation_ignored " . "SUM(invitationstate = :invitation_ignored) AS invitations_ignored " .
"FROM {chatthread} " . "FROM {chatthread} " .
"WHERE (dtmcreated - :start) > 24*60*60 " . "WHERE (dtmcreated - :start) > 24*60*60 " .
// Calculate statistics only for threads that older than one day // Calculate statistics only for threads that older than one day
@ -186,7 +197,11 @@ function calculate_thread_statistics() {
); );
// Add invitation info to statistics data // Add invitation info to statistics data
$statistics = extend_statistics_info($statistics, $db_results); $statistics = extend_statistics_info(
$statistics,
$db_results,
array('date')
);
// Sort statistics by date before save it in the database // Sort statistics by date before save it in the database
ksort($statistics); ksort($statistics);
@ -200,10 +215,10 @@ function calculate_thread_statistics() {
'user_msgs' => 0, 'user_msgs' => 0,
'avg_chat_time' => 0, 'avg_chat_time' => 0,
'avg_waiting_time' => 0, 'avg_waiting_time' => 0,
'invitation_sent' => 0, 'invitations_sent' => 0,
'invitation_accepted' => 0, 'invitations_accepted' => 0,
'invitation_rejected' => 0, 'invitations_rejected' => 0,
'invitation_ignored' => 0 'invitations_ignored' => 0
); );
// Prepare data for insert // Prepare data for insert
@ -220,9 +235,9 @@ function calculate_thread_statistics() {
"ignoredinvitations, operatormessages, usermessages, " . "ignoredinvitations, operatormessages, usermessages, " .
"averagewaitingtime, averagechattime " . "averagewaitingtime, averagechattime " .
") VALUES (" . ") VALUES (" .
":date, :threads, :missed_threads, :invitation_sent, " . ":date, :threads, :missed_threads, :invitations_sent, " .
":invitation_accepted, :invitation_rejected, " . ":invitations_accepted, :invitations_rejected, " .
":invitation_ignored, :operator_msgs, :user_msgs, " . ":invitations_ignored, :operator_msgs, :user_msgs, " .
":avg_waiting_time, :avg_chat_time " . ":avg_waiting_time, :avg_chat_time " .
")", ")",
$insert_data $insert_data
@ -270,8 +285,6 @@ 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
// Get base operator's info // Get base operator's info
$db_results = $db->query( $db_results = $db->query(
@ -304,22 +317,20 @@ function calculate_operator_statistics() {
); );
// Store retrieved data as statistics info // Store retrieved data as statistics info
foreach($db_results as $result) { $statistics = extend_statistics_info(
// Cast types of some fields array(),
$result['avg_msg_length'] = (float)$result['avg_msg_length']; $db_results,
$result['messages'] = (int)$result['messages']; array('date', 'operator_id')
);
$statistics[$result['date'] . '_' . $result['operator_id']] = $result;
}
// Get info about invitations // Get info about invitations
$db_results = $db->query( $db_results = $db->query(
"SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " . "SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " .
"agentId as operator_id, " . "agentId as operator_id, " .
"COUNT(threadid) AS invitation_sent, " . "COUNT(threadid) AS invitations_sent, " .
"SUM(invitationstate = :invitation_accepted) AS invitation_accepted, " . "SUM(invitationstate = :invitation_accepted) AS invitations_accepted, " .
"SUM(invitationstate = :invitation_rejected) AS invitation_rejected, " . "SUM(invitationstate = :invitation_rejected) AS invitations_rejected, " .
"SUM(invitationstate = :invitation_ignored) AS invitation_ignored " . "SUM(invitationstate = :invitation_ignored) AS invitations_ignored " .
"FROM {chatthread} " . "FROM {chatthread} " .
"WHERE (dtmcreated - :start) > 24*60*60 " . "WHERE (dtmcreated - :start) > 24*60*60 " .
// Calculate statistics only for threads that older than one day // Calculate statistics only for threads that older than one day
@ -342,13 +353,11 @@ function calculate_operator_statistics() {
); );
// Store retrieved data as statistics info // Store retrieved data as statistics info
foreach($db_results as $result) { $statistics = extend_statistics_info(
$key = $result['date'] . '_' . $result['operator_id']; $statistics,
if (empty($statistics[$key])) { $db_results,
$statistics[$key] = array(); array('date', 'operator_id')
} );
$statistics[$key] += $result;
}
// Sort statistics by date before save it in the database // Sort statistics by date before save it in the database
ksort($statistics); ksort($statistics);
@ -359,10 +368,10 @@ function calculate_operator_statistics() {
'threads' => 0, 'threads' => 0,
'messages' => 0, 'messages' => 0,
'avg_msg_length' => 0, 'avg_msg_length' => 0,
'invitation_sent' => 0, 'invitations_sent' => 0,
'invitation_accepted' => 0, 'invitations_accepted' => 0,
'invitation_rejected' => 0, 'invitations_rejected' => 0,
'invitation_ignored' => 0 'invitations_ignored' => 0
); );
// Prepare data for insert // Prepare data for insert
@ -378,9 +387,9 @@ function calculate_operator_statistics() {
"rejectedinvitations, ignoredinvitations " . "rejectedinvitations, ignoredinvitations " .
") VALUES (". ") VALUES (".
":date, :operator_id, :threads, :messages, " . ":date, :operator_id, :threads, :messages, " .
":avg_msg_length, :invitation_sent, " . ":avg_msg_length, :invitations_sent, " .
":invitation_accepted, :invitation_rejected, " . ":invitations_accepted, :invitations_rejected, " .
":invitation_ignored " . ":invitations_ignored " .
")", ")",
$insert_data $insert_data
); );
@ -447,9 +456,12 @@ function calculate_page_statistics() {
array('return_rows' => Database::RETURN_ALL_ROWS) array('return_rows' => Database::RETURN_ALL_ROWS)
); );
foreach($db_results as $result) { // Store info in statistics data
$statistics[$result['date'] . '_' . $result['address']] = $result; $statistics = extend_statistics_info(
} array(),
$db_results,
array('date', 'address')
);
// Get total chats count // Get total chats count
$db_results = $db->query( $db_results = $db->query(
@ -487,13 +499,11 @@ function calculate_page_statistics() {
); );
// Store info in statistics data // Store info in statistics data
foreach($db_results as $result) { $statistics = extend_statistics_info(
$key = $result['date'] . '_' . $result['address']; $statistics,
if (empty($statistics[$key])) { $db_results,
$statistics[$key] = array(); array('date', 'address')
} );
$statistics[$key] += $result;
}
// Get info about accepted invitations // Get info about accepted invitations
$db_results = $db->query( $db_results = $db->query(
@ -518,13 +528,11 @@ function calculate_page_statistics() {
); );
// Store info in statistics data // Store info in statistics data
foreach($db_results as $result) { $statistics = extend_statistics_info(
$key = $result['date'] . '_' . $result['address']; $statistics,
if (empty($statistics[$key])) { $db_results,
$statistics[$key] = array(); array('date', 'address')
} );
$statistics[$key] += $result;
}
// Get info about rejected invitations // Get info about rejected invitations
$db_results = $db->query( $db_results = $db->query(
@ -549,13 +557,11 @@ function calculate_page_statistics() {
); );
// Store info in statistics data // Store info in statistics data
foreach($db_results as $result) { $statistics = extend_statistics_info(
$key = $result['date'] . '_' . $result['address']; $statistics,
if (empty($statistics[$key])) { $db_results,
$statistics[$key] = array(); array('date', 'address')
} );
$statistics[$key] += $result;
}
// Get info about ignored invitations // Get info about ignored invitations
$db_results = $db->query( $db_results = $db->query(
@ -580,13 +586,11 @@ function calculate_page_statistics() {
); );
// Store info in statistics data // Store info in statistics data
foreach($db_results as $result) { $statistics = extend_statistics_info(
$key = $result['date'] . '_' . $result['address']; $statistics,
if (empty($statistics[$key])) { $db_results,
$statistics[$key] = array(); array('date', 'address')
} );
$statistics[$key] += $result;
}
// Sort statistics by date before save it in the database // Sort statistics by date before save it in the database
ksort($statistics); ksort($statistics);
@ -658,21 +662,39 @@ function calculate_page_statistics() {
} }
/** /**
* Add info from $additional_info to $stat_info using value of 'date' item as * Add info from $additional_info to $stat_info using specified keys.
* a key.
* *
* Triggers an error with E_WARNING level if row of $additional_info array does
* not contain one of specified keys.
* @param array $stat_info Statistics info * @param array $stat_info Statistics info
* @param array $additional_info Data that must be added to statistics info * @param array $additional_info Data that must be added to statistics info
* @return array Extended statistics info * @param array $keys List of keys.
* @return array|boolean Extended statistics info or boolean false on failure
*/ */
function extend_statistics_info($stat_info, $additional_info) { function extend_statistics_info($stat_info, $additional_info, $keys) {
$result = $stat_info; $result = $stat_info;
foreach($additional_info as $row) { foreach($additional_info as $row) {
$date = $row['date']; // Build key field
if (empty($result[$date])) { $key_field = array();
$result[$date] = array(); foreach($keys as $key) {
if (!array_key_exists($key, $row)) {
trigger_error(
"There is no '{$key}' key in additional_info row!",
E_USER_WARNING
);
return false;
} }
$result[$date] += $row; $key_field[] = $row[$key];
}
$key_field = implode('_', $key_field);
// Index info
if (empty($result[$key_field])) {
$result[$key_field] = array();
}
// Extend info
$result[$key_field] += $row;
} }
return $result; return $result;
} }