Campustream 1.0
A social network MQP for WPI
|
00001 <? 00002 00003 group('analyze'); 00004 00005 desc('Analyze the social graph'); 00006 task('social', function ($args) { 00007 00008 force_execution_on(any_web_server); 00009 00010 $num = 0; 00011 if (count($args)) { 00012 $num = array_shift($args); 00013 } 00014 00015 echo "Retrieving all users\n"; 00016 $users = ActiveRecord::find_all("User_Model", "SELECT * FROM users"); 00017 00018 if (!count($users)) { 00019 echo "Error retrieving users\n"; 00020 return false; 00021 } 00022 00023 $user_follows_stats = array(); 00024 $user_followers_stats = array(); 00025 $user_posts = array(); 00026 00027 echo "Getting social graph stats\n"; 00028 foreach ($users as $user) { 00029 $user_follows_stats[$user->username] = $user->follows_count(); 00030 $user_followers_stats[$user->username] = $user->followers_count(); 00031 00032 $result = DatabaseManager::readConnection()->query("SELECT COUNT(*) as count FROM statuses WHERE user_id = {$user->id}"); 00033 if ($result) { 00034 $data = $result->fetch_assoc(); 00035 $user_posts[$user->username] = $data['count']; 00036 } else { 00037 $user_posts[$user->username] = 0; 00038 } 00039 } 00040 00041 echo "Sorting results\n"; 00042 arsort($user_follows_stats); 00043 arsort($user_followers_stats); 00044 arsort($user_posts); 00045 00046 if ($num > 0) { 00047 $follows_stats = array_slice($user_follows_stats, 0, $num, true); 00048 $followers_stats = array_slice($user_followers_stats, 0, $num, true); 00049 } else { 00050 $follows_stats = $user_follows_stats; 00051 $followers_stats = $user_followers_stats; 00052 } 00053 00054 echo "##############################\n"; 00055 echo "Follows Stats\n"; 00056 echo "username, posts, follows, followed by\n"; 00057 foreach ($follows_stats as $username => $num) { 00058 echo "$username, {$user_posts[$username]}, $num, {$user_followers_stats[$username]}\n"; 00059 } 00060 00061 echo "##############################\n"; 00062 echo "Follower Stats\n"; 00063 echo "username, posts, follows, followed by\n"; 00064 foreach ($followers_stats as $username => $num) { 00065 echo "$username, {$user_posts[$username]}, {$user_follows_stats[$username]}, $num \n"; 00066 } 00067 00068 echo "##############################\n"; 00069 echo "Post Stats\n"; 00070 echo "username, posts, follows, followed by\n"; 00071 foreach ($user_posts as $username => $num) { 00072 echo "$username, {$num}, {$user_follows_stats[$username]}, {$user_followers_stats[$username]}\n"; 00073 } 00074 }); 00075 00076 desc("Analyze mutual relationships between users"); 00077 task("mutual", function () { 00078 00079 force_execution_on( any_web_server ); 00080 00081 echo "Retrieving all users\n"; 00082 $users = ActiveRecord::find_all("User_Model", "SELECT * FROM users"); 00083 00084 if (!count($users)) { 00085 echo "Error retrieving users\n"; 00086 return false; 00087 } 00088 00089 $data = array(); 00090 $failed = array(); 00091 00092 echo "Analyzing the social graph\n"; 00093 foreach ($users as $user) { 00094 echo "Checking user {$user->username}\n"; 00095 $data[$user->username] = 0; 00096 00097 foreach ($users as $other_user) { 00098 try { 00099 if ($user->is_following($other_user) && $user->is_followed_by($other_user)) { 00100 $data[$user->username]++; 00101 } 00102 } catch (Exception $e) { 00103 echo "Exception caught for {$user->username}, will try again later\n"; 00104 $failed[] = $user; 00105 00106 break; 00107 } 00108 } 00109 } 00110 00111 if (count($failed) > 0) { 00112 echo count($failed) . " users failed, trying again\n"; 00113 foreach ($failed as $user) { 00114 echo "Checking user {$user->username}\n"; 00115 $data[$user->username] = 0; 00116 00117 foreach ($users as $other_user) { 00118 try { 00119 if ($user->is_following($other_user) && $user->is_followed_by($other_user)) { 00120 $data[$user->username]++; 00121 } 00122 } catch (Exception $e) { 00123 echo "Exception caught again for {$user->username}, giving up."; 00124 00125 break; 00126 } 00127 } 00128 } 00129 } 00130 00131 echo "##############################\n"; 00132 echo "Mutual Followers\n"; 00133 echo "username, mutual connections\n"; 00134 foreach ($data as $username => $num) { 00135 echo "$username, $num\n"; 00136 } 00137 00138 });