Skip to content

Commit

Permalink
Translate outreach times to owner timezone.
Browse files Browse the repository at this point in the history
Also add timezone to OwnerMySQLDAO::getById()

Closes #1810, closes #1797
  • Loading branch information
cdmoyer authored and ginatrapani committed Feb 4, 2014
1 parent eac5cc1 commit 3eac9e5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 71 deletions.
1 change: 1 addition & 0 deletions tests/TestOfOwnerMySQLDAO.php
Expand Up @@ -112,6 +112,7 @@ public function testGetById() {
$this->assertEqual($existing_owner->account_status, '');
$this->assertEqual($existing_owner->api_key, 'c9089f3c9adaf0186f6ffb1ee8d6501c');
$this->assertEqual($existing_owner->email_notification_frequency, 'both');
$this->assertEqual($existing_owner->timezone, 'UTC');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions webapp/_lib/dao/class.OwnerMySQLDAO.php
Expand Up @@ -64,7 +64,7 @@ public function getByEmail($email) {

public function getById($id) {
$q = 'SELECT id,full_name,email,is_admin,last_login,is_activated,password_token,' .
'account_status,failed_logins,api_key,email_notification_frequency ' .
'account_status,failed_logins,api_key,email_notification_frequency,timezone ' .
'FROM #prefix#owners AS o WHERE id = :id';
$vars = array(
':id'=>$id
Expand Down Expand Up @@ -336,7 +336,7 @@ public function setEmailNotificationFrequency($email, $email_notification_freque
SET email_notification_frequency=:email_notification_frequency
WHERE email=:email";
if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
$stmt = $this->execute($q, array(':email_notification_frequency' => $email_notification_frequency,
$stmt = $this->execute($q, array(':email_notification_frequency' => $email_notification_frequency,
':email' => $email));
return $this->getUpdateCount($stmt);
}
Expand Down
43 changes: 21 additions & 22 deletions webapp/plugins/insightsgenerator/insights/outreachpunchcard.php
Expand Up @@ -38,8 +38,23 @@ public function generateInsight(Instance $instance, $last_week_of_posts, $number

if (parent::shouldGenerateInsight('outreach_punchcard', $instance, $insight_date='today',
$regenerate_existing_insight=false, $day_of_week=6, count($last_week_of_posts))) {
$cfg = Config::getInstance();
$local_timezone = new DateTimeZone($cfg->getValue('timezone'));

$owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
$owner_dao = DAOFactory::getDAO('OwnerDAO');

$owner_instance = $owner_instance_dao->getByInstance($instance->id);
$owner_id = $owner_instance[0]->owner_id;
$owner = $owner_dao->getById($owner_instance[0]->owner_id);
try {
$owner_timezone = new DateTimeZone($owner->timezone);
} catch (Exception $e) {
// In the odd case the owner has no or a malformed timezone
$cfg = Config::getInstance();
$owner_timezone = new DateTimeZone($cfg->getValue('timezone'));
}
$now = new DateTime();
$offset = timezone_offset_get($owner_timezone, $now);


$post_dao = DAOFactory::getDAO('PostDAO');
$punchcard = array();
Expand All @@ -62,32 +77,16 @@ public function generateInsight(Instance $instance, $last_week_of_posts, $number

foreach ($responses as $response) {
$response_pub_date = new DateTime($response->pub_date);
$response_dotw = date('N',
(date('U',
strtotime($response->pub_date)) + timezone_offset_get($local_timezone, $response_pub_date)
)
); // Day of the week
$response_hotd = date('G',
(date('U',
strtotime($response->pub_date)) + timezone_offset_get($local_timezone, $response_pub_date)
)
); // Hour of the day
$response_dotw = date('N', (date('U', strtotime($response->pub_date)+$offset))); // Day of week
$response_hotd = date('G', (date('U', strtotime($response->pub_date)+$offset))); // Hour of day
$punchcard['responses'][$response_dotw][$response_hotd]++;

$responses_chron[$response_hotd]++;
}

$post_pub_date = new DateTime($post->pub_date);
$post_dotw = date('N',
(date('U',
strtotime($post->pub_date)) + timezone_offset_get($local_timezone, $post_pub_date)
)
); // Day of the week
$post_hotd = date('G',
(date('U',
strtotime($post->pub_date)) + timezone_offset_get($local_timezone, $post_pub_date)
)
); // Hour of the day
$post_dotw = date('N', (date('U', strtotime($post->pub_date)+$offset))); // Day of the week
$post_hotd = date('G', (date('U', strtotime($post->pub_date)+$offset))); // Hour of the day
$punchcard['posts'][$post_dotw][$post_hotd]++;
}

Expand Down
Expand Up @@ -48,86 +48,72 @@ public function tearDown() {

public function testOutreachPunchcardInsight() {
$cfg = Config::getInstance();
$local_timezone = new DateTimeZone($cfg->getValue('timezone'));
$install_timezone = new DateTimeZone($cfg->getValue('timezone'));
$owner_timezone = new DateTimeZone($test_timezone='America/Los_Angeles');

// Get data ready that insight requires
$posts = self::getTestPostObjects();

$post_pub_date = new DateTime($posts[0]->pub_date);
$post_dotw = date('N',
(date('U',
strtotime($posts[0]->pub_date)) + timezone_offset_get($local_timezone, $post_pub_date)
)
); // Day of the week
$post_hotd = date('G',
(date('U',
strtotime($posts[0]->pub_date)) + timezone_offset_get($local_timezone, $post_pub_date)
)
); // Hour of the day
$now = new DateTime();
$offset = timezone_offset_get($owner_timezone, $now) - timezone_offset_get($install_timezone, $now);
$post_dotw = date('N', (date('U', strtotime($posts[0]->pub_date)))+ timezone_offset_get($owner_timezone, $now));
$post_hotd = date('G', (date('U', strtotime($posts[0]->pub_date)))+ timezone_offset_get($owner_timezone, $now));

$builders = array();

$builders[] = FixtureBuilder::build('users', array('user_id'=>'7654321', 'user_name'=>'twitteruser',
'full_name'=>'Twitter User', 'avatar'=>'avatar.jpg', 'follower_count'=>36000, 'is_protected'=>0,
'network'=>'twitter', 'description'=>'A test Twitter User'));

$date_r = date("Y-m-d",strtotime('-1 day'));
$instance_id = 10;
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User',
'email'=>'test@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'never', 'is_admin' => 0,
'timezone' => $test_timezone));
$builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>'1','instance_id'=>$instance_id));

// Response between 1pm and 2pm
$install_offset = $install_timezone->getOffset(new DateTime());
$date_r = date("Y-m-d",strtotime('-1 day')-$install_offset);

// Response between 1pm and 2pm install time
$time = gmdate('Y-m-d H:i:s', strtotime('yesterday 13:11:09'));
$builders[] = FixtureBuilder::build('posts', array('id'=>136, 'post_id'=>136, 'author_user_id'=>7654321,
'author_username'=>'twitteruser', 'author_fullname'=>'Twitter User', 'author_avatar'=>'avatar.jpg',
'network'=>'twitter', 'post_text'=>'This is a reply.', 'source'=>'web',
'pub_date'=>$date_r.' 13:11:09', 'in_reply_to_post_id'=>133, 'reply_count_cache'=>0, 'is_protected'=>0));
'pub_date'=>$time, 'in_reply_to_post_id'=>133, 'reply_count_cache'=>0, 'is_protected'=>0));

// Response between 1pm and 2pm
// Response between 1pm and 2pm install time
$time = gmdate('Y-m-d H:i:s', strtotime('yesterday 13:01:13'));
$builders[] = FixtureBuilder::build('posts', array('id'=>137, 'post_id'=>137, 'author_user_id'=>7654321,
'author_username'=>'twitteruser', 'author_fullname'=>'Twitter User', 'author_avatar'=>'avatar.jpg',
'network'=>'twitter', 'post_text'=>'This is a reply.', 'source'=>'web',
'pub_date'=>$date_r.' 13:01:13', 'in_reply_to_post_id'=>133, 'reply_count_cache'=>0, 'is_protected'=>0));
'pub_date'=>$time, 'in_reply_to_post_id'=>133, 'reply_count_cache'=>0, 'is_protected'=>0));

// Response between 1pm and 2pm
// Response between 1pm and 2pm install time
$time = gmdate('Y-m-d H:i:s', strtotime('yesterday 13:13:56'));
$builders[] = FixtureBuilder::build('posts', array('id'=>138, 'post_id'=>138, 'author_user_id'=>7654321,
'author_username'=>'twitteruser', 'author_fullname'=>'Twitter User', 'author_avatar'=>'avatar.jpg',
'network'=>'twitter', 'post_text'=>'This is a reply.', 'source'=>'web',
'pub_date'=>$date_r.' 13:13:56', 'in_reply_to_post_id'=>135, 'reply_count_cache'=>0, 'is_protected'=>0));
'pub_date'=>$time, 'in_reply_to_post_id'=>135, 'reply_count_cache'=>0, 'is_protected'=>0));

// Response between 11am and 12pm
// Response between 11am and 12pm install time
$time = gmdate('Y-m-d H:i:s', strtotime('yesterday 11:07:42'));
$builders[] = FixtureBuilder::build('posts', array('id'=>139, 'post_id'=>139, 'author_user_id'=>7654321,
'author_username'=>'twitteruser', 'author_fullname'=>'Twitter User', 'author_avatar'=>'avatar.jpg',
'network'=>'twitter', 'source'=>'web',
'post_text'=>'RT @testeriffic: New Year\'s Eve! Feeling very gay today, but not very homosexual.',
'pub_date'=>$date_r.' 11:07:42', 'in_retweet_of_post_id'=>134, 'reply_count_cache'=>0, 'is_protected'=>0));

$time1_low = new DateTime($date_r.' 13:00:00');
$time1_high = new DateTime($date_r.' 14:00:00');
$time1str_low = date('ga',
(date('U',
strtotime($date_r.' 13:00:00')) + timezone_offset_get($local_timezone, $time1_low)
)
);
$time1str_high = date('ga',
(date('U',
strtotime($date_r.' 14:00:00')) + timezone_offset_get($local_timezone, $time1_high)
)
);
'pub_date'=>$time, 'in_retweet_of_post_id'=>134, 'reply_count_cache'=>0, 'is_protected'=>0));

$time1str_low = date('ga', (date('U', strtotime($date_r.' 13:00:00')) + $offset));
$time1str_high = date('ga', (date('U', strtotime($date_r.' 14:00:00')) + $offset));
$time1str = $time1str_low." and ".$time1str_high;

$time2_low = new DateTime($date_r.' 13:00:00');
$time2_high = new DateTime($date_r.' 14:00:00');
$time2str_low = date('ga',
(date('U',
strtotime($date_r.' 11:00:00')) + timezone_offset_get($local_timezone, $time2_low)
)
);
$time2str_high = date('ga',
(date('U',
strtotime($date_r.' 12:00:00')) + timezone_offset_get($local_timezone, $time2_high)
)
);
$time2str_low = date('ga', (date('U', strtotime($date_r.' 11:00:00')) + $offset));
$time2str_high = date('ga', (date('U', strtotime($date_r.' 12:00:00')) + $offset));
$time2str = $time2str_low." and ".$time2str_high;

$instance = new Instance();
$instance->id = 10;
$instance->id = $instance_id;
$instance->network_username = 'testeriffic';
$instance->network = 'twitter';
$insight_plugin = new OutreachPunchcardInsight();
Expand All @@ -147,6 +133,12 @@ public function testOutreachPunchcardInsight() {
}

public function testOutreachPunchcardInsightNoResponse() {
$instance_id = 10;
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User',
'email'=>'test@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'never', 'is_admin' => 0,
'timezone' => 'UTC'));
$builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>'1','instance_id'=>$instance_id));

// Get data ready that insight requires
$posts = self::getTestPostObjects();
$instance = new Instance();
Expand Down Expand Up @@ -183,7 +175,7 @@ private function getTestPostObjects() {
$p->post_id = $counter++;
$p->network = 'twitter';
$p->post_text = $test_text;
$p->pub_date = date("Y-m-d H:i:s", strtotime('-2 days'));
$p->pub_date = gmdate("Y-m-d H:i:s", strtotime('-2 days'));
$posts[] = $p;
}
return $posts;
Expand Down

0 comments on commit 3eac9e5

Please sign in to comment.