Skip to content

Commit

Permalink
Simplify insight plugin code
Browse files Browse the repository at this point in the history
  • Loading branch information
ginatrapani committed Jul 22, 2012
1 parent 446d058 commit 010495e
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 158 deletions.
43 changes: 17 additions & 26 deletions webapp/plugins/insightsgenerator/insights/flashbacks.php
Expand Up @@ -37,34 +37,25 @@ public function generateInsight(Instance $instance, $last_week_of_posts, $number

$insight_dao = DAOFactory::getDAO('InsightDAO');
$post_dao = DAOFactory::getDAO('PostDAO');
// Flashback posts
$days_ago = $number_days;
while ($days_ago > -1) {
$insight_date = new DateTime();
$insight_date->modify('-'.$days_ago.' day');
$insight_day_of_week = (int) $insight_date->format('w');
$this->logger->logInfo("Insight day of week is ".$insight_day_of_week, __METHOD__.','
.__LINE__);
$insight_date_formatted = $insight_date->format('Y-m-d');

$existing_insight = $insight_dao->getInsight("posts_on_this_day_flashback", $instance->id,
$insight_date_formatted);
if (!isset($existing_insight)) {
//Generate flashback post list
$flashback_posts = $post_dao->getOnThisDayFlashbackPosts($instance->network_user_id,
$instance->network, $insight_date_formatted);
if (isset($flashback_posts) && sizeof($flashback_posts) > 0 ) {
$oldest_post_year = date(date( 'Y' , strtotime($flashback_posts[0]->pub_date)));
$current_year = date('Y');
$number_of_years_ago = $current_year - $oldest_post_year;
$plural = ($number_of_years_ago > 1 )?'s':'';
$insight_dao->insertInsight("posts_on_this_day_flashback", $instance->id,
$insight_date_formatted, $oldest_post_year." flashback:", $number_of_years_ago." year".
$plural. " ago today, you posted: ", Insight::EMPHASIS_MED, serialize($flashback_posts));
}
}
$insight_date = new DateTime();
$insight_date_formatted = $insight_date->format('Y-m-d');

$days_ago--;
$existing_insight = $insight_dao->getInsight("posts_on_this_day_flashback", $instance->id,
$insight_date_formatted);
if (!isset($existing_insight)) {
//Generate flashback post list
$flashback_posts = $post_dao->getOnThisDayFlashbackPosts($instance->network_user_id,
$instance->network, $insight_date_formatted);
if (isset($flashback_posts) && sizeof($flashback_posts) > 0 ) {
$oldest_post_year = date(date( 'Y' , strtotime($flashback_posts[0]->pub_date)));
$current_year = date('Y');
$number_of_years_ago = $current_year - $oldest_post_year;
$plural = ($number_of_years_ago > 1 )?'s':'';
$insight_dao->insertInsight("posts_on_this_day_flashback", $instance->id,
$insight_date_formatted, $oldest_post_year." flashback:", $number_of_years_ago." year".
$plural. " ago today, you posted: ", Insight::EMPHASIS_MED, serialize($flashback_posts));
}
}
$this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
}
Expand Down
99 changes: 46 additions & 53 deletions webapp/plugins/insightsgenerator/insights/followercounthistory.php
Expand Up @@ -39,65 +39,58 @@ public function generateInsight(Instance $instance, $last_week_of_posts, $number
$insight_dao = DAOFactory::getDAO('InsightDAO');

// Follower count history milestone
$days_ago = $number_days;
while ($days_ago > -1) {
$insight_date = new DateTime();
$insight_date->modify('-'.$days_ago.' day');
$insight_day_of_week = (int) $insight_date->format('w');
$this->logger->logInfo("Insight day of week is ".$insight_day_of_week, __METHOD__.','
.__LINE__);
$insight_date_formatted = $insight_date->format('Y-m-d');
$insight_date = new DateTime();
$insight_day_of_week = (int) $insight_date->format('w');
$insight_day_of_month = (int) $insight_date->format('j');
$insight_date_formatted = $insight_date->format('Y-m-d');

$insight_day_of_month = (int) $insight_date->format('j');
if ($insight_day_of_month == 1) { //it's the first day of the month
$follower_count_dao = DAOFactory::getDAO('FollowerCountDAO');
//by month
$follower_count_history_by_month = $follower_count_dao->getHistory($instance->network_user_id,
$instance->network, 'MONTH', 15, $insight_date_formatted);
$insight_text = "<strong>";
if ( isset($follower_count_history_by_month['milestone'])
&& $follower_count_history_by_month["milestone"]["will_take"] > 0
&& $follower_count_history_by_month["milestone"]["next_milestone"] > 0) {
$insight_text .= $follower_count_history_by_month['milestone']['will_take'].' month';
if ($follower_count_history_by_month['milestone']['will_take'] > 1) {
$insight_text .= 's';
}
$insight_text .= '</strong> till you reach <strong>'.
number_format($follower_count_history_by_month['milestone']['next_milestone']);
$insight_text .= '</strong> followers at your current growth rate.';
if ($insight_day_of_month == 1) { //it's the first day of the month
$follower_count_dao = DAOFactory::getDAO('FollowerCountDAO');
//by month
$follower_count_history_by_month = $follower_count_dao->getHistory($instance->network_user_id,
$instance->network, 'MONTH', 15, $insight_date_formatted);
$insight_text = "<strong>";
if ( isset($follower_count_history_by_month['milestone'])
&& $follower_count_history_by_month["milestone"]["will_take"] > 0
&& $follower_count_history_by_month["milestone"]["next_milestone"] > 0) {
$insight_text .= $follower_count_history_by_month['milestone']['will_take'].' month';
if ($follower_count_history_by_month['milestone']['will_take'] > 1) {
$insight_text .= 's';
}
$insight_text .= '</strong> till you reach <strong>'.
number_format($follower_count_history_by_month['milestone']['next_milestone']);
$insight_text .= '</strong> followers at your current growth rate.';

$insight_dao->insertInsight('follower_count_history_by_month_milestone', $instance->id,
$insight_date_formatted, "Upcoming milestone:", $insight_text, Insight::EMPHASIS_HIGH,
serialize($follower_count_history_by_month));
$insight_dao->insertInsight('follower_count_history_by_month_milestone', $instance->id,
$insight_date_formatted, "Upcoming milestone:", $insight_text, Insight::EMPHASIS_HIGH,
serialize($follower_count_history_by_month));
}
} else if ($insight_day_of_week == 0) { //it's Sunday
$follower_count_dao = DAOFactory::getDAO('FollowerCountDAO');
//by week
$follower_count_history_by_week = $follower_count_dao->getHistory($instance->network_user_id,
$instance->network, 'WEEK', 15, $insight_date_formatted);
$this->logger->logInfo($insight_date_formatted." is Sunday; Count by week stats are ".
Utils::varDumpToString($follower_count_history_by_week) , __METHOD__.','
.__LINE__);
$insight_text = "<strong>";
if ( isset($follower_count_history_by_week['milestone'])
&& $follower_count_history_by_week["milestone"]["will_take"] > 0
&& $follower_count_history_by_week["milestone"]["next_milestone"] > 0 ) {
$insight_text .= $follower_count_history_by_week['milestone']['will_take'].' week';
if ($follower_count_history_by_week['milestone']['will_take'] > 1) {
$insight_text .= 's';
}
} else if ($insight_day_of_week == 0) { //it's Sunday
$follower_count_dao = DAOFactory::getDAO('FollowerCountDAO');
//by week
$follower_count_history_by_week = $follower_count_dao->getHistory($instance->network_user_id,
$instance->network, 'WEEK', 15, $insight_date_formatted);
$this->logger->logInfo($insight_date_formatted." is Sunday; Count by week stats are ".
Utils::varDumpToString($follower_count_history_by_week) , __METHOD__.','
$insight_text .= '</strong> till you reach <strong>'.
number_format($follower_count_history_by_week['milestone']['next_milestone']);
$insight_text .= '</strong> followers at your current growth rate.';
$this->logger->logInfo("Storing insight ".$insight_text, __METHOD__.','
.__LINE__);
$insight_text = "<strong>";
if ( isset($follower_count_history_by_week['milestone'])
&& $follower_count_history_by_week["milestone"]["will_take"] > 0
&& $follower_count_history_by_week["milestone"]["next_milestone"] > 0 ) {
$insight_text .= $follower_count_history_by_week['milestone']['will_take'].' week';
if ($follower_count_history_by_week['milestone']['will_take'] > 1) {
$insight_text .= 's';
}
$insight_text .= '</strong> till you reach <strong>'.
number_format($follower_count_history_by_week['milestone']['next_milestone']);
$insight_text .= '</strong> followers at your current growth rate.';
$this->logger->logInfo("Storing insight ".$insight_text, __METHOD__.','
.__LINE__);

$insight_dao->insertInsight('follower_count_history_by_week_milestone', $instance->id,
$insight_date_formatted, "Upcoming milestone:", $insight_text, Insight::EMPHASIS_HIGH,
serialize($follower_count_history_by_week));
}
$insight_dao->insertInsight('follower_count_history_by_week_milestone', $instance->id,
$insight_date_formatted, "Upcoming milestone:", $insight_text, Insight::EMPHASIS_HIGH,
serialize($follower_count_history_by_week));
}
$days_ago--;
}
$this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
}
Expand Down
47 changes: 19 additions & 28 deletions webapp/plugins/insightsgenerator/insights/interestingfollowers.php
Expand Up @@ -39,37 +39,28 @@ public function generateInsight(Instance $instance, $last_week_of_posts, $number

// Least likely followers insights
$follow_dao = DAOFactory::getDAO('FollowDAO');
$days_ago = 0;
while ($days_ago < $number_days) {
//For each of the past 7 days (remove this later & just do day by day?)
//get least likely followers for that day
$least_likely_followers = $follow_dao->getLeastLikelyFollowersByDay($instance->network_user_id,
$instance->network, $days_ago, 3);
if (sizeof($least_likely_followers) > 0 ) { //if not null, store insight
//If followers have more followers than half of what the instance has, jack up emphasis
$emphasis = Insight::EMPHASIS_LOW;
foreach ($least_likely_followers as $least_likely_follower) {
if ($least_likely_follower->follower_count > ($this->user->follower_count/2)) {
$emphasis = Insight::EMPHASIS_HIGH;
}
}

$insight_date = new DateTime();
//Not PHP 5.2 compatible
//$insight_date->sub(new DateInterval('P'.$days_ago.'D'));
$insight_date->modify('-'.$days_ago.' day');
$insight_date = $insight_date->format('Y-m-d');
if (sizeof($least_likely_followers) > 1) {
$insight_dao->insertInsight('least_likely_followers', $instance->id, $insight_date,
"Good people:", sizeof($least_likely_followers)." interesting users followed you.",
$emphasis, serialize($least_likely_followers));
} else {
$insight_dao->insertInsight('least_likely_followers', $instance->id, $insight_date,
"Hey!", "An interesting user followed you.",
$emphasis, serialize($least_likely_followers));
$least_likely_followers = $follow_dao->getLeastLikelyFollowersByDay($instance->network_user_id,
$instance->network, 0, 3);
if (sizeof($least_likely_followers) > 0 ) { //if not null, store insight
//If followers have more followers than half of what the instance has, jack up emphasis
$emphasis = Insight::EMPHASIS_LOW;
foreach ($least_likely_followers as $least_likely_follower) {
if ($least_likely_follower->follower_count > ($this->user->follower_count/2)) {
$emphasis = Insight::EMPHASIS_HIGH;
}
}
$days_ago++;

$insight_date = new DateTime();
$insight_date = $insight_date->format('Y-m-d');
if (sizeof($least_likely_followers) > 1) {
$insight_dao->insertInsight('least_likely_followers', $instance->id, $insight_date,
"Good people:", sizeof($least_likely_followers)." interesting users followed you.",
$emphasis, serialize($least_likely_followers));
} else {
$insight_dao->insertInsight('least_likely_followers', $instance->id, $insight_date,
"Hey!", "An interesting user followed you.", $emphasis, serialize($least_likely_followers));
}
}
$this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
}
Expand Down
69 changes: 31 additions & 38 deletions webapp/plugins/insightsgenerator/insights/listmembership.php
Expand Up @@ -40,48 +40,41 @@ public function generateInsight(Instance $instance, $last_week_of_posts, $number

// List membership insights
$group_membership_dao = DAOFactory::getDAO('GroupMemberDAO');
$days_ago = 0;
while ($days_ago < $number_days) {
$insight_date = new DateTime();
$insight_date->modify('-'.$days_ago.' day');
$insight_date = $insight_date->format('Y-m-d');
$this->logger->logInfo("Getting new group memberships for ".$insight_date, __METHOD__.','
.__LINE__);
//get new group memberships per day
$new_groups = $group_membership_dao->getNewMembershipsByDate($instance->network,
$instance->network_user_id,
$insight_date);
if (sizeof($new_groups) > 0 ) { //if not null, store insight
$group_membership_count_dao = DAOFactory::getDAO('GroupMembershipCountDAO');
$list_membership_count_history_by_day = $group_membership_count_dao->getHistory(
$instance->network_user_id, $instance->network, 'DAY', 15);
if (sizeof($new_groups) > 1) {
$group_name_list = '';
foreach ($new_groups as $group) {
if ($group == end($new_groups)) {
$group_name_list .= " and ";
} else {
if ($group_name_list != '') {
$group_name_list .= ", ";
}
$insight_date = new DateTime();
$insight_date = $insight_date->format('Y-m-d');
$this->logger->logInfo("Getting new group memberships for ".$insight_date, __METHOD__.',' .__LINE__);
//get new group memberships per day
$new_groups = $group_membership_dao->getNewMembershipsByDate($instance->network, $instance->network_user_id,
$insight_date);
if (sizeof($new_groups) > 0 ) { //if not null, store insight
$group_membership_count_dao = DAOFactory::getDAO('GroupMembershipCountDAO');
$list_membership_count_history_by_day = $group_membership_count_dao->getHistory(
$instance->network_user_id, $instance->network, 'DAY', 15);
if (sizeof($new_groups) > 1) {
$group_name_list = '';
foreach ($new_groups as $group) {
if ($group == end($new_groups)) {
$group_name_list .= " and ";
} else {
if ($group_name_list != '') {
$group_name_list .= ", ";
}
$group->setMetadata();
$group_name_list .= '<a href="'.$group->url.'">'.$group->keyword.'</a>';
}
$insight_dao->insertInsight('new_group_memberships', $instance->id, $insight_date,
"Filed:", "You got added to ".sizeof($new_groups)." lists: ".$group_name_list.
", bringing your total to ".number_format(end($list_membership_count_history_by_day['history'])).
".", Insight::EMPHASIS_LOW, serialize($list_membership_count_history_by_day));
} else {
$new_groups[0]->setMetadata();
$insight_dao->insertInsight('new_group_memberships', $instance->id, $insight_date, "Filed:",
"You got added to a new list, ".'<a href="'.$new_groups[0]->url.'">'.$new_groups[0]->keyword.
"</a>, bringing your total to <strong>".
number_format(end($list_membership_count_history_by_day['history'])).
" lists</strong>.", Insight::EMPHASIS_LOW, serialize($list_membership_count_history_by_day));
$group->setMetadata();
$group_name_list .= '<a href="'.$group->url.'">'.$group->keyword.'</a>';
}
$insight_dao->insertInsight('new_group_memberships', $instance->id, $insight_date,
"Filed:", "You got added to ".sizeof($new_groups)." lists: ".$group_name_list.
", bringing your total to ".number_format(end($list_membership_count_history_by_day['history'])).
".", Insight::EMPHASIS_LOW, serialize($list_membership_count_history_by_day));
} else {
$new_groups[0]->setMetadata();
$insight_dao->insertInsight('new_group_memberships', $instance->id, $insight_date, "Filed:",
"You got added to a new list, ".'<a href="'.$new_groups[0]->url.'">'.$new_groups[0]->keyword.
"</a>, bringing your total to <strong>".
number_format(end($list_membership_count_history_by_day['history'])).
" lists</strong>.", Insight::EMPHASIS_LOW, serialize($list_membership_count_history_by_day));
}
$days_ago++;
}
$this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
}
Expand Down
3 changes: 1 addition & 2 deletions webapp/plugins/insightsgenerator/insights/map.php
Expand Up @@ -35,11 +35,10 @@ public function generateInsight(Instance $instance, $last_week_of_posts, $number
parent::generateInsight($instance, $last_week_of_posts, $number_days);
$this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);

$plugin_option_dao = DAOFactory::getDAO('PluginOptionDAO');
$insight_dao = DAOFactory::getDAO('InsightDAO');

$simplified_post_date = "";
foreach ($last_week_of_posts as $post) {
$simplified_post_date = date('Y-m-d', strtotime($post->pub_date));
// Map insight: If not a reply or retweet and geoencoded, show the map in the stream
if (!isset($post->in_reply_to_user_id) && !isset($post->in_reply_to_post_id)
&& !isset($post->in_retweet_of_post_id) && $post->reply_count_cache > 5) {
Expand Down

0 comments on commit 010495e

Please sign in to comment.