From 16453a3433aece6531a6af26193f7eb473feadab Mon Sep 17 00:00:00 2001 From: mokaddem Date: Tue, 16 Apr 2019 11:02:52 +0200 Subject: [PATCH] chg: [event:view] Major performance improvement for large event. Due to the introduction of the event filtering widget, attributes were fetched 2 additional times. --- app/Controller/EventsController.php | 18 ++++------- app/Model/AttributeTag.php | 32 +++++++++++++++++++ .../View/eventFilteringQueryBuilder.ctp | 6 ++-- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index 96535ea91..5cd7d8a1b 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -1195,12 +1195,9 @@ class EventsController extends AppController $this->set('advancedFilteringActive', $advancedFiltering['active'] ? 1 : 0); $this->set('advancedFilteringActiveRules', $advancedFiltering['activeRules']); $this->set('defaultFilteringRules', $this->defaultFilteringRules); - $attributeTags = $this->Event->Attribute->AttributeTag->getAttributesTags($this->Auth->user(), $event['Event']['id']); - $attributeTags = array_column($attributeTags, 'name'); - $this->set('attributeTags', $attributeTags); - $attributeClusters = $this->Event->Attribute->AttributeTag->getAttributesClusters($this->Auth->user(), $event['Event']['id']); - $attributeClusters = array_column($attributeClusters, 'value'); - $this->set('attributeClusters', $attributeClusters); + $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'); @@ -1476,12 +1473,9 @@ class EventsController extends AppController $this->set('advancedFilteringActive', $advancedFiltering['active'] ? 1 : 0); $this->set('advancedFilteringActiveRules', $advancedFiltering['activeRules']); $this->set('defaultFilteringRules', $this->defaultFilteringRules); - $attributeTags = $this->Event->Attribute->AttributeTag->getAttributesTags($this->Auth->user(), $event['Event']['id']); - $attributeTags = array_column($attributeTags, 'name'); - $this->set('attributeTags', $attributeTags); - $attributeClusters = $this->Event->Attribute->AttributeTag->getAttributesClusters($this->Auth->user(), $event['Event']['id']); - $attributeClusters = array_column($attributeClusters, 'value'); - $this->set('attributeClusters', $attributeClusters); + $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 c9690be93..bee4df891 100644 --- a/app/Model/AttributeTag.php +++ b/app/Model/AttributeTag.php @@ -234,4 +234,36 @@ class AttributeTag extends AppModel return $allClusters; } + public function extractAttributeTagsNameFromEvent(&$event_elements, $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); + } + } + } + } + return $attribute_tags_name; + } + } diff --git a/app/View/Elements/Events/View/eventFilteringQueryBuilder.ctp b/app/View/Elements/Events/View/eventFilteringQueryBuilder.ctp index c20956e54..aca4e9a5b 100644 --- a/app/View/Elements/Events/View/eventFilteringQueryBuilder.ctp +++ b/app/View/Elements/Events/View/eventFilteringQueryBuilder.ctp @@ -207,7 +207,7 @@ function triggerEventFilteringTool(clicked) { "unique": true, "id": "taggedAttributes", "label": "Tags", - "values": + "values": }, @@ -220,7 +220,7 @@ function triggerEventFilteringTool(clicked) { "unique": true, "id": "galaxyAttachedAttributes", "label": "Galaxies", - "values": + "values": }, { @@ -339,14 +339,12 @@ function triggerEventFilteringTool(clicked) { { field: 'taggedAttributes', id: 'taggedAttributes', - value: '' }, { field: 'galaxyAttachedAttributes', id: 'galaxyAttachedAttributes', - value: '' }, ],