public function generateInsight(Instance $instance, User $user, $last_weeks_posts, $number_days) { parent::generateInsight($instance, $user, $last_week_of_posts, $number_days); $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); $filename = basename(__FILE__, ".php"); $insight_text = ''; $video_dao = DAOFactory::getDAO('VideoDAO'); // Get the users subscriber count for comparing with later $subscriber_count = $user->follower_count; foreach ($last_weeks_posts as $post) { if ($post->network == 'youtube') { $video = $video_dao->getVideoByID($post->post_id, 'youtube'); $simplified_post_date = date('Y-m-d', strtotime($video->pub_date)); } else { break; } $gain_or_loss = $video->subscribers_gained - $video->subscribers_lost; // if we lost subscribers then we will be doing subscriber_count minus a negative number, so adding $total_before_video = $subscriber_count - $gain_or_loss; $percent_change = round(abs($gain_or_loss) / $total_before_video * 100, 2); $verb = $gain_or_loss < 0 ? ' decreased' : ' increased'; $headline = $video->post_text . $verb . " " . $instance->network_username . "'s "; $headline .= "subscriber count by <strong>" . $percent_change . "%</strong>."; $insight_text = "<a href=http://plus.google.com/{$instance->network_user_id}>{$instance->network_username}</a>'s "; $insight_text .= "video <a href=http://www.youtube.com/watch?v={$video->post_id}>{$video->post_text}</a> "; $insight_text .= "left an impression on {$gain_or_loss} subscribers."; $subscriber_count = intval($subscriber_count); $total_before_video = intval($total_before_video); $rows = $video_dao->getNetSubscriberChange($instance->network_username, 'youtube', 10); $chart = VideoMySQLDAO::getHotVideosVisualizationData($rows, 'Subscriber Change'); if ($percent_change >= 50) { $this->insight_dao->insertInsightDeprecated('subscriber_change' . $video->id, $instance->id, $simplified_post_date, $headline, $insight_text, $filename, Insight::EMPHASIS_HIGH, serialize(array($chart, $video))); } elseif ($percent_change >= 25) { $this->insight_dao->insertInsightDeprecated('subscriber_change' . $video->id, $instance->id, $simplified_post_date, $headline, $insight_text, $filename, Insight::EMPHASIS_MED, serialize(array($chart, $video))); } elseif ($percent_change >= 10) { $this->insight_dao->insertInsightDeprecated('subscriber_change' . $video->id, $instance->id, $simplified_post_date, $headline, $insight_text, $filename, Insight::EMPHASIS_LOW, serialize(array($chart, $video))); } } }
public function generateInsight(Instance $instance, $last_week_of_posts, $number_days) { parent::generateInsight($instance, $last_week_of_posts, $number_days); self::generateInsightBaselines($instance, $number_days); $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); $insight_baseline_dao = DAOFactory::getDAO('InsightBaselineDAO'); $filename = basename(__FILE__, ".php"); $video_dao = DAOFactory::getDAO('VideoDAO'); $simplified_post_date = ""; foreach ($last_week_of_posts as $post) { if ($post->network == 'youtube') { $video = $video_dao->getVideoByID($post->post_id, 'youtube'); } else { break; } if ($video->likes > 2) { //Only show insight for more than 2 likes // First get spike/high 30/90/365 day baselines if ($simplified_post_date != date('Y-m-d', strtotime($post->pub_date))) { $simplified_post_date = date('Y-m-d', strtotime($post->pub_date)); $average_like_count_30_days = $insight_baseline_dao->getInsightBaseline('avg_like_count_last_30_days', $instance->id, $simplified_post_date); $average_like_count_90_days = $insight_baseline_dao->getInsightBaseline('avg_like_count_last_90_days', $instance->id, $simplified_post_date); $high_like_count_30_days = $insight_baseline_dao->getInsightBaseline('high_like_count_last_30_days', $instance->id, $simplified_post_date); $high_like_count_90_days = $insight_baseline_dao->getInsightBaseline('high_like_count_last_90_days', $instance->id, $simplified_post_date); $high_like_count_365_days = $insight_baseline_dao->getInsightBaseline('high_like_count_last_365_days', $instance->id, $simplified_post_date); $hot_videos_data = $video_dao->getHotVideos($instance->network_username, 'youtube', 10, 'likes', 'Likes'); $chart_data = VideoMySQLDAO::getHotVideosVisualizationData($hot_videos_data, 'Likes'); } // Next compare post like counts to baselines and store insights where there's a spike or high if (isset($high_like_count_365_days->value) && $video->likes >= $high_like_count_365_days->value) { if (isset($chart_data)) { $prefix = "New 365-day record!"; $text = "<strong>" . number_format($video->likes) . " people</strong> liked "; $text .= "<a href=http://plus.google.com/{$instance->network_user_id}>"; $text .= "{$instance->network_username}</a>'s video <a href=http://www.youtube.com/watch?v="; $text .= "{$video->post_id}>{$video->post_text}</a>."; $this->insight_dao->insertInsightDeprecated('like_high_365_day_' . $video->id, $instance->id, $simplified_post_date, $prefix, $text, $filename, Insight::EMPHASIS_HIGH, serialize(array($video, $chart_data))); $this->insight_dao->deleteInsight('like_high_90_day_' . $post->id, $instance->id, $simplified_post_date); $this->insight_dao->deleteInsight('like_high_90_day_' . $post->id, $instance->id, $simplified_post_date); $this->insight_dao->deleteInsight('like_spike_30_day_' . $post->id, $instance->id, $simplified_post_date); $this->insight_dao->deleteInsight('like_spike_30_day_' . $post->id, $instance->id, $simplified_post_date); } } elseif (isset($high_like_count_90_days->value) && $video->likes >= $high_like_count_90_days->value) { if (isset($chart_data)) { $prefix = "New 90-day record!"; $text = "<strong>" . number_format($video->likes) . " people</strong> liked "; $text .= "<a href=http://plus.google.com/{$instance->network_user_id}>"; $text .= "{$instance->network_username}</a>'s video <a href=http://www.youtube.com/watch?v="; $text .= "{$video->post_id}>{$video->post_text}</a>."; $this->insight_dao->insertInsightDeprecated('like_high_90_day_' . $video->id, $instance->id, $simplified_post_date, $prefix, $text, $filename, Insight::EMPHASIS_HIGH, serialize(array($video, $chart_data))); $this->insight_dao->deleteInsight('like_high_30_day_' . $post->id, $instance->id, $simplified_post_date); $this->insight_dao->deleteInsight('like_spike_30_day_' . $post->id, $instance->id, $simplified_post_date); } } elseif (isset($high_like_count_30_days->value) && $video->likes >= $high_like_count_30_days->value) { if (isset($chart_data)) { $prefix = "New 30-day record!"; $text = "<strong>" . number_format($video->likes) . " people</strong> liked "; $text .= "<a href=http://plus.google.com/{$instance->network_user_id}>"; $text .= "{$instance->network_username}</a>'s video <a href=http://www.youtube.com/watch?v="; $text .= "{$video->post_id}>{$video->post_text}</a>."; $this->insight_dao->insertInsightDeprecated('like_high_30_day_' . $video->id, $instance->id, $simplified_post_date, $prefix, $text, $filename, Insight::EMPHASIS_HIGH, serialize(array($video, $chart_data))); } } if (isset($average_like_count_90_days->value) && $video->likes > $average_like_count_90_days->value * 2) { if (isset($chart_data)) { $multiplier = floor($video->likes / $average_like_count_30_days->value); $multiplier = $this->terms->getMultiplierAdverb($multiplier); $prefix = "Thumbs up:"; $text = "<strong>" . number_format($video->likes) . " people</strong> liked "; $text .= "<a href=http://plus.google.com/{$instance->network_user_id}>"; $text .= "{$instance->network_username}</a>'s video <a href=http://www.youtube.com/watch?v="; $text .= "{$video->post_id}>{$video->post_text}</a>, more than <strong>" . $multiplier . "</strong> "; $text .= "the 90-day average."; $this->insight_dao->insertInsightDeprecated('like_spike_90_day_' . $post->id, $instance->id, $simplified_post_date, $prefix, $text, $filename, Insight::EMPHASIS_LOW, serialize(array($video, $chart_data))); $this->insight_dao->deleteInsight('like_spike_30_day_' . $post->id, $instance->id, $simplified_post_date); } } elseif (isset($average_like_count_30_days->value) && $video->likes > $average_like_count_30_days->value * 2) { if (isset($chart_data)) { $multiplier = floor($video->likes / $average_like_count_30_days->value); $multiplier = $this->terms->getMultiplierAdverb($multiplier); $prefix = "Thumbs up:"; $text = "<strong>" . number_format($video->likes) . " people</strong> liked "; $text .= "<a href=http://plus.google.com/{$instance->network_user_id}>"; $text .= "{$instance->network_username}</a>'s video <a href=http://www.youtube.com/watch?v="; $text .= "{$video->post_id}>{$video->post_text}</a>, more than <strong>" . $multiplier . "</strong> "; $text .= "the 30-day average."; $this->insight_dao->insertInsightDeprecated('like_spike_30_day_' . $post->id, $instance->id, $simplified_post_date, $prefix, $text, $filename, Insight::EMPHASIS_LOW, serialize(array($video, $chart_data))); } } } } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }
public function testGetNetSubscriberChange() { $post_builder = FixtureBuilder::build('posts', array('id' => 1, 'post_id' => '1', 'author_username' => 'ev', 'post_text' => 'My Great Video', 'pub_date' => '-40d', 'network' => 'youtube')); $video_builder = FixtureBuilder::build('videos', array('id' => 1, 'post_key' => '1', 'subscribers_gained' => 10, 'subscribers_lost' => 5)); $post_builder2 = FixtureBuilder::build('posts', array('id' => 2, 'post_id' => '2', 'author_username' => 'ev', 'post_text' => 'My Great Video 2', 'pub_date' => '-2d', 'network' => 'youtube')); $video_builder2 = FixtureBuilder::build('videos', array('id' => 2, 'post_key' => '2', 'subscribers_gained' => 5, 'subscribers_lost' => 10)); $post_builder3 = FixtureBuilder::build('posts', array('id' => 3, 'post_id' => '3', 'author_username' => 'ev', 'post_text' => 'My Great Video 3', 'pub_date' => '-2d', 'network' => 'youtube')); $video_builder3 = FixtureBuilder::build('videos', array('id' => 3, 'post_key' => '3', 'subscribers_gained' => 20, 'subscribers_lost' => 5)); $video_dao = new VideoMySQLDAO(); $result = $video_dao->getNetSubscriberChange('ev', 'youtube', 3); $this->assertEqual(sizeof($result), 3); $this->assertEqual($result[0]['Subscriber Change'], 5); $this->assertEqual($result[1]['Subscriber Change'], -5); $this->assertEqual($result[2]['Subscriber Change'], 15); }
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, $last_weeks_posts, $number_days) { parent::generateInsight($instance, $last_weeks_posts, $number_days); self::generateBaselines($instance); $video_dao = DAOFactory::getDAO('VideoDAO'); $baseline_dao = DAOFactory::getDAO('InsightBaselineDAO'); $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); $filename = basename(__FILE__, ".php"); foreach ($last_weeks_posts as $post) { // YouTube users can post replies to their videos and these get passed to us here also and have no // associated videos so check we actually got a video post. if ($post->network == 'youtube' && $post->in_reply_to_post_id == null) { $video = $video_dao->getVideoByID($post->post_id, 'youtube'); $simplified_post_date = date('Y-m-d', strtotime($post->pub_date)); $average_month = $baseline_dao->getInsightBaseline('avg_view_percentage_month', $instance->id, date('Y-m-d')); $average_all_time = $baseline_dao->getInsightBaseline('avg_view_percentage_all_time', $instance->id, date('Y-m-d')); $average_90 = $baseline_dao->getInsightBaseline('avg_view_percentage_90', $instance->id, date('Y-m-d')); $high = $baseline_dao->getInsightBaseline('view_percentage_all_time_high', $instance->id, date('Y-m-d')); $high_365 = $baseline_dao->getInsightBaseline('view_percentage_year_high', $instance->id, date('Y-m-d')); $high_90 = $baseline_dao->getInsightBaseline('view_percentage_90_high', $instance->id, date('Y-m-d')); $low = $baseline_dao->getInsightBaseline('view_percentage_all_time_low', $instance->id, date('Y-m-d')); $low_365 = $baseline_dao->getInsightBaseline('view_percentage_year_low', $instance->id, date('Y-m-d')); $low_90 = $baseline_dao->getInsightBaseline('view_percentage_90_low', $instance->id, date('Y-m-d')); $hot_videos = $video_dao->getHotVideos($instance->network_username, 'youtube', 10, 'average_view_percentage', 'Average View Percentage'); $chart = VideoMySQLDAO::getHotVideosVisualizationData($hot_videos, 'Average View Percentage'); } else { break; } $view_duration = round($video->average_view_percentage, 0); // The analytics data isn't always available for new videos so check this value isn't 0 if ($view_duration == 0) { break; } $change_month = round($view_duration - $average_month->value, 0); $change_all_time = round($view_duration - $average_all_time->value, 0); $change_90 = round($view_duration - $average_90->value, 0); $text = "On average, viewers watched <a href=http://plus.google.com/" . $instance->network_user_id . ">"; $text .= $instance->network_username . "</a>'s video"; $text .= " <a href=http://www.youtube.com/watch?v=" . $video->post_id . ">" . $video->post_text . "</a> "; $text .= "<strong>" . $view_duration . "%</strong> of the way through, <strong>"; $increase_prefix = 'Captive audience:'; $decrease_prefix = 'Viewer time:'; $can_insert = false; // Increases or decreases compared to the average if (abs($change_all_time) >= 30) { $can_insert = true; $emphasis = Insight::EMPHASIS_HIGH; if ($change_all_time >= 30) { $prefix = $increase_prefix; $text .= $change_all_time . "%</strong> longer than the all-time average."; } else { $prefix = $decrease_prefix; $text .= abs($change_all_time) . "%</strong> less than the all-time average."; } } elseif (abs($change_90) >= 30) { $can_insert = true; $emphasis = Insight::EMPHASIS_HIGH; if ($change_90 >= 30) { $prefix = $increase_prefix; $text .= $change_90 . "%</strong> longer than the 90-day average."; } else { $prefix = $decrease_prefix; $text .= abs($change_90) . "%</strong> less than the 90-day average."; } } elseif (abs($change_month) >= 30) { $can_insert = true; $emphasis = Insight::EMPHASIS_HIGH; if ($change_month >= 30) { $prefix = $increase_prefix; $text .= $change_month . "%</strong> longer than the 30-day average."; } else { $prefix = $decrease_prefix; $text .= abs($change_month) . "%</strong> less than the 30-day average."; } } elseif (abs($change_all_time) >= 15) { $can_insert = true; $emphasis = Insight::EMPHASIS_MED; if ($change_all_time >= 15) { $prefix = $increase_prefix; $text .= $change_all_time . "%</strong> longer than the all-time average."; } else { $prefix = $decrease_prefix; $text .= abs($change_all_time) . "%</strong> less than the all-time average."; } } elseif (abs($change_90) >= 15) { $can_insert = true; $emphasis = Insight::EMPHASIS_MED; if ($change_90 >= 15) { $prefix = $increase_prefix; $text .= $change_90 . "%</strong> longer than the 90-day average."; } else { $prefix = $decrease_prefix; $text .= abs($change_90) . "%</strong> less than the 90-day average."; } } elseif (abs($change_month) >= 15) { $can_insert = true; $emphasis = Insight::EMPHASIS_MED; if ($change_month >= 15) { $prefix = $increase_prefix; $text .= $change_month . "%</strong> longer than the 30-day average."; } else { $prefix = $decrease_prefix; $text .= abs($change_month) . "%</strong> less than the 30-day average."; } } elseif (abs($change_all_time) >= 5) { $can_insert = true; $emphasis = Insight::EMPHASIS_LOW; if ($change_all_time >= 5) { $prefix = $increase_prefix; $text .= $change_all_time . "%</strong> longer than the all-time average."; } else { $prefix = $decrease_prefix; $text .= abs($change_all_time) . "%</strong> less than the all-time average."; } } elseif (abs($change_90) >= 5) { $can_insert = true; $emphasis = Insight::EMPHASIS_LOW; if ($change_90 >= 5) { $prefix = $increase_prefix; $text .= $change_90 . "%</strong> longer than the 90-day average."; } else { $prefix = $decrease_prefix; $text .= abs($change_90) . "%</strong> less than the 90-day average."; } } elseif (abs($change_month) >= 5) { $can_insert = true; $emphasis = Insight::EMPHASIS_LOW; if ($change_month >= 5) { $prefix = $increase_prefix; $text .= $change_month . "%</strong> longer than the 30-day average."; } else { $prefix = $decrease_prefix; $text .= abs($change_month) . "%</strong> less than the 30-day average."; } } if ($can_insert) { $this->insight_dao->insertInsightDeprecated('view_duration', $instance->id, $simplified_post_date, $prefix, $text, $filename, $emphasis, serialize(array($chart, $video))); } // New highs and lows $can_insert = false; $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> was "; $text .= "viewed <strong>" . $view_duration . "%</strong> of the way through on average."; if ($view_duration >= $high->value && $high->value != 0) { $prefix = 'New all-time high!'; $emphasis = Insight::EMPHASIS_HIGH; $can_insert = true; } elseif ($view_duration <= $low->value && $low->value != 0) { $prefix = 'New all-time low!'; $emphasis = Insight::EMPHASIS_HIGH; $can_insert = true; } elseif ($view_duration >= $high_365->value && $high_365->value != 0) { $prefix = 'New 365-day record!'; $emphasis = Insight::EMPHASIS_MED; $can_insert = true; } elseif ($view_duration <= $low_365->value && $low_365->value != 0) { $prefix = 'New 365-day record!'; $emphasis = Insight::EMPHASIS_MED; $can_insert = true; } elseif ($view_duration >= $high_90->value && $high_90->value != 0) { $prefix = 'New 90-day record!'; $emphasis = Insight::EMPHASIS_LOW; $can_insert = true; } elseif ($view_duration <= $low_90->value && $low_90->value != 0) { $prefix = 'New 90-day record!'; $emphasis = Insight::EMPHASIS_LOW; $can_insert = true; } if ($can_insert) { $this->insight_dao->insertInsightDeprecated('view_duration_record', $instance->id, $simplified_post_date, $prefix, $text, $filename, $emphasis, serialize(array($chart, $video))); } } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }
public function testFetchInstanceUserVideos() { $builders = self::buildData(); $ytc = new YouTubeCrawler($this->instance, 'at', 20); $ytc->fetchInstanceUserVideos(); $video_dao = new VideoMySQLDAO(); $post_dao = new PostMySQLDAO(); $count_history_dao = new CountHistoryMySQLDAO(); // Check the first video got added $video_one = $video_dao->getVideoByID('H', 'youtube'); $this->assertEqual($video_one->post_text, 'My Slideshow'); $this->assertEqual($video_one->description, 'I created this video with the YouTube Slideshow Creator (http://www.youtube.com/upload)'); $this->assertEqual($video_one->likes, 1); $this->assertEqual($video_one->dislikes, 0); $this->assertEqual($video_one->views, 6); $this->assertEqual($video_one->post_id, 'H'); $this->assertEqual($video_one->is_protected, 0); $this->assertEqual($video_one->favorites_added, 0); $this->assertEqual($video_one->favorites_removed, 0); $this->assertEqual($video_one->shares, 0); $this->assertEqual($video_one->subscribers_gained, 0); $this->assertEqual($video_one->subscribers_lost, 0); $this->assertEqual($video_one->minutes_watched, 4); $this->assertEqual($video_one->average_view_duration, 52); $this->assertEqual($video_one->average_view_percentage, 24.8284); $this->assertEqual($video_one->author_user_id, '113612142759476883204'); $this->assertEqual($video_one->author_username, 'Gina Trapani'); $this->assertEqual($video_one->author_fullname, 'Gina Trapani'); $this->assertEqual($video_one->author_avatar, 'http://www.myavatar.com'); $this->assertEqual($video_one->source, ''); $this->assertEqual($video_one->network, 'youtube'); // Check the all time counts were added correctly $favs_added_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'favorites_added_all_time'); $favs_removed_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'favorites_removed_all_time'); $shares_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'shares_all_time'); $subs_gained_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'subscribers_gained_all_time'); $subs_lost_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'subscribers_lost_all_time'); $mins_watched_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'minutes_watched_all_time'); $avg_view_percent_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'average_view_percentage_all_time'); $avg_view_dur_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'average_view_duration_all_time'); $views_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'views_all_time'); $likes_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'likes_all_time'); $dislikes_all_time = $count_history_dao->getLatestCountByPostIDAndType('H', 'dislikes_all_time'); $this->assertEqual($favs_added_all_time['count'], 0); $this->assertEqual($favs_removed_all_time['count'], 0); $this->assertEqual($shares_all_time['count'], 0); $this->assertEqual($subs_gained_all_time['count'], 0); $this->assertEqual($subs_lost_all_time['count'], 0); $this->assertEqual($mins_watched_all_time['count'], 4); $this->assertEqual($avg_view_percent_all_time['count'], 25); $this->assertEqual($avg_view_dur_all_time['count'], 52); $this->assertEqual($views_all_time['count'], 6); $this->assertEqual($likes_all_time['count'], 1); $this->assertEqual($dislikes_all_time['count'], 0); // Check the replies to it got added // Basic 1st reply $reply_one = $post_dao->getPost('jm_SGXNfF6AmF20tsHqF_2h_S_fV_0l2DU3AfqjbsNc', 'youtube'); $this->assertEqual($reply_one->post_id, 'jm_SGXNfF6AmF20tsHqF_2h_S_fV_0l2DU3AfqjbsNc'); $this->assertEqual($reply_one->post_text, 'Test comment'); $this->assertEqual($reply_one->author_username, 'Aaron Kalair'); $this->assertEqual($reply_one->author_fullname, 'Aaron Kalair'); $this->assertEqual($reply_one->author_avatar, 'https://lh5.googleusercontent.com/-Z2vFxu2wO6E/AAAAAAAAAAI/AAAAAAAAANQ/Pp0EB7dNKLY/photo.jpg?sz=50'); $this->assertEqual($reply_one->author_user_id, '115383827382290096528'); $this->assertEqual($reply_one->pub_date, '2013-06-05 08:13:43'); $this->assertEqual($reply_one->source, ''); $this->assertEqual($reply_one->is_protected, 0); $this->assertEqual($reply_one->network, 'youtube'); $this->assertEqual($reply_one->in_reply_to_user_id, '113612142759476883204'); $this->assertEqual($reply_one->in_reply_to_post_id, 'H'); // Check we can itterate over replies in a single page $reply_two = $post_dao->getPost('gm_SGXNffMJT58F20tsHqF_2h_S_fV_0l2DU3AfqjbsNc', 'youtube'); $this->assertEqual($reply_two->post_id, 'gm_SGXNffMJT58F20tsHqF_2h_S_fV_0l2DU3AfqjbsNc'); $this->assertEqual($reply_two->post_text, 'yet another comment'); $this->assertEqual($reply_two->author_username, 'Aaron Kalair'); $this->assertEqual($reply_two->author_fullname, 'Aaron Kalair'); $this->assertEqual($reply_two->author_avatar, 'https://lh5.googleusercontent.com/-Z2vFxu2wO6E/AAAAAAAAAAI/AAAAAAAAANQ/Pp0EB7dNKLY/photo.jpg?sz=50'); $this->assertEqual($reply_two->author_user_id, '115383827382290096528'); $this->assertEqual($reply_two->pub_date, '2013-06-15 07:45:11'); $this->assertEqual($reply_two->source, ''); $this->assertEqual($reply_two->is_protected, 0); $this->assertEqual($reply_two->network, 'youtube'); $this->assertEqual($reply_two->in_reply_to_user_id, '113612142759476883204'); $this->assertEqual($reply_two->in_reply_to_post_id, 'H'); // Check we can get a reply from another page $reply_three = $post_dao->getPost('hg_HF75HJSNY38JH_ht5_fh', 'youtube'); $this->assertEqual($reply_three->post_id, 'hg_HF75HJSNY38JH_ht5_fh'); $this->assertEqual($reply_three->post_text, 'A comment on the second page'); $this->assertEqual($reply_three->author_username, 'Aaron Kalair'); $this->assertEqual($reply_three->author_fullname, 'Aaron Kalair'); $this->assertEqual($reply_three->author_avatar, 'https://lh5.googleusercontent.com/-Z2vFxu2wO6E/AAAAAAAAAAI/AAAAAAAAANQ/Pp0EB7dNKLY/photo.jpg?sz=50'); $this->assertEqual($reply_three->author_user_id, '115383827382290096528'); $this->assertEqual($reply_three->pub_date, '2013-06-10 18:14:33'); $this->assertEqual($reply_three->source, ''); $this->assertEqual($reply_three->is_protected, 0); $this->assertEqual($reply_three->network, 'youtube'); $this->assertEqual($reply_three->in_reply_to_user_id, '113612142759476883204'); $this->assertEqual($reply_three->in_reply_to_post_id, 'H'); // Check we can itterate over videos on the same page of replies $video_two = $video_dao->getVideoByID('a', 'youtube'); $this->assertEqual($video_two->post_text, 'Same Page Video'); $this->assertEqual($video_two->description, 'This video is on the same page'); $this->assertEqual($video_two->likes, 10); $this->assertEqual($video_two->dislikes, 110); $this->assertEqual($video_two->views, 50); $this->assertEqual($video_two->post_id, 'a'); $this->assertEqual($video_two->is_protected, 0); $this->assertEqual($video_two->favorites_added, 5); $this->assertEqual($video_two->favorites_removed, 1); $this->assertEqual($video_two->shares, 0); $this->assertEqual($video_two->subscribers_gained, 0); $this->assertEqual($video_two->subscribers_lost, 0); $this->assertEqual($video_two->minutes_watched, 1); $this->assertEqual($video_two->average_view_duration, 2); $this->assertEqual($video_two->average_view_percentage, 24.8284); $this->assertEqual($video_two->author_user_id, '113612142759476883204'); $this->assertEqual($video_two->author_username, 'Gina Trapani'); $this->assertEqual($video_two->author_fullname, 'Gina Trapani'); $this->assertEqual($video_two->author_avatar, 'http://www.myavatar.com'); $this->assertEqual($video_two->source, ''); $this->assertEqual($video_two->network, 'youtube'); // Check the per day counts got added correctly $favs_added = $count_history_dao->getLatestCountByPostIDAndType('a', 'favorites_added'); $favs_removed = $count_history_dao->getLatestCountByPostIDAndType('a', 'favorites_removed'); $shares = $count_history_dao->getLatestCountByPostIDAndType('a', 'shares'); $subs_gained = $count_history_dao->getLatestCountByPostIDAndType('a', 'subscribers_gained'); $subs_lost = $count_history_dao->getLatestCountByPostIDAndType('a', 'subscribers_lost'); $mins_watched = $count_history_dao->getLatestCountByPostIDAndType('a', 'minutes_watched'); $avg_view_percent = $count_history_dao->getLatestCountByPostIDAndType('a', 'average_view_percentage'); $avg_view_dur = $count_history_dao->getLatestCountByPostIDAndType('a', 'average_view_duration'); $views = $count_history_dao->getLatestCountByPostIDAndType('a', 'views'); $likes = $count_history_dao->getLatestCountByPostIDAndType('a', 'likes'); $dislikes = $count_history_dao->getLatestCountByPostIDAndType('a', 'dislikes'); $this->assertEqual($favs_added['count'], 4); $this->assertEqual($favs_removed['count'], 2); $this->assertEqual($shares['count'], 3); $this->assertEqual($subs_gained['count'], 10); $this->assertEqual($subs_lost['count'], 11); $this->assertEqual($mins_watched['count'], 11); $this->assertEqual($avg_view_percent['count'], 14); $this->assertEqual($avg_view_dur['count'], 15); $this->assertEqual($views['count'], 100); $this->assertEqual($likes['count'], 0); $this->assertEqual($dislikes['count'], 0); // Check the all time counts were added correctly $favs_added_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'favorites_added_all_time'); $favs_removed_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'favorites_removed_all_time'); $shares_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'shares_all_time'); $subs_gained_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'subscribers_gained_all_time'); $subs_lost_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'subscribers_lost_all_time'); $mins_watched_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'minutes_watched_all_time'); $avg_view_percent_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'average_view_percentage_all_time'); $avg_view_dur_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'average_view_duration_all_time'); $views_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'views_all_time'); $likes_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'likes_all_time'); $dislikes_all_time = $count_history_dao->getLatestCountByPostIDAndType('a', 'dislikes_all_time'); $this->assertEqual($favs_added_all_time['count'], 5); $this->assertEqual($favs_removed_all_time['count'], 1); $this->assertEqual($shares_all_time['count'], 0); $this->assertEqual($subs_gained_all_time['count'], 0); $this->assertEqual($subs_lost_all_time['count'], 0); $this->assertEqual($mins_watched_all_time['count'], 1); $this->assertEqual($avg_view_percent_all_time['count'], 25); $this->assertEqual($avg_view_dur_all_time['count'], 2); $this->assertEqual($views_all_time['count'], 50); $this->assertEqual($likes_all_time['count'], 10); $this->assertEqual($dislikes_all_time['count'], 110); // Check we can get a video from the next page of videos $video_three = $video_dao->getVideoByID('g', 'youtube'); $this->assertEqual($video_three->post_text, 'My Slideshow'); $this->assertEqual($video_three->description, 'I created this video with the YouTube Slideshow Creator (http://www.youtube.com/upload)'); $this->assertEqual($video_three->likes, 0); $this->assertEqual($video_three->dislikes, 0); $this->assertEqual($video_three->views, 5); $this->assertEqual($video_three->post_id, 'g'); $this->assertEqual($video_three->is_protected, 0); $this->assertEqual($video_three->favorites_added, 0); $this->assertEqual($video_three->favorites_removed, 0); $this->assertEqual($video_three->shares, 0); $this->assertEqual($video_three->subscribers_gained, 0); $this->assertEqual($video_three->subscribers_lost, 0); $this->assertEqual($video_three->minutes_watched, 0); $this->assertEqual($video_three->average_view_duration, 15); $this->assertEqual($video_three->average_view_percentage, 42.7689); $this->assertEqual($video_three->author_user_id, '113612142759476883204'); $this->assertEqual($video_three->author_username, 'Gina Trapani'); $this->assertEqual($video_three->author_fullname, 'Gina Trapani'); $this->assertEqual($video_three->author_avatar, 'http://www.myavatar.com'); $this->assertEqual($video_three->source, ''); $this->assertEqual($video_three->network, 'youtube'); // Check the all time counts were added correctly $favs_added_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'favorites_added_all_time'); $favs_removed_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'favorites_removed_all_time'); $shares_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'shares_all_time'); $subs_gained_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'subscribers_gained_all_time'); $subs_lost_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'subscribers_lost_all_time'); $mins_watched_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'minutes_watched_all_time'); $avg_view_percent_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'average_view_percentage_all_time'); $avg_view_dur_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'average_view_duration_all_time'); $views_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'views_all_time'); $likes_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'likes_all_time'); $dislikes_all_time = $count_history_dao->getLatestCountByPostIDAndType('g', 'dislikes_all_time'); $this->assertEqual($favs_added_all_time['count'], 0); $this->assertEqual($favs_removed_all_time['count'], 0); $this->assertEqual($shares_all_time['count'], 0); $this->assertEqual($subs_gained_all_time['count'], 0); $this->assertEqual($subs_lost_all_time['count'], 0); $this->assertEqual($mins_watched_all_time['count'], 0); $this->assertEqual($avg_view_percent_all_time['count'], 43); $this->assertEqual($avg_view_dur_all_time['count'], 15); $this->assertEqual($views_all_time['count'], 5); $this->assertEqual($likes_all_time['count'], 0); $this->assertEqual($dislikes_all_time['count'], 0); }