Merge pull request #179 from annando/master

Moving the calculation for statistics to cron/vanishing links for twitter and statusnet
This commit is contained in:
Tobias Diekershoff 2014-02-02 01:49:21 -08:00
commit 14383ab2dd
3 changed files with 51 additions and 21 deletions

View file

@ -8,10 +8,12 @@
*/ */
function statistics_json_install() { function statistics_json_install() {
register_hook('cron', 'addon/statistics_json/statistics_json.php', 'statistics_json_cron');
} }
function statistics_json_uninstall() { function statistics_json_uninstall() {
unregister_hook('cron', 'addon/statistics_json/statistics_json.php', 'statistics_json_cron');
} }
function statistics_json_module() {} function statistics_json_module() {}
@ -23,8 +25,32 @@ function statistics_json_init() {
"name" => $a->config["sitename"], "name" => $a->config["sitename"],
"version" => FRIENDICA_VERSION, "version" => FRIENDICA_VERSION,
"registrations_open" => ($a->config['register_policy'] != 0), "registrations_open" => ($a->config['register_policy'] != 0),
"total_users" => get_config('statistics_json','total_users'),
"active_users_halfyear" => get_config('statistics_json','active_users_halfyear'),
"active_users_monthly" => get_config('statistics_json','active_users_monthly'),
"local_posts" => get_config('statistics_json','local_posts')
); );
header("Content-Type: application/json");
echo json_encode($statistics);
logger("statistics_init: printed ".print_r($statistics, true));
killme();
}
function statistics_json_cron($a,$b) {
$last = get_config('statistics_json','last_calucation');
if($last) {
// Calculate all 3 hours
$next = $last + (180 * 60);
if($next > time()) {
logger('statistics_json_cron: calculation intervall not reached');
return;
}
}
logger('statistics_json_cron: cron_start');
$users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date` $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid` FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
FROM `item` FROM `item`
@ -39,14 +65,10 @@ function statistics_json_init() {
AND NOT `user`.`account_removed` AND NOT `user`.`account_removed`
AND NOT `user`.`account_expired`"); AND NOT `user`.`account_expired`");
if (!is_array($users)) { if (is_array($users)) {
$statistics["total_users"] = -1; $total_users = count($users);
$statistics["active_users_halfyear"] = -1; $active_users_halfyear = 0;
$statistics["active_users_monthly"] = -1; $active_users_monthly = 0;
} else {
$statistics["total_users"] = count($users);
$statistics["active_users_halfyear"] = 0;
$statistics["active_users_monthly"] = 0;
$halfyear = time() - (180 * 24 * 60 * 60); $halfyear = time() - (180 * 24 * 60 * 60);
$month = time() - (30 * 24 * 60 * 60); $month = time() - (30 * 24 * 60 * 60);
@ -54,23 +76,31 @@ function statistics_json_init() {
foreach ($users AS $user) { foreach ($users AS $user) {
if ((strtotime($user['login_date']) > $halfyear) OR if ((strtotime($user['login_date']) > $halfyear) OR
(strtotime($user['lastitem_date']) > $halfyear)) (strtotime($user['lastitem_date']) > $halfyear))
++$statistics["active_users_halfyear"]; ++$active_users_halfyear;
if ((strtotime($user['login_date']) > $month) OR if ((strtotime($user['login_date']) > $month) OR
(strtotime($user['lastitem_date']) > $month)) (strtotime($user['lastitem_date']) > $month))
++$statistics["active_users_monthly"]; ++$active_users_monthly;
} }
set_config('statistics_json','total_users', $total_users);
logger('statistics_json_cron: total_users: '.$total_users, LOGGER_DEBUG);
set_config('statistics_json','active_users_halfyear', $active_users_halfyear);
set_config('statistics_json','active_users_monthly', $active_users_monthly);
} }
$posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall`"); $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND id=parent");
if (!is_array($posts)) if (!is_array($posts))
$statistics["local_posts"] = -1; $local_posts = -1;
else else
$statistics["local_posts"] = $posts[0]["local_posts"]; $local_posts = $posts[0]["local_posts"];
header("Content-Type: application/json"); set_config('statistics_json','local_posts', $local_posts);
echo json_encode($statistics);
logger("statistics_init: printed ".print_r($statistics, true)); logger('statistics_json_cron: local_posts: '.$local_posts, LOGGER_DEBUG);
killme();
logger('statistics_json_cron: cron_end');
set_config('statistics_json','last_calucation', time());
} }

View file

@ -502,7 +502,7 @@ function statusnet_shortenmsg($b, $max_char) {
//$body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body); //$body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body);
// At first convert the text to html // At first convert the text to html
$html = bbcode(api_clean_plain_items($body), false, false, 2, true); $html = bbcode(api_clean_plain_items($body), false, false, 2);
// Then convert it to plain text // Then convert it to plain text
//$msg = trim($b['title']." \n\n".html2plain($html, 0, true)); //$msg = trim($b['title']." \n\n".html2plain($html, 0, true));

View file

@ -428,7 +428,7 @@ function twitter_shortenmsg($b, $shortlink = false) {
//$body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body); //$body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body);
// At first convert the text to html // At first convert the text to html
$html = bbcode(api_clean_plain_items($body), false, false, 2, true); $html = bbcode(api_clean_plain_items($body), false, false, 2);
// Then convert it to plain text // Then convert it to plain text
$msg = trim(html2plain($html, 0, true)); $msg = trim(html2plain($html, 0, true));