Calculate page statistics by cron

This commit is contained in:
Dmitriy Simushev 2013-05-07 10:35:38 +00:00
parent b337c689b9
commit c8039ec248
6 changed files with 101 additions and 16 deletions

View File

@ -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'];

View File

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

View File

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

View File

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

View File

@ -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,7 +194,10 @@ function track_remove_old_tracks() {
// Remove old visitors' tracks
$db->query(
"DELETE FROM {visitedpage} WHERE (:now - visittime) > :lifetime " .
"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'),

View File

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