Add statistics aggregation interval

This commit is contained in:
Dmitriy Simushev 2013-07-03 08:31:36 +00:00
parent dae493b1a8
commit 1a0b315365
7 changed files with 93 additions and 47 deletions

View File

@ -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 */

View File

@ -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
)
);

View File

@ -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

View File

@ -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=Показать/спрятать поле ввода адреса электронной почты

View File

@ -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')) {

View File

@ -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,

View File

@ -86,6 +86,15 @@ require_once('inc_errors.php');
<br clear="all"/>
</div>
<div class="field">
<div class="flabel"><?php echo getlocal('settings.statistics_aggregation_interval') ?></div>
<div class="fvalue">
<input type="text" name="statistics_aggregation_interval" size="40" value="<?php echo form_value('statistics_aggregation_interval') ?>" class="formauth"/>
</div>
<div class="fdescr"> &mdash; <?php echo getlocal('settings.statistics_aggregation_interval.description') ?></div>
<br clear="all"/>
</div>
<?php if ($page['enabletracking']) { ?>
<div class="field">
<div class="flabel"><?php echo getlocal('settings.frequencytracking') ?></div>