mirror of https://github.com/MISP/MISP
fix: [internal] Optimise fetching related attributes
parent
a3b02cf037
commit
21335d7d1f
|
@ -337,7 +337,6 @@ class DefaultCorrelationBehavior extends ModelBehavior
|
|||
[
|
||||
'1_attribute_id',
|
||||
'1_object_id',
|
||||
'1_event_id',
|
||||
'1_distribution',
|
||||
'1_object_distribution',
|
||||
'1_event_distribution',
|
||||
|
@ -345,12 +344,10 @@ class DefaultCorrelationBehavior extends ModelBehavior
|
|||
'1_object_sharing_group_id',
|
||||
'1_event_sharing_group_id',
|
||||
'1_org_id',
|
||||
'value_id'
|
||||
],
|
||||
[
|
||||
'attribute_id',
|
||||
'object_id',
|
||||
'event_id',
|
||||
'distribution',
|
||||
'object_distribution',
|
||||
'event_distribution',
|
||||
|
@ -358,11 +355,10 @@ class DefaultCorrelationBehavior extends ModelBehavior
|
|||
'object_sharing_group_id',
|
||||
'event_sharing_group_id',
|
||||
'org_id',
|
||||
'value_id'
|
||||
]
|
||||
];
|
||||
$prefixes = ['1_', ''];
|
||||
$correlated_attribute_ids = [];
|
||||
$correlatedAttributeIds = [];
|
||||
foreach ($conditions as $k => $condition) {
|
||||
$temp_correlations = $Model->find('all', [
|
||||
'recursive' => -1,
|
||||
|
@ -376,10 +372,15 @@ class DefaultCorrelationBehavior extends ModelBehavior
|
|||
continue;
|
||||
}
|
||||
}
|
||||
$correlated_attribute_ids[] = $temp_correlation['Correlation'][$prefixes[$k] . 'attribute_id'];
|
||||
$correlatedAttributeIds[] = $temp_correlation['Correlation'][$prefixes[$k] . 'attribute_id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($correlatedAttributeIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$contain = [];
|
||||
if (!empty($includeEventData)) {
|
||||
$contain['Event'] = [
|
||||
|
@ -402,7 +403,7 @@ class DefaultCorrelationBehavior extends ModelBehavior
|
|||
$relatedAttributes = $Model->Attribute->find('all', [
|
||||
'recursive' => -1,
|
||||
'conditions' => [
|
||||
'Attribute.id' => $correlated_attribute_ids
|
||||
'Attribute.id' => $correlatedAttributeIds
|
||||
],
|
||||
'fields' => $fields,
|
||||
'contain' => $contain
|
||||
|
|
|
@ -254,54 +254,38 @@ class NoAclCorrelationBehavior extends ModelBehavior
|
|||
|
||||
/**
|
||||
* @param Correlation $Model
|
||||
* @param $user
|
||||
* @param $sgids
|
||||
* @param array $user Not used
|
||||
* @param array $sgids Not used
|
||||
* @param array $attribute
|
||||
* @param array $fields
|
||||
* @param array $fields Attribute fields to fetch
|
||||
* @param bool $includeEventData
|
||||
* @return array
|
||||
*/
|
||||
public function runGetRelatedAttributes(Model $Model, $user, $sgids, $attribute, $fields = [], $includeEventData = false)
|
||||
{
|
||||
// LATER getRelatedAttributes($attribute) this might become a performance bottleneck
|
||||
// prepare the conditions
|
||||
$conditions = [
|
||||
[
|
||||
$correlatedAttributeIds = $Model->find('column', [
|
||||
'conditions' => [
|
||||
'Correlation.1_event_id !=' => $attribute['event_id'],
|
||||
'Correlation.attribute_id' => $attribute['id']
|
||||
'Correlation.attribute_id' => $attribute['id'],
|
||||
],
|
||||
[
|
||||
'fields' => ['1_attribute_id'],
|
||||
]);
|
||||
|
||||
$correlatedAttributeIds2 = $Model->find('column', [
|
||||
'conditions' => [
|
||||
'Correlation.event_id !=' => $attribute['event_id'],
|
||||
'Correlation.1_attribute_id' => $attribute['id']
|
||||
]
|
||||
];
|
||||
$corr_fields = [
|
||||
[
|
||||
'1_attribute_id',
|
||||
'1_event_id',
|
||||
'value_id'
|
||||
'Correlation.1_attribute_id' => $attribute['id'],
|
||||
],
|
||||
[
|
||||
'attribute_id',
|
||||
'event_id',
|
||||
'value_id'
|
||||
]
|
||||
];
|
||||
$prefixes = ['1_', ''];
|
||||
$correlated_attribute_ids = [];
|
||||
foreach ($conditions as $k => $condition) {
|
||||
$temp_correlations = $Model->find('all', [
|
||||
'recursive' => -1,
|
||||
'conditions' => $condition,
|
||||
'fields' => $corr_fields[$k]
|
||||
]);
|
||||
if (!empty($temp_correlations)) {
|
||||
foreach ($temp_correlations as $temp_correlation) {
|
||||
$correlated_attribute_ids[] = $temp_correlation['Correlation'][$prefixes[$k] . 'attribute_id'];
|
||||
}
|
||||
}
|
||||
'fields' => ['attribute_id'],
|
||||
]);
|
||||
foreach ($correlatedAttributeIds2 as $tempCorrelation) {
|
||||
$correlatedAttributeIds[] = $tempCorrelation;
|
||||
}
|
||||
$contain = [];
|
||||
|
||||
if (empty($correlatedAttributeIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!empty($includeEventData)) {
|
||||
$contain['Event'] = [
|
||||
'fields' => [
|
||||
|
@ -319,11 +303,14 @@ class NoAclCorrelationBehavior extends ModelBehavior
|
|||
'Event.org_id'
|
||||
]
|
||||
];
|
||||
} else {
|
||||
$contain = [];
|
||||
}
|
||||
|
||||
$relatedAttributes = $Model->Attribute->find('all', [
|
||||
'recursive' => -1,
|
||||
'conditions' => [
|
||||
'Attribute.id' => $correlated_attribute_ids
|
||||
'Attribute.id' => $correlatedAttributeIds
|
||||
],
|
||||
'fields' => $fields,
|
||||
'contain' => $contain
|
||||
|
|
Loading…
Reference in New Issue