Skip to content

Commit

Permalink
Remove old dashboard insights before inserting the current one
Browse files Browse the repository at this point in the history
  • Loading branch information
ginatrapani committed Jul 4, 2012
1 parent b9c9efd commit a646de8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
39 changes: 35 additions & 4 deletions tests/TestOfInsightMySQLDAO.php
Expand Up @@ -161,15 +161,46 @@ public function testUpdateInsight() {
public function testDeleteInsight() {
$dao = new InsightMySQLDAO();

//delete existing baseline
$result = $dao->deleteInsight('avg_replies_per_week', 1, '2012-05-01', 'LOLlerskates', Insight::EMPHASIS_MED);
//delete existing insight
$result = $dao->deleteInsight('avg_replies_per_week', 1, '2012-05-01');
$this->assertTrue($result);
//check that insight was deleted
$result = $dao->getInsight('avg_replies_per_week', 1, '2012-05-01');
$this->assertNull($result);

//delete nonexistent baseline
$result = $dao->deleteInsight('avg_replies_per_week', 1, '2012-05-10', 'ooooh burn');
//delete nonexistent insight
$result = $dao->deleteInsight('avg_replies_per_week', 1, '2012-05-10');
$this->assertFalse($result);
}

public function testDeleteInsightsBySlug() {
$builders = array();
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'another_slug',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));

$dao = new InsightMySQLDAO();

//delete all insights for slug/instance
$result = $dao->deleteInsightsBySlug('avg_replies_per_week', 1);
$this->assertTrue($result);
//check that insights for that slug and instance were deleted
$result = $dao->getInsight('avg_replies_per_week', 1, '2012-05-01');
$this->assertNull($result);
$result = $dao->getInsight('avg_replies_per_week', 1, '2012-05-02');
$this->assertNull($result);
//check that insight with that slug but not for another instance were NOT deleted
$result = $dao->getInsight('avg_replies_per_week', 2, '2012-05-01');
$this->assertNotNull($result);

//delete nonexistent slug
$result = $dao->deleteInsightsBySlug('avg_replies_per_week_another_slug', 1);
$this->assertFalse($result);
}
}
14 changes: 14 additions & 0 deletions webapp/_lib/model/class.InsightMySQLDAO.php
Expand Up @@ -138,4 +138,18 @@ public function updateInsight($slug, $instance_id, $date, $text, $emphasis=Insig
$result = $this->getUpdateCount($ps);
return ($result > 0);
}

public function deleteInsightsBySlug($slug, $instance_id) {
$q = "DELETE FROM #prefix#insights WHERE ";
$q .= "slug=:slug AND instance_id=:instance_id";
$vars = array(
':slug'=>$slug,
':instance_id'=>$instance_id
);
if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);

$ps = $this->execute($q, $vars);
$result = $this->getUpdateCount($ps);
return ($result > 0);
}
}
18 changes: 18 additions & 0 deletions webapp/_lib/model/class.InsightsGenerator.php
Expand Up @@ -51,6 +51,9 @@ public function generateInsights() {
$results = $follow_dao->getLeastLikelyFollowersThisWeek($this->instance->network_user_id,
$this->instance->network, 13, 1);
if (isset($results)) {
//delete existing
$insight_dao->deleteInsightsBySlug("FollowMySQLDAO::getLeastLikelyFollowersThisWeek", $this->instance->id);
//insert new
$insight_dao->insertInsight("FollowMySQLDAO::getLeastLikelyFollowersThisWeek", $this->instance->id,
$simplified_date, '', Insight::EMPHASIS_LOW, serialize($results));
}
Expand All @@ -60,6 +63,9 @@ public function generateInsights() {
$hot_posts = $post_dao->getHotPosts($this->instance->network_user_id, $this->instance->network, 10);
if (sizeof($hot_posts) > 3) {
$hot_posts_data = self::getHotPostVisualizationData($hot_posts, $this->instance->network);
//delete existing
$insight_dao->deleteInsightsBySlug("PostMySQLDAO::getHotPosts", $this->instance->id);
//insert new
$insight_dao->insertInsight("PostMySQLDAO::getHotPosts", $this->instance->id,
$simplified_date, '', Insight::EMPHASIS_LOW, serialize($hot_posts_data));
}
Expand All @@ -69,6 +75,9 @@ public function generateInsights() {
$click_stats = $short_link_dao->getRecentClickStats($this->instance, 10);
if (sizeof($click_stats) > 3) {
$click_stats_data = self::getClickStatsVisualizationData($click_stats);
//delete existing
$insight_dao->deleteInsightsBySlug("ShortLinkMySQLDAO::getRecentClickStats", $this->instance->id);
//insert new
$insight_dao->insertInsight("ShortLinkMySQLDAO::getRecentClickStats", $this->instance->id,
$simplified_date, '', Insight::EMPHASIS_LOW, serialize($click_stats_data));
}
Expand All @@ -77,6 +86,9 @@ public function generateInsights() {
$most_replied_to_1wk = $post_dao->getMostRepliedToPostsInLastWeek($this->instance->network_username,
$this->instance->network, 5);
if (sizeof($most_replied_to_1wk) > 1) {
//delete existing
$insight_dao->deleteInsightsBySlug("PostMySQLDAO::getMostRepliedToPostsInLastWeek", $this->instance->id);
//insert new
$insight_dao->insertInsight("PostMySQLDAO::getMostRepliedToPostsInLastWeek", $this->instance->id,
$simplified_date, '', Insight::EMPHASIS_LOW, serialize($most_replied_to_1wk));
}
Expand All @@ -85,13 +97,19 @@ public function generateInsights() {
$most_retweeted_1wk = $post_dao->getMostRetweetedPostsInLastWeek($this->instance->network_username,
$this->instance->network, 5);
if (sizeof($most_retweeted_1wk) > 1) {
//delete existing
$insight_dao->deleteInsightsBySlug("PostMySQLDAO::getMostRetweetedPostsInLastWeek", $this->instance->id);
//insert new
$insight_dao->insertInsight("PostMySQLDAO::getMostRetweetedPostsInLastWeek", $this->instance->id,
$simplified_date, '', Insight::EMPHASIS_LOW, serialize($most_retweeted_1wk));
}

//Cache PostMySQLDAO::getClientsUsedByUserOnNetwork
$clients_usage = $post_dao->getClientsUsedByUserOnNetwork($this->instance->network_user_id,
$this->instance->network);
//delete existing
$insight_dao->deleteInsightsBySlug("PostMySQLDAO::getClientsUsedByUserOnNetwork", $this->instance->id);
//insert new
$insight_dao->insertInsight("PostMySQLDAO::getClientsUsedByUserOnNetwork", $this->instance->id,
$simplified_date, '', Insight::EMPHASIS_LOW, serialize($clients_usage));
}
Expand Down
7 changes: 7 additions & 0 deletions webapp/_lib/model/interface.InsightDAO.php
Expand Up @@ -63,6 +63,13 @@ public function getPreCachedInsightData($slug, $instance_id, $date);
* @return bool
*/
public function deleteInsight($slug, $instance_id, $date);
/**
* Remove insights for an instance by slug from storage.
* @param str $slug
* @param int $instance_id
* @return bool
*/
public function deleteInsightsBySlug($slug, $instance_id);
/**
* Get a page of insights for an instance.
* @param int $instance_id
Expand Down

0 comments on commit a646de8

Please sign in to comment.