Skip to content

Commit

Permalink
Collapse Flashback insights
Browse files Browse the repository at this point in the history
Emphasize Good people w/ high follower counts relative to the instance user
[ci skip]
  • Loading branch information
ginatrapani committed Jun 14, 2012
1 parent e85f238 commit c5b7448
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 34 deletions.
38 changes: 33 additions & 5 deletions tests/TestOfPostMySQLDAO.php
Expand Up @@ -2869,7 +2869,7 @@ public function testGetAverageRetweetCount() {
$this->assertEqual($average_retweet_count, 17);
}

public function testGetAllPostsFromThisDayYearAgo(){
public function testGetOnThisDayFlashbackPosts(){
// Generate the date string for 1 year ago today
$year_ago_date = date(date( 'Y-m-d H:i:s' , strtotime("today -1 year")));

Expand All @@ -2886,6 +2886,35 @@ public function testGetAllPostsFromThisDayYearAgo(){
// Add a link for this post
$link_builder = FixtureBuilder::build('links', array('post_key'=>$post_key, 'url'=>'http://bit.ly/blah'));

// Add a post from 2 years ago that's not a reply or retweet
$two_years_ago_date = date(date( 'Y-m-d H:i:s' , strtotime("today -2 year")));
$post_builder2 = FixtureBuilder::build('posts', array('post_id'=>'149', 'author_user_id'=>'20',
'author_username'=>'user1', 'author_fullname'=>'User 1', 'network'=>'foursquare',
'post_text'=>'I just checked in', 'source'=>'', 'pub_date'=>$two_years_ago_date, 'location'=>'England',
'old_retweet_count_cache' => 0, 'in_rt_of_user_id' => null, 'place'=>'The Park', 'place_id'=>'12345a',
'reply_count_cache'=>0, 'retweet_count_cache'=>0, 'network'=>'foursquare',
'in_reply_to_user_id' =>null, 'in_reply_to_post_id' => null, 'in_retweet_of_post_id'=>null,
'geo'=>'52.477192843264,-1.484333726346'));

$post_key = $post_builder2->columns['last_insert_id'];
// Add a link for this post
$link_builder2 = FixtureBuilder::build('links', array('post_key'=>$post_key, 'url'=>'http://bit.ly/blahb'));

// Add a post from today that's not a reply or retweet
$today_date = date(date( 'Y-m-d H:i:s' , strtotime("today")));

$post_builder3 = FixtureBuilder::build('posts', array('post_id'=>'150', 'author_user_id'=>'20',
'author_username'=>'user1', 'author_fullname'=>'User 1', 'network'=>'foursquare',
'post_text'=>'I just checked in', 'source'=>'', 'pub_date'=>$today_date, 'location'=>'England',
'old_retweet_count_cache' => 0, 'in_rt_of_user_id' => null, 'place'=>'The Park', 'place_id'=>'12345a',
'reply_count_cache'=>0, 'retweet_count_cache'=>0, 'network'=>'foursquare',
'in_reply_to_user_id' =>null, 'in_reply_to_post_id' => null, 'in_retweet_of_post_id'=>null,
'geo'=>'52.477192843264,-1.484333726346'));

$post_key = $post_builder3->columns['last_insert_id'];
// Add a link for this post
$link_builder3 = FixtureBuilder::build('links', array('post_key'=>$post_key, 'url'=>'http://bit.ly/blahb'));

/* Add the place information for future foursquare checkin test (We do it this way due to the fixture builder
not being able to handle the MySQL point type
// Set all possible fields
Expand Down Expand Up @@ -2921,12 +2950,11 @@ public function testGetAllPostsFromThisDayYearAgo(){
// Query the database for last year's post
$post_dao = new PostMySQLDAO();
// Get the year to query for
$query_year = date(date( 'Y' , strtotime("today -1 year")));
$res = $post_dao->getPostsFromThisDayThatYear(20, 'foursquare', $query_year);
$res = $post_dao->getOnThisDayFlashbackPosts(20, 'foursquare');

//print_r($res);
$this->debug(Utils::varDumpToString($res));
// Check only the 1 checkin we inserted is returned
$this->assertEqual(sizeof($res), 1);
$this->assertEqual(sizeof($res), 2);
// Check the author user id was set correctly
$this->assertEqual($res[0]->author_user_id, '20');
// Check the username was set correctly
Expand Down
9 changes: 4 additions & 5 deletions webapp/_lib/model/class.PostMySQLDAO.php
Expand Up @@ -1700,9 +1700,8 @@ public function getAverageRetweetCount($author_username, $network, $last_x_days,
return $result["average_retweet_count"];
}

public function getPostsFromThisDayThatYear($author_id, $network, $year, $from_date=null) {
public function getOnThisDayFlashbackPosts($author_id, $network, $from_date=null) {
$vars = array(
':year'=> $year,
':author'=> $author_id,
':network'=>$network
);
Expand All @@ -1716,8 +1715,8 @@ public function getPostsFromThisDayThatYear($author_id, $network, $year, $from_d
$q .= "pl.place_type, pl.name, pl.full_name, pl.country_code, pl.country, pl.longlat, pl.bounding_box ";
$q .= "FROM #prefix#posts po ";
$q .= "LEFT JOIN #prefix#places pl ON po.place_id = pl.place_id ";
$q .= "WHERE (YEAR(pub_date)=:year) AND ";
$q .= "(DAYOFMONTH(pub_date)=DAYOFMONTH($from_date)) AND (MONTH(pub_date)=MONTH($from_date)) AND ";
$q .= "WHERE (YEAR(pub_date)!=YEAR(CURRENT_DATE())) ";
$q .= "AND (DAYOFMONTH(pub_date)=DAYOFMONTH($from_date)) AND (MONTH(pub_date)=MONTH($from_date)) AND ";
$q .= "author_user_id=:author AND po.network=:network AND ";
$q .= "in_reply_to_post_id IS null AND in_reply_to_user_id IS NULL AND ";
$q .= "in_retweet_of_post_id IS NULL AND in_rt_of_user_id IS NULL ";
Expand Down Expand Up @@ -1748,6 +1747,6 @@ public function getPostsFromThisDayThatYear($author_id, $network, $year, $from_d
}
$all_posts[] = $post;
}
return $all_posts;
return array_reverse($all_posts);
}
}
5 changes: 2 additions & 3 deletions webapp/_lib/model/interface.PostDAO.php
Expand Up @@ -575,12 +575,11 @@ public function getPostsToUserIterator($user_id, $network, $count, $is_public=fa
public function getAverageRetweetCount($username, $network, $last_x_days, $since=null);

/**
* Get posts from this day in a given year that aren't replies or reshares/retweets.
* Get posts from this day in every year except this one that aren't replies or reshares/retweets.
* @param str $author_id
* @param str $network
* @param str $year For example, '2009' or '2005'.
* @param str $from_date If not specified, defaults to current date
* @return array Post objects
*/
public function getPostsFromThisDayThatYear($author_id, $network, $year, $from_date=null);
public function getOnThisDayFlashbackPosts($author_id, $network, $from_date=null);
}
34 changes: 31 additions & 3 deletions webapp/_lib/view/_insights.posts.tpl
@@ -1,3 +1,31 @@
{foreach from=$i->related_data key=uid item=p name=bar}
{include file="_insights.post.tpl" post=$p}
{/foreach}
{if $i->slug eq 'posts_on_this_day_flashback'}
{foreach from=$i->related_data key=uid item=p name=bar}
{* Hide posts after the first one *}
{if $smarty.foreach.bar.index eq 1}
<div style="display:none" id="flashback-{$i->id}">
{/if}

{* Show "X years ago you posted" text if post is from a different year than the last one *}
{if !$smarty.foreach.bar.first and $prev_post_year neq $p->adj_pub_date|date_format:"%Y"}
<span style="color:gray">{$p->adj_pub_date|relative_datetime} ago, you posted:</span>
{/if}

{include file="_insights.post.tpl" post=$p}

{* Show more link if there are more posts after the first one *}
{if $smarty.foreach.bar.total gt 0 and $smarty.foreach.bar.first}
<div align="right"><a href="javascript:;" title="See more flashback posts" onclick="{literal}${/literal}('#flashback-{$i->id}').show(); return false;">more...</a>&nbsp;&nbsp;</div>
{/if}
{* Close up hidden div if there is one *}
{if $smarty.foreach.bar.total gt 0 and $smarty.foreach.bar.last}
</div>
{/if}
{assign var="prev_post_year" value=$p->adj_pub_date|date_format:"%Y"}
{/foreach}
{else}
{foreach from=$i->related_data key=uid item=p name=bar}
{include file="_insights.post.tpl" post=$p}
{/foreach}
{/if}
42 changes: 24 additions & 18 deletions webapp/plugins/twitter/model/class.TwitterCrawler.php
Expand Up @@ -1944,6 +1944,14 @@ private function generateInsightFeedItems($number_days=3) {
$least_likely_followers = $follow_dao->getLeastLikelyFollowersByDay($this->instance->network_user_id,
'twitter', $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'));
Expand All @@ -1952,11 +1960,11 @@ private function generateInsightFeedItems($number_days=3) {
if (sizeof($least_likely_followers) > 1) {
$insight_dao->insertInsight('least_likely_followers', $this->instance->id, $insight_date,
"Good people: ".sizeof($least_likely_followers)." interesting users followed you.",
Insight::EMPHASIS_LOW, serialize($least_likely_followers));
$emphasis, serialize($least_likely_followers));
} else {
$insight_dao->insertInsight('least_likely_followers', $this->instance->id, $insight_date,
"An interesting user followed you.",
Insight::EMPHASIS_LOW, serialize($least_likely_followers));
$emphasis, serialize($least_likely_followers));
}
}
$days_ago++;
Expand Down Expand Up @@ -2025,23 +2033,21 @@ private function generateInsightFeedItems($number_days=3) {
}

$years_to_rewind = 6;
$year_to_process = 1;
while ($year_to_process <= $years_to_rewind) {
$existing_insight = $insight_dao->getInsight("posts_from_".$year_to_process."_years_ago",
$this->instance->id, $insight_date_formatted);
if (!isset($existing_insight)) {
//Generate year ago insights
$query_year = date(date( 'Y', strtotime("today -".$year_to_process." year")));
$year_ago_posts = $post_dao->getPostsFromThisDayThatYear($this->instance->network_user_id,
'twitter', $query_year, $insight_date_formatted);
if (isset($year_ago_posts) && sizeof($year_ago_posts) > 0 ) {
$plural = ($year_to_process > 1 )?'s':'';
$insight_dao->insertInsight("posts_from_".$year_to_process."_years_ago", $this->instance->id,
$insight_date_formatted, "Flashback: ".$year_to_process." year".$plural.
" ago today, you posted: ", Insight::EMPHASIS_MED, serialize($year_ago_posts));
}
$existing_insight = $insight_dao->getInsight("posts_on_this_day_flashback", $this->instance->id,
$insight_date_formatted);
if (!isset($existing_insight)) {
//Generate flashback post list
$flashback_posts = $post_dao->getOnThisDayFlashbackPosts($this->instance->network_user_id, 'twitter',
$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", $this->instance->id,
$insight_date_formatted, "Flashback: ".$number_of_years_ago." year".$plural.
" ago today, you posted: ", Insight::EMPHASIS_MED, serialize($flashback_posts));
}
$year_to_process++;
}

$days_ago--;
Expand Down

0 comments on commit c5b7448

Please sign in to comment.