First, what do you mean by CPU spikes? What action causes it, and have
you run a profiler on it?

Second, why are you using raw SQL queries?

On Jun 23, 4:33 am, PD <[email protected]> wrote:
> Hello Everyone,
>
> I am developing a  simple cake application which is causing CPU
> spikes. I dont know what I am doing wrong. I have pasted my controller
> code below.
>
> class DatasyncController extends AppController
> {
>
>         public $uses = array('LiveServer', 'LivePhone', 'SyncLog', 'Server',
> 'Phone');
>
>         public function index() {
>                 echo 'This is Datasync->index()';
>                 exit();
>         }
>
>         public function serversync() {
>                 // sync server
>                 $traverseOrder = array('LiveServer.result_id ASC'); // 
> traverse the
> list starting with the first record
>
>                 $current = $this->LiveServer->find('first', array('order' =>
> $traverseOrder, 'fields' => array('LiveServer.result_id',
> 'LiveServer.server_id', 'LiveServer.reported')));
>                 $hasNext = true;
>
>                 do
>                 {
>                         //process current
>                         if (!$this->SyncLog->find('first', array('conditions' 
> => array('id'
> => $current['LiveServer']['result_id']))))
>                         {
>                                 if (!$this->Server->find('first', 
> array('conditions' => array('id'
> => $current['LiveServer']['server_id']))))
>                                         $this->Server->save(array('Server' => 
> array('id' => $current
> ['LiveServer']['server_id'], 'first_log_date' => $current['LiveServer']
> ['reported'], 'last_log_date' => $current['LiveServer']['reported'],
> 'total_logs' => '1')));
>                                 else
>                                         $this->Server->query("       UPDATE 
> {$this->Server->useTable}
>                                                                               
>           SET last_log_date = '{$current['LiveServer']['reported']}',
> total_logs = total_logs + 1
>                                                                               
>           WHERE id = '{$current['LiveServer']['server_id']}'
>                                                                               
>           LIMIT 1"
>                                                                               
>   );
>
>                                 // record the result id to keep track;
>                                 $this->SyncLog->save(array('SyncLog' => 
> array('id' => $current
> ['LiveServer']['result_id'])));
>                                 echo " + ";
>                                 //echo "Inserted 
> {$current['LiveServer']['result_id']} - {$current
> ['LiveServer']['server_id']}<br />";
>                         }
>
>                         // find next
>                         $neighbors = $this->LiveServer->find('neighbors', 
> array('field' =>
> 'result_id', 'value' => $current['LiveServer']['result_id'], 'order'
> => $traverseOrder, 'fields' => array('LiveServer.result_id',
> 'LiveServer.server_id', 'LiveServer.reported')));
>                         $hasNext = is_array($neighbors['next']);
>                         $current = $neighbors['next'];
>
>                 } while ($hasNext);
>                 echo "done";exit();
>                 // sync phones
>         }
>
>         public function phonestats() {
>
>                 // sync phones
>                 $traverseOrder = array('LivePhone.phone_id');
>
>                 $current = $this->LivePhone->find('first', array('order' =>
> $traverseOrder, 'fields' => array('DISTINCT LivePhone.mac',
> 'LivePhone.useragent', )));
>                 $hasNext = true;
>                 do {
>                         foreach ($current['LivePhone'] as &$value) $value = 
> trim($value);
>                         $phoneTypes = array( "Aastra", "Cisco", 
> "Grandstream", "Polycom",
> "Linksys", "snom", "X-Lite", "Zoiper", "eyeBeam", "Sipura", "Siemens",
> "Pirelli");
>                         foreach ($phoneTypes as $phoneType) {
>                                 if 
> (stripos($current['LivePhone']['useragent'], $phoneType)) {
>
>                                         $q = "     UPDATE 
> {$this->PhoneStat->useTable}
>                                                         SET value = value + 1
>                                                         WHERE id = 
> '{$current['LivePhone']['server_id']}'
>                                                         LIMIT 1";
>                                         $c = "CREATE TABLE stats (
>                                                         id int(11) NOT NULL,
>                                                         type enum('server', 
> 'phone') NOT NULL,
>                                                         attribute 
> varchar(255) NOT NULL,
>                                                         )";
>                                         $this->Phone->query($q);
>                                         //echo "<br />" . $q;
>                                         break;
>                                 }
>                         }
>
>                         // find next
>                         $neighbors = $this->LivePhone->find('neighbors', 
> array('field' =>
> 'phone_id', 'value' => $current['LivePhone']['phone_id'], 'order' =>
> $traverseOrder, 'fields' => array('LivePhone.server_id',
> 'LivePhone.mac', 'LivePhone.add_date')));
>                         $hasNext = is_array($neighbors['next']);
>                         $current = $neighbors['next'];
>                 } while ($hasNext);
>                 exit();
>         }
>
>         public function phonesync() {
>                 // sync phones
>                 $traverseOrder = array('LivePhone.phone_id');
>
>                 $current = $this->LivePhone->find('first', array('order' =>
> $traverseOrder, 'fields' => array('LivePhone.server_id',
> 'LivePhone.mac', 'LivePhone.add_date')));
>                 $hasNext = true;
>                 do {
>                         foreach ($current['LivePhone'] as &$value) $value = 
> trim($value);
>
>                         // if this record has not been processed - process it
>                         if (!$this->SyncLog->find('first', array('conditions' 
> => array('id'
> => $current['LivePhone']['phone_id']))) && strlen($current['LivePhone']
> ['mac'])) {
>                                 if (!$this->Phone->find('first', 
> array('conditions' => array('mac'
> => $current['LivePhone']['mac'])))) {
>                                         echo "<br />added new phone";
>                                         $phoneData = array('Phone' =>
>                                                                               
>           array(
>                                                                               
>                           'server_id' => $current['LivePhone']['server_id'],
>                                                                               
>                           'mac' => $current['LivePhone']['mac'],
>                                                                               
>                           'first_log_date' => 
> $current['LivePhone']['add_date'],
>                                                                               
>                           'last_log_date' => 
> $current['LivePhone']['add_date'],
>                                                                               
>                           'total_logs' => '1'
>                                                                               
>           )
>                                                                               
>   );
>                                         $this->Phone->save($phoneData);
>                                         $q = "     UPDATE 
> {$this->Server->useTable}
>                                                         SET total_phones = 
> total_phones + 1
>                                                         WHERE id = 
> '{$current['LivePhone']['server_id']}'
>                                                         LIMIT 1";
>                                         $this->Phone->query($q);
>                                         //echo "<br />" . $q;
>                                 } else {
>                                         echo "<br />updated existing phone";
>                                         $q = "     UPDATE 
> {$this->Phone->useTable}
>                                                         SET last_log_date = 
> '{$current['LivePhone']['add_date']}',
> total_logs = total_logs + 1
>                                                         WHERE mac = 
> '{$current['LivePhone']['mac']}'
>                                                         LIMIT 1";
>                                         $this->Phone->query($q);
>                                         //echo "<br />" . $q;
>                                 }
>
>                                 // record the processed phone id to keep 
> track;
>                                 $this->SyncLog->save(array('SyncLog' => 
> array('id' => $current
> ['LivePhone']['phone_id'])));
>                                 //echo 
> "<pre>";print_r($current['LivePhone']);echo "</pre>";
>                                 echo " + ";
>
>                                 // find next
>                                 $neighbors = 
> $this->LivePhone->find('neighbors', array('field' =>
> 'phone_id', 'value' => $current['LivePhone']['phone_id'], 'order' =>
> $traverseOrder, 'fields' => array('LivePhone.server_id',
> 'LivePhone.mac', 'LivePhone.add_date')));
>                                 $hasNext = is_array($neighbors['next']);
>                                 $current = $neighbors['next'];
>                         }
>                 } while ($hasNext);
>                 exit();
>         }
>
> }
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to