public function generateInsight(Instance $instance, $last_week_of_posts, $number_days) { parent::generateInsight($instance, $last_week_of_posts, $number_days); $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); if (self::shouldGenerateInsight('response_time', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = 5, count($last_week_of_posts))) { $response_count = array('reply' => 0, 'retweet' => 0, 'like' => 0); 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; $response_count['reply'] += $reply_count; $response_count['retweet'] += $retweet_count; $response_count['like'] += $fav_count; } arsort($response_count); $response_factor = each($response_count); if ($response_factor['value']) { $insight_baseline_dao = DAOFactory::getDAO('InsightBaselineDAO'); foreach ($response_count as $key => $value) { $insight_baseline_dao->insertInsightBaseline('response_count_' . $key, $instance->id, $value, $this->insight_date); } $time_per_response = floor(60 * 60 * 24 * 7 / $response_factor['value']); $time_str = strncmp(InsightTerms::getSyntacticTimeDifference($time_per_response), "1 ", 2) == 0 ? substr(InsightTerms::getSyntacticTimeDifference($time_per_response), 2) : InsightTerms::getSyntacticTimeDifference($time_per_response); $insight_text = $this->username . "'s " . $this->terms->getNoun('post', InsightTerms::PLURAL) . " averaged <strong>1 new " . $this->terms->getNoun($response_factor['key']) . "</strong> every <strong>" . $time_str . "</strong> over the last week"; $last_fri = date('Y-m-d', strtotime('-7 day')); $last_fri_insight_baseline = $insight_baseline_dao->getInsightBaseline('response_count_' . $response_factor['key'], $instance->id, $last_fri); if (isset($last_fri_insight_baseline) && $last_fri_insight_baseline->value > 0) { $last_fri_time_per_response = floor(60 * 60 * 24 * 7 / $last_fri_insight_baseline->value); $time_str1 = strncmp(InsightTerms::getSyntacticTimeDifference($last_fri_time_per_response), "1 ", 2) == 0 ? substr(InsightTerms::getSyntacticTimeDifference($last_fri_time_per_response), 2) : InsightTerms::getSyntacticTimeDifference($last_fri_time_per_response); if ($last_fri_time_per_response < $time_per_response) { $insight_text .= ", slower than the previous week's average of 1 " . $this->terms->getNoun($response_factor['key']) . " every " . $time_str1; } elseif ($last_fri_time_per_response > $time_per_response) { $insight_text .= ", faster than the previous week's average of 1 " . $this->terms->getNoun($response_factor['key']) . " every " . $time_str1; } } $insight_text .= '.'; $this->insight_dao->insertInsightDeprecated("response_time", $instance->id, $this->insight_date, "Response time:", $insight_text, basename(__FILE__, ".php"), Insight::EMPHASIS_LOW); } } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }
/** * Dump rendered insight for stream and email, plus the email subject line and optional message. * @param Insight $insight * @param Instance $instance * @param str $message default null * @return void */ protected function dumpRenderedInsight(Insight $insight, Instance $instance, $message = null) { if (isset($message)) { $this->debug("<h4 style=\"text-align: center; margin-top: 20px;\">{$message}</h4>"); } $terms = new InsightTerms($instance->network); $email_subject_line = $terms->swapInSecondPerson($instance->network_username, $insight->headline); $this->debug("<h4 style=\"text-align: center; margin-top: 20px;\">Email Subject: " . $email_subject_line . "</h4>"); $this->debug($this->getRenderedInsightInHTML($insight)); $this->debug($this->getRenderedInsightInEmail($insight)); }
public function testResponseTimeInsightForFoursquarePriorSmallerBaseline() { // Get data ready that insight requires $instance = new Instance(); $instance->id = 10; $instance->network_username = '******'; $instance->network = 'foursquare'; $posts = array(); $posts[] = new Post(array('reply_count_cache' => 13, 'retweet_count_cache' => 1, 'favlike_count_cache' => 3)); $posts[] = new Post(array('reply_count_cache' => 2, 'retweet_count_cache' => 5, 'favlike_count_cache' => 7)); $posts[] = new Post(array('reply_count_cache' => 2, 'retweet_count_cache' => 5, 'favlike_count_cache' => 1)); // Add a baseline from prior week $last_week = date('Y-m-d', strtotime('-7 day')); $builder = FixtureBuilder::build('insight_baselines', array('date' => $last_week, 'slug' => 'response_count_reply', 'instance_id' => 10, 'value' => 12)); // Calculate time for each new favorite $time_per_response = InsightTerms::getSyntacticTimeDifference(floor(60 * 60 * 24 * 7 / 17)); $last_week_time_per_response = InsightTerms::getSyntacticTimeDifference(floor(60 * 60 * 24 * 7 / 12)); $insight_plugin = new ResponseTimeInsight(); $insight_plugin->generateInsight($instance, null, $posts, 3); // Assert that insight got inserted $insight_dao = new InsightMySQLDAO(); $today = date('Y-m-d'); $result = $insight_dao->getInsight('response_time', 10, $today); $this->debug(Utils::varDumpToString($result)); $this->assertNotNull($result); $this->assertIsA($result, "Insight"); $this->assertPattern('/testeriffic\'s checkins averaged <strong>1 new comment/', $result->headline); $this->assertPattern('/every <strong>' . $time_per_response . '<\\/strong>/', $result->headline); $this->assertPattern('/That\'s faster than the previous week\'s average/', $result->text); $this->assertPattern('/of 1 comment every ' . $last_week_time_per_response . './', $result->text); $this->debug($this->getRenderedInsightInHTML($result)); $this->debug($this->getRenderedInsightInEmail($result)); }
/** * Take an array of strings, pick one at random and substitute each token with a value. * Text is processed with InsightTerms::getProcessedText() * The normal usage would be to pass a list of string choices for an Insight field, such as text, headline, etc. * * @param array $copy_array Array of possible strings * @param array $substitutions Text replacement token/value pairs passed to getProccessedText(), in the form of * '%token'=>'value'. See also: InsightTerms::getProcessedText * @return str The chosen and processed array */ public function getVariableCopy($copy_array, $substitutions = array()) { $substitutions['username'] = $this->username; $choice = $copy_array[TimeHelper::getTime() % count($copy_array)]; return $this->terms->getProcessedText($choice, $substitutions); }
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 = 5; } elseif ($instance->network == 'instagram') { $day_of_week = 1; } else { $day_of_week = 4; } $should_generate_insight = self::shouldGenerateWeeklyInsight('response_time', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = $day_of_week, count($last_week_of_posts)); if ($should_generate_insight) { $response_count = array('reply' => 0, 'retweet' => 0, 'like' => 0); 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; $response_count['reply'] += $reply_count; $response_count['retweet'] += $retweet_count; $response_count['like'] += $fav_count; } arsort($response_count); $response_factor = each($response_count); if ($response_factor['value']) { $insight_baseline_dao = DAOFactory::getDAO('InsightBaselineDAO'); foreach ($response_count as $key => $value) { $insight_baseline_dao->insertInsightBaseline('response_count_' . $key, $instance->id, $value, $this->insight_date); } $time_per_response = floor(60 * 60 * 24 * 7 / $response_factor['value']); $time_str = strncmp(InsightTerms::getSyntacticTimeDifference($time_per_response), "1 ", 2) == 0 ? substr(InsightTerms::getSyntacticTimeDifference($time_per_response), 2) : InsightTerms::getSyntacticTimeDifference($time_per_response); if ($instance->network !== 'instagram') { $headline = $this->username . "'s " . $this->terms->getNoun('post', InsightTerms::PLURAL) . " averaged <strong>1 new " . $this->terms->getNoun($response_factor['key']) . "</strong> every <strong>" . $time_str . "</strong> this week"; } else { //Instagram: be photo and video inclusive $headline = $this->username . "'s Instagram posts" . " averaged <strong>1 new " . $this->terms->getNoun($response_factor['key']) . "</strong> every <strong>" . $time_str . "</strong> this week"; } $last_fri = date('Y-m-d', strtotime('-7 day')); $last_fri_insight_baseline = $insight_baseline_dao->getInsightBaseline('response_count_' . $response_factor['key'], $instance->id, $last_fri); if (isset($last_fri_insight_baseline) && $last_fri_insight_baseline->value > 0) { $last_fri_time_per_response = floor(60 * 60 * 24 * 7 / $last_fri_insight_baseline->value); $time_str1 = strncmp(InsightTerms::getSyntacticTimeDifference($last_fri_time_per_response), "1 ", 2) == 0 ? substr(InsightTerms::getSyntacticTimeDifference($last_fri_time_per_response), 2) : InsightTerms::getSyntacticTimeDifference($last_fri_time_per_response); $tachy_markup = "<i class=\"fa fa-tachometer fa-3x text-muted\" style=\"float: right; " . "color: #ddd;\"></i> That's "; // Only show a comparison string if the rates are substantially different if ($last_fri_time_per_response < $time_per_response && $time_str1 != $time_str) { $insight_text .= $tachy_markup . "slower than the previous week's average of 1 " . $this->terms->getNoun($response_factor['key']) . " every " . $time_str1 . "."; } elseif ($last_fri_time_per_response > $time_per_response && $time_str1 != $time_str) { $insight_text .= $tachy_markup . "faster than the previous week's average of 1 " . $this->terms->getNoun($response_factor['key']) . " every " . $time_str1 . "."; } } if (!isset($insight_text)) { $options = array(); $options[] = 'If you %posted once every waking hour, that would be roughly 120 times a week.'; if (strstr($time_str, 'hour') !== false) { $options[] = "For comparison, the average smartphone owner unlocks their phone" . " 7 times each waking hour."; } if ($instance->network == 'twitter' && $response_factor['key'] != 'like') { $options[] = "That's a healthy share of the 21 million tweets each hour."; } if (strstr($time_str, 'day') !== false) { $options[] = "The average person sneezes 4 times or less each day, just for reference."; } $insight_text = $this->getVariableCopy($options); } //Instantiate the Insight object $my_insight = new Insight(); //REQUIRED: Set the insight's required attributes $my_insight->instance_id = $instance->id; $my_insight->slug = 'response_time'; //slug to label this insight's content $my_insight->date = $this->insight_date; //date of the data this insight applies to $my_insight->headline = $headline; $my_insight->text = $insight_text; $my_insight->header_image = ''; $my_insight->emphasis = Insight::EMPHASIS_LOW; //Set emphasis optionally $my_insight->filename = basename(__FILE__, ".php"); //Same for every insight $this->insight_dao->insertInsight($my_insight); } } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }
public function testSwapInSecondPerson() { $terms = new InsightTerms('twitter'); $username = '******'; $text = "2 interesting people followed @buffysummers."; $new_text = "2 interesting people followed you."; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "@buffysummers has it good"; $new_text = "You have it good"; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $terms = new InsightTerms('facebook'); $username = '******'; $text = "Buffy Summers's status update got 17 comments."; $new_text = "Your status update got 17 comments."; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "Looks like it will be 8 weeks before Willow Rosenberg reaches 100,000 followers."; $new_text = "Looks like it will be 8 weeks before you reach 100,000 followers."; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "Willow Rosenberg reaches 100,000 followers in 8 weeks."; $new_text = "You reach 100,000 followers in 8 weeks."; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "Hey, did you see that Xander Harris followed Willow Rosenberg?"; $new_text = "Hey, did you see that Xander Harris followed you?"; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "Where in the world is Willow Rosenberg?"; $new_text = "Where in the world are you?"; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "Is Willow Rosenberg the best?"; $new_text = "Are you the best?"; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "Willow Rosenberg hasn't replied to Andre Durand in over a year."; $new_text = "You haven't replied to Andre Durand in over a year."; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "That's why Willow Rosenberg hasn't replied to Andre Durand in over a year."; $new_text = "That's why you haven't replied to Andre Durand in over a year."; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "5 people thought Willow Rosenberg was worth retweeting"; $new_text = "5 people thought you were worth retweeting"; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "Willow Rosenberg was on fire this week!"; $new_text = "You were on fire this week!"; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); $username = '******'; $text = "Congratulations on the congrats, Willow Rosenberg!"; $new_text = "Congratulations on the congrats!"; $result = $terms->swapInSecondPerson($username, $text); $this->assertEqual($result, $new_text); }
/** * Return email subject line based on the insight headline of a high or medium insight (converted to second person). * If neither exist, use generic headline text. * @param str $daily_or_weekly "Daily" or "Weekly" * @param arr $insights Insight objects * @return str */ public function getEmailMessageSubjectLine($daily_or_weekly, $insights) { $num_insights = count($insights); $insight_headline_subject = null; // Use a HIGH emphasis insight headline as the email subject line foreach ($insights as $insight) { if ($insight->emphasis == Insight::EMPHASIS_HIGH) { $terms = new InsightTerms($insight->instance->network); $insight_headline_subject = $terms->swapInSecondPerson($insight->instance->network_username, strip_tags(html_entity_decode($insight->headline, ENT_NOQUOTES, 'UTF-8'))); break; } } // If no HIGH insights existed, check medium if (!isset($insight_headline_subject)) { foreach ($insights as $insight) { if ($insight->emphasis == Insight::EMPHASIS_MED) { $terms = new InsightTerms($insight->instance->network); $insight_headline_subject = $terms->swapInSecondPerson($insight->instance->network_username, strip_tags(html_entity_decode($insight->headline, ENT_NOQUOTES, 'UTF-8'))); break; } } } // If neither high nor medium are available, use a generic headline if (!isset($insight_headline_subject)) { if ($daily_or_weekly == "Daily") { $subject_line_choices = array("ThinkUp has new insights for you! Take a look", "You have new insights from ThinkUp", "Your new insights from ThinkUp", "New ThinkUp insights are ready for you", "These are your latest ThinkUp insights", "A few new ThinkUp insights for you", "New ThinkUp insights are waiting for you", "ThinkUp: Today's insights", "These are your ThinkUp insights for " . date('l', $this->current_timestamp)); if ($num_insights > 1) { $subject_line_choices[] = "ThinkUp found %total insights for you today. Here's a look."; $subject_line_choices[] = "You have %total new insights from ThinkUp"; } } else { $subject_line_choices = array("This week was great! ThinkUp's got details", "How did you do online this week? Here are your ThinkUp insights", "Your ThinkUp insights this week", "New ThinkUp insights are ready for you", "This week's ThinkUp insights"); } $rand_index = TimeHelper::getTime() % count($subject_line_choices); $subject = $subject_line_choices[$rand_index]; $subject = str_replace('%total', number_format($num_insights), $subject); } else { $subject = $insight_headline_subject; } return $subject; }
public function generateInsight(Instance $instance, $last_week_of_posts, $number_days) { parent::generateInsight($instance, $last_week_of_posts, $number_days); $video_dao = DAOFactory::getDAO('VideoDAO'); $baseline_dao = DAOFactory::getDAO('InsightBaselineDAO'); self::generateBaselines($instance); $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); $filename = basename(__FILE__, ".php"); foreach ($last_week_of_posts as $post) { $simplified_post_date = date('Y-m-d', strtotime($post->pub_date)); if ($post->network == 'youtube') { $video = $video_dao->getVideoByID($post->post_id, 'youtube'); $average_mins_viewed_month = $baseline_dao->getInsightBaseline('avg_minutes_viewed_month', $instance->id, date('Y-m-d')); $average_mins_viewed_90 = $baseline_dao->getInsightBaseline('avg_minutes_viewed_90', $instance->id, date('Y-m-d')); $average_mins_viewed_all_time = $baseline_dao->getInsightBaseline('avg_minutes_viewed_all_time', $instance->id, date('Y-m-d')); $max_mins_viewed = $baseline_dao->getInsightBaseline('all_time_mins_viewed_high', $instance->id, date('Y-m-d')); $year_mins_viewed = $baseline_dao->getInsightBaseline('year_mins_viewed_high', $instance->id, date('Y-m-d')); $ninety_mins_viewed = $baseline_dao->getInsightBaseline('90_mins_viewed_high', $instance->id, date('Y-m-d')); $hot_videos = $video_dao->getHotVideos($instance->network_username, 'youtube', 10, 'minutes_watched', 'Minutes Watched'); $chart = VideoMySQLDAO::getHotVideosVisualizationData($hot_videos, 'Minutes Watched'); } else { break; } $text = "Viewers watched "; $text .= "<a href=http://plus.google.com/{$instance->network_user_id}>" . $instance->network_username . "</a>'s "; $text .= "video <a href=http://www.youtube.com/watch?v={$video->post_id}>" . $video->post_text . "</a> "; $text .= 'for a total of <strong>'; $text .= InsightTerms::getSyntacticTimeDifference($video->minutes_watched * 60) . ', '; $can_insert = false; $prefix = 'Making an impression:'; // Higher than averages if ($video->minutes_watched >= $average_mins_viewed_all_time->value * 10 && $average_mins_viewed_all_time->value != 0) { $multiplier = $this->terms->getMultiplierAdverb(round($video->minutes_watched / $average_mins_viewed_all_time->value, 2), 'multiplier'); $text .= $multiplier . "</strong> the all-time average."; $emphasis = Insight::EMPHASIS_HIGH; $can_insert = true; } elseif ($video->minutes_watched >= $average_mins_viewed_90->value * 10 && $average_mins_viewed_90->value != 0) { $multiplier = $this->terms->getMultiplierAdverb(round($video->minutes_watched / $average_mins_viewed_90->value, 2), 'multiplier'); $text .= $multiplier . "</strong> the 90-day average."; $emphasis = Insight::EMPHASIS_HIGH; $can_insert = true; } elseif ($video->minutes_watched >= $average_mins_viewed_month->value * 10 && $average_mins_viewed_month->value != 0) { $multiplier = $this->terms->getMultiplierAdverb(round($video->minutes_watched / $average_mins_viewed_month->value, 2), 'multiplier'); $text .= $multiplier . "</strong> the 30-day average."; $emphasis = Insight::EMPHASIS_HIGH; $can_insert = true; } elseif ($video->minutes_watched >= $average_mins_viewed_all_time->value * 5 && $average_mins_viewed_all_time->value != 0) { $multiplier = $this->terms->getMultiplierAdverb(round($video->minutes_watched / $average_mins_viewed_all_time->value, 2), 'multiplier'); $text .= $multiplier . "</strong> the all-time average."; $emphasis = Insight::EMPHASIS_MED; $can_insert = true; } elseif ($video->minutes_watched >= $average_mins_viewed_90->value * 5 && $average_mins_viewed_90->value != 0) { $multiplier = $this->terms->getMultiplierAdverb(round($video->minutes_watched / $average_mins_viewed_90->value, 2), 'multiplier'); $text .= $multiplier . "</strong> the 90-day average."; $emphasis = Insight::EMPHASIS_MED; $can_insert = true; } elseif ($video->minutes_watched >= $average_mins_viewed_month->value * 5 && $average_mins_viewed_month->value != 0) { $multiplier = $this->terms->getMultiplierAdverb(round($video->minutes_watched / $average_mins_viewed_month->value, 2), 'multiplier'); $text .= $multiplier . "</strong> the 30-day average."; $emphasis = Insight::EMPHASIS_MED; $can_insert = true; } elseif ($video->minutes_watched >= $average_mins_viewed_all_time->value * 2 && $average_mins_viewed_all_time->value != 0) { $multiplier = $this->terms->getMultiplierAdverb(round($video->minutes_watched / $average_mins_viewed_all_time->value, 2), 'multiplier'); $text .= $multiplier . "</strong> the all-time average."; $emphasis = Insight::EMPHASIS_LOW; $can_insert = true; } elseif ($video->minutes_watched >= $average_mins_viewed_90->value * 2 && $average_mins_viewed_90->value != 0) { $multiplier = $this->terms->getMultiplierAdverb(round($video->minutes_watched / $average_mins_viewed_90->value, 2), 'multiplier'); $text .= $multiplier . "</strong> the 90-day average."; $emphasis = Insight::EMPHASIS_LOW; $can_insert = true; } elseif ($video->minutes_watched >= $average_mins_viewed_month->value * 2 && $average_mins_viewed_month->value != 0) { $multiplier = $this->terms->getMultiplierAdverb(round($video->minutes_watched / $average_mins_viewed_month->value, 2), 'multiplier'); $text .= $multiplier . "</strong> the 30-day average."; $emphasis = Insight::EMPHASIS_LOW; $can_insert = true; } if ($can_insert) { $this->insight_dao->insertInsightDeprecated('minutes_viewed' . $video->id, $instance->id, $simplified_post_date, $prefix, $text, $filename, $emphasis, serialize(array($video, $chart))); } $text = "Viewers watched "; $text .= "<a href=http://plus.google.com/{$instance->network_user_id}>" . $instance->network_username . "</a>'s "; $text .= "video <a href=http://www.youtube.com/watch?v={$video->post_id}>" . $video->post_text . "</a> "; $text .= 'for a total of <strong>'; $text .= InsightTerms::getSyntacticTimeDifference($video->minutes_watched * 60) . '</strong>.'; $can_insert = false; // All time highs if ($video->minutes_watched >= $max_mins_viewed->value && $max_mins_viewed->value != 0) { $prefix = "New all-time high!"; $emphasis = Insight::EMPHASIS_HIGH; $can_insert = true; } elseif ($video->minutes_watched >= $year_mins_viewed->value && $year_mins_viewed->value != 0) { $prefix = "New 365-day high!"; $emphasis = Insight::EMPHASIS_MED; $can_insert = true; } elseif ($video->minutes_watched >= $ninety_mins_viewed->value && $ninety_mins_viewed->value != 0) { $prefix = "New 90-day high!"; $emphasis = Insight::EMPHASIS_LOW; $can_insert = true; } if ($can_insert) { $this->insight_dao->insertInsightDeprecated('minutes_viewed_high' . $video->id, $instance->id, $simplified_post_date, $prefix, $text, $filename, $emphasis, serialize(array($video, $chart))); } } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }
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__); $year = date('Y'); $regenerate = false; //testing //$regenerate = true; $should_generate_insight = self::shouldGenerateEndOfYearAnnualInsight($this->slug, $instance, $insight_date = "{$year}-{$this->run_date}", $regenerate, $day_of_year = $this->run_date); if ($should_generate_insight) { $this->logger->logInfo("Should generate", __METHOD__ . ',' . __LINE__); $insight = new Insight(); $insight->instance_id = $instance->id; $insight->slug = $this->slug; $insight->date = "{$year}-{$this->run_date}"; $count = 0; $post_dao = DAOFactory::getDAO('PostDAO'); /** * Track occurences of exclamations per month */ $point_chart = array(); $last_year_of_posts = $post_dao->getThisYearOfPostsIterator($author_id = $instance->network_user_id, $network = $instance->network); $total_posts = 0; $months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); foreach ($months as $month) { $point_chart[$month] = 0; } foreach ($last_year_of_posts as $post) { if ($this->hasFBomb($post, $instance)) { $date = new DateTime($post->pub_date); $month = $date->format('M'); $point_chart[$month]++; $count++; } $total_posts++; } $percent = round($count / $total_posts * 100); $max_month = $this->getMaxMonth($point_chart); $earliest_pub_date = $post_dao->getEarliestCapturedPostPubDate($instance); $qualified_year = ""; if (date('Y', strtotime($earliest_pub_date)) == date('Y')) { if (date('n', strtotime($earliest_pub_date)) > 1) { //not January //Earliest post was this year; figure out what month we have data since this year $since = date('F', strtotime($earliest_pub_date)); $qualified_year = " (at least since " . $since . ")"; $since_int = date('n', strtotime($earliest_pub_date)); $since_int--; $point_chart = array_slice($point_chart, $since_int); } } $copy = array('twitter' => array('normal' => array('headline' => "%username gave %total f***s on Twitter in %year", 'body' => "Whiskey Tango Foxtrot: %username said “f**k” " . "<strong>%adverbed_total</strong> on Twitter this year, with %month eliciting the most " . "f***s%qualified_year."), 'one' => array('headline' => "%username really gave a f**k on Twitter in %year", 'body' => "F**k yeah: %username said “f**k” <strong>once</strong> " . "on Twitter this year%qualified_year, in %month.")), 'facebook' => array('normal' => array('headline' => "%username put the “F” in “Facebook” this year", 'body' => "%username dropped <strong>%total F-bombs</strong> on Facebook in %year, " . "with %month on the receiving end of the most f***s%qualified_year. WTF?!"), 'one' => array('headline' => "%username put the “F” in “Facebook” this year", 'body' => "%username dropped <strong>1 F-bomb</strong> on Facebook in %year, in %month."))); if ($count > 1) { $type = 'normal'; $rows = array(); $do_include_chart = false; foreach ($point_chart as $label => $number) { $rows[] = array('c' => array(array('v' => $label), array('v' => $number))); if ($number >= 4) { //Y-axis always renders 4 points $do_include_chart = true; } } if ($do_include_chart && sizeof($rows) > 2) { $insight->setLineChart(array('cols' => array(array('label' => 'Month', 'type' => 'string'), array('label' => 'Occurences', 'type' => 'number')), 'rows' => $rows)); } } elseif ($count == 1) { $type = "one"; } else { return; } $terms = new InsightTerms($instance->network); $adverbed_total = $terms->getOccurrencesAdverb($count); $headline = $this->getVariableCopy(array($copy[$instance->network][$type]['headline']), array('total' => $count, 'year' => $year)); $insight_text = $this->getVariableCopy(array($copy[$instance->network][$type]['body']), array('year' => $year, 'total' => $count, 'month' => $max_month, 'qualified_year' => $qualified_year, 'adverbed_total' => $adverbed_total)); $insight->headline = $headline; $insight->text = $insight_text; $insight->header_image = $user->avatar; $insight->filename = basename(__FILE__, ".php"); $insight->emphasis = Insight::EMPHASIS_HIGH; $this->insight_dao->insertInsight($insight); } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }
public function testTwitter() { $today = date('Y-m-d'); $insight_dao = DAOFactory::getDAO('InsightDAO'); $post_builders = array(); $post_builders[] = FixtureBuilder::build('posts', array('in_reply_to_user_id' => 1, 'author_username' => 'testy', 'network' => $this->instance->network, 'pub_date' => date('Y-m-d', strtotime('January 9')), 'author_user_id' => $this->instance->network_user_id, 'post_text' => "I hate it all.")); $post_builders[] = FixtureBuilder::build('posts', array('in_reply_to_user_id' => 1, 'author_username' => 'testy', 'network' => $this->instance->network, 'pub_date' => date('Y-m-d', strtotime('January 9')), 'author_user_id' => $this->instance->network_user_id, 'post_text' => "Thanks everyone")); $post_builders[] = FixtureBuilder::build('posts', array('in_reply_to_user_id' => 2, 'author_username' => 'testy', 'network' => $this->instance->network, 'pub_date' => date('Y-m-d', strtotime('January 9')), 'author_user_id' => $this->instance->network_user_id, 'post_text' => "Thanks everyone")); $insight_plugin = new ThanksgivingWhoYouThankedInsight(); $insight_plugin->generateInsight($this->instance, null, $posts, 3); $result = $insight_dao->getInsight($insight_plugin->slug, $this->instance->id, $today); $data = unserialize($result->related_data); $this->assertEqual($result->headline, 'Who @Thankster was thankful for in ' . date('Y')); $this->assertEqual($result->text, 'These are all the people @Thankster thanked this year.'); $this->assertEqual(count($data['people']), 2); $this->assertEqual($data['people'][0]->username, 'two'); $this->assertEqual($data['people'][1]->username, 'one'); $this->assertEqual($data['hero_image']['alt_text'], $result->headline); $this->assertEqual($data['hero_image']['img_link'], 'https://www.flickr.com/photos/voght/2441818832/'); $this->debug($this->getRenderedInsightInHTML($result)); $this->debug($this->getRenderedInsightInEmail($result)); $terms = new InsightTerms('twitter'); $notification_line = $terms->swapInSecondPerson($this->instance->network_username, $result->headline); $this->assertEqual($notification_line, 'Who you were thankful for in ' . date('Y')); }
public function testGetPhraseForAddingAsFriend() { $terms = new InsightTerms('google+'); $result_1 = $terms->getPhraseForAddingAsFriend('testeriffic'); $terms = new InsightTerms('facebook'); $result_2 = $terms->getPhraseForAddingAsFriend('testeriffic'); $terms = new InsightTerms('twitter'); $result_3 = $terms->getPhraseForAddingAsFriend('@testeriffic'); $this->assertEqual($result_1, "added testeriffic to new circles"); $this->assertEqual($result_2, "added testeriffic as a friend"); $this->assertEqual($result_3, "followed @testeriffic"); }