From 6e2635a346b8752bd3dbcbb550bd9a7d0ba94257 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Tue, 16 Apr 2019 14:14:51 +0200 Subject: [PATCH] chg: [event:view] Increase tag colleciton efficiency --- app/Controller/EventsController.php | 13 +++++---- app/Model/AttributeTag.php | 45 ++++++++++++++--------------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index 5cd7d8a1b..9a0c73432 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -1086,6 +1086,10 @@ class EventsController extends AppController } $event = $results[0]; + $attributeTagsName = $this->Event->Attribute->AttributeTag->extractAttributeTagsNameFromEvent($event, 'both'); + $this->set('attributeTags', array_values($attributeTagsName['tags'])); + $this->set('attributeClusters', array_values($attributeTagsName['clusters'])); + if (isset($filters['distribution'])) { if (!is_array($filters['distribution'])) { $filters['distribution'] = array($filters['distribution']); @@ -1195,9 +1199,6 @@ class EventsController extends AppController $this->set('advancedFilteringActive', $advancedFiltering['active'] ? 1 : 0); $this->set('advancedFilteringActiveRules', $advancedFiltering['activeRules']); $this->set('defaultFilteringRules', $this->defaultFilteringRules); - $attributeTagsName = $this->Event->Attribute->AttributeTag->extractAttributeTagsNameFromEvent($event['objects'], 'both'); - $this->set('attributeTags', $attributeTagsName['tags']); - $this->set('attributeClusters', $attributeTagsName['clusters']); $this->disableCache(); $this->layout = 'ajax'; $this->loadModel('Sighting'); @@ -1337,6 +1338,9 @@ class EventsController extends AppController } } } + $attributeTagsName = $this->Event->Attribute->AttributeTag->extractAttributeTagsNameFromEvent($event, 'both'); + $this->set('attributeTags', array_values($attributeTagsName['tags'])); + $this->set('attributeClusters', array_values($attributeTagsName['clusters'])); $startDate = $event['Event']['timestamp']; $modDate = date("Y-m-d", $event['Event']['timestamp']); $modificationMap[$modDate] = 1; @@ -1473,9 +1477,6 @@ class EventsController extends AppController $this->set('advancedFilteringActive', $advancedFiltering['active'] ? 1 : 0); $this->set('advancedFilteringActiveRules', $advancedFiltering['activeRules']); $this->set('defaultFilteringRules', $this->defaultFilteringRules); - $attributeTagsName = $this->Event->Attribute->AttributeTag->extractAttributeTagsNameFromEvent($event['objects'], 'both'); - $this->set('attributeTags', $attributeTagsName['tags']); - $this->set('attributeClusters', $attributeTagsName['clusters']); $this->set('mitreAttackGalaxyId', $this->Event->GalaxyCluster->Galaxy->getMitreAttackGalaxyId()); $this->set('modificationMapCSV', $modificationMapCSV); } diff --git a/app/Model/AttributeTag.php b/app/Model/AttributeTag.php index bee4df891..6eda31d0d 100644 --- a/app/Model/AttributeTag.php +++ b/app/Model/AttributeTag.php @@ -234,35 +234,32 @@ class AttributeTag extends AppModel return $allClusters; } - public function extractAttributeTagsNameFromEvent(&$event_elements, $to_extract='both') + public function extractAttributeTagsNameFromEvent(&$event, $to_extract='both') { $attribute_tags_name = array('tags' => array(), 'clusters' => array()); - if ($to_extract == 'tags' || $to_extract == 'both') { - foreach ($event_elements as $i => $element) { - if ($element['objectType'] == 'object') { - $new_tags = $this->extractAttributeTagsNameFromEvent($element['Attribute'], 'tags'); - $attribute_tags_name['tags'] = array_merge($attribute_tags_name['tags'], $new_tags['tags']); - } else { - if (!empty($element['AttributeTag'])) { - $new_tags = Hash::extract($element['AttributeTag'], '{n}.Tag.name'); - $attribute_tags_name['tags'] = array_merge($attribute_tags_name['tags'], $new_tags); - } - } - } - } - if ($to_extract == 'clusters' || $to_extract == 'both') { - foreach ($event_elements as $i => $element) { - if ($element['objectType'] == 'object') { - $new_tags = $this->extractAttributeTagsNameFromEvent($element['Attribute'], 'clusters'); - $attribute_tags_name['clusters'] = array_merge($attribute_tags_name['clusters'], $new_tags['clusters']); - } else { - if (!empty($element['Galaxy'])) { - $new_tags = Hash::extract($element['Galaxy'], '{n}.GalaxyCluster.{n}.tag_name'); - $attribute_tags_name['clusters'] = array_merge($attribute_tags_name['clusters'], $new_tags); - } + foreach ($event['Attribute'] as $i => $attribute) { + if ($to_extract == 'tags' || $to_extract == 'both') { + $new_tags = Hash::combine($attribute['AttributeTag'], '{n}.Tag.name', '{n}.Tag.name'); + $attribute_tags_name['tags'] = array_merge($attribute_tags_name['tags'], $new_tags); + } + if ($to_extract == 'clusters' || $to_extract == 'both') { + $new_tags = Hash::combine($attribute['Galaxy'], '{n}.GalaxyCluster.{n}.tag_name', '{n}.GalaxyCluster.{n}.tag_name'); + $attribute_tags_name['clusters'] = array_merge($attribute_tags_name['clusters'], $new_tags); + } + } + foreach ($event['Object'] as $i => $object) { + foreach ($object['Attribute'] as $j => $object_attribute) { + if ($to_extract == 'tags' || $to_extract == 'both') { + $new_tags = Hash::combine($object_attribute['AttributeTag'], '{n}.Tag.name', '{n}.Tag.name'); + $attribute_tags_name['tags'] = array_merge($attribute_tags_name['tags'], $new_tags); + } + if ($to_extract == 'clusters' || $to_extract == 'both') { + $new_tags = Hash::combine($object_attribute['Galaxy'], '{n}.GalaxyCluster.{n}.tag_name', '{n}.GalaxyCluster.{n}.tag_name'); + $attribute_tags_name['clusters'] = array_merge($attribute_tags_name['clusters'], $new_tags); } } } + $attribute_tags_name['tags'] = array_diff_key($attribute_tags_name['tags'], $attribute_tags_name['clusters']); return $attribute_tags_name; }