Skip to content

Commit

Permalink
Optimize auto_emal and hooks to use active_user view #109
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuNls committed Feb 8, 2016
1 parent f3dc526 commit 5681d44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion application/controllers/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function index() {
$text = $this->input->post('text');
$quote = $this->quotes[rand(0, 18)];
$result["text"] = $quote;
$activeUser = new MY_MODEL("active_user");
$activeUser = new MY_MODEL("active_user");


if (startsWith($text, "Jack nbusers")) {
Expand Down
51 changes: 26 additions & 25 deletions application/libraries/Auto_email.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ function __construct() {
$this->CI->load->library("__");
$this->CI->load->model("watch");
$this->CI->load->model("measure");
$this->CI->load->model("user");
$this->CI->load->helper("email_content");
$this->CI->load->library("mcapi");
$this->CI->config->load('config');
Expand Down Expand Up @@ -147,6 +146,7 @@ public function cronCheck($timeOffset = 0) {
$emailsMeasureSent = array();

$this->emailBatchModel = new MY_MODEL("email_batch");
$this->activeUser = new MY_MODEL('active_user');

$this->lastBatchDate = $this->findLastBatchDate();

Expand Down Expand Up @@ -218,7 +218,11 @@ private function showSentEmails($emails, $title){

echo "<h2> ".$title." </h2>";
foreach ($emails as $email) {
echo 'TO ' . $this->CI->user->find_by('userId', $email['userId'])->email;

if(isset($email['userId'])){
echo 'TO ' . $this->CI->user->find_by('userId', $email['userId'])->email;
}

echo '\n'; var_dump($email['mandrill']); echo '\n';
echo $email['content'];
}
Expand Down Expand Up @@ -386,8 +390,7 @@ private function inactiveUser(&$queuedEmail) {

log_message('info', 'inactiveUser');

$inactiveUsers = $this->CI
->user
$inactiveUsers = $this->activeUser
->select()
->where('lastLogin <', $this->getBatchUpperBound($this->day*100))
->where('lastLogin >', $this->getBatchLowerBound($this->day*100))
Expand Down Expand Up @@ -434,10 +437,9 @@ private function userWithoutWatch(&$queuedEmail) {

log_message('info', 'userWithoutWatch');

$userWithoutWatch = $this->CI
->user
->select('user.userId, user.name, firstname, email, lastLogin')
->where('(select count(1) from watch where user.userId = watch.userId) =', 0)
$userWithoutWatch = $this->activeUser
->select('userId, name, firstname, email, lastLogin')
->where('watches', 0)
->where('lastLogin <', $this->getBatchUpperBound($this->day))
->where('lastLogin >', $this->getBatchLowerBound($this->day))
->find_all();
Expand Down Expand Up @@ -479,9 +481,9 @@ private function userWithWatchWithoutMeasure(&$queuedEmail) {

$userWithWatchWithoutMeasure = $this->CI
->watch
->select('user.userId, watch.watchId, watch.brand, watch.name as watchName,
user.name as lastname, user.firstname, email')
->join('user', 'watch.userId = user.userId')
->select('active_user.userId, watch.watchId, watch.brand, watch.name as watchName,
active_user.name as lastname, firstname, email')
->join('active_user', 'watch.userId = active_user.userId')
->where('(select count(1) from measure where watch.watchId = measure.watchId) = ', 0)
->where('creationDate < ', $this->getBatchUpperBound($this->day))
->where('creationDate > ', $this->getBatchLowerBound($this->day))
Expand All @@ -497,7 +499,7 @@ private function userWithWatchWithoutMeasure(&$queuedEmail) {
$emailcontent = $this->CI->load->view(
'email/generic',
makeFirstMeasureContent(
$user->firstname,
$user[0]['firstname'],
$user,
$this->CI->measure->getMeasuresByUser($user[0]['userId'])
),
Expand Down Expand Up @@ -534,13 +536,12 @@ private function userWithOneCompleteMeasureAndOneWatch(&$queuedEmail) {

log_message('info', 'userWithOneCompleteMeasureAndOneWatch');

$userWithOneCompleteMeasureAndOneWatch = $this->CI
->user
->select('user.userId, user.name, firstname, email')
->where('(select count(1) from watch where user.userId = watch.userId) = ', 1)
$userWithOneCompleteMeasureAndOneWatch = $this->activeUser
->select('active_user.userId, active_user.name, firstname, email')
->where('watches', 1)
->where('(select count(1) from measure
join watch on measure.watchId = watch.watchId
where user.userId = watch.userId
where active_user.userId = watch.userId
and measure.statusId = 2
and measure.accuracyReferenceTime < '.$this->getBatchUpperBound($this->day*2).'
and measure.accuracyReferenceTime > '.$this->getBatchLowerBound($this->day*2). ') = ', 1)
Expand Down Expand Up @@ -599,10 +600,10 @@ private function checkAccuracy(&$queuedEmail) {
$measureWithoutAccuracy = $this->CI
->measure
->select('measure.id as measureId, measure.*, watch.*,
watch.name as watchName, user.userId, user.name as lastname,
user.firstname, email')
watch.name as watchName, active_user.userId, active_user.name as lastname,
active_user.firstname, email')
->join('watch', 'watch.watchId = measure.watchId')
->join('user', 'watch.userId = user.userId')
->join('active_user', 'watch.userId = active_user.userId')
->where('statusId', 1)
->where('measureReferenceTime <', $this->getBatchUpperBound($this->day))
->where('measureReferenceTime >', $this->getBatchLowerBound($this->day))
Expand Down Expand Up @@ -656,10 +657,10 @@ private function checkAccuracyOneWeek(&$queuedEmail) {

$measureWithoutAccuracy = $this->CI
->measure
->select('measure.id as measureId, watch.*, user.userId,
measure.*, watch.name as watchName, user.name as lastname, user.firstname, email')
->select('measure.id as measureId, watch.*, active_user.userId,
measure.*, watch.name as watchName, active_user.name as lastname, active_user.firstname, email')
->join('watch', 'watch.watchId = measure.watchId')
->join('user', 'watch.userId = user.userId')
->join('active_user', 'watch.userId = active_user.userId')
->where('statusId', 1)
->where('measureReferenceTime <', $this->getBatchUpperBound($this->day*7))
->where('measureReferenceTime >', $this->getBatchLowerBound($this->day*7))
Expand Down Expand Up @@ -714,9 +715,9 @@ private function startANewMeasure(&$queuedEmail) {
$watchesInNeedOfNewMeasure = $this->CI
->measure
->select('watch.watchId, watch.name as watchName, watch.brand,
user.userId, user.name as lastname, user.firstname, email, measure.*')
active_user.userId, active_user.name as lastname, active_user.firstname, email, measure.*')
->join('watch', 'watch.watchId = measure.watchId')
->join('user', 'watch.userId = user.userId')
->join('active_user', 'watch.userId = active_user.userId')
->where('statusId', 2)
->where('accuracyReferenceTime <', $this->getBatchUpperBound($this->day*30))
->where('accuracyReferenceTime >', $this->getBatchLowerBound($this->day*30))
Expand Down
15 changes: 7 additions & 8 deletions application/tests/libraries/Auto_email_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public static function setUpBeforeClass() {

$emailBatch = new MY_Model("email_batch");
$emailBatch->delete_where(array("id >=" => "0"));

$CI->measure->delete_where(array("id >=" => "0"));
$CI->Watch->delete_where(array("watchId >=" => "0"));
$CI->User->delete_where(array("userId >=" => "0"));
Expand Down Expand Up @@ -438,13 +437,13 @@ public function test_addFirstMeasure(){
}

public static function tearDownAfterClass() {
$CI = &get_instance();
$CI->load->model('User');
$CI->load->model('Watch');
$CI->load->model('Measure');
$CI->watch->delete_where(array("watchId >=" => "0"));
$CI->User->delete_where(array("userId >=" => "0"));
$CI->Measure->delete_where(array("id >=" => "0"));
$CI = &get_instance();
$CI->load->model('User');
$CI->load->model('Watch');
$CI->load->model('Measure');
$CI->watch->delete_where(array("watchId >=" => "0"));
$CI->User->delete_where(array("userId >=" => "0"));
$CI->Measure->delete_where(array("id >=" => "0"));
}

}

0 comments on commit 5681d44

Please sign in to comment.