fix: Further performance tweaks to the feed fetcher

pull/2327/head
iglocska 2017-07-07 12:58:51 +02:00
parent 6b6ea52b58
commit 6c7c40e773
3 changed files with 36 additions and 23 deletions

View File

@ -279,25 +279,25 @@ class FeedsController extends AppController {
true true
); );
$this->Job->saveField('process_id', $process_id); $this->Job->saveField('process_id', $process_id);
$message = array('result' => 'Pull queued for background execution.'); $message = 'Pull queued for background execution.';
} else { } else {
$result = $this->Feed->downloadFromFeedInitiator($feedId, $this->Auth->user()); $result = $this->Feed->downloadFromFeedInitiator($feedId, $this->Auth->user());
if (!$result) { if (!$result) {
if ($this->_isRest()) { if ($this->_isRest()) {
return $this->RestResponse->viewData('Fetching the feed has failed.', $this->response->type()); return $this->RestResponse->viewData(array('result' => 'Fetching the feed has failed.'), $this->response->type());
} else { } else {
$this->Session->setFlash('Fetching the feed has failed.'); $this->Session->setFlash('Fetching the feed has failed.');
$this->redirect(array('action' => 'index')); $this->redirect(array('action' => 'index'));
} }
} }
$message = array('result' => 'Fetching the feed has successfuly completed.'); $message = 'Fetching the feed has successfuly completed.';
if ($this->Feed->data['Feed']['source_format'] == 'misp') { if ($this->Feed->data['Feed']['source_format'] == 'misp') {
if (isset($result['add'])) $message['result'] .= ' Downloaded ' . count($result['add']) . ' new event(s).'; if (isset($result['add'])) $message['result'] .= ' Downloaded ' . count($result['add']) . ' new event(s).';
if (isset($result['edit'])) $message['result'] .= ' Updated ' . count($result['edit']) . ' event(s).'; if (isset($result['edit'])) $message['result'] .= ' Updated ' . count($result['edit']) . ' event(s).';
} }
} }
if ($this->_isRest()) { if ($this->_isRest()) {
return $this->RestResponse->viewData($message, $this->response->type()); return $this->RestResponse->viewData(array('result' => $message), $this->response->type());
} else { } else {
$this->Session->setFlash($message); $this->Session->setFlash($message);
$this->redirect(array('action' => 'index')); $this->redirect(array('action' => 'index'));

View File

@ -37,6 +37,13 @@ class UsersController extends AppController {
if (!$this->_isSiteAdmin() && $this->Auth->user('id') != $id) { if (!$this->_isSiteAdmin() && $this->Auth->user('id') != $id) {
throw new NotFoundException(__('Invalid user or not authorised.')); throw new NotFoundException(__('Invalid user or not authorised.'));
} }
if (!is_numeric($id) && !empty($id)) {
$userId = $this->User->find('first', array(
'conditions' => array('email' => $id),
'fields' => array('id')
));
$id = $userid['User']['id'];
}
$this->User->id = $id; $this->User->id = $id;
$this->User->recursive = 0; $this->User->recursive = 0;
if (!$this->User->exists()) { if (!$this->User->exists()) {

View File

@ -704,33 +704,39 @@ class Feed extends AppModel {
} }
} }
if ($feed['Feed']['fixed_event']) { if ($feed['Feed']['fixed_event']) {
$event = $this->Event->find('first', array( $temp = $this->Event->Attribute->find('all', array(
'conditions' => array( 'conditions' => array(
'Event.id' => $event['Event']['id']), 'Attribute.deleted' => 0,
'recursive' => -1, 'Attribute.event_id' => $event['Event']['id']
'contain' => array( ),
'Attribute' => array( 'recursive' => -1,
'conditions' => array( 'fields' => array('id', 'value1', 'value2')
'Attribute.deleted' => 0) ));
) $event['Attribute'] = array();
) foreach ($temp as $k => $t) {
) $start = microtime(true);
); if (!empty($t['Attribute']['value2'])) {
$t['Attribute']['value'] = $t['Attribute']['value1'] . '|' . $t['Attribute']['value2'];
} else {
$t['Attribute']['value'] = $t['Attribute']['value1'];
}
$event['Attribute'][$t['Attribute']['id']] = $t['Attribute']['value'];
}
unset($temp);
$to_delete = array(); $to_delete = array();
foreach ($data as $k => $dataPoint) { foreach ($data as $k => $dataPoint) {
foreach ($event['Attribute'] as $attribute_key => $attribute) { $finder = array_search($dataPoint['value'], $event['Attribute']);
if ($dataPoint['value'] == $attribute['value'] && $dataPoint['type'] == $attribute['type'] && $attribute['category'] == $dataPoint['category']) { if ($finder !== false) {
unset($data[$k]); unset($data[$k]);
unset($event['Attribute'][$attribute_key]); unset($event['Attribute'][$finder]);
}
} }
} }
if ($feed['Feed']['delta_merge']) { if ($feed['Feed']['delta_merge']) {
foreach ($event['Attribute'] as $attribute) { foreach ($event['Attribute'] as $k => $attribute) {
$to_delete[] = $attribute['id']; $to_delete[] = $k;
} }
if (!empty($to_delete)) { if (!empty($to_delete)) {
$this->Event->Attribute->deleteAll(array('Attribute.id' => $to_delete)); $this->Event->Attribute->deleteAll(array('Attribute.id' => $to_delete, 'Attribute.deleted' => 0));
} }
} }
} }