From 1a0b3153650b8a7fd07e0662d104bd5adab050a2 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Wed, 3 Jul 2013 08:31:36 +0000 Subject: [PATCH] Add statistics aggregation interval --- src/messenger/webim/libs/classes/settings.php | 2 + src/messenger/webim/libs/statistics.php | 114 +++++++++++------- src/messenger/webim/locales/en/properties | 2 + src/messenger/webim/locales/ru/properties | 2 + src/messenger/webim/operator/performance.php | 8 +- src/messenger/webim/operator/statistics.php | 1 + src/messenger/webim/view/performance.php | 11 +- 7 files changed, 93 insertions(+), 47 deletions(-) diff --git a/src/messenger/webim/libs/classes/settings.php b/src/messenger/webim/libs/classes/settings.php index d72613fa..1cd92808 100644 --- a/src/messenger/webim/libs/classes/settings.php +++ b/src/messenger/webim/libs/classes/settings.php @@ -98,6 +98,8 @@ Class Settings { 'updatefrequency_operator' => 2, 'updatefrequency_chat' => 2, + 'statistics_aggregation_interval' => 24*60*60, + 'updatefrequency_tracking' => 10, 'visitors_limit' => 20, /* Number of visitors to look over */ 'invitation_lifetime' => 60, /* Lifetime for invitation to chat */ diff --git a/src/messenger/webim/libs/statistics.php b/src/messenger/webim/libs/statistics.php index 522ec714..afee2b91 100644 --- a/src/messenger/webim/libs/statistics.php +++ b/src/messenger/webim/libs/statistics.php @@ -47,6 +47,8 @@ function calculate_thread_statistics() { $db = Database::getInstance(); $db_throw_exceptions = $db->throwExeptions(true); + $interval = Settings::get('statistics_aggregation_interval'); + try { // Start transaction $db->query('START TRANSACTION'); @@ -59,12 +61,12 @@ function calculate_thread_statistics() { ); $start = empty($result['start']) ? 0 : $result['start']; - $today = floor(time() / (24*60*60)) * 24*60*60; + $today = floor(time() / $interval) * $interval; // Calculate statistics // Get base threads info $db_results = $db->query( - "SELECT (FLOOR(t.dtmcreated / (24*60*60)) * 24*60*60) AS date, " . + "SELECT (FLOOR(t.dtmcreated / :interval) * :interval) AS date, " . "COUNT(t.threadid) AS threads, " . "SUM(tmp.operator_msgs) AS operator_msgs, " . "SUM(tmp.user_msgs) AS user_msgs, " . @@ -88,9 +90,10 @@ function calculate_thread_statistics() { "OR m.ikind = :kind_agent " . "GROUP BY m.threadid) tmp " . "WHERE t.threadid = tmp.threadid " . - "AND (t.dtmcreated - :start) > 24*60*60 " . - // Calculate statistics only for threads that older than one day - "AND (:today - t.dtmcreated) > 24*60*60 " . + "AND (t.dtmcreated - :start) > :interval " . + // Calculate statistics only for threads that older than + // statistics_aggregation_interval + "AND (:today - t.dtmcreated) > :interval " . // Ignore threads when operator does not start chat "AND t.dtmchatstarted <> 0 " . // Ignore not accepted invitations @@ -100,6 +103,7 @@ function calculate_thread_statistics() { array( ':start' => $start, ':today' => $today, + ':interval' => $interval, ':not_invited' => Thread::INVITATION_NOT_INVITED, ':invitation_accepted' => Thread::INVITATION_ACCEPTED, ':kind_agent' => Thread::KIND_AGENT, @@ -117,12 +121,13 @@ function calculate_thread_statistics() { // Get info about missed threads $db_results = $db->query( - "SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " . + "SELECT (FLOOR(dtmcreated / :interval) * :interval) AS date, " . "COUNT(*) as missed_threads " . "FROM {chatthread} " . - "WHERE (dtmcreated - :start) > 24*60*60 " . - // Calculate statistics only for threads that older than one day - "AND (:today - dtmcreated) > 24*60*60 " . + "WHERE (dtmcreated - :start) > :interval " . + // Calculate statistics only for threads that older than + // statistics_aggregation_interval + "AND (:today - dtmcreated) > :interval " . // Ignore threads when operator does not start chat "AND dtmchatstarted = 0 " . // Ignore not accepted invitations @@ -131,6 +136,7 @@ function calculate_thread_statistics() { array( ':start' => $start, ':today' => $today, + ':interval' => $interval, ':not_invited' => Thread::INVITATION_NOT_INVITED ), array('return_rows' => Database::RETURN_ALL_ROWS) @@ -145,12 +151,13 @@ function calculate_thread_statistics() { // Get info about average waiting time. $db_results = $db->query( - "SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " . + "SELECT (FLOOR(dtmcreated / :interval) * :interval) AS date, " . "ROUND(AVG(dtmchatstarted-dtmcreated),1) AS avg_waiting_time " . "FROM {chatthread} " . - "WHERE (dtmcreated - :start) > 24*60*60 " . - // Calculate statistics only for threads that older than one day - "AND (:today - dtmcreated) > 24*60*60 " . + "WHERE (dtmcreated - :start) > :interval " . + // Calculate statistics only for threads that older than + // statistics_aggregation_interval + "AND (:today - dtmcreated) > :interval " . // Ignore threads when operator does not start chat "AND dtmchatstarted <> 0 " . // Ignore all invitations @@ -159,6 +166,7 @@ function calculate_thread_statistics() { array( ':start' => $start, ':today' => $today, + ':interval' => $interval, ':not_invited' => Thread::INVITATION_NOT_INVITED ), array('return_rows' => Database::RETURN_ALL_ROWS) @@ -173,15 +181,16 @@ function calculate_thread_statistics() { // Get invitation info $db_results = $db->query( - "SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " . + "SELECT (FLOOR(dtmcreated / :interval) * :interval) AS date, " . "COUNT(*) AS invitations_sent, " . "SUM(invitationstate = :invitation_accepted) AS invitations_accepted, " . "SUM(invitationstate = :invitation_rejected) AS invitations_rejected, " . "SUM(invitationstate = :invitation_ignored) AS invitations_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 " . + "WHERE (dtmcreated - :start) > :interval " . + // Calculate statistics only for threads that older than + // statistics_aggregation_interval + "AND (:today - dtmcreated) > :interval " . "AND (invitationstate = :invitation_accepted " . "OR invitationstate = :invitation_rejected " . "OR invitationstate = :invitation_ignored) " . @@ -189,6 +198,7 @@ function calculate_thread_statistics() { array( ':start' => $start, ':today' => $today, + ':interval' => $interval, ':invitation_accepted' => Thread::INVITATION_ACCEPTED, ':invitation_rejected' => Thread::INVITATION_REJECTED, ':invitation_ignored' => Thread::INVITATION_IGNORED @@ -271,6 +281,8 @@ function calculate_operator_statistics() { $db = Database::getInstance(); $db_throw_exceptions = $db->throwExeptions(true); + $interval = Settings::get('statistics_aggregation_interval'); + try { // Start transaction $db->query('START TRANSACTION'); @@ -283,12 +295,12 @@ function calculate_operator_statistics() { ); $start = empty($result['start']) ? 0 : $result['start']; - $today = floor(time() / (24*60*60)) * 24*60*60; + $today = floor(time() / $interval) * $interval; // Caclculate statistics // Get base operator's info $db_results = $db->query( - "SELECT (FLOOR(m.dtmcreated / (24*60*60)) * 24*60*60) AS date, " . + "SELECT (FLOOR(m.dtmcreated / :interval) * :interval) AS date, " . "m.agentId AS operator_id, " . "COUNT(distinct m.threadid) AS threads, " . "COUNT(m.messageid) AS messages, " . @@ -299,9 +311,10 @@ function calculate_operator_statistics() { "FROM {chatmessage} m, {chatthread} t " . "WHERE m.ikind = :kind_agent " . "AND m.threadid = t.threadid " . - "AND (m.dtmcreated - :start) > 24*60*60 " . - // Calculate statistics only for messages that older one day - "AND (:today - m.dtmcreated) > 24*60*60 " . + "AND (m.dtmcreated - :start) > :interval " . + // Calculate statistics only for messages that older + // statistics_aggregation_interval + "AND (:today - m.dtmcreated) > :interval " . // Ignore not accepted invitations "AND (t.invitationstate = :not_invited " . "OR t.invitationstate = :invitation_accepted) " . @@ -309,6 +322,7 @@ function calculate_operator_statistics() { array( ':start' => $start, ':today' => $today, + ':interval' => $interval, ':not_invited' => Thread::INVITATION_NOT_INVITED, ':invitation_accepted' => Thread::INVITATION_ACCEPTED, ':kind_agent' => Thread::KIND_AGENT @@ -325,16 +339,17 @@ function calculate_operator_statistics() { // Get info about invitations $db_results = $db->query( - "SELECT (FLOOR(dtmcreated / (24*60*60)) * 24*60*60) AS date, " . + "SELECT (FLOOR(dtmcreated / :interval) * :interval) AS date, " . "agentId as operator_id, " . "COUNT(threadid) AS invitations_sent, " . "SUM(invitationstate = :invitation_accepted) AS invitations_accepted, " . "SUM(invitationstate = :invitation_rejected) AS invitations_rejected, " . "SUM(invitationstate = :invitation_ignored) AS invitations_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 " . + "WHERE (dtmcreated - :start) > :interval " . + // Calculate statistics only for threads that older than + // statistics_aggregation_interval + "AND (:today - dtmcreated) > :interval " . // Check if thread has related operator "AND agentId != 0 " . // Ignore not accepted invitations @@ -345,6 +360,7 @@ function calculate_operator_statistics() { array( ':start' => $start, ':today' => $today, + ':interval' => $interval, ':invitation_accepted' => Thread::INVITATION_ACCEPTED, ':invitation_rejected' => Thread::INVITATION_REJECTED, ':invitation_ignored' => Thread::INVITATION_IGNORED @@ -422,6 +438,8 @@ function calculate_page_statistics() { $db = Database::getInstance(); $db_throw_exceptions = $db->throwExeptions(true); + $interval = Settings::get('statistics_aggregation_interval'); + try { // Start transaction $db->query('START TRANSACTION'); @@ -434,24 +452,25 @@ function calculate_page_statistics() { ); $start = empty($result['start']) ? 0 : $result['start']; - $today = floor(time() / (24*60*60)) * 24*60*60; + $today = floor(time() / $interval) * $interval; $statistics = array(); // Calculate statistics // Get main pages info $db_results = $db->query( - "SELECT FLOOR(visittime / (24*60*60)) * 24*60*60 AS date, " . + "SELECT FLOOR(visittime / :interval) * :interval AS date, " . "address, " . "COUNT(DISTINCT pageid) AS visits " . "FROM {visitedpage} ". "WHERE calculated = 0 " . - "AND (visittime - :start) > 24*60*60 " . - "AND (:today - visittime) > 24*60*60 " . + "AND (visittime - :start) > :interval " . + "AND (:today - visittime) > :interval " . "GROUP BY date, address", array( ':start' => $start, - ':today' => $today + ':today' => $today, + ':interval' => $interval ), array('return_rows' => Database::RETURN_ALL_ROWS) ); @@ -465,7 +484,7 @@ function calculate_page_statistics() { // Get total chats count $db_results = $db->query( - "SELECT FLOOR(p.visittime / (24*60*60)) * 24*60*60 AS date, " . + "SELECT FLOOR(p.visittime / :interval) * :interval AS date, " . "p.address AS address, " . "COUNT(DISTINCT t.threadid) AS chats " . "FROM {visitedpage} p, {chatthread} t, " . @@ -480,8 +499,8 @@ function calculate_page_statistics() { "AND t.threadid = tmp.threadid " . "AND tmp.msgs > 0 " . "AND t.dtmchatstarted <> 0 " . - "AND (p.visittime - :start) > 24*60*60 " . - "AND (:today - p.visittime) > 24*60*60 " . + "AND (p.visittime - :start) > :interval " . + "AND (:today - p.visittime) > :interval " . "AND DATE(FROM_UNIXTIME(p.visittime)) " . "= DATE(FROM_UNIXTIME(t.dtmcreated)) " . "AND (t.invitationstate = :not_invited " . @@ -490,6 +509,7 @@ function calculate_page_statistics() { array( ':start' => $start, ':today' => $today, + ':interval' => $interval, ':not_invited' => Thread::INVITATION_NOT_INVITED, ':invitation_accepted' => Thread::INVITATION_ACCEPTED, ':kind_agent' => Thread::KIND_AGENT, @@ -507,14 +527,14 @@ function calculate_page_statistics() { // Get info about accepted invitations $db_results = $db->query( - "SELECT FLOOR(p.visittime / (24*60*60)) * 24*60*60 AS date, " . + "SELECT FLOOR(p.visittime / :interval) * :interval AS date, " . "p.address AS address, " . "COUNT(DISTINCT t.threadid) AS invitations_accepted " . "FROM {visitedpage} p, {chatthread} t " . "WHERE t.referer = p.address " . "AND p.calculated = 0 " . - "AND (p.visittime - :start) > 24*60*60 " . - "AND (:today - p.visittime) > 24*60*60 " . + "AND (p.visittime - :start) > :interval " . + "AND (:today - p.visittime) > :interval " . "AND DATE(FROM_UNIXTIME(p.visittime)) " . "= DATE(FROM_UNIXTIME(t.dtmcreated)) " . "AND t.invitationstate = :invitation_accepted " . @@ -522,6 +542,7 @@ function calculate_page_statistics() { array( ':start' => $start, ':today' => $today, + ':interval' => $interval, ':invitation_accepted' => Thread::INVITATION_ACCEPTED ), array('return_rows' => Database::RETURN_ALL_ROWS) @@ -536,14 +557,14 @@ function calculate_page_statistics() { // Get info about rejected invitations $db_results = $db->query( - "SELECT FLOOR(p.visittime / (24*60*60)) * 24*60*60 AS date, " . + "SELECT FLOOR(p.visittime / :interval) * :interval AS date, " . "p.address AS address, " . "COUNT(DISTINCT t.threadid) AS invitations_rejected " . "FROM {visitedpage} p, {chatthread} t " . "WHERE t.referer = p.address " . "AND p.calculated = 0 " . - "AND (p.visittime - :start) > 24*60*60 " . - "AND (:today - p.visittime) > 24*60*60 " . + "AND (p.visittime - :start) > :interval " . + "AND (:today - p.visittime) > :interval " . "AND DATE(FROM_UNIXTIME(p.visittime)) " . "= DATE(FROM_UNIXTIME(t.dtmcreated)) " . "AND t.invitationstate = :invitation_rejected " . @@ -551,6 +572,7 @@ function calculate_page_statistics() { array( ':start' => $start, ':today' => $today, + ':interval' => $interval, ':invitation_rejected' => Thread::INVITATION_REJECTED ), array('return_rows' => Database::RETURN_ALL_ROWS) @@ -565,14 +587,14 @@ function calculate_page_statistics() { // Get info about ignored invitations $db_results = $db->query( - "SELECT FLOOR(p.visittime / (24*60*60)) * 24*60*60 AS date, " . + "SELECT FLOOR(p.visittime / :interval) * :interval AS date, " . "p.address AS address, " . "COUNT(DISTINCT t.threadid) AS invitations_ignored " . "FROM {visitedpage} p, {chatthread} t " . "WHERE t.referer = p.address " . "AND p.calculated = 0 " . - "AND (p.visittime - :start) > 24*60*60 " . - "AND (:today - p.visittime) > 24*60*60 " . + "AND (p.visittime - :start) > :interval " . + "AND (:today - p.visittime) > :interval " . "AND DATE(FROM_UNIXTIME(p.visittime)) " . "= DATE(FROM_UNIXTIME(t.dtmcreated)) " . "AND t.invitationstate = :invitation_ignored " . @@ -580,6 +602,7 @@ function calculate_page_statistics() { array( ':start' => $start, ':today' => $today, + ':interval' => $interval, ':invitation_ignored' => Thread::INVITATION_IGNORED ), array('return_rows' => Database::RETURN_ALL_ROWS) @@ -632,10 +655,11 @@ function calculate_page_statistics() { // Mark all visited pages as 'calculated' $db->query( "UPDATE {visitedpage} SET calculated = 1 " . - "WHERE (:today - visittime) > 24*60*60 " . + "WHERE (:today - visittime) > :interval " . "AND calculated = 0", array( - ':today' => $today + ':today' => $today, + ':interval' => $interval ) ); diff --git a/src/messenger/webim/locales/en/properties b/src/messenger/webim/locales/en/properties index bec09c18..c358f7bb 100644 --- a/src/messenger/webim/locales/en/properties +++ b/src/messenger/webim/locales/en/properties @@ -562,6 +562,8 @@ settings.saved=Changes saved settings.sendmessagekey=Send messages with: settings.show_online_operators.description=Can slow down the update rate of the list settings.show_online_operators=Show online operators on "List of awaiting visitors" page +settings.statistics_aggregation_interval=Statistics aggregation interval +settings.statistics_aggregation_interval.description=Specify interval for statistics aggregation in seconds. Default is 86400 (one day) settings.survey.askgroup.description=Show/hide department selection field in the survey settings.survey.askgroup=Allows a visitor to choose department/group settings.survey.askmail.description=Show/hide email field in the survey diff --git a/src/messenger/webim/locales/ru/properties b/src/messenger/webim/locales/ru/properties index 42322d02..fe31e457 100644 --- a/src/messenger/webim/locales/ru/properties +++ b/src/messenger/webim/locales/ru/properties @@ -561,6 +561,8 @@ settings.saved= settings.sendmessagekey=Посылать сообщение по: settings.show_online_operators.description=Может замедлить обновление списка settings.show_online_operators=Показывать доступных операторов на странице ожидающих посетителей +settings.statistics_aggregation_interval=Интервал суммирования статистики +settings.statistics_aggregation_interval.description=Укажите интервал для суммирования статистики в секундах. По умолчанию, 86400 секунд (один день) settings.survey.askgroup.description=Показать/спрятать выбор группы в диалоге перед началом чата settings.survey.askgroup=Позволять посетителю выбирать группу операторов settings.survey.askmail.description=Показать/спрятать поле ввода адреса электронной почты diff --git a/src/messenger/webim/operator/performance.php b/src/messenger/webim/operator/performance.php index 510aa47d..fb5e56f2 100644 --- a/src/messenger/webim/operator/performance.php +++ b/src/messenger/webim/operator/performance.php @@ -29,7 +29,7 @@ $options = array( 'online_timeout', 'updatefrequency_operator', 'updatefrequency_chat', 'max_connections_from_one_host', 'updatefrequency_tracking', 'visitors_limit', 'invitation_lifetime', - 'tracking_lifetime', 'thread_lifetime' ); + 'tracking_lifetime', 'thread_lifetime', 'statistics_aggregation_interval'); $params = array(); foreach ($options as $opt) { @@ -62,6 +62,11 @@ if (isset($_POST['onlinetimeout'])) { $errors[] = getlocal("settings.wrong.threadlifetime"); } + $params['statistics_aggregation_interval'] = getparam('statistics_aggregation_interval'); + if (!is_numeric($params['statistics_aggregation_interval'])) { + $errors[] = wrong_field("settings.statistics_aggregation_interval"); + } + if (Settings::get('enabletracking')) { $params['updatefrequency_tracking'] = getparam('frequencytracking'); @@ -101,6 +106,7 @@ $page['formfrequencyoperator'] = $params['updatefrequency_operator']; $page['formfrequencychat'] = $params['updatefrequency_chat']; $page['formonehostconnections'] = $params['max_connections_from_one_host']; $page['formthreadlifetime'] = $params['thread_lifetime']; +$page['formstatistics_aggregation_interval'] = $params['statistics_aggregation_interval']; if (Settings::get('enabletracking')) { diff --git a/src/messenger/webim/operator/statistics.php b/src/messenger/webim/operator/statistics.php index 3d34daa0..c0da1b03 100644 --- a/src/messenger/webim/operator/statistics.php +++ b/src/messenger/webim/operator/statistics.php @@ -95,6 +95,7 @@ if ($statisticstype == 'bydate') { "FROM {chatthreadstatistics} s " . "WHERE s.date >= :start " . "AND s.date < :end " . + "GROUP BY DATE(FROM_UNIXTIME(date)) " . "ORDER BY s.date DESC", array( ':start' => $start, diff --git a/src/messenger/webim/view/performance.php b/src/messenger/webim/view/performance.php index 8984eeb5..7efd0135 100644 --- a/src/messenger/webim/view/performance.php +++ b/src/messenger/webim/view/performance.php @@ -85,7 +85,16 @@ require_once('inc_errors.php');

- + +
+
+
+ +
+
+
+
+