Skip to content

Commit

Permalink
Add Biggest Fans--users who liked your posts most over past 7 and 30 …
Browse files Browse the repository at this point in the history
…days--insight
  • Loading branch information
ginatrapani committed Aug 16, 2012
1 parent 6c7f3b7 commit f191158
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/TestOfFavoritePostMySQLDAO.php
Expand Up @@ -376,6 +376,42 @@ public function testGetFavoritesFromOneYearAgo() {
$this->assertEqual($result[0]->post_id, 'abadadfd1213');
}

public function testGetUsersWhoFavoritedMostOfYourPosts() {
//build post published 3 days ago
$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'=>'-3d',
'reply_count_cache'=>0, 'retweet_count_cache'=>0, 'network'=>'twitter',
'is_protected' => 0));

//build post published 4 days ago
$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'=>'-4d',
'reply_count_cache'=>0, 'retweet_count_cache'=>0, 'network'=>'twitter',
'is_protected' => 0));

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

$builders[] = FixtureBuilder::build('favorites', array('post_id'=>'abadadfd1213', 'author_user_id'=>'19',
'fav_of_user_id'=>'13', 'network'=>'twitter'));

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

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

$result = $this->dao->getUsersWhoFavoritedMostOfYourPosts('19', 'twitter', 7);
$this->debug(Utils::varDumpToString($result));
$this->assertEqual(sizeof($result), 1);
$this->assertEqual($result[0]->username, 'ev');
}

/**
* helper method to build a post
*/
Expand Down
24 changes: 24 additions & 0 deletions webapp/_lib/model/class.FavoritePostMySQLDAO.php
Expand Up @@ -363,4 +363,28 @@ public function getFavoritesFromOneYearAgo($fav_of_user_id, $network, $from_date
}
return $posts;
}

public function getUsersWhoFavoritedMostOfYourPosts($author_user_id, $network, $last_x_days) {
//$q = "SELECT u.user_name, fav_of_user_id, count(f.post_id) AS total_favs from tu_favorites f ";
$q = "SELECT * FROM ( ";
$q .= "SELECT u.*, count(f.post_id) AS total_favs from tu_favorites f ";
$q .= "INNER JOIN tu_users u ON u.user_id = f.fav_of_user_id ";
$q .= "INNER JOIN tu_posts p ON f.post_id = p.post_id ";
$q .= "WHERE f.author_user_id = :author_user_id and f.network=:network ";
$q .= "AND p.pub_date >= date_sub(current_date, INTERVAL :last_x_days day) ";
$q .= "GROUP BY f.fav_of_user_id ORDER BY total_favs DESC";
$q .= ") favs WHERE favs.total_favs > 1 LIMIT 3";

$vars = array(
':author_user_id'=> $author_user_id,
':network'=>$network,
':last_x_days'=>$last_x_days
);

//echo Utils::mergeSQLVars($q, $vars);
if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);

$ps = $this->execute($q, $vars);
return $this->getDataRowsAsObjects($ps, 'User');
}
}
8 changes: 8 additions & 0 deletions webapp/_lib/model/interface.FavoritePostDAO.php
Expand Up @@ -120,4 +120,12 @@ public function getUsersWhoFavedPost($post_id, $network='twitter', $is_public =
* @return array Post objects
*/
public function getFavoritesFromOneYearAgo($fav_of_user_id, $network, $from_date=null);
/**
* Get users who favorited most of an author's posts over the past specified number of days.
* @param $author_user_id
* @param $network
* @param $last_x_days
* @return array User objects
*/
public function getUsersWhoFavoritedMostOfYourPosts($author_user_id, $network, $last_x_days);
}
8 changes: 8 additions & 0 deletions webapp/_lib/view/_insights.users.tpl
Expand Up @@ -4,12 +4,20 @@

{foreach from=$i->related_data key=uid item=u name=bar}
<div class="avatar-container" style="float:left;margin:7px; clear : left;">
{if $u->network eq 'twitter'}
<a href="https://twitter.com/intent/user?user_id={$u->user_id}" title="{$u->username} has {$u->follower_count|number_format} followers and {$u->friend_count|number_format} friends"><img src="{$u->avatar}" class="avatar2"/><img src="{$site_root_path}plugins/{$u->network}/assets/img/favicon.png" class="service-icon2"/></a>
{else}
<img src="{$u->avatar}" class="avatar2"/><img src="{$site_root_path}plugins/{$u->network}/assets/img/favicon.png" class="service-icon2"/>
{/if}
</div>
<div style="margin-left:60px">
{if $u->network eq 'twitter'}
<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>
{else}
{$u->full_name}<br><br><br>
{/if}
</div>
<div style="clear:all">&nbsp;</div>

Expand Down
78 changes: 78 additions & 0 deletions webapp/plugins/insightsgenerator/insights/biggestfans.php
@@ -0,0 +1,78 @@
<?php
/*
Plugin Name: Biggest Fans
Description: Users who have favorited or liked your posts the most over the last 7 and 30 days.
*/

/**
*
* ThinkUp/webapp/plugins/insightsgenerator/insights/biggestfans.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 BiggestFansInsight 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__);

$since_date = date("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');

if ($insight_day_of_month == 1) { //it's the first day of the month
// Past 30 days
$existing_insight = $this->insight_dao->getInsight("biggest_fans_last_30_days", $instance->id,
$since_date);
if (!isset($existing_insight)) {
$fav_dao = DAOFactory::getDAO('FavoritePostDAO');
$fans = $fav_dao->getUsersWhoFavoritedMostOfYourPosts($instance->network_user_id,
$instance->network, 30);
if (isset($fans) && sizeof($fans) > 0 ) {
$this->insight_dao->insertInsight("biggest_fans_last_30_days", $instance->id,
$since_date, "Biggest fans:", "People who liked your posts the most over the last 30 days: ",
Insight::EMPHASIS_MED, serialize($fans));
}
}
} else if ($insight_day_of_week == 0) { //it's Sunday
// Past 7 days
$existing_insight = $this->insight_dao->getInsight("biggest_fans_last_7_days", $instance->id,
$since_date);
if (!isset($existing_insight)) {
$fav_dao = DAOFactory::getDAO('FavoritePostDAO');
$fans = $fav_dao->getUsersWhoFavoritedMostOfYourPosts($instance->network_user_id,
$instance->network, 7);
if (isset($fans) && sizeof($fans) > 0 ) {
$this->insight_dao->insertInsight("biggest_fans_last_7_days", $instance->id,
$since_date, "Biggest fans:", "People who liked your posts the most over the last 7 days: ",
Insight::EMPHASIS_MED, serialize($fans));
}
}
}
$this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
}
}

$insights_plugin_registrar = PluginRegistrarInsights::getInstance();
$insights_plugin_registrar->registerInsightPlugin('BiggestFansInsight');

0 comments on commit f191158

Please sign in to comment.