Last active
December 24, 2015 13:29
-
-
Save overnin/6805904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//The issue is this function is not easy to test our new behavior we need to refactor it to make it easy to test | |
protected function _getProgramStats($database) | |
{ | |
$programStats = array( | |
'active-participant-count' => 'N/A', | |
'participant-count' => 'N/A', | |
'all-received-messages-count'=> 'N/A', | |
'current-month-received-messages-count' => 'N/A', | |
'all-sent-messages-count' => 'N/A', | |
'current-month-sent-messages-count' => 'N/A', | |
'total-current-month-messages-count' => 'N/A', | |
'history-count' => 'N/A', | |
'today-schedule-count' => 'N/A', | |
'schedule-count' => 'N/A', | |
'object-type' => 'program-stats', | |
'model-version'=> '1'); | |
try{ | |
$this->ProgramSetting = new ProgramSetting(array('database' => $database)); | |
$programTimeNow = $this->ProgramSetting->getProgramTimeNow(); | |
if(empty($programTimeNow)){ | |
return $programStats; | |
} | |
$tempParticipant = new Participant(array('database' => $database)); | |
$activeParticipantCount = $tempParticipant->find( | |
'count', array( | |
'conditions' => array('session-id' => array( | |
'$ne' => null) | |
) | |
) | |
); | |
$participantCount = $tempParticipant->find('count'); | |
$tempSchedule = new Schedule(array('database' => $database)); | |
$programTimeToday = $programTimeNow->modify('+1 day'); | |
$todayScheduleCount = $tempSchedule->find( | |
'count',array( | |
'conditions' => array( | |
'date-time' => array( | |
'$lt' => $programTimeToday->format(DateTime::ISO8601)) | |
) | |
)); | |
$scheduleCount = $tempSchedule->find('count'); | |
$tempHistory = new History(array('database' => $database)); | |
$programTimeForMonth = $programTimeNow->format("Y-m-d\TH:i:s"); | |
$first_second = date('Y-m-01\TH:i:s', strtotime($programTimeForMonth)); | |
$last_second = date('Y-m-t\TH:i:s', strtotime($programTimeForMonth)); | |
$allReceivedMessagesCount = $tempHistory->find( | |
'count',array( | |
'conditions' => array('message-direction' => 'incoming')) | |
); | |
$currentMonthReceivedMessagesCount = $tempHistory->find( | |
'count',array( | |
'conditions' => array( | |
'timestamp' => array( | |
'$gt' => $first_second, | |
'$lt' => $last_second | |
), | |
'message-direction' => 'incoming' | |
) | |
) | |
); | |
$currentMonthSentMessagesCount = $tempHistory->find( | |
'count',array( | |
'conditions' => array( | |
'timestamp' => array( | |
'$gt' => $first_second, | |
'$lt' => $last_second | |
), | |
'message-direction' => 'outgoing' | |
) | |
) | |
); | |
$totalCurrentMonthMessagesCount = $currentMonthSentMessagesCount + $currentMonthReceivedMessagesCount; | |
$allSentMessagesCount = $tempHistory->find( | |
'count',array( | |
'conditions' => array('message-direction' => 'outgoing')) | |
); | |
$historyCount = $tempHistory->find( | |
'count', array( | |
'conditions' =>array( | |
'$or' => array( | |
array('object-type' => array('$in' => $tempHistory->messageType)), | |
array('object-type' => array('$exists' => false )) | |
) | |
))); | |
$programStats = array( | |
'active-participant-count' => $activeParticipantCount, | |
'participant-count' => $participantCount, | |
'all-received-messages-count'=> $allReceivedMessagesCount, | |
'current-month-received-messages-count' => $currentMonthReceivedMessagesCount, | |
'all-sent-messages-count' => $allSentMessagesCount, | |
'current-month-sent-messages-count' => $currentMonthSentMessagesCount, | |
'total-current-month-messages-count' => $totalCurrentMonthMessagesCount, | |
'history-count' => $historyCount, | |
'today-schedule-count' => $todayScheduleCount, | |
'schedule-count' => $scheduleCount, | |
'object-type' => 'program-stats', | |
'model-version'=> '1'); | |
return $programStats; | |
} catch (Exception $e) { | |
return $programStats; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//For that we need a new function | |
protected function _getProgramStat($model, $conditions=array()) | |
{ | |
try { | |
return $model->find('count', $conditions); | |
} catch { | |
return 'N/A'; | |
} | |
} | |
protected function _getProgramStats($database) { | |
$programStats = array( | |
'active-participant-count' => 'N/A', | |
'participant-count' => 'N/A', | |
'all-received-messages-count'=> 'N/A', | |
'current-month-received-messages-count' => 'N/A', | |
'all-sent-messages-count' => 'N/A', | |
'current-month-sent-messages-count' => 'N/A', | |
'total-current-month-messages-count' => 'N/A', | |
'history-count' => 'N/A', | |
'today-schedule-count' => 'N/A', | |
'schedule-count' => 'N/A', | |
'object-type' => 'program-stats', | |
'model-version'=> '1'); | |
$this->ProgramSetting = new ProgramSetting(array('database' => $database)); | |
$programTimeNow = $this->ProgramSetting->getProgramTimeNow(); | |
if(empty($programTimeNow)){ | |
return $programStats; | |
} | |
/** participant stats **/ | |
$tempParticipant = new Participant(array('database' => $database)); | |
$programStats['participant-count'] = $this->_getProgramStat($tempParticipant); | |
$activeParticipantCount = $tempParticipant->find( | |
'count', array( | |
'conditions' => array('session-id' => array( | |
'$ne' => null) | |
) | |
) | |
); | |
$programStats['active-participant-count'] = $this->_getProgramStat($tempParticipant, $activeParticipantCount); | |
/** Schedule stats **/ | |
$tempSchedule = new Schedule(array('database' => $database)); | |
$programStats['schedule-count'] = $this->_getProgramStat(tempSchedule); | |
$programTimeToday = $programTimeNow->modify('+1 day'); | |
$todayScheduleCount = $tempSchedule->find( | |
'count',array( | |
'conditions' => array( | |
'date-time' => array( | |
'$lt' => $programTimeToday->format(DateTime::ISO8601)) | |
) | |
)); | |
$programStats['today-schedule-count'] = $this->_getProgramStat(tempSchedule, $todayScheduleCount); | |
.... | |
..... | |
.... | |
return $programStats; | |
} |
With this _getProgramStat() the _getProgramStats() doesn't need any more try {} catch {} as it's handle for each cound.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With the new function _getProgramStat() we can easily test the result with different cakephp timeout value