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__); }
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)); }
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 testGetSyntacticTimeDifference() { $delta_1 = 60 * 60 * 3; // 3 hours $delta_2 = 60 * 6; // 6 minutes $delta_3 = 60 * 60 * 24 * 4; // 4 days $delta_4 = 60 * 60 * 24; // 1 day $result_1 = InsightTerms::getSyntacticTimeDifference($delta_1); $result_2 = InsightTerms::getSyntacticTimeDifference($delta_2); $result_3 = InsightTerms::getSyntacticTimeDifference($delta_3); $result_4 = InsightTerms::getSyntacticTimeDifference($delta_4); $this->assertEqual($result_1, '3 hours'); $this->assertEqual($result_2, '6 minutes'); $this->assertEqual($result_3, '4 days'); $this->assertEqual($result_4, '1 day'); }
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__); }