示例#1
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     $baseline_dao = DAOFactory::getDAO('InsightBaselineDAO');
     foreach ($this->getSchedule() as $baseline_slug => $data) {
         $now = TimeHelper::getTime();
         if ($now >= strtotime($data['start']) && $now <= strtotime($data['end'])) {
             $this->logger->logInfo("{$now} is in-schedule", __METHOD__ . ',' . __LINE__);
             $baseline = $baseline_dao->getMostRecentInsightBaseline($baseline_slug, $instance->id);
             if (!$baseline) {
                 if ($instance->network == 'facebook' && date('w') == 4 || $instance->network == 'twitter' && date('w') == 1 || Utils::isTest()) {
                     $found = $this->runInsightForConfig($data, $instance);
                     $baseline_dao->insertInsightBaseline($baseline_slug, $instance->id, $found);
                 } else {
                     $this->logger->logInfo("Not today", __METHOD__ . ',' . __LINE__);
                 }
             } else {
                 $this->logger->logInfo("Already exists", __METHOD__ . ',' . __LINE__);
             }
         } else {
             $this->logger->logInfo("Not in-schedule", __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__);
     $possible_messaging = array(array('headline' => 'Ohai', 'text' => 'Greetings, humans', 'header_image' => 'http://farm3.staticflickr.com/2713/4098259769_725b5fb65b_o.jpg'), array('headline' => 'Hello', 'text' => 'Greetings, earthlings', 'header_image' => 'http://farm9.staticflickr.com/8078/8276342554_5a51725f5f_n.jpg'), array('headline' => 'Yo', 'text' => 'Greetings, peeps', 'header_image' => 'http://farm6.staticflickr.com/5006/5367216303_83c5f2dc39_n.jpg'));
     //Instantiate the Insight object
     $my_insight = new Insight();
     //REQUIRED: Set the insight's required attributes
     //We pull some from the options above.  But the could just be strings like 'Ohai'
     $which_messaging = TimeHelper::getTime() % count($possible_messaging);
     foreach ($possible_messaging[$which_messaging] as $field => $value) {
         $my_insight->{$field} = $value;
     }
     $my_insight->slug = 'my_test_insight_hello_thinkup';
     //slug to label this insight's content
     $my_insight->instance_id = $instance->id;
     $my_insight->date = $this->insight_date;
     //date is often this or $simplified_post_date
     $my_insight->filename = basename(__FILE__, ".php");
     //Same for every insight, must be set exactly this way
     $my_insight->emphasis = Insight::EMPHASIS_MED;
     //Set emphasis optionally, default is Insight::EMPHASIS_LOW
     //OPTIONAL: Attach related data of various types using Insight setter functions
     //$my_insight->setPosts($my_insight_posts);
     //$my_insight->setLinks($my_insight_links);
     //$my_insight->setPeople($my_insight_people);
     //$my_insight->setMilestones($my_insight_milestones);
     //etc
     $this->insight_dao->insertInsight($my_insight);
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
示例#3
0
 public function testSetClearTime()
 {
     $start = TimeHelper::getTime();
     $this->assertNotEqual($start, 4179);
     TimeHelper::setTime(4179);
     $now = TimeHelper::getTime();
     $this->assertEqual(4179, $now);
     TimeHelper::clearTime();
     $now = TimeHelper::getTime();
     $this->assertNotEqual(4179, $now);
 }
 /**
  * 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);
 }
示例#5
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     $filename = basename(__FILE__, ".php");
     $insight_text = '';
     //Find out if insight already exists
     $should_generate_insight = self::shouldGenerateInsight('top_amplifier', $instance, date('Y-m-d'));
     if ($should_generate_insight) {
         //insight does not exist
         //Get yesterday's retweets
         $yesterdays_retweets = array();
         $simplified_date_yesterday = date('Y-m-d', strtotime('-1 day'));
         foreach ($last_week_of_posts as $post) {
             if ($post->in_retweet_of_post_id != null && $post->in_rt_of_user_id != null) {
                 $simplified_post_date = date('Y-m-d', strtotime($post->pub_date));
                 if ($simplified_date_yesterday == $simplified_post_date) {
                     $yesterdays_retweets[] = $post;
                 }
             }
         }
         $largest_added_audience = 0;
         $insight_retweeted_user = null;
         $insight_retweet = null;
         //Get top amplifier from yesterday
         foreach ($yesterdays_retweets as $post) {
             if (!isset($user_dao)) {
                 $user_dao = DAOFactory::getDAO('UserDAO');
             }
             $retweeted_user = $user_dao->getDetails($post->in_rt_of_user_id, $post->network);
             //if user exists and has fewer followers than instance user compare to current top
             if (isset($retweeted_user) && $retweeted_user->follower_count < $user->follower_count) {
                 $added_audience = $user->follower_count - $retweeted_user->follower_count;
                 if ($added_audience > $largest_added_audience) {
                     $largest_added_audience = $added_audience;
                     $insight_retweeted_user = $retweeted_user;
                     $insight_retweet = $post;
                 }
             }
         }
         //If there's a top amplifier from yesterday, insert the insight
         if ($largest_added_audience > 0 && isset($insight_retweeted_user) && isset($insight_retweet)) {
             $multiplier = floor($user->follower_count / $insight_retweeted_user->follower_count);
             if ($multiplier > 1 && TimeHelper::getTime() / 10 % 2 == 1) {
                 $largest_added_audience = number_format($multiplier) . 'x';
             } else {
                 $largest_added_audience = number_format($largest_added_audience);
             }
             $retweeted_username = $insight_retweeted_user->username;
             if ($instance->network == 'twitter') {
                 $retweeted_username = '******' . $retweeted_username;
             }
             $headline = $this->getVariableCopy(array($insight_retweeted_user->full_name . " can thank %username for %added more people seeing this %post", "%added more people saw %repostedee's %post thanks to %username", '%username boosted ' . $insight_retweeted_user->full_name . '\'s %post to %added more people'), array('repostedee' => $retweeted_username, 'added' => $largest_added_audience));
             $my_insight = new Insight();
             $my_insight->instance_id = $instance->id;
             $my_insight->slug = 'top_amplifier';
             //slug to label this insight's content
             $my_insight->date = date('Y-m-d');
             //date of the data this insight applies to
             $my_insight->headline = $headline;
             // or just set a string like 'Ohai';
             $my_insight->text = $insight_text;
             // or just set a strong like "Greetings humans";
             $my_insight->header_image = $insight_retweeted_user->avatar;
             $my_insight->emphasis = Insight::EMPHASIS_MED;
             $my_insight->filename = basename(__FILE__, ".php");
             $my_insight->setPosts(array($insight_retweet));
             $my_insight->setPeople(array($insight_retweeted_user));
             $this->insight_dao->insertInsight($my_insight);
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
示例#6
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     if ($instance->network == 'twitter' && isset($user->joined)) {
         $firstrun = !$this->insight_dao->doesInsightExist($this->slug, $instance->id);
         if ($firstrun) {
             $joined_timestamp = strtotime($user->joined);
             $twitter_start = strtotime('July 15, 2006');
             $first_half_of_twitters_life = floor((TimeHelper::getTime() - $twitter_start) * 0.5);
             // $start = $twitter_start + $first_half_of_twitters_life;
             // echo "Second half of Twitter's lifetime currently starts on ".gmdate("Y-m-d", $start);
             $explainer = '';
             if ($joined_timestamp >= strtotime('March 20, 2006')) {
                 //Date's gotta be in range of Twitter's life
                 if ($joined_timestamp <= strtotime('July 15, 2006')) {
                     $headline = $this->username . ' was either a super-early Twitter user or an Odeo employee';
                     $explainer = "That's before Twitter even launched!";
                 } else {
                     if ($joined_timestamp < strtotime('March 5, 2007')) {
                         $headline = 'Before Barack Obama joined Twitter...';
                     } else {
                         if ($joined_timestamp < strtotime('August 23, 2007')) {
                             $headline = 'Before the hashtag, there was ' . $this->username;
                             $explainer = 'That\'s before the hashtag was even ' . ' <a href="https://twitter.com/chrismessina/status/223115412">' . 'invented</a>!';
                         } else {
                             if ($joined_timestamp < strtotime('March 26, 2008')) {
                                 $headline = 'Before Lady Gaga joined Twitter...';
                             } else {
                                 if ($joined_timestamp < strtotime('August 14, 2008')) {
                                     $headline = 'Before Ellen DeGeneres joined Twitter...';
                                 } else {
                                     if ($joined_timestamp < strtotime('January 16, 2009')) {
                                         $headline = 'Before Ashton Kutcher joined Twitter...';
                                     } else {
                                         if ($joined_timestamp < strtotime('January 23, 2009')) {
                                             $headline = 'Before Oprah Winfrey joined Twitter...';
                                         } else {
                                             if ($joined_timestamp < strtotime('February 20, 2009')) {
                                                 $headline = 'Before Katy Perry joined Twitter...';
                                             } else {
                                                 if ($joined_timestamp < strtotime('March 28, 2009')) {
                                                     $headline = 'Before Justin Bieber joined Twitter...';
                                                 } else {
                                                     if ($joined_timestamp < strtotime('July 2, 2009')) {
                                                         $headline = 'Before Tyra Banks joined Twitter...';
                                                         // At time of dev, 50% of Twitter's life ago was 2010-06-12
                                                         // As time passes, this date will get later
                                                     } else {
                                                         if ($joined_timestamp < $twitter_start + $first_half_of_twitters_life) {
                                                             $headline = $this->getVariableCopy(array('Somebody is an early bird!', 'Achievement unlocked: %username is old-school'));
                                                         } else {
                                                             if ($joined_timestamp < strtotime('September 8, 2011')) {
                                                                 $headline = 'One of the first 100 million Twitter users...';
                                                             } else {
                                                                 if ($joined_timestamp < strtotime('November 7, 2013')) {
                                                                     $headline = 'Pre-IPO!';
                                                                     $explainer = "That's even before Twitter's initial public offering on November 7, 2013.";
                                                                 } else {
                                                                     if ($joined_timestamp > strtotime('-6 months')) {
                                                                         $headline = 'Welcome to the party';
                                                                     } else {
                                                                         $headline = 'One in 200 million...';
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $seconds_joined = TimeHelper::getTime() - $joined_timestamp;
                 $year_seconds = 365 * 24 * 60 * 60;
                 $month_seconds = 30 * 24 * 60 * 60;
                 $week_seconds = 7 * 24 * 60 * 60;
                 $years = floor($seconds_joined / $year_seconds);
                 $months = floor(($seconds_joined - $year_seconds * $years) / $month_seconds);
                 $weeks = floor(($seconds_joined - $year_seconds * $years - $month_seconds * $months) / $week_seconds);
                 $percentage = sprintf('%d%%', $seconds_joined / (TimeHelper::getTime() - $twitter_start) * 100);
                 if ($seconds_joined >= $year_seconds) {
                     $text = $this->username . " joined Twitter {$years} year" . ($years == 1 ? '' : 's');
                     if ($months) {
                         $text .= " and {$months} month" . ($months == 1 ? '' : 's');
                     }
                     $text .= " ago.";
                     if ($explainer == '') {
                         $explainer = "That's over {$percentage} of Twitter's lifetime!";
                     }
                 } else {
                     if ($seconds_joined >= $month_seconds) {
                         $text = $this->username . " joined Twitter {$months} month" . ($months == 1 ? '' : 's');
                         if ($weeks) {
                             $text .= " and {$weeks} week" . ($weeks == 1 ? '' : 's');
                         }
                         $text .= " ago.";
                         if ($explainer == '') {
                             $explainer = "That's over {$percentage} of Twitter's lifetime!";
                         }
                     } else {
                         $text = $this->username . " joined Twitter ";
                         if ($weeks < 2) {
                             $text .= "this week.";
                         } else {
                             $text .= $weeks . " weeks ago.";
                         }
                         $text .= " Take a bow!";
                     }
                 }
                 if ($explainer !== '') {
                     $text .= " " . $explainer;
                 }
                 $insight = new Insight();
                 $insight->slug = $this->slug;
                 $insight->instance_id = $instance->id;
                 $insight->date = $this->insight_date;
                 $insight->headline = $headline;
                 $insight->text = $text;
                 $insight->emphasis = Insight::EMPHASIS_HIGH;
                 $insight->filename = basename(__FILE__, ".php");
                 $this->insight_dao->insertInsight($insight);
             }
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
 /**
  * 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;
 }
示例#8
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     if (self::shouldGenerateInsight('posts_on_this_day_popular_flashback', $instance)) {
         //Generate flashback post list
         $post_dao = DAOFactory::getDAO('PostDAO');
         $flashback_posts = $post_dao->getOnThisDayFlashbackPosts($instance->network_user_id, $instance->network, $this->insight_date);
         $posts = array();
         $most_popular_post = null;
         $most_responses = 0;
         $insight_text = '';
         if (isset($flashback_posts) && sizeof($flashback_posts) > 0) {
             foreach ($flashback_posts as $post) {
                 if ($post->network == 'instagram') {
                     $photo_dao = DAOFactory::getDAO('PhotoDAO');
                     $post = $photo_dao->getPhoto($post->post_id, 'instagram');
                 }
                 $total_responses = $post->reply_count_cache + $post->all_retweets + $post->favlike_count_cache;
                 if ($total_responses > 0 && $total_responses > $most_responses) {
                     $most_popular_post = $post;
                     $most_responses = $total_responses;
                 }
             }
             if (isset($most_popular_post)) {
                 $post_year = date(date('Y', strtotime($most_popular_post->pub_date)));
                 $current_year = date('Y');
                 $number_of_years_ago = $current_year - $post_year;
                 $plural = $number_of_years_ago > 1 ? 's' : '';
                 $time = strtotime("-" . $number_of_years_ago . " year", time());
                 $past_date = date('Y', $time);
                 $post_term = $most_popular_post->is_short_video ? 'video' : $this->terms->getNoun('post');
                 if (TimeHelper::getTime() % 2 == 0) {
                     if ($number_of_years_ago == 1) {
                         $headlines = array("A year ago today");
                     } else {
                         $headlines = array("%number_of_years_ago years ago today");
                     }
                     $insight_text = "On this day in " . $past_date . ", this was {$this->username}'s most popular " . $post_term . ".";
                 } else {
                     $headlines = array("On this day in " . $past_date);
                     $insight_text = "This was {$this->username}'s most popular " . $post_term . " <strong>{$number_of_years_ago} year{$plural} ago</strong>.";
                 }
                 $posts[] = $most_popular_post;
                 $my_insight = new Insight();
                 $my_insight->instance_id = $instance->id;
                 $my_insight->slug = 'posts_on_this_day_popular_flashback';
                 //slug to label this insight's content
                 $my_insight->date = $this->insight_date;
                 //date of the data this insight applies to
                 $my_insight->headline = $this->getVariableCopy($headlines, array('number_of_years_ago' => $number_of_years_ago));
                 $my_insight->text = $insight_text;
                 $my_insight->emphasis = Insight::EMPHASIS_LOW;
                 $my_insight->filename = basename(__FILE__, ".php");
                 $my_insight->setPosts($posts);
                 $this->insight_dao->insertInsight($my_insight);
             }
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
示例#9
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     $joined_ts = strtotime($user->joined, TimeHelper::getTime());
     $viable = $joined_ts > strtotime('2000-01-1');
     $joined_day = date('m-d', $joined_ts);
     $is_twitter = $instance->network == 'twitter';
     if ($viable && $is_twitter && date('m-d', TimeHelper::getTime()) == $joined_day) {
         $insight = new Insight();
         $insight->slug = "twitterbirthday";
         $insight->instance_id = $instance->id;
         $insight->date = $this->insight_date;
         $insight->emphasis = Insight::EMPHASIS_HIGH;
         $insight->filename = basename(__FILE__, ".php");
         $follow_dao = DAOFactory::getDAO('FollowDAO');
         $all_friends = $follow_dao->countTotalFriends($instance->network_user_id, $instance->network);
         $late_friends = $follow_dao->countTotalFriendsJoinedAfterDate($instance->network_user_id, $instance->network, $user->joined);
         $years = date('Y', TimeHelper::getTime()) - date('Y', $joined_ts);
         $ordinal_age = $this->terms->getOrdinalAdverb($years);
         $insight->headline = "A Very Happy {$ordinal_age} Twitter Birthday!";
         $insight->text = $this->username . " joined Twitter {$years} year" . ($years == 1 ? '' : 's') . " ago today";
         if ($all_friends > 0 && $late_friends > 0) {
             $percent_before = floor($late_friends / $all_friends * 100);
             $insight->text .= ", " . "before " . $percent_before . "% of the people " . $this->username . " follows did.";
         } else {
             $insight->text .= ".";
         }
         $week_seconds = 60 * 60 * 24 * 7;
         $followers = $follow_dao->getFriendsJoinedInTimeFrame($user->user_id, $instance->network, date('Y-m-d', $joined_ts - $week_seconds), date('Y-m-d', $joined_ts + $week_seconds));
         $just_before = null;
         $just_after = null;
         $last_user = null;
         foreach ($followers as $follower) {
             if (strtotime($follower->joined, TimeHelper::getTime()) > $joined_ts) {
                 $just_after = $follower;
                 $just_before = $last_user;
                 break;
             }
             $last_user = $follower;
         }
         if (!$just_after && $last_user) {
             $just_before = $last_user;
         }
         $bonus_text = array();
         $people = array();
         if ($just_before) {
             $time = TimeHelper::secondsToGeneralTime(abs($joined_ts - strtotime($just_before->joined, TimeHelper::getTime())));
             $bonus_text[] = "@" . $just_before->username . " just beat " . $this->username . ", joining {$time} earlier";
             $people[] = $just_before;
         }
         if ($just_after) {
             $time = TimeHelper::secondsToGeneralTime(abs($joined_ts - strtotime($just_after->joined, TimeHelper::getTime())));
             $bonus_text[] = "@" . $just_after->username . " was a little slower, getting on Twitter {$time} later";
             $people[] = $just_after;
         }
         if (count($bonus_text)) {
             $insight->text .= " " . join(', and ', $bonus_text) . ".";
             $insight->setPeople($people);
         }
         $res = $this->insight_dao->insertInsight($insight);
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
示例#10
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     $filename = basename(__FILE__, ".php");
     $last_insight = $this->insight_dao->getMostRecentInsight('new_group_memberships', $instance->id);
     $last_insight_ts = 0;
     if ($last_insight) {
         $last_insight_ts = strtotime($last_insight->time_generated);
     }
     if ($last_insight_ts < time() - 60 * 60 * 24 * 7) {
         //get new group memberships per day
         $group_membership_dao = DAOFactory::getDAO('GroupMemberDAO');
         $new_groups = $group_membership_dao->getNewMembershipsSince($instance->network, $instance->network_user_id, date('Y-m-d h:i', $last_insight_ts));
         if (sizeof($new_groups) > 0) {
             // Clean up non-unique names, which just looks weird/bad
             $unique_new_groups = array();
             $seen_groups = array();
             foreach ($new_groups as $group) {
                 $group->setMetadata();
                 if (!in_array($group->keyword, $seen_groups)) {
                     $seen_groups[] = $group->keyword;
                     $unique_new_groups[] = $group;
                 }
             }
             $new_groups = $unique_new_groups;
             $count_history_dao = DAOFactory::getDAO('CountHistoryDAO');
             $list_membership_count_history_by_day = $count_history_dao->getHistory($instance->network_user_id, $instance->network, 'DAY', 15, null, 'group_memberships');
             if (sizeof($new_groups) > 1) {
                 $group_names = array();
                 $group_urls = array();
                 foreach ($new_groups as $group) {
                     $group_names[] = '&ldquo;' . str_replace('-', ' ', $group->keyword) . '&rdquo;';
                     $group_urls[] = '<a href="' . $group->url . '">' . $group->keyword . '</a>';
                 }
                 $headline_groups = array_slice($group_names, 0, 4);
                 $number = count($headline_groups);
                 foreach ($headline_groups as $i => &$name) {
                     if ($number == 2) {
                         if ($i == 1) {
                             $name = 'and ' . $name;
                         }
                     } else {
                         $name = $i == $number - 1 ? 'and ' . $name : $name . ',';
                     }
                 }
                 $headline = "{$number} new lists for {$this->username}'s collection";
                 if (count($group_urls) > 4) {
                     $group_name_list = join(', ', array_slice($group_urls, 0, 4)) . ', and ' . (count($group_urls) - 4) . ' more';
                 } else {
                     if (count($group_urls) > 2) {
                         $group_urls[count($group_urls) - 1] = 'and ' . $group_urls[count($group_urls) - 1];
                         $group_name_list = join(', ', $group_urls);
                     } else {
                         $group_name_list = join(' and ', $group_urls);
                     }
                 }
                 $insight_text = "{$this->username} is on " . sizeof($new_groups) . " new lists: " . $group_name_list;
                 if (is_array($list_membership_count_history_by_day['history']) && end($list_membership_count_history_by_day['history']) > sizeof($new_groups)) {
                     $total_lists = end($list_membership_count_history_by_day['history']) + sizeof($new_groups);
                     $insight_text .= ", bringing the total to <strong>" . number_format($total_lists) . " lists</strong>.";
                 } else {
                     $insight_text .= ".";
                 }
             } else {
                 if (TimeHelper::getTime() % 2 == 1 && $instance->network == 'twitter') {
                     $headline = "A new list for {$this->username}'s collection";
                 } else {
                     $headline = "A new list called &ldquo;" . str_replace('-', ' ', $new_groups[0]->keyword) . "&rdquo;";
                 }
                 $insight_text = "{$this->username} got added to a new list, " . '<a href="' . $new_groups[0]->url . '">' . $new_groups[0]->keyword . "</a>";
                 if (end($list_membership_count_history_by_day['history']) > sizeof($new_groups)) {
                     $total_lists = end($list_membership_count_history_by_day['history']) + sizeof($new_groups);
                     $insight_text .= ", bringing the total to <strong>" . number_format($total_lists) . " lists</strong>";
                 }
                 $insight_text .= ".";
             }
             $insight = new Insight();
             $insight->slug = 'new_group_memberships';
             $insight->instance_id = $instance->id;
             $insight->date = $this->insight_date;
             $insight->filename = basename(__FILE__, ".php");
             $insight->emphasis = Insight::EMPHASIS_LOW;
             $insight->headline = $headline;
             $insight->text = $insight_text;
             if (count($list_membership_count_history_by_day['history']) >= 3) {
                 $insight->related_data = serialize($list_membership_count_history_by_day);
             }
             $this->insight_dao->insertInsight($insight);
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }