mirror of https://github.com/MISP/MISP
fix: Fixed several issues with the sightings
- Main issue was the expensive and potentially large query used to find all sightings for a list of tags (used on the tag and galaxy cluster index) potentially fixes #1993pull/1994/head
parent
ac460f16c0
commit
72a326f50f
|
@ -31,18 +31,16 @@ class GalaxyClustersController extends AppController {
|
||||||
public function index($id) {
|
public function index($id) {
|
||||||
$this->paginate['conditions'] = array('GalaxyCluster.galaxy_id' => $id);
|
$this->paginate['conditions'] = array('GalaxyCluster.galaxy_id' => $id);
|
||||||
$clusters = $this->paginate();
|
$clusters = $this->paginate();
|
||||||
$sightedEventsToFetch = array();
|
$tagIds = array();
|
||||||
$sightings = array();
|
$sightings = array();
|
||||||
if (!empty($clusters)) {
|
if (!empty($clusters)) {
|
||||||
$galaxyType = $clusters[0]['GalaxyCluster']['type'];
|
$galaxyType = $clusters[0]['GalaxyCluster']['type'];
|
||||||
foreach ($clusters as $k => $v) {
|
foreach ($clusters as $k => $v) {
|
||||||
$clusters[$k]['event_ids'] = array();
|
$clusters[$k]['event_ids'] = array();
|
||||||
if (!empty($v['Tag']['EventTag'])) {
|
if (!empty($v['Tag']['EventTag'])) {
|
||||||
|
$tagIds[] = $v['Tag']['id'];
|
||||||
$clusters[$k]['GalaxyCluster']['tags'] = array('tag_id' => $v['Tag']['id'], 'count' => count($v['Tag']['EventTag']));
|
$clusters[$k]['GalaxyCluster']['tags'] = array('tag_id' => $v['Tag']['id'], 'count' => count($v['Tag']['EventTag']));
|
||||||
foreach ($v['Tag']['EventTag'] as $eventTag) {
|
foreach ($v['Tag']['EventTag'] as $eventTag) {
|
||||||
if (!in_array($eventTag['event_id'], $sightedEventsToFetch)) {
|
|
||||||
$sightedEventsToFetch[] = $eventTag['event_id'];
|
|
||||||
}
|
|
||||||
$clusters[$k]['event_ids'][] = $eventTag['event_id'];
|
$clusters[$k]['event_ids'][] = $eventTag['event_id'];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -55,7 +53,7 @@ class GalaxyClustersController extends AppController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->loadModel('Sighting');
|
$this->loadModel('Sighting');
|
||||||
$sightings['event'] = $this->Sighting->getSightingsForObjectIds($this->Auth->user(), $sightedEventsToFetch);
|
$sightings['event'] = $this->Sighting->getSightingsForObjectIds($this->Auth->user(), $tagIds);
|
||||||
foreach ($clusters as $k => $cluster) {
|
foreach ($clusters as $k => $cluster) {
|
||||||
$objects = array('event');
|
$objects = array('event');
|
||||||
foreach ($objects as $object) {
|
foreach ($objects as $object) {
|
||||||
|
|
|
@ -49,10 +49,10 @@ class TagsController extends AppController {
|
||||||
} else {
|
} else {
|
||||||
$paginated = $this->paginate();
|
$paginated = $this->paginate();
|
||||||
}
|
}
|
||||||
$sightedEventsToFetch = array();
|
$tagList = array();
|
||||||
$sightedAttributesToFetch = array();
|
|
||||||
$csv = array();
|
$csv = array();
|
||||||
foreach ($paginated as $k => $tag) {
|
foreach ($paginated as $k => $tag) {
|
||||||
|
$tagList[] = $tag['Tag']['id'];
|
||||||
if (empty($tag['EventTag'])) {
|
if (empty($tag['EventTag'])) {
|
||||||
$paginated[$k]['Tag']['count'] = 0;
|
$paginated[$k]['Tag']['count'] = 0;
|
||||||
} else {
|
} else {
|
||||||
|
@ -79,16 +79,10 @@ class TagsController extends AppController {
|
||||||
$paginated[$k]['event_ids'] = array();
|
$paginated[$k]['event_ids'] = array();
|
||||||
$paginated[$k]['attribute_ids'] = array();
|
$paginated[$k]['attribute_ids'] = array();
|
||||||
foreach($paginated[$k]['EventTag'] as $et) {
|
foreach($paginated[$k]['EventTag'] as $et) {
|
||||||
if (!in_array($et['event_id'], $sightedEventsToFetch)) {
|
|
||||||
$sightedEventsToFetch[] = $et['event_id'];
|
|
||||||
}
|
|
||||||
$paginated[$k]['event_ids'][] = $et['event_id'];
|
$paginated[$k]['event_ids'][] = $et['event_id'];
|
||||||
}
|
}
|
||||||
unset($paginated[$k]['EventTag']);
|
unset($paginated[$k]['EventTag']);
|
||||||
foreach($paginated[$k]['AttributeTag'] as $at) {
|
foreach($paginated[$k]['AttributeTag'] as $at) {
|
||||||
if (!in_array($at['attribute_id'], $sightedAttributesToFetch)) {
|
|
||||||
$sightedAttributesToFetch[] = $at['attribute_id'];
|
|
||||||
}
|
|
||||||
$paginated[$k]['attribute_ids'][] = $at['attribute_id'];
|
$paginated[$k]['attribute_ids'][] = $at['attribute_id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,8 +132,8 @@ class TagsController extends AppController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->loadModel('Sighting');
|
$this->loadModel('Sighting');
|
||||||
$sightings['event'] = $this->Sighting->getSightingsForObjectIds($this->Auth->user(), $sightedEventsToFetch);
|
$sightings['event'] = $this->Sighting->getSightingsForObjectIds($this->Auth->user(), $tagList);
|
||||||
$sightings['attribute'] = $this->Sighting->getSightingsForObjectIds($this->Auth->user(), $sightedAttributesToFetch, 'attribute');
|
$sightings['attribute'] = $this->Sighting->getSightingsForObjectIds($this->Auth->user(), $tagList, 'attribute');
|
||||||
foreach ($paginated as $k => $tag) {
|
foreach ($paginated as $k => $tag) {
|
||||||
$objects = array('event', 'attribute');
|
$objects = array('event', 'attribute');
|
||||||
foreach ($objects as $object) {
|
foreach ($objects as $object) {
|
||||||
|
|
|
@ -462,6 +462,10 @@ class Attribute extends AppModel {
|
||||||
public $hasMany = array(
|
public $hasMany = array(
|
||||||
'AttributeTag' => array(
|
'AttributeTag' => array(
|
||||||
'dependent' => true
|
'dependent' => true
|
||||||
|
),
|
||||||
|
'Sighting' => array(
|
||||||
|
'className' => 'Sighting',
|
||||||
|
'dependent' => true,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -318,6 +318,10 @@ class Event extends AppModel {
|
||||||
'EventTag' => array(
|
'EventTag' => array(
|
||||||
'className' => 'EventTag',
|
'className' => 'EventTag',
|
||||||
'dependent' => true,
|
'dependent' => true,
|
||||||
|
),
|
||||||
|
'Sighting' => array(
|
||||||
|
'className' => 'Sighting',
|
||||||
|
'dependent' => true,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -3239,10 +3243,11 @@ class Event extends AppModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return array(
|
return array(
|
||||||
'data' => $sightingsData,
|
'data' => $sightingsData,
|
||||||
'csv' => $csv
|
'csv' => $csv
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
return array('data' => array(), 'csv' => array());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -198,20 +198,29 @@ class Sighting extends AppModel {
|
||||||
return $id;
|
return $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSightingsForObjectIds($user, $ids, $context = 'event', $type = '0') {
|
public function getSightingsForObjectIds($user, $tagList, $context = 'event', $type = '0') {
|
||||||
//$attributes = $this->Attribute->listVisibleAttributes($user, array('conditions' => array('Attribute.event_id' => array_values($eventIds))));
|
|
||||||
$range = (!empty(Configure::read('MISP.Sightings_range')) && is_numeric(Configure::read('MISP.Sightings_range'))) ? Configure::read('MISP.Sightings_range') : 365;
|
$range = (!empty(Configure::read('MISP.Sightings_range')) && is_numeric(Configure::read('MISP.Sightings_range'))) ? Configure::read('MISP.Sightings_range') : 365;
|
||||||
$conditions = array(
|
$conditions = array(
|
||||||
'Sighting.' . $context . '_id' => $ids,
|
'Sighting.date_sighting >' => strtotime("-" . $range . " days"),
|
||||||
'Sighting.date_sighting >' => strtotime("-" . $range . " days")
|
ucfirst($context) . 'Tag.tag_id' => $tagList
|
||||||
|
|
||||||
|
);
|
||||||
|
$contain = array(
|
||||||
|
ucfirst($context) => array(
|
||||||
|
ucfirst($context) . 'Tag' => array(
|
||||||
|
'Tag'
|
||||||
|
)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
if ($type !== false) {
|
if ($type !== false) {
|
||||||
$conditions['Sighting.type'] = $type;
|
$conditions['Sighting.type'] = $type;
|
||||||
}
|
}
|
||||||
|
$this->bindModel(array('hasOne' => array(ucfirst($context) . 'Tag' => array('foreignKey' => false, 'conditions' => ucfirst($context) . 'Tag.' . $context . '_id = Sighting.' . $context . '_id'))));
|
||||||
$sightings = $this->find('all', array(
|
$sightings = $this->find('all', array(
|
||||||
'recursive' => -1,
|
'recursive' => -1,
|
||||||
|
'contain' => array(ucfirst($context) . 'Tag'),
|
||||||
'conditions' => $conditions,
|
'conditions' => $conditions,
|
||||||
'fields' => array('Sighting.id', 'Sighting.' . $context . '_id', 'Sighting.date_sighting')
|
'fields' => array('Sighting.id', 'Sighting.' . $context . '_id', 'Sighting.date_sighting', ucfirst($context) . 'Tag.tag_id')
|
||||||
));
|
));
|
||||||
$sightingsRearranged = array();
|
$sightingsRearranged = array();
|
||||||
foreach ($sightings as $sighting) {
|
foreach ($sightings as $sighting) {
|
||||||
|
|
|
@ -465,7 +465,7 @@
|
||||||
</td>
|
</td>
|
||||||
<td class="short <?php echo $extra; ?>">
|
<td class="short <?php echo $extra; ?>">
|
||||||
<?php
|
<?php
|
||||||
if ($object['objectType'] == 0) {
|
if ($object['objectType'] == 0 && !empty($temp)) {
|
||||||
echo $this->element('sparkline', array('id' => $object['id'], 'csv' => $temp));
|
echo $this->element('sparkline', array('id' => $object['id'], 'csv' => $temp));
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -175,7 +175,11 @@
|
||||||
<dt>Activity</dt>
|
<dt>Activity</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<?php
|
<?php
|
||||||
|
if (!empty($sightingsData['data'])) {
|
||||||
echo $this->element('sparkline', array('id' => $event['Event']['id'], 'csv' => $sightingsData['csv']['event']));
|
echo $this->element('sparkline', array('id' => $event['Event']['id'], 'csv' => $sightingsData['csv']['event']));
|
||||||
|
} else {
|
||||||
|
echo ' ';
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
</dd>
|
</dd>
|
||||||
<?php
|
<?php
|
||||||
|
|
Loading…
Reference in New Issue