Skip to content

Commit

Permalink
Favorite flashback insight
Browse files Browse the repository at this point in the history
  • Loading branch information
ginatrapani committed Aug 16, 2012
1 parent 5f35c7f commit a97b5cb
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 18 deletions.
55 changes: 47 additions & 8 deletions tests/TestOfFavoritePostMySQLDAO.php
Expand Up @@ -101,10 +101,10 @@ protected function buildData() {
}
$is_protected = $counter == 18 ? 1 : 0; // post with id 18 is protected
$builders[] = FixtureBuilder::build('posts', array('post_id'=>$counter, 'author_user_id'=>13,
'author_username'=>'ev', 'author_fullname'=>'Ev Williams', 'author_avatar'=>'avatar.jpg',
'author_username'=>'ev', 'author_fullname'=>'Ev Williams', 'author_avatar'=>'avatar.jpg',
'post_text'=>'This is post '.$counter, 'source'=>$source, 'pub_date'=>'2006-01-01 00:'.
$pseudo_minute.':00', 'reply_count_cache'=>rand(0, 4), 'retweet_count_cache'=>5, 'network'=>'twitter',
'in_reply_to_post_id'=>null, 'in_retweet_of_post_id'=>null, 'is_geo_encoded'=>0,
'in_reply_to_post_id'=>null, 'in_retweet_of_post_id'=>null, 'is_geo_encoded'=>0,
'is_protected' => $is_protected));
$counter++;
}
Expand All @@ -122,17 +122,17 @@ protected function buildData() {
'is_protected' => $is_protected));

$builders[] = FixtureBuilder::build('links', array('url'=>'http://example.com/'.$counter,
'explanded_url'=>'http://example.com/'.$counter.'.html', 'title'=>'Link $counter', 'clicks'=>0,
'explanded_url'=>'http://example.com/'.$counter.'.html', 'title'=>'Link $counter', 'clicks'=>0,
'post_id'=>$post_id, 'image_src'=>''));

$counter++;
}

$builders[] = FixtureBuilder::build('posts', array('post_id'=>10822735852740608, 'author_user_id'=>23,
'author_username'=>'user3', 'author_fullname'=>'User 3', 'network'=>'twitter',
'author_username'=>'user3', 'author_fullname'=>'User 3', 'network'=>'twitter',
'post_text'=>'@nytimes has posted an interactive panoramic photo that shows how Times Square has changed over'.
' the last 20 years http://nyti.ms/hmTVzP',
'source'=>'web', 'pub_date'=>'-300s', 'reply_count_cache'=>0, 'retweet_count_cache'=>0,
' the last 20 years http://nyti.ms/hmTVzP',
'source'=>'web', 'pub_date'=>'-300s', 'reply_count_cache'=>0, 'retweet_count_cache'=>0,
'location'=>'New York City', 'is_geo_encoded'=>0, 'is_protected' => 0));

// have 'user1' favorite some of ev's posts
Expand Down Expand Up @@ -337,12 +337,51 @@ public function testGetFavdsOfPost() {
$this->assertEqual(count($res), 2);
}

public function testGetFavoritesFromOneYearAgo() {
//build post published one year ago today
$builders[] = FixtureBuilder::build('posts', array('post_id'=>'abadadfd1212', 'author_user_id'=>'19',
'author_username'=>'linkbaiter', 'author_fullname'=>'Link Baiter', 'is_geo_encoded'=>0,
'post_text'=>'This is link post '.$counter, 'source'=>'web', 'pub_date'=>'-366d',
'reply_count_cache'=>0, 'retweet_count_cache'=>0, 'network'=>'twitter',
'is_protected' => 0));

//build favorite of that post by test user ev
$builders[] = FixtureBuilder::build('favorites', array('post_id'=>'abadadfd1212', 'author_user_id'=>'19',
'fav_of_user_id'=>'13', 'network'=>'twitter'));

//get favorites from one year ago today
$result = $this->dao->getFavoritesFromOneYearAgo('13', 'twitter');

//assert post is returned
$this->assertEqual(sizeof($result), 1);
$this->assertEqual($result[0]->post_id, 'abadadfd1212');

//build post published one year and 4 days ago today
$builders[] = FixtureBuilder::build('posts', array('post_id'=>'abadadfd1213', 'author_user_id'=>'19',
'author_username'=>'linkbaiter', 'author_fullname'=>'Link Baiter', 'is_geo_encoded'=>0,
'post_text'=>'This is link post '.$counter, 'source'=>'web', 'pub_date'=>'-370d',
'reply_count_cache'=>0, 'retweet_count_cache'=>0, 'network'=>'twitter',
'is_protected' => 0));

//build favorite of that post by test user ev
$builders[] = FixtureBuilder::build('favorites', array('post_id'=>'abadadfd1213', 'author_user_id'=>'19',
'fav_of_user_id'=>'13', 'network'=>'twitter'));

$since_date = date("Y-m-d", strtotime("-4 day"));
//get favorites from one year ago today
$result = $this->dao->getFavoritesFromOneYearAgo('13', 'twitter', $since_date);

//assert post is returned
$this->assertEqual(sizeof($result), 1);
$this->assertEqual($result[0]->post_id, 'abadadfd1213');
}

/**
* helper method to build a post
*/
private function buildPostArray1() {
$vals = array();
$vals['post_id']=2904;
$vals['post_id']='2904';
$vals['author_username']='quoter';
$vals['author_fullname']="Quoter of Quotables";
$vals['author_avatar']='avatar.jpg';
Expand All @@ -360,7 +399,7 @@ private function buildPostArray1() {
*/
private function buildPostArray2() {
$vals = array();
$vals['post_id']=10822735852740608;
$vals['post_id']='10822735852740608';
$vals['author_username']='user3';
$vals['author_fullname']="User 3";
$vals['author_avatar']='avatar.jpg';
Expand Down
29 changes: 29 additions & 0 deletions webapp/_lib/model/class.FavoritePostMySQLDAO.php
Expand Up @@ -334,4 +334,33 @@ public function getUsersWhoFavedPost($post_id, $network='twitter', $is_public =
$all_rows = $this->getDataRowsAsArrays($ps);
return $all_rows;
}

public function getFavoritesFromOneYearAgo($fav_of_user_id, $network, $from_date=null) {
$q = "SELECT p.*, pub_date - interval #gmt_offset# hour AS adj_pub_date ";
$q .= "FROM #prefix#posts p INNER JOIN #prefix#favorites f on f.post_id = p.post_id
WHERE f.fav_of_user_id = :fav_of_user_id AND p.network=:network AND p.is_protected = 0 ";

$vars = array(
':fav_of_user_id'=> $fav_of_user_id,
':network'=>$network
);
if (!isset($from_date)) {
$from_date = 'CURRENT_DATE()';
} else {
$from_date = "'$from_date'";
}
$q .= "AND (YEAR(pub_date)!=YEAR(CURRENT_DATE())) ";
$q .= "AND (DAYOFMONTH(pub_date)=DAYOFMONTH($from_date)) AND (MONTH(pub_date)=MONTH($from_date)) ";
$q .= "ORDER BY pub_date DESC ";

if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);

$ps = $this->execute($q, $vars);
$rows = $this->getDataRowsAsArrays($ps);
$posts = array();
foreach ($rows as $row) {
$posts[] = new Post($row);
}
return $posts;
}
}
3 changes: 0 additions & 3 deletions webapp/_lib/model/class.PostMySQLDAO.php
Expand Up @@ -1687,9 +1687,6 @@ public function getOnThisDayFlashbackPosts($author_id, $network, $from_date=null
);
if (!isset($from_date)) {
$from_date = 'CURRENT_DATE()';
} else {
$vars[':from_date'] = $from_date;
$from_date = ':from_date';
}
$q = "SELECT po.*, po.id AS post_key, po.pub_date + interval #gmt_offset# hour as adj_pub_date, ";
$q .= "pl.place_type, pl.name, pl.full_name, pl.country_code, pl.country, pl.longlat, pl.bounding_box ";
Expand Down
9 changes: 9 additions & 0 deletions webapp/_lib/model/interface.FavoritePostDAO.php
Expand Up @@ -111,4 +111,13 @@ public function getAllFavoritedPosts($author_user_id, $network, $count, $page=1)
* @return array users table array of rows who have favorited a post
*/
public function getUsersWhoFavedPost($post_id, $network='twitter', $is_public = false);

/**
* Get the posts a user favorited a year ago today.
* @param $fav_of_user_id
* @param $network
* @param $from_date
* @return array Post objects
*/
public function getFavoritesFromOneYearAgo($fav_of_user_id, $network, $from_date=null);
}
8 changes: 7 additions & 1 deletion webapp/_lib/view/_insights.post.tpl
Expand Up @@ -7,11 +7,17 @@
{/if}

<div class="post lead">
{if $i->instance->network_username != $post->author_username}
<div class="avatar-container" style="float:left;margin:7px; clear : left;">
<a href="https://twitter.com/intent/user?user_id={$post->author_username}" title="{$post->author_username}"><img src="{$post->author_avatar}" class="avatar2"/><img src="{$site_root_path}plugins/{$post->network}/assets/img/favicon.png" class="service-icon2"/></a>
</div>
{/if}

{if $post->post_text}
{if $scrub_reply_username}
{if $reply_count && $reply_count > $top_20_post_min}
<div class="reply_text" id="reply_text-{$smarty.foreach.foo.iteration}">
{/if}
{/if}
{$post->post_text|filter_xss|regex_replace:"/^@[a-zA-Z0-9_]+/":""|link_usernames_to_twitter}
{if $reply_count && $reply_count > $top_20_post_min}</div>{/if}
{else}
Expand Down
4 changes: 4 additions & 0 deletions webapp/_lib/view/_insights.posts.tpl
Expand Up @@ -28,6 +28,10 @@

{assign var="prev_post_year" value=$p->adj_pub_date|date_format:"%Y"}
{/foreach}
{elseif $i->slug eq 'favorites_year_ago_flashback'}
{foreach from=$i->related_data key=uid item=p name=bar}
{include file="_insights.post.tpl" post=$p hide_insight_header='1'}
{/foreach}
{else}
{foreach from=$i->related_data key=uid item=p name=bar}
{include file="_insights.post.tpl" post=$p}
Expand Down
3 changes: 1 addition & 2 deletions webapp/_lib/view/_insights.users.tpl
Expand Up @@ -10,8 +10,7 @@
<a href="https://twitter.com/intent/user?user_id={$u->user_id}">{$u->full_name}</a> <small style="color:gray">{$u->follower_count|number_format} followers</small><br>
<span style="color:gray">{$u->description|link_usernames_to_twitter}</span>
{$u->url}<br>

</div>
</div>
<div style="clear:all">&nbsp;</div>

{/foreach}
4 changes: 2 additions & 2 deletions webapp/_lib/view/insights.tpl
Expand Up @@ -48,7 +48,7 @@
<div class="alert {if $i->emphasis eq '1'}alert-info{elseif $i->emphasis eq '2'}alert-info{elseif $i->emphasis eq '3'}alert-error{else}alert-success{/if} {$i->emphasis} insight-item">
<p>
{$i->instance->network_username} {$i->instance->network|capitalize}<br>
<!-- begin related_data_type attachment data -->
<!-- begin {$i->related_data_type} attachment data -->
{if $i->related_data_type eq 'users'}
{include file="_insights.users.tpl"}
{elseif $i->related_data_type eq 'post'}
Expand All @@ -58,7 +58,7 @@
{elseif $i->related_data_type eq 'count_history'}
{include file="_insights.count_history.tpl"}
{/if}
<!--end related_data_type attachment data-->
<!--end {$i->related_data_type} attachment data-->
</p>
</div>
</div><!--/span9-->
Expand Down
63 changes: 63 additions & 0 deletions webapp/plugins/insightsgenerator/insights/favoriteflashbacks.php
@@ -0,0 +1,63 @@
<?php
/*
Plugin Name: Favorite Flashback
Description: Posts you favorited on this day one year ago.
*/

/**
*
* ThinkUp/webapp/plugins/insightsgenerator/insights/flashbacks.php
*
* Copyright (c) 2012 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012
*/

class FavoriteFlashbackInsight extends InsightPluginParent implements InsightPlugin {

public function generateInsight(Instance $instance, $last_week_of_posts, $number_days) {
parent::generateInsight($instance, $last_week_of_posts, $number_days);
$this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
$fav_dao = DAOFactory::getDAO('FavoritePostDAO');

$number_days = 10;
$days_ago = 0;
while ($days_ago < $number_days) {
$since_date = date("Y-m-d", strtotime("-".$days_ago." day"));
// $existing_insight = $this->insight_dao->getInsight("favorites_year_ago_flashback", $instance->id,
// $since_date);
// if (!isset($existing_insight)) {
//Generate flashback post list
$flashback_favs = $fav_dao->getFavoritesFromOneYearAgo($instance->network_user_id,
$instance->network, $since_date);
if (isset($flashback_favs) && sizeof($flashback_favs) > 0 ) {
$this->insight_dao->insertInsight("favorites_year_ago_flashback", $instance->id,
$since_date, "Favorite flashback:", "On this day in years past, you favorited: ",
Insight::EMPHASIS_MED, serialize($flashback_favs));
// }
}
$days_ago++;
}

$this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
}
}

$insights_plugin_registrar = PluginRegistrarInsights::getInstance();
$insights_plugin_registrar->registerInsightPlugin('FavoriteFlashbackInsight');
2 changes: 1 addition & 1 deletion webapp/plugins/insightsgenerator/insights/flashbacks.php
@@ -1,7 +1,7 @@
<?php
/*
Plugin Name: Flashback
Description: Posts on this day in years past.
Description: Posts you published on this day in years past.
*/

/**
Expand Down
Expand Up @@ -34,7 +34,7 @@ class ListMembershipInsight extends InsightPluginParent implements InsightPlugin

public function generateInsight(Instance $instance, $last_week_of_posts, $number_days) {
parent::generateInsight($instance, $last_week_of_posts, $number_days);
$this->logger->logUserInfo("Begin generating insight", __METHOD__.','.__LINE__);
$this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);

//get new group memberships per day
$group_membership_dao = DAOFactory::getDAO('GroupMemberDAO');
Expand Down

0 comments on commit a97b5cb

Please sign in to comment.