5 * Description: Generates some statistics for http://pods.jasonrobinson.me/
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
10 function statistics_json_install() {
11 register_hook('cron', 'addon/statistics_json/statistics_json.php', 'statistics_json_cron');
15 function statistics_json_uninstall() {
16 unregister_hook('cron', 'addon/statistics_json/statistics_json.php', 'statistics_json_cron');
19 function statistics_json_module() {}
21 function statistics_json_init() {
25 "name" => $a->config["sitename"],
26 "network" => FRIENDICA_PLATFORM,
27 "version" => FRIENDICA_VERSION,
28 "registrations_open" => ($a->config['register_policy'] != 0),
29 "total_users" => get_config('statistics_json','total_users'),
30 "active_users_halfyear" => get_config('statistics_json','active_users_halfyear'),
31 "active_users_monthly" => get_config('statistics_json','active_users_monthly'),
32 "local_posts" => get_config('statistics_json','local_posts')
35 header("Content-Type: application/json");
36 echo json_encode($statistics);
37 logger("statistics_init: printed ".print_r($statistics, true));
41 function statistics_json_cron($a,$b) {
42 $last = get_config('statistics_json','last_calucation');
45 // Calculate all 3 hours
46 $next = $last + (180 * 60);
48 logger('statistics_json_cron: calculation intervall not reached');
52 logger('statistics_json_cron: cron_start');
55 $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
56 FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
58 WHERE `item`.`type` = 'wall'
59 GROUP BY `item`.`uid`) AS `lastitem`
60 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
62 `user`.`uid` = `contact`.`uid` AND `profile`.`uid` = `user`.`uid`
63 AND `profile`.`is-default` AND (`profile`.`publish` OR `profile`.`net-publish`)
64 AND `user`.`verified` AND `contact`.`self`
65 AND NOT `user`.`blocked`
66 AND NOT `user`.`account_removed`
67 AND NOT `user`.`account_expired`");
69 if (is_array($users)) {
70 $total_users = count($users);
71 $active_users_halfyear = 0;
72 $active_users_monthly = 0;
74 $halfyear = time() - (180 * 24 * 60 * 60);
75 $month = time() - (30 * 24 * 60 * 60);
77 foreach ($users AS $user) {
78 if ((strtotime($user['login_date']) > $halfyear) OR
79 (strtotime($user['lastitem_date']) > $halfyear))
80 ++$active_users_halfyear;
82 if ((strtotime($user['login_date']) > $month) OR
83 (strtotime($user['lastitem_date']) > $month))
84 ++$active_users_monthly;
87 set_config('statistics_json','total_users', $total_users);
88 logger('statistics_json_cron: total_users: '.$total_users, LOGGER_DEBUG);
90 set_config('statistics_json','active_users_halfyear', $active_users_halfyear);
91 set_config('statistics_json','active_users_monthly', $active_users_monthly);
94 $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` left(body, 6) != '[share'");
96 if (!is_array($posts))
99 $local_posts = $posts[0]["local_posts"];
101 set_config('statistics_json','local_posts', $local_posts);
103 logger('statistics_json_cron: local_posts: '.$local_posts, LOGGER_DEBUG);
105 logger('statistics_json_cron: cron_end');
106 set_config('statistics_json','last_calucation', time());