From c8039ec2482523f20ea56eedd144e01693140010 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Tue, 7 May 2013 10:35:38 +0000 Subject: [PATCH] Calculate page statistics by cron --- src/messenger/webim/cron.php | 1 + src/messenger/webim/install/dbinfo.php | 6 +- .../webim/libs/classes/users_processor.php | 3 +- src/messenger/webim/libs/cron.php | 81 +++++++++++++++++++ src/messenger/webim/libs/track.php | 15 ++-- src/messenger/webim/operator/statistics.php | 11 ++- 6 files changed, 101 insertions(+), 16 deletions(-) diff --git a/src/messenger/webim/cron.php b/src/messenger/webim/cron.php index af5dbdf9..2018457a 100644 --- a/src/messenger/webim/cron.php +++ b/src/messenger/webim/cron.php @@ -18,6 +18,7 @@ // Initialize librariess require_once('libs/init.php'); require_once('libs/classes/thread.php'); +require_once('libs/track.php'); require_once('libs/cron.php'); $cron_key = empty($_GET['cron_key']) ? '' : $_GET['cron_key']; diff --git a/src/messenger/webim/install/dbinfo.php b/src/messenger/webim/install/dbinfo.php index 57771e8a..16105282 100644 --- a/src/messenger/webim/install/dbinfo.php +++ b/src/messenger/webim/install/dbinfo.php @@ -186,12 +186,16 @@ $dbtables = array( "address" => "varchar(1024)", "visittime" => "int NOT NULL DEFAULT 0", "visitorid" => "INT", + // Indicates if path included in 'by page' statistics + "calculated" => "tinyint NOT NULL DEFAULT 0" ), "${mysqlprefix}visitedpagestatistics" => array( "pageid" => "INT NOT NULL auto_increment PRIMARY KEY", + "date" => "int NOT NULL DEFAULT 0", "address" => "varchar(1024)", - "visittime" => "int NOT NULL DEFAULT 0" + "visits" => "int NOT NULL DEFAULT 0", + "chats" => "int NOT NULL DEFAULT 0" ), ); diff --git a/src/messenger/webim/libs/classes/users_processor.php b/src/messenger/webim/libs/classes/users_processor.php index 56740a9d..197b245f 100644 --- a/src/messenger/webim/libs/classes/users_processor.php +++ b/src/messenger/webim/libs/classes/users_processor.php @@ -360,9 +360,8 @@ class UsersProcessor extends ClientSideProcessor { // Close old invitations invitation_close_old(); - // Remove old visitors and visitors tracks + // Remove old visitors track_remove_old_visitors(); - track_remove_old_tracks(); // Get instance of event dispatcher $dispatcher = EventDispatcher::getInstance(); diff --git a/src/messenger/webim/libs/cron.php b/src/messenger/webim/libs/cron.php index 6f206fa9..2d758b9d 100644 --- a/src/messenger/webim/libs/cron.php +++ b/src/messenger/webim/libs/cron.php @@ -305,6 +305,87 @@ function cron_calculate_statistics() { ':start' => $start ) ); + + // Build 'by page' statistics + $visited_pages = $db->query( + "SELECT FLOOR(p.visittime / (24*60*60)) * 24*60*60 AS date, " . + "p.address AS address, " . + // 'visittimes' is not calculated pages count. It means that + // 'visittimes' is count of NEW visited pages, not total count. + "COUNT(DISTINCT p.pageid) AS visittimes, " . + // 'chattimes' is total count of threads related with a page + // address, not a visited page row. It means that 'chattimes' is + // TOTAL chats count from this page, not only new. + "COUNT(DISTINCT t.threadid) AS chattimes " . + "FROM {visitedpage} p " . + "LEFT OUTER JOIN {chatthread} t ON (" . + "p.address = t.referer " . + "AND DATE(FROM_UNIXTIME(p.visittime)) = " . + "DATE(FROM_UNIXTIME(t.dtmcreated))) " . + "WHERE p.calculated = 0 " . + "GROUP BY date, address " . + "ORDER BY date", + array(), + array('return_rows' => Database::RETURN_ALL_ROWS) + ); + + foreach($visited_pages as $visited_page) { + // Check is there statistics for current visited page in database. + $count_result = $db->query( + "SELECT COUNT(*) AS count " . + "FROM {visitedpagestatistics} " . + "WHERE date = :date AND address = :address", + array( + ':date' => $visited_page['date'], + ':address' => $visited_page['address'] + ), + array('return_rows' => Database::RETURN_ONE_ROW) + ); + + if (! empty($count_result['count'])) { + // Stat already in database. Update it. + $db->query( + "UPDATE {visitedpagestatistics} SET " . + "visits = visits + :visits, " . + // Do not add chat because of it is total count of chats + // related with this page. + // TODO: Think about old threads removing. In current + // configuration it can cause problems with wrong + // 'by page' statistics. + "chats = :chats " . + "WHERE date = :date " . + "AND address = :address " . + "LIMIT 1", + array( + ':date' => $visited_page['date'], + ':address' => $visited_page['address'], + ':visits' => $visited_page['visittimes'], + ':chats' => $visited_page['chattimes'] + ) + ); + } else { + // Create stat row in database. + $db->query( + "INSERT INTO {visitedpagestatistics} (" . + "date, address, visits, chats" . + ") VALUES ( " . + ":date, :address, :visits, :chats" . + ")", + array( + ':date' => $visited_page['date'], + ':address' => $visited_page['address'], + ':visits' => $visited_page['visittimes'], + ':chats' => $visited_page['chattimes'] + ) + ); + } + } + + // Mark all visited pages as 'calculated' + $db->query("UPDATE {visitedpage} SET calculated = 1"); + + // Remove old tracks from the system + track_remove_old_tracks(); } catch(Exception $e) { // Something went wrong: warn and rollback transaction. trigger_error( diff --git a/src/messenger/webim/libs/track.php b/src/messenger/webim/libs/track.php index d3405f38..ef25cdde 100644 --- a/src/messenger/webim/libs/track.php +++ b/src/messenger/webim/libs/track.php @@ -123,14 +123,6 @@ function track_visit_page($visitorid, $page) ':now' => time() ) ); - $db->query( - "insert into {visitedpagestatistics} (address, visittime) " . - "values (:page, :now)", - array( - ':page' => $page, - ':now' => time() - ) - ); } } @@ -202,8 +194,11 @@ function track_remove_old_tracks() { // Remove old visitors' tracks $db->query( - "DELETE FROM {visitedpage} WHERE (:now - visittime) > :lifetime " . - " AND visitorid NOT IN (SELECT visitorid FROM {chatsitevisitor})", + "DELETE FROM {visitedpage} " . + "WHERE (:now - visittime) > :lifetime " . + // Remove only tracks that are included in statistics + "AND calculated = 1 " . + "AND visitorid NOT IN (SELECT visitorid FROM {chatsitevisitor}) ", array( ':lifetime' => Settings::get('tracking_lifetime'), ':now' => time() diff --git a/src/messenger/webim/operator/statistics.php b/src/messenger/webim/operator/statistics.php index 07c1ee52..f0b07999 100644 --- a/src/messenger/webim/operator/statistics.php +++ b/src/messenger/webim/operator/statistics.php @@ -137,12 +137,17 @@ if ($statisticstype == 'bydate') { $activetab = 1; } elseif($statisticstype == 'bypage') { $page['reportByPage'] = $db->query( - "SELECT COUNT(DISTINCT p.pageid) as visittimes, p.address, COUNT(DISTINCT t.threadid) as chattimes " . - "FROM {visitedpagestatistics} p LEFT OUTER JOIN {chatthread} t ON (p.address = t.referer AND DATE(FROM_UNIXTIME(p.visittime)) = DATE(FROM_UNIXTIME(t.dtmcreated))) " . - "WHERE p.visittime >= :start AND p.visittime < :end GROUP BY p.address", + "SELECT SUM(visits) as visittimes, " . + "address, " . + "SUM(chats) as chattimes " . + "FROM {visitedpagestatistics} " . + "WHERE date >= :start " . + "AND date < :end " . + "GROUP BY address", array(':start' => $start, ':end' => $end), array('return_rows' => Database::RETURN_ALL_ROWS) ); + $page['noresults'] = empty($page['reportByPage']); $activetab = 2; } $page['showresults'] = count($errors) == 0;