Skip to content

Instantly share code, notes, and snippets.

@markphi2013
Created July 23, 2013 06:20
Show Gist options
  • Save markphi2013/6060225 to your computer and use it in GitHub Desktop.
Save markphi2013/6060225 to your computer and use it in GitHub Desktop.
error
<?php
App::uses('Component', 'Controller');
App::uses('Program', 'Model');
App::uses('ProgramSetting', 'Model');
class StatsComponent extends Component {
public $Controller = null;
public $redis = null;
public function initialize(Controller $controller)
//public function setUp()
{
parent::startup($controller);
$this->Controller = $controller;
if(isset($this->Controller->redis)){
$this->redis = $this->Controller->redis;
}else{
$this->redis = new Redis();
$this->redis->connect('127.0.0.1');
}
}
protected function _getProgramStats($database)
{
$this->ProgramSetting = new ProgramSetting(array('database' => $database));
$programTimeNow = $this->ProgramSetting->getProgramTimeNow();
$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('object-type' => array('$in' => $tempHistory->messageType))));
$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;
}
public function getProgramStats($database)
{
$redis = $this->redis;
$statsKey = 'vusion:programs:'.$database.':stats';
$stats = $this->redis->get($statsKey);
if($this->redis->strlen($statsKey) > 0){
$programStats = (array)json_decode($stats);
}else{
$programStats = $this->_getProgramStats($database);
$this->redis->setex($statsKey, 6,json_encode($programStats));
}
return $programStats;
}
}
?>
@markphi2013
Copy link
Author

redis =new Redis(); $this->redis->connect('127.0.0.1'); } ``` } class TestStatsComponent extends CakeTestCase { ``` public $StatsComponent = null; public $Controller = null; public function setUp() { parent::setUp(); $Collection = new ComponentCollection(); $this->StatsComponent = new StatsComponent($Collection); $CakeRequest = new CakeRequest(); $CakeResponse = new CakeResponse(); $this->Controller = new TestStatsComponentController($CakeRequest, $CakeResponse); $this->Controller->constructClasses(); $this->StatsComponent->initialize($this->Controller); $this->redis = $this->Controller->redis; } public function tearDown() { $keys = $this->redis->keys('vusion:programs:*'); foreach ($keys as $key){ $this->redis->delete($key); } } public function mockStats() { return array( 'active-participant-count' => '2', 'participant-count' => '2', 'all-received-messages-count'=> '2', 'current-month-received-messages-count' => '2', 'all-sent-messages-count' => '2', 'current-month-sent-messages-count' => '2', 'total-current-month-messages-count' => '2', 'history-count' => '2', 'today-schedule-count' => '2', 'schedule-count' => '2', 'object-type' => 'program-stats', 'model-version'=> '1' ); } public function instanciateModels() { $options = array('database' => 'testdbprogram'); $this->Participant = new Participant($options); $this->Schedule = new Schedule($options); $this->History = new History($options); } /* public function instanciateExternalModels($databaseName) { $this->externalModel['Participant'] = new Participant(array('database' => $databaseName)); $this->externalModel['Schedule'] = new Schedule(array('database' => $databaseName)); $this->externalModel['History'] = new History(array('database' => $databaseName)); } */ public function dropData() { $this->Participant->deleteAll(true, false); $this->Schedule->deleteAll(true, false); $this->History->deleteAll(true, false); foreach ($this->externalModels as $model) { $model->deleteAll(true, false); } } public function testGetStats_noStatsInRedis() { /*$this->Participant->create(); $this->externalModels['programSetting']->save( array( 'key'=>'shortcode', 'value'=>'256-8181' ) ); */ $key = "vusion:programs:test1:stats"; $stats = $this->redis->get($key); $this->assertEqual( null, $stats); } public function testGetStats() { $Stats = $this->mockStats(); $program = array( 'Program'=> array( 'database' => 'test1')); $key = "vusion:programs:test1:stats"; $this->redis->set($key, json_encode($Stats)); $programTestStats = $this->StatsComponent->getProgramStats($program['Program']['database']); $this->assertEqual( $Stats, $programTestStats); } ``` } ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment