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}"; $post_dao = DAOFactory::getDAO('PostDAO'); $network = $instance->network; $last_year_of_posts = $post_dao->getThisYearOfPostsIterator($author_id = $instance->network_user_id, $network = $network); $streaks = $this->getStreaks($last_year_of_posts); $longest_streak = $this->getLongestStreak($streaks); $earliest_pub_date = $post_dao->getEarliestCapturedPostPubDate($instance); $qualified_year = $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 = $year . " (at least since " . $since . ")"; } } $copy = array('twitter' => array('normal' => array('headline' => "%username's longest tweet-streak of %year", 'body' => "Sometimes the tweets flow like water and you just " . "don't need a day off. In %qualified_year, %username's longest " . "tweeting streak lasted for <strong>%total days</strong>, from %date1 to %date2."), 'everyday' => array('headline' => "%username has tweeted every single day in %year!", 'body' => "Sometimes the tweets flow like water and you just " . "don't need a day off. So far in %qualified_year, %username hasn't taken off " . "a single day, with a streak that has so far lasted for <strong>%total " . "days</strong>, from %date1 to %date2.")), 'facebook' => array('normal' => array('headline' => "%username's longest update streak of %year", 'body' => "Facebook is great for sharing what we're up to, " . "and sometimes we're up to a lot. In %qualified_year, %username " . "posted at least one status update or comment to Facebook " . "for <strong>%total days</strong> in a row, from %date1 to %date2."), 'everyday' => array('headline' => "%username has posted to Facebook every single day in %year!", 'body' => "Facebook is great for sharing what we're up to, and in %year, " . "%username was up to a lot — posting at least one time every day " . "so far in %qualified_year for a streak of <strong>%total days</strong>, " . "from %date1 through %date2.")), 'instagram' => array('normal' => array('headline' => "%username's longest Instagram streak of %year", 'body' => "Instagram is the place to share scenes from your life, " . "and sometimes there's a lot of life to share. In %qualified_year, %username " . "posted at least one photo or video on Instagram " . "for <strong>%total days</strong> in a row, from %date1 to %date2."), 'everyday' => array('headline' => "%username has posted to Instagram every single day in %year!", 'body' => "Instagram is the place to share scenes from your life, and in %year, " . "%username had lots of life to share — posting at least one time every day " . "so far in %qualified_year for a streak of <strong>%total days</strong>, " . "from %date1 through %date2."))); if ($longest_streak['length'] - 2 == date('z') - 1) { $type = 'everyday'; } else { $type = 'normal'; } $headline = $this->getVariableCopy(array($copy[$network][$type]['headline']), array('year' => $year)); $insight_text = $this->getVariableCopy(array($copy[$network][$type]['body']), array('year' => $year, 'total' => $longest_streak['length'], 'date1' => $this->getDateFromDay($longest_streak['start_day'], 'F jS'), 'date2' => $this->getDateFromDay($longest_streak['end_day'], 'F jS'), 'qualified_year' => $qualified_year)); $milestones = array("per_row" => 1, "label_type" => "text", "items" => array(0 => array("number" => $longest_streak['length'], "label" => "days"))); $insight->setMilestones($milestones); $insight->headline = $headline; $insight->text = $insight_text; $insight->filename = basename(__FILE__, ".php"); $insight->emphasis = Insight::EMPHASIS_HIGH; $insight->header_image = $user->avatar; $this->insight_dao->insertInsight($insight); } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }
public function testInsightRelatedDataSetters() { $i = new Insight(); // @TODO Assign and assert that the data is in the array structure it should be $i->setPhotos("this is my list of photos"); $this->assertEqual($i->related_data["photos"], "this is my list of photos"); $i->setPosts("my posts"); $this->assertEqual($i->related_data["posts"], "my posts"); $i->setLineChart("line chart data goes here"); $this->assertEqual($i->related_data["line_chart"], "line chart data goes here"); $i->setBarChart("bar chart data goes here"); $this->assertEqual($i->related_data["bar_chart"], "bar chart data goes here"); $i->setPeople("list 'o users"); $this->assertEqual($i->related_data["people"], "list 'o users"); $i->setLinks("listoflinks"); $this->assertEqual($i->related_data["links"], "listoflinks"); $i->setMilestones("milestones"); $this->assertEqual($i->related_data["milestones"], "milestones"); $i->setButton("button"); $this->assertEqual($i->related_data["button"], "button"); }
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__); $milestones = array(); $info = array('text' => ''); if ($instance->network == 'twitter') { $day_of_week = 1; } elseif ($instance->network == 'instagram') { $day_of_week = 3; } else { $day_of_week = 5; } $should_generate_insight = self::shouldGenerateWeeklyInsight('frequency', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = $day_of_week); if ($should_generate_insight) { $count = sizeof($last_week_of_posts); $this->logger->logInfo("Last week had {$count} posts", __METHOD__ . ',' . __LINE__); //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; $photo_count = 0; $video_count = 0; foreach ($last_week_of_posts as $post) { if ($post->is_short_video) { $video_count++; } else { $photo_count++; } } } if ($count > 1) { if ($instance->network !== 'instagram') { $info['headline'] = $this->getVariableCopy(array('%username %posted <strong>%count times</strong> in the past week', '%username had <strong>%count %posts</strong> over the past week', '%username had <strong>%count %posts</strong> over the past week'), array('count' => number_format($count))); $milestones = array("per_row" => 1, "label_type" => "icon", "items" => array(0 => array("number" => number_format($count), "label" => $this->terms->getNoun('post', $count)))); } else { //Network is Instagram so count photos and videos separately if ($photo_count > 0 && $video_count > 0) { $photos_term = $photo_count == 1 ? 'photo' : 'photos'; $videos_term = $video_count == 1 ? 'video' : 'videos'; $headline = $this->username . " had {$photo_count} {$photos_term} and {$video_count} {$videos_term} over the past week"; } else { if ($photo_count > 0) { $headline = $this->username . " had {$photo_count} photos over the past week"; } else { $headline = $this->username . " had {$video_count} videos over the past week"; } } $info['headline'] = $headline; $milestones = array("per_row" => 1, "label_type" => "icon", "items" => array(0 => array("number" => number_format($count), "label" => $this->terms->getNoun('post', $count)))); } } elseif ($count == 1) { $info['headline'] = $this->getVariableCopy(array('%username %posted <strong>once</strong> in the past week'), array('count' => number_format($count))); $milestones = array("per_row" => 1, "label_type" => "icon", "items" => array(0 => array("number" => number_format($count), "label" => $this->terms->getNoun('post', $count)))); } else { if ($instance->network == 'twitter') { $info = $this->getVariableCopyArray(array(array('headline' => "%username didn't have any new %posts this week", 'text' => "Nothing wrong with waiting until there's something to say.", 'button' => array("url" => "http://twitter.com/intent/tweet?text=Hey there, friends.", "label" => "Have anything to say now?")), array('headline' => '%username didn\'t post anything new on Twitter in the past week', 'text' => 'Sometimes we just don\'t have anything to say. Maybe let someone know you ' . 'appreciate their work?', 'button' => array("url" => "http://twitter.com/intent/tweet?text=You know who is really great?", "label" => "Tweet a word of praise")), array('headline' => "Seems like %username was pretty quiet on Twitter this past week", 'text' => "Nothing wrong with waiting until there's something to say.", 'button' => array("url" => "http://twitter.com/intent/tweet?text=Sorry I haven't tweeted in a while!", "label" => "Or just say hi to everyone.")))); } else { if ($instance->network == 'facebook') { $info = $this->getVariableCopyArray(array(array('headline' => '%username didn\'t have any new %posts this week', 'text' => 'Nothing wrong with waiting until there\'s something to say.', 'button' => array('url' => 'http://www.facebook.com/sharer/sharer.php?t=ThinkUp told me to say hi.', 'label' => 'Maybe you\'ve got something to say now?')), array('headline' => '%username didn\'t post anything new on Facebook in the past week', 'text' => 'Nothing wrong with being quiet. If you want, you could ask your friends ' . 'what they\'ve read lately.', 'button' => array('url' => 'http://www.facebook.com/sharer/sharer.php' . '?u=http://upload.wikimedia.org/wikipedia/en/4/43/' . 'FlanneryOConnorCompleteStories.jpg&' . 't=Ready any good books lately?', 'label' => 'Read any good books lately?')), array('headline' => 'Seems like %username was pretty quiet on Facebook this past week', 'text' => "Nothing wrong with waiting until there's something to say.", 'button' => array('url' => 'http://www.facebook.com/sharer/sharer.php?t=Hey there, friends!', 'label' => 'Or just say hi to your friends?')))); } else { $info = array('headline' => $this->getVariableCopy(array('%username didn\'t post any new %posts this week')), 'text' => "Huh, nothing. Fill the emptiness inside you by donating to an underfunded classroom.", 'button' => array("url" => "http://www.donorschoose.org/", "label" => "Give to DonorsChoose.org")); } } } $insight_baseline_dao = DAOFactory::getDAO('InsightBaselineDAO'); $insight_baseline_dao->insertInsightBaseline("frequency", $instance->id, $count, $this->insight_date); if ($instance->network == 'instagram') { $insight_baseline_dao->insertInsightBaseline("frequency_photo_count", $instance->id, $photo_count, $this->insight_date); $insight_baseline_dao->insertInsightBaseline("frequency_video_count", $instance->id, $video_count, $this->insight_date); } if ($count > 0) { //Week over week comparison $week_ago_date = date('Y-m-d', strtotime('-7 day')); if ($instance->network !== 'instagram') { //Just compare total posts //Get insight baselines from a week ago $week_ago_insight_baseline = $insight_baseline_dao->getInsightBaseline("frequency", $instance->id, $week_ago_date); if (isset($week_ago_insight_baseline)) { $this->logger->logInfo("Baseline had {$week_ago_insight_baseline->value} posts", __METHOD__ . ',' . __LINE__); //compare it to today's number, and add a sentence comparing it $diff = abs($week_ago_insight_baseline->value - $count); $comp = $week_ago_insight_baseline->value > $count ? 'fewer' : 'more'; if ($diff == 1) { $info['text'] = $this->terms->getProcessedText("That's 1 {$comp} %post than the prior week."); } elseif ($diff > 0) { $info['text'] = $this->terms->getProcessedText("That's " . number_format($diff) . " {$comp} %posts than the prior week."); } } } else { //Compare photos and videos separately //Get insight baselines from a week ago $week_ago_photo_count_insight_baseline = $insight_baseline_dao->getInsightBaseline("frequency_photo_count", $instance->id, $week_ago_date); $week_ago_video_count_insight_baseline = $insight_baseline_dao->getInsightBaseline("frequency_video_count", $instance->id, $week_ago_date); if (isset($week_ago_photo_count_insight_baseline) && isset($week_ago_video_count_insight_baseline)) { $this->logger->logInfo("Baseline had {$week_ago_photo_count_insight_baseline->value} photos " . " and {$week_ago_video_count_insight_baseline->value} videos", __METHOD__ . ',' . __LINE__); //compare it to today's number, and add a sentence comparing it //First, photos $diff_photos = abs($week_ago_photo_count_insight_baseline->value - $photo_count); $comp_photos = $week_ago_photo_count_insight_baseline->value > $photo_count ? 'fewer' : 'more'; //Next, videos $diff_videos = abs($week_ago_video_count_insight_baseline->value - $video_count); $comp_videos = $week_ago_video_count_insight_baseline->value > $video_count ? 'fewer' : 'more'; $this->logger->logInfo("Diff photos " . $diff_photos . ", diff videos " . $diff_videos, __METHOD__ . ',' . __LINE__); if ($diff_photos > 0) { if ($diff_photos == 1) { $photo_comp_phrase = " 1 {$comp_photos} photo "; } else { $photo_comp_phrase = " " . number_format($diff_photos) . " {$comp_photos} photos "; } if ($diff_videos > 0) { if ($diff_videos == 1) { $video_comp_phrase = " and 1 {$comp_videos} video "; } else { $video_comp_phrase = " and " . number_format($diff_videos) . " {$comp_videos} videos "; } } else { $video_comp_phrase = ''; } $info['text'] = "That's " . $photo_comp_phrase . $video_comp_phrase . " than the prior week."; } } } } //Instantiate the Insight object $my_insight = new Insight(); //REQUIRED: Set the insight's required attributes $my_insight->instance_id = $instance->id; $my_insight->slug = 'frequency'; //slug to label this insight's content $my_insight->date = $this->insight_date; //date of the data this insight applies to $my_insight->header_image = $user->avatar; $my_insight->emphasis = Insight::EMPHASIS_LOW; //Set emphasis optionally, default is Insight::EMPHASIS_LOW $my_insight->filename = basename(__FILE__, ".php"); //Same for every insight, must be set exactly this way $my_insight->setMilestones($milestones); if (isset($info['button'])) { $my_insight->setButton($info['button']); } $my_insight->headline = $info['headline']; if (!empty($info['text'])) { $my_insight->text = $info['text']; $this->insight_dao->insertInsight($my_insight); } else { $this->logger->logInfo("No insight text set, so no insight inserted", __METHOD__ . ',' . __LINE__); } } $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, $count_related_posts = null, array('instagram')); 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}"; $insight->emphasis = Insight::EMPHASIS_HIGH; $insight->filename = basename(__FILE__, ".php"); $insight->header_image = $user->avatar; $post_dao = DAOFactory::getDAO('PostDAO'); $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 . ")"; } } $copy = array('twitter' => array('normal' => array('headline' => "%username tweeted %total times in %year", 'body' => "In %year, %username posted a total of <b>%total tweets</b>%qualified_year. " . "At 15 seconds per tweet, that amounts to <b>%time</b>. %username's followers probably " . "appreciated it.")), 'facebook' => array('normal' => array('headline' => "%username posted to Facebook %total times in %year", 'body' => "This year, %username posted a grand total of " . "<b>%total times</b> on Facebook%qualified_year. If each status update and comment " . "took about 15 seconds, that's " . "over <b>%time</b> dedicated to keeping in touch with friends."))); $network = $instance->network; $total_post_count = $this->getTotalPostCount($instance, $year); $posting_time = $this->getPostingTime($total_post_count); if ($total_post_count > 1) { $type = 'normal'; } else { return; } $insight->headline = $this->getVariableCopy(array($copy[$network][$type]['headline']), array('total' => number_format($total_post_count), 'year' => $year)); $insight->text = $this->getVariableCopy(array($copy[$network][$type]['body']), array('total' => number_format($total_post_count), 'year' => $year, 'time' => $posting_time, 'qualified_year' => $qualified_year)); $milestones = array("label_type" => "icon", "items" => array(0 => array("number" => number_format($total_post_count)))); $insight->setMilestones($milestones); $this->insight_dao->insertInsight($insight); $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__); if ($instance->network == 'twitter') { $day_of_week = 3; } else { $day_of_week = 6; } $should_generate_insight = self::shouldGenerateWeeklyInsight('interactions', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = 3, count($last_week_of_posts), $excluded_networks = array('facebook', 'google+', 'foursquare', 'youtube')); if ($should_generate_insight) { $user_dao = DAOFactory::getDAO('UserDAO'); $mentions_count = array(); $mentions_info = array(); $mentions_avatars = array(); $insight_data = array(); $insight_text = ''; if ($instance->network == 'twitter') { $talk_time = 15; } else { $talk_time = 38; } foreach ($last_week_of_posts as $post) { if ($post->in_reply_to_user_id && $instance->network_user_id != $post->in_reply_to_user_id) { $mentioned_user = $user_dao->getDetails($post->in_reply_to_user_id, $instance->network); if (isset($mentioned_user)) { $mention_in_post = $instance->network == 'twitter' ? '@' : ''; $mention_in_post .= $mentioned_user->username; $mentions_info[$mention_in_post] = $mentioned_user; // Update mention count if (array_key_exists($mention_in_post, $mentions_count)) { $mentions_count[$mention_in_post]++; } else { $mentions_count[$mention_in_post] = 1; } } } } if (count($mentions_count)) { // Get most mentioned user arsort($mentions_count); $most_mentioned_user = each($mentions_count); // Add mentions to dataset $users_mentioned = array(); foreach (array_slice($mentions_count, 0, 10) as $mention => $count) { $mention_info['mention'] = $mention; $mention_info['count'] = $count; $mention_info['user'] = $mentions_info[$mention]; $users_mentioned[] = $mention_info; } } if (isset($most_mentioned_user) && $talk_time * $most_mentioned_user['value'] >= 60) { $headline = $this->username . " replied to " . $most_mentioned_user['key'] . " <strong>" . $this->terms->getOccurrencesAdverb($most_mentioned_user['value']) . "</strong> last week"; $conversation_seconds = $this->terms->getOccurrencesAdverb($most_mentioned_user['value']) * $talk_time; $milestones = $this->convertSecondsToMilestone($conversation_seconds); $insight_text = 'Time having good conversation is time well spent.'; // $header_image = $users_mentioned[0][user]->avatar; $header_image = $users_mentioned[0]["user"]->avatar; //Instantiate the Insight object $my_insight = new Insight(); //REQUIRED: Set the insight's required attributes $my_insight->instance_id = $instance->id; $my_insight->slug = 'interactions'; //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 = $header_image; $my_insight->emphasis = Insight::EMPHASIS_MED; $my_insight->filename = basename(__FILE__, ".php"); $my_insight->setPeople($users_mentioned); $my_insight->setMilestones($milestones); $this->insight_dao->insertInsight($my_insight); } } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }