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 "version" => FRIENDICA_VERSION,
27 "registrations_open" => ($a->config['register_policy'] != 0),
28 "total_users" => get_config('statistics_json','total_users'),
29 "active_users_halfyear" => get_config('statistics_json','active_users_halfyear'),
30 "active_users_monthly" => get_config('statistics_json','active_users_monthly'),
31 "local_posts" => get_config('statistics_json','local_posts')
34 header("Content-Type: application/json");
35 echo json_encode($statistics);
36 logger("statistics_init: printed ".print_r($statistics, true));
40 function statistics_json_cron($a,$b) {
41 $last = get_config('statistics_json','last_calucation');
44 // Calculate all 3 hours
45 $next = $last + (180 * 60);
47 logger('statistics_json_cron: calculation intervall not reached');
51 logger('statistics_json_cron: cron_start');
54 $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
55 FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
57 WHERE `item`.`type` = 'wall'
58 GROUP BY `item`.`uid`) AS `lastitem`
59 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
61 `user`.`uid` = `contact`.`uid` AND `profile`.`uid` = `user`.`uid`
62 AND `profile`.`is-default` AND (`profile`.`publish` OR `profile`.`net-publish`)
63 AND `user`.`verified` AND `contact`.`self`
64 AND NOT `user`.`blocked`
65 AND NOT `user`.`account_removed`
66 AND NOT `user`.`account_expired`");
68 if (is_array($users)) {
69 $total_users = count($users);
70 $active_users_halfyear = 0;
71 $active_users_monthly = 0;
73 $halfyear = time() - (180 * 24 * 60 * 60);
74 $month = time() - (30 * 24 * 60 * 60);
76 foreach ($users AS $user) {
77 if ((strtotime($user['login_date']) > $halfyear) OR
78 (strtotime($user['lastitem_date']) > $halfyear))
79 ++$active_users_halfyear;
81 if ((strtotime($user['login_date']) > $month) OR
82 (strtotime($user['lastitem_date']) > $month))
83 ++$active_users_monthly;
86 set_config('statistics_json','total_users', $total_users);
87 logger('statistics_json_cron: total_users: '.$total_users, LOGGER_DEBUG);
89 set_config('statistics_json','active_users_halfyear', $active_users_halfyear);
90 set_config('statistics_json','active_users_monthly', $active_users_monthly);
93 $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND id=parent");
95 if (!is_array($posts))
98 $local_posts = $posts[0]["local_posts"];
100 set_config('statistics_json','local_posts', $local_posts);
102 logger('statistics_json_cron: local_posts: '.$local_posts, LOGGER_DEBUG);
104 logger('statistics_json_cron: cron_end');
105 set_config('statistics_json','last_calucation', time());