From 5681d44c0f0d339bf92a23403d425263629d076c Mon Sep 17 00:00:00 2001 From: Mathieu Nayrolles Date: Mon, 8 Feb 2016 09:39:01 -0500 Subject: [PATCH] Optimize auto_emal and hooks to use active_user view #109 --- application/controllers/Hooks.php | 2 +- application/libraries/Auto_email.php | 51 ++++++++++--------- .../tests/libraries/Auto_email_test.php | 15 +++--- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/application/controllers/Hooks.php b/application/controllers/Hooks.php index f4cc7381..2b74c868 100644 --- a/application/controllers/Hooks.php +++ b/application/controllers/Hooks.php @@ -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")) { diff --git a/application/libraries/Auto_email.php b/application/libraries/Auto_email.php index 7db40bc9..190f3b96 100644 --- a/application/libraries/Auto_email.php +++ b/application/libraries/Auto_email.php @@ -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'); @@ -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(); @@ -218,7 +218,11 @@ private function showSentEmails($emails, $title){ echo "

".$title."

"; 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']; } @@ -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)) @@ -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(); @@ -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)) @@ -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']) ), @@ -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) @@ -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)) @@ -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)) @@ -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)) diff --git a/application/tests/libraries/Auto_email_test.php b/application/tests/libraries/Auto_email_test.php index 69b978a9..b3abab04 100644 --- a/application/tests/libraries/Auto_email_test.php +++ b/application/tests/libraries/Auto_email_test.php @@ -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")); @@ -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")); } }