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