Skip to content

Instantly share code, notes, and snippets.

@markphi2013
Created July 12, 2013 11:45
Show Gist options
  • Save markphi2013/5983860 to your computer and use it in GitHub Desktop.
Save markphi2013/5983860 to your computer and use it in GitHub Desktop.
i working on this with Gerald and seems to work well with the saving to redis and getting the values from there
<?php
App::uses('Component', 'Controller');
App::uses('Program', 'Model');
class StatsComponent extends Component {
public function getRedis()
{
$redis = new Redis();
$redis->connect('127.0.0.1');
return $redis;
}
protected function _getProgramStats($program)
{
$database = $program['Program']['database'];
$tempParticipant = new Participant(array('database' => $database));
$activeParticipantCount = $tempParticipant->find(
'count', array(
'conditions' => array('session-id' => array(
'$ne' => null)
)
)
);
$participantCount = $tempParticipant->find('count');
$tempHistory = new History(array('database' => $database));
$AllReceivedMessagesCount = $tempHistory->find(
'count',array(
'conditions' => array('message-direction' => 'incoming'))
);
$AllSentMessagesCount = $tempHistory->find(
'count',array(
'conditions' => array('message-direction' => 'outgoing'))
);
$historyCount = $tempHistory->find(
'count', array(
'conditions' => array('object-type' => array('$in' => $tempHistory->messageType))));
$tempSchedule = new Schedule(array('database' => $database));
$now = new DateTime('now');
$todayScheduleCount = $tempSchedule->find(
'count',array(
'conditions' => array('date-time' => $now)
));
$scheduleCount = $tempSchedule->find('count');
$programStats = array(
'active-participant-count' => $activeParticipantCount,
'participant-count' => $participantCount,
'all-received-messages-count'=> $AllReceivedMessagesCount,
'all-sent-messages-count' => $AllSentMessagesCount,
'history-count' => $historyCount,
'today-schedule-count' => $todayScheduleCount,
'schedule-count' => $scheduleCount);
return $programStats;
}
public function getProgramStats($program)
{
$database = $program['Program']['database'];
$redis = $this->getRedis();
$statsKey = 'vusion:programs:'.$database.':stats';
$stats = $redis->get($statsKey);
if($redis->strlen($statsKey) > 0){
$programStats = (array)json_decode($stats);
}else{
$programStats = $this->_getProgramStats($program);
$redis->setex($statsKey, 6,json_encode($programStats));
}
$program['Program']['stats'] = $programStats;
$program['Program']['stats']['active-participant-count'] = $programStats['active-participant-count'];
$program['Program']['stats']['participant-count'] = $programStats['participant-count'];
$program['Program']['stats']['all-received-messages-count'] = $programStats['all-received-messages-count'];
$program['Program']['stats']['all-sent-messages-count'] = $programStats['all-sent-messages-count'];
$program['Program']['stats']['history-count'] = $programStats['history-count'];
$program['Program']['stats']['today-schedule-count'] = $programStats['today-schedule-count'];
$program['Program']['stats']['schedule-count'] = $programStats['schedule-count'];
return $program;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment