public function testGetPostActivityVisualizationData()
 {
     $hot_posts = array((object) array('post_text' => 'First Û Post', 'favlike_count_cache' => 1, 'all_retweets' => 2, 'reply_count_cache' => 3), (object) array('post_text' => 'Second Post', 'favlike_count_cache' => 10, 'all_retweets' => 20, 'reply_count_cache' => 30));
     $result = ChartHelper::getPostActivityVisualizationData($hot_posts, 'twitter');
     $this->assertEqual(gettype($result), 'string');
     $visualization_object = json_decode($result);
     $this->assertEqual(sizeof($visualization_object->rows), 2);
     $this->assertEqual(sizeof($visualization_object->cols), 4);
     $this->assertEqual($visualization_object->cols[0]->label, 'Tweet');
     $this->assertEqual($visualization_object->cols[1]->label, 'Replies');
     $this->assertEqual($visualization_object->cols[2]->label, 'Retweets');
     $this->assertEqual($visualization_object->cols[3]->label, 'Favorites');
     //Different PHP versions handle this differently
     if (version_compare(phpversion(), '5.4', '<')) {
         $this->assertEqual($visualization_object->rows[0]->c[0]->v, 'First  Post...');
     } else {
         $this->assertEqual($visualization_object->rows[0]->c[0]->v, 'First ? Post...');
     }
     $this->assertEqual($visualization_object->rows[0]->c[1]->v, 3);
     $this->assertEqual($visualization_object->rows[0]->c[2]->v, 2);
     $this->assertEqual($visualization_object->rows[0]->c[3]->v, 1);
     $result = ChartHelper::getPostActivityVisualizationData($hot_posts, 'facebook');
     $this->assertEqual(gettype($result), 'string');
     $visualization_object = json_decode($result);
     $this->assertEqual(sizeof($visualization_object->rows), 2);
     $this->assertEqual(sizeof($visualization_object->cols), 4);
     $this->assertEqual($visualization_object->cols[0]->label, 'Post');
     $this->assertEqual($visualization_object->cols[1]->label, 'Comments');
     $this->assertEqual($visualization_object->cols[2]->label, 'Shares');
     $this->assertEqual($visualization_object->cols[3]->label, 'Likes');
     $this->assertEqual($visualization_object->rows[1]->c[0]->v, 'Second Post...');
     $this->assertEqual($visualization_object->rows[1]->c[1]->v, 30);
     $this->assertEqual($visualization_object->rows[1]->c[2]->v, 20);
     $this->assertEqual($visualization_object->rows[1]->c[3]->v, 10);
 }
Example #2
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     if ($instance->network == 'twitter') {
         $day_of_week = 3;
     } elseif ($instance->network == 'instagram') {
         $day_of_week = 5;
     } else {
         $day_of_week = 0;
     }
     $should_generate_insight = self::shouldGenerateWeeklyInsight('weekly_graph', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = $day_of_week, count($last_week_of_posts));
     if ($should_generate_insight) {
         $most_popular_post = null;
         $best_popularity_params = array('index' => 0, 'reply' => 0, 'retweet' => 0, 'like' => 0);
         $total_replies = 0;
         $total_retweets = 0;
         $total_likes = 0;
         $engaged_posts = array();
         //Load photos for Instagram to display whether or not it was a video
         if ($instance->network == 'instagram') {
             $photo_dao = DAOFactory::getDAO('PhotoDAO');
             $last_week_of_posts_with_photos = array();
             foreach ($last_week_of_posts as $post) {
                 $post = $photo_dao->getPhoto($post->post_id, 'instagram');
                 $last_week_of_posts_with_photos[] = $post;
             }
             $last_week_of_posts = $last_week_of_posts_with_photos;
         }
         foreach ($last_week_of_posts as $post) {
             $reply_count = $post->reply_count_cache;
             $retweet_count = $post->retweet_count_cache;
             $fav_count = $post->favlike_count_cache;
             $total_replies += $reply_count;
             $total_retweets += $retweet_count;
             $total_likes += $fav_count;
             if ($instance->network !== 'instagram') {
                 $popularity_index = 5 * $reply_count + 3 * $retweet_count + 2 * $fav_count;
             } else {
                 $popularity_index = 3 * $reply_count + 0 * $retweet_count + 5 * $fav_count;
             }
             if ($popularity_index > $best_popularity_params['index']) {
                 $best_popularity_params['index'] = $popularity_index;
                 $best_popularity_params['reply'] = $reply_count;
                 if ($instance->network !== 'instagram') {
                     $best_popularity_params['retweet'] = $retweet_count;
                 }
                 $best_popularity_params['like'] = $fav_count;
                 $most_popular_post = $post;
             }
             if ($popularity_index > 0) {
                 $post->popularity_index = $popularity_index;
                 $engaged_posts[] = $post;
             }
         }
         if (isset($most_popular_post)) {
             usort($engaged_posts, array($this, 'compareEngagedPosts'));
             $posts = array_slice($engaged_posts, 0, 10);
             if ($total_replies >= $total_likes && $total_replies >= $total_retweets) {
                 $insight_headline = $this->username . " really inspired conversations";
                 $lower = array();
                 if ($total_replies > $total_likes) {
                     $lower[] = $this->terms->getNoun('like', InsightTerms::PLURAL);
                 }
                 if ($total_replies > $total_retweets) {
                     $lower[] = $this->terms->getNoun('retweet', InsightTerms::PLURAL);
                 }
                 if (count($lower) == 0) {
                     $insight_text = $this->username . ' got more ' . $this->terms->getNoun('reply', InsightTerms::PLURAL) . " than anything else.";
                 } else {
                     $insight_text = 'In the past week, ' . $this->terms->getNoun('reply', InsightTerms::PLURAL) . ' to ' . $this->username . ' outnumbered ' . join(' or ', $lower) . '.';
                 }
             } else {
                 if ($total_likes >= $total_replies && $total_likes >= $total_retweets) {
                     if ($instance->network !== 'instagram') {
                         $insight_headline = "Whatever " . $this->username . " said must have been memorable";
                     } else {
                         $insight_headline = $this->username . " really won hearts";
                     }
                     $insight_text = 'In the past week, ' . $this->username . ' got ' . number_format($total_likes) . ' ' . $this->terms->getNoun('like', $total_likes == 1 ? InsightTerms::SINGULAR : InsightTerms::PLURAL);
                     $lower = array();
                     if ($total_likes > $total_replies && $total_replies > 0) {
                         $plural = $total_replies == 1 ? InsightTerms::SINGULAR : InsightTerms::PLURAL;
                         $lower[] = number_format($total_replies) . ' ' . $this->terms->getNoun('reply', $plural);
                     }
                     if ($total_likes > $total_retweets && $total_retweets > 0) {
                         $plural = $total_retweets == 1 ? InsightTerms::SINGULAR : InsightTerms::PLURAL;
                         $lower[] = number_format($total_retweets) . ' ' . $this->terms->getNoun('retweet', $plural);
                     }
                     if (count($lower) == 0) {
                         $insight_text .= '.';
                     } else {
                         $insight_text .= ', beating out ' . join(' and ', $lower) . '.';
                     }
                 } else {
                     $insight_headline = $this->username . " shared lots of things people wanted to amplify";
                     $lower = array();
                     if ($total_retweets > $total_replies) {
                         $lower[] = $this->terms->getNoun('reply', InsightTerms::PLURAL) . ' by ' . number_format($total_retweets - $total_replies);
                     }
                     if ($total_retweets > $total_likes) {
                         $lower[] = $this->terms->getNoun('like', InsightTerms::PLURAL) . ' by ' . number_format($total_retweets - $total_likes);
                     }
                     if (count($lower) > 0) {
                         $insight_text = 'This past week, ' . $this->username . "'s " . $this->terms->getNoun('retweet', InsightTerms::PLURAL) . ' outnumbered ' . join(' and ', $lower) . '.';
                     }
                 }
             }
             //Only show this insight if there's a chart with more than 3 posts on it
             if (count($posts) > 3) {
                 $my_insight = new Insight();
                 $my_insight->slug = 'weekly_graph';
                 $my_insight->instance_id = $instance->id;
                 $my_insight->date = $this->insight_date;
                 $my_insight->headline = $insight_headline;
                 $my_insight->text = $insight_text;
                 $my_insight->header_image = $user->avatar;
                 $my_insight->filename = basename(__FILE__, ".php");
                 $my_insight->emphasis = Insight::EMPHASIS_MED;
                 $formatted_posts = array(ChartHelper::getPostActivityVisualizationData($posts, $instance->network));
                 $my_insight->setPosts($formatted_posts);
                 $this->insight_dao->insertInsight($my_insight);
             } else {
                 $this->logger->logInfo("Not enough posts to display chart", __METHOD__ . ',' . __LINE__);
             }
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
 /**
  * Pre-fetch dashboard module data and store.
  */
 public function cacheDashboardModules()
 {
     $insight_dao = DAOFactory::getDAO('InsightDAO');
     $simplified_date = date('Y-m-d');
     //Cache FollowMySQLDAO::getLeastLikelyFollowersThisWeek
     $follow_dao = DAOFactory::getDAO('FollowDAO');
     $results = $follow_dao->getLeastLikelyFollowersThisWeek($this->instance->network_user_id, $this->instance->network, 13, 1);
     if (isset($results)) {
         //delete existing
         $insight_dao->deleteInsightsBySlug("FollowMySQLDAO::getLeastLikelyFollowersThisWeek", $this->instance->id);
         //insert new
         $insight_dao->insertInsightDeprecated("FollowMySQLDAO::getLeastLikelyFollowersThisWeek", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($results));
     }
     //Cache PostMySQLDAO::getHotPosts
     $post_dao = DAOFactory::getDAO('PostDAO');
     $hot_posts = $post_dao->getHotPosts($this->instance->network_user_id, $this->instance->network, 10);
     if (sizeof($hot_posts) > 3) {
         $hot_posts_data = ChartHelper::getPostActivityVisualizationData($hot_posts, $this->instance->network);
         //delete existing
         //TODO Go back to deleting this existing data once insights stream doesn't reference it
         //$insight_dao->deleteInsightsBySlug("PostMySQLDAO::getHotPosts", $this->instance->id);
         //insert new
         $insight_dao->insertInsightDeprecated("PostMySQLDAO::getHotPosts", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($hot_posts_data));
     }
     //Cache ShortLinkMySQLDAO::getRecentClickStats
     $short_link_dao = DAOFactory::getDAO('ShortLinkDAO');
     $click_stats = $short_link_dao->getRecentClickStats($this->instance, 10);
     if (sizeof($click_stats) > 3) {
         $click_stats_data = self::getClickStatsVisualizationData($click_stats);
         //delete existing
         //TODO Go back to deleting this existing data once insights stream doesn't reference it
         //$insight_dao->deleteInsightsBySlug("ShortLinkMySQLDAO::getRecentClickStats", $this->instance->id);
         //insert new
         $insight_dao->insertInsightDeprecated("ShortLinkMySQLDAO::getRecentClickStats", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($click_stats_data));
     }
     //Cache PostMySQLDAO::getAllPostsByUsernameOrderedBy // getMostRepliedToPostsInLastWeek
     $most_replied_to_1wk = $post_dao->getMostRepliedToPostsInLastWeek($this->instance->network_username, $this->instance->network, 5);
     if (sizeof($most_replied_to_1wk) > 1) {
         //delete existing
         $insight_dao->deleteInsightsBySlug("PostMySQLDAO::getMostRepliedToPostsInLastWeek", $this->instance->id);
         //insert new
         $insight_dao->insertInsightDeprecated("PostMySQLDAO::getMostRepliedToPostsInLastWeek", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($most_replied_to_1wk));
     }
     //Cache PostMySQLDAO::getAllPostsByUsernameOrderedBy // getMostRetweetedPostsInLastWeek
     $most_retweeted_1wk = $post_dao->getMostRetweetedPostsInLastWeek($this->instance->network_username, $this->instance->network, 5);
     if (sizeof($most_retweeted_1wk) > 1) {
         //delete existing
         $insight_dao->deleteInsightsBySlug("PostMySQLDAO::getMostRetweetedPostsInLastWeek", $this->instance->id);
         //insert new
         $insight_dao->insertInsightDeprecated("PostMySQLDAO::getMostRetweetedPostsInLastWeek", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($most_retweeted_1wk));
     }
     //Cache PostMySQLDAO::getClientsUsedByUserOnNetwork
     $clients_usage = $post_dao->getClientsUsedByUserOnNetwork($this->instance->network_user_id, $this->instance->network);
     //delete existing
     $insight_dao->deleteInsightsBySlug("PostMySQLDAO::getClientsUsedByUserOnNetwork", $this->instance->id);
     //insert new
     $insight_dao->insertInsightDeprecated("PostMySQLDAO::getClientsUsedByUserOnNetwork", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($clients_usage));
     //Cache PostMySQLDAO::getOnThisDayFlashbackPosts
     $posts_flashback = $post_dao->getOnThisDayFlashbackPosts($this->instance->network_user_id, $this->instance->network);
     //delete existing
     $insight_dao->deleteInsightsBySlug("PostMySQLDAO::getOnThisDayFlashbackPosts", $this->instance->id);
     //insert new
     $insight_dao->insertInsightDeprecated("PostMySQLDAO::getOnThisDayFlashbackPosts", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($posts_flashback));
     //if it's December or January, cache PostMySQLDAO::getMostPopularPostsOfTheYear
     if (date('n') == 12 || date('n') == 1) {
         if (date('n') == 12) {
             $year = date('Y');
         } else {
             $year = intval(date('Y')) - 1;
         }
         //Cache PostMySQLDAO::getMostPopularPostsOfTheYear
         $posts_yearly_popular = $post_dao->getMostPopularPostsOfTheYear($this->instance->network_user_id, $this->instance->network, $year, 5);
         //delete existing
         $insight_dao->deleteInsightsBySlug("PostMySQLDAO::getMostPopularPostsOfTheYear", $this->instance->id);
         //insert new
         $insight_dao->insertInsightDeprecated("PostMySQLDAO::getMostPopularPostsOfTheYear", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($posts_yearly_popular));
     }
     if ($this->instance->network == 'foursquare') {
         // Cache PostMySQLDAO::countCheckinsToPlaceTypesLastWeek
         $checkins_count = $post_dao->countCheckinsToPlaceTypesLastWeek($this->instance->network_user_id, $this->instance->network);
         //delete existing
         $insight_dao->deleteInsightsBySlug("PostMySQLDAO::countCheckinsToPlaceTypesLastWeek", $this->instance->id);
         //insert new
         $insight_dao->insertInsightDeprecated("PostMySQLDAO::countCheckinsToPlaceTypesLastWeek", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($checkins_count));
         // Cache PostMySQLDAO::countCheckinsToPlaceTypes
         $checkins_all_time_count = $post_dao->countCheckinsToPlaceTypes($this->instance->network_user_id, $this->instance->network);
         //delete existing
         $insight_dao->deleteInsightsBySlug("PostMySQLDAO::countCheckinsToPlaceTypes", $this->instance->id);
         //insert new
         $insight_dao->insertInsightDeprecated("PostMySQLDAO::countCheckinsToPlaceTypes", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($checkins_all_time_count));
         // Cache PostMySQLDAO::getPostsPerHourDataVis
         $hourly_checkin_datavis = $post_dao->getPostsPerHourDataVis($this->instance->network_user_id, $this->instance->network);
         //delete existing
         $insight_dao->deleteInsightsBySlug("PostMySQLDAO::getPostsPerHourDataVis", $this->instance->id);
         //insert new
         $insight_dao->insertInsightDeprecated("PostMySQLDAO::getPostsPerHourDataVis", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($hourly_checkin_datavis));
         // Cache PostMySQLDAO::getAllCheckinsInLastWeekAsGoogleMap
         $checkins_map = $post_dao->getAllCheckinsInLastWeekAsGoogleMap($this->instance->network_user_id, $this->instance->network);
         //delete existing
         $insight_dao->deleteInsightsBySlug("PostMySQLDAO::getAllCheckinsInLastWeekAsGoogleMap", $this->instance->id);
         //insert new
         $insight_dao->insertInsightDeprecated("PostMySQLDAO::getAllCheckinsInLastWeekAsGoogleMap", $this->instance->id, $simplified_date, '', '', 'dashboard', Insight::EMPHASIS_LOW, serialize($checkins_map));
     }
 }