chg: [internal] Faster capturing object attributes

pull/7733/head
Jakub Onderka 2021-09-07 10:09:08 +02:00
parent c00b710c1e
commit d582893bdf
3 changed files with 18 additions and 13 deletions

View File

@ -404,11 +404,9 @@ class Attribute extends AppModel
public function afterSave($created, $options = array())
{
$passedEvent = false;
if (isset($options['parentEvent'])) {
$passedEvent = $options['parentEvent'];
}
parent::afterSave($created, $options);
// Passing event in `parentEvent` field will speed up correlation
$passedEvent = isset($options['parentEvent']) ? $options['parentEvent'] : false;
// add attributeTags via the shorthand ID list
if (!empty($this->data['Attribute']['tag_ids'])) {
foreach ($this->data['Attribute']['tag_ids'] as $tag_id) {

View File

@ -3850,21 +3850,18 @@ class Event extends AppModel
$attributeHashes = [];
foreach ($data['Event']['Attribute'] as $k => $attribute) {
$attributeHash = sha1($attribute['value'] . '|' . $attribute['type'] . '|' . $attribute['category'], true);
if (isset($attributeHashes[$attributeHash])) {
unset($data['Event']['Attribute'][$k]); // remove duplicate attribute
} else {
if (!isset($attributeHashes[$attributeHash])) { // do not save duplicate values
$attributeHashes[$attributeHash] = true;
$data['Event']['Attribute'][$k] = $this->Attribute->captureAttribute($attribute, $this->id, $user, 0, null, $parentEvent);
}
}
unset($attributeHashes);
$data['Event']['Attribute'] = array_values($data['Event']['Attribute']);
}
if (!empty($data['Event']['Object'])) {
$referencesToCapture = [];
foreach ($data['Event']['Object'] as $object) {
$result = $this->Object->captureObject($object, $this->id, $user, null, false, $breakOnDuplicate);
$result = $this->Object->captureObject($object, $this->id, $user, null, false, $breakOnDuplicate, $parentEvent);
if (isset($object['ObjectReference'])) {
foreach ($object['ObjectReference'] as $objectRef) {
$objectRef['source_uuid'] = $object['uuid'];

View File

@ -969,7 +969,18 @@ class MispObject extends AppModel
return $this->id;
}
public function captureObject($object, $eventId, $user, $log = false, $unpublish = true, $breakOnDuplicate = false)
/**
* @param array $object
* @param int $eventId
* @param array $user
* @param false $log - Not used anymore
* @param bool $unpublish
* @param false $breakOnDuplicate
* @param array|false $parentEvent
* @return bool|string
* @throws Exception
*/
public function captureObject($object, $eventId, $user, $log = false, $unpublish = true, $breakOnDuplicate = false, $parentEvent = false)
{
$this->create();
if (!isset($object['Object'])) {
@ -995,10 +1006,9 @@ class MispObject extends AppModel
$this->Event->unpublishEvent($eventId);
}
$objectId = $this->id;
$partialFails = array();
if (!empty($object['Object']['Attribute'])) {
foreach ($object['Object']['Attribute'] as $attribute) {
$this->Attribute->captureAttribute($attribute, $eventId, $user, $objectId);
$this->Attribute->captureAttribute($attribute, $eventId, $user, $objectId, false, $parentEvent);
}
}
return true;